Skip to content

Commit 19c30a8

Browse files
committed
Refactoring tests
1 parent b94e766 commit 19c30a8

File tree

15 files changed

+265
-168
lines changed

15 files changed

+265
-168
lines changed

src/RestSharp/IRestClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
namespace RestSharp;
1919

2020
public interface IRestClient : IDisposable {
21+
/// <summary>
22+
/// Client options that aren't used for configuring HttpClient
23+
/// </summary>
2124
IRestClientOptions Options { get; }
2225

26+
/// <summary>
27+
/// Client-level serializers
28+
/// </summary>
2329
RestSerializers Serializers { get; }
2430

2531
/// <summary>

src/RestSharp/Request/RestRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Net;
1616
using RestSharp.Extensions;
1717

18+
// ReSharper disable ReplaceSubstringWithRangeIndexer
1819
// ReSharper disable UnusedAutoPropertyAccessor.Global
1920

2021
namespace RestSharp;

src/RestSharp/Request/UriExtensions.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ params ParametersCollection[] parametersCollections
4747
if (parameters.Count == 0) return mergedUri;
4848

4949
var uri = mergedUri.AbsoluteUri;
50-
var separator = uri.Contains("?") ? "&" : "?";
50+
var separator = uri.Contains('?') ? "&" : "?";
5151

5252
return new Uri(string.Concat(uri, separator, EncodeParameters()));
5353

5454
string EncodeParameters() => string.Join("&", parameters.Select(EncodeParameter).ToArray());
5555

56-
string EncodeParameter(Parameter parameter) {
57-
return
58-
!parameter.Encode
59-
? $"{parameter.Name}={StringOrEmpty(parameter.Value)}"
60-
: $"{encodeQuery(parameter.Name!, encoding)}={encodeQuery(StringOrEmpty(parameter.Value), encoding)}";
61-
62-
static string StringOrEmpty(object? value) => value == null ? "" : value.ToString()!;
56+
string GetString(string name, string? value, Func<string, string>? encode) {
57+
var val = encode != null && value != null ? encode(value) : value;
58+
return val == null ? name : $"{name}={val}";
6359
}
60+
61+
string EncodeParameter(Parameter parameter)
62+
=> !parameter.Encode
63+
? GetString(parameter.Name!, parameter.Value?.ToString(), null)
64+
: GetString(encodeQuery(parameter.Name!, encoding), parameter.Value?.ToString(), x => encodeQuery(x, encoding));
6465
}
6566

6667
public static UrlSegmentParamsValues GetUrlSegmentParamsValues(
@@ -91,4 +92,4 @@ params ParametersCollection[] parametersCollections
9192
}
9293
}
9394

94-
record UrlSegmentParamsValues(Uri Uri, string Resource);
95+
record UrlSegmentParamsValues(Uri Uri, string Resource);

src/RestSharp/RestClient.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,24 @@ public partial class RestClient : IRestClient {
3939

4040
HttpClient HttpClient { get; }
4141

42-
public IRestClientOptions Options { get; }
43-
public RestSerializers Serializers { get; }
42+
/// <inheritdoc />>
43+
public IRestClientOptions Options { get; }
44+
45+
/// <inheritdoc />>
46+
public RestSerializers Serializers { get; }
4447

4548
[Obsolete("Use RestClientOptions.Authenticator instead")]
4649
public IAuthenticator? Authenticator {
4750
get => Options.Authenticator;
4851
set => Options.Authenticator = value;
4952
}
5053

54+
/// <summary>
55+
/// Creates an instance of RestClient using the provided <see cref="RestClientOptions"/>
56+
/// </summary>
57+
/// <param name="options">Client options</param>
58+
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
59+
/// <param name="configureSerialization">Delegate to configure serialization</param>
5160
public RestClient(
5261
RestClientOptions options,
5362
ConfigureHeaders? configureDefaultHeaders = null,
@@ -77,28 +86,49 @@ static RestClientOptions ConfigureOptions(RestClientOptions options, ConfigureRe
7786
/// Creates an instance of RestClient using the default <see cref="RestClientOptions"/>
7887
/// </summary>
7988
/// <param name="configureRestClient">Delegate to configure the client options</param>
89+
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
8090
/// <param name="configureSerialization">Delegate to configure serialization</param>
81-
public RestClient(ConfigureRestClient? configureRestClient = null, ConfigureSerialization? configureSerialization = null)
82-
: this(ConfigureOptions(new RestClientOptions(), configureRestClient), configureSerialization: configureSerialization) { }
91+
public RestClient(
92+
ConfigureRestClient? configureRestClient = null,
93+
ConfigureHeaders? configureDefaultHeaders = null,
94+
ConfigureSerialization? configureSerialization = null
95+
)
96+
: this(ConfigureOptions(new RestClientOptions(), configureRestClient), configureDefaultHeaders, configureSerialization) { }
8397

8498
/// <inheritdoc />
8599
/// <summary>
86100
/// Creates an instance of RestClient using a specific BaseUrl for requests made by this client instance
87101
/// </summary>
88102
/// <param name="baseUrl">Base URI for the new client</param>
89103
/// <param name="configureRestClient">Delegate to configure the client options</param>
104+
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
90105
/// <param name="configureSerialization">Delegate to configure serialization</param>
91-
public RestClient(Uri baseUrl, ConfigureRestClient? configureRestClient = null, ConfigureSerialization? configureSerialization = null)
92-
: this(ConfigureOptions(new RestClientOptions { BaseUrl = baseUrl }, configureRestClient), configureSerialization: configureSerialization) { }
106+
public RestClient(
107+
Uri baseUrl,
108+
ConfigureRestClient? configureRestClient = null,
109+
ConfigureHeaders? configureDefaultHeaders = null,
110+
ConfigureSerialization? configureSerialization = null
111+
)
112+
: this(
113+
ConfigureOptions(new RestClientOptions { BaseUrl = baseUrl }, configureRestClient),
114+
configureDefaultHeaders,
115+
configureSerialization
116+
) { }
93117

94118
/// <summary>
95119
/// Creates an instance of RestClient using a specific BaseUrl for requests made by this client instance
96120
/// </summary>
97121
/// <param name="baseUrl">Base URI for this new client as a string</param>
98122
/// <param name="configureRestClient">Delegate to configure the client options</param>
123+
/// <param name="configureDefaultHeaders">Delegate to add default headers to the wrapped HttpClient instance</param>
99124
/// <param name="configureSerialization">Delegate to configure serialization</param>
100-
public RestClient(string baseUrl, ConfigureRestClient? configureRestClient = null, ConfigureSerialization? configureSerialization = null)
101-
: this(new Uri(Ensure.NotEmptyString(baseUrl, nameof(baseUrl))), configureRestClient, configureSerialization) { }
125+
public RestClient(
126+
string baseUrl,
127+
ConfigureRestClient? configureRestClient = null,
128+
ConfigureHeaders? configureDefaultHeaders = null,
129+
ConfigureSerialization? configureSerialization = null
130+
)
131+
: this(new Uri(Ensure.NotEmptyString(baseUrl, nameof(baseUrl))), configureRestClient, configureDefaultHeaders, configureSerialization) { }
102132

103133
/// <summary>
104134
/// Creates an instance of RestClient using a shared HttpClient and specific RestClientOptions and does not allocate one internally.

test/RestSharp.Tests.Integrated/Authentication/AuthenticationTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public async Task Can_Authenticate_With_Basic_Http_Auth() {
2020
const string userName = "testuser";
2121
const string password = "testpassword";
2222

23-
var client = new RestClient(_fixture.Server.Url, o =>
24-
o.Authenticator = new HttpBasicAuthenticator(userName, password)
23+
var client = new RestClient(
24+
_fixture.Server.Url,
25+
o => o.Authenticator = new HttpBasicAuthenticator(userName, password)
2526
);
2627
var request = new RestRequest("headers");
2728
var response = await client.GetAsync<TestServerResponse[]>(request);
@@ -34,4 +35,4 @@ public async Task Can_Authenticate_With_Basic_Http_Auth() {
3435
parts[0].Should().Be(userName);
3536
parts[1].Should().Be(password);
3637
}
37-
}
38+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using RestSharp.Authenticators.OAuth2;
2-
using RestSharp.Tests.Integrated.Fixtures;
32
using RestSharp.Tests.Integrated.Server;
43

5-
namespace RestSharp.Tests.Integrated.Authentication;
4+
namespace RestSharp.Tests.Integrated.Authentication;
65

76
[Collection(nameof(TestServerCollection))]
87
public class OAuth2Tests {
@@ -12,14 +11,13 @@ public class OAuth2Tests {
1211

1312
[Fact]
1413
public async Task ShouldHaveProperHeader() {
15-
var client = new RestClient(_fixture.Server.Url);
1614
var auth = new OAuth2AuthorizationRequestHeaderAuthenticator("token", "Bearer");
17-
client.Authenticator = auth;
15+
var client = new RestClient(_fixture.Server.Url, o => o.Authenticator = auth);
1816

1917
var response = await client.GetJsonAsync<TestServerResponse[]>("headers");
2018
var authHeader = response!.FirstOrDefault(x => x.Name == KnownHeaders.Authorization);
2119

2220
authHeader.Should().NotBeNull();
2321
authHeader!.Value.Should().Be("Bearer token");
2422
}
25-
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Net;
2+
using RestSharp.Tests.Integrated.Server;
3+
4+
namespace RestSharp.Tests.Integrated;
5+
6+
[Collection(nameof(TestServerCollection))]
7+
public class CookieTests {
8+
readonly RestClient _client;
9+
readonly string _host;
10+
11+
public CookieTests(TestServerFixture fixture) {
12+
_client = new RestClient(fixture.Server.Url);
13+
_host = _client.Options.BaseUrl!.Host;
14+
}
15+
16+
[Fact]
17+
public async Task Can_Perform_GET_Async_With_Request_Cookies() {
18+
var request = new RestRequest("get-cookies") {
19+
CookieContainer = new CookieContainer()
20+
};
21+
request.CookieContainer.Add(new Cookie("cookie", "value", null, _host));
22+
request.CookieContainer.Add(new Cookie("cookie2", "value2", null, _host));
23+
var response = await _client.ExecuteAsync(request);
24+
response.Content.Should().Be("[\"cookie=value\",\"cookie2=value2\"]");
25+
}
26+
27+
[Fact]
28+
public async Task Can_Perform_GET_Async_With_Response_Cookies() {
29+
var request = new RestRequest("set-cookies");
30+
var response = await _client.ExecuteAsync(request);
31+
response.Content.Should().Be("success");
32+
33+
AssertCookie("cookie1", "value1", x => x == DateTime.MinValue);
34+
FindCookie("cookie2").Should().BeNull("Cookie 2 should vanish as the path will not match");
35+
AssertCookie("cookie3", "value3", x => x > DateTime.Now);
36+
AssertCookie("cookie4", "value4", x => x > DateTime.Now);
37+
FindCookie("cookie5").Should().BeNull("Cookie 5 should vanish as the request is not SSL");
38+
AssertCookie("cookie6", "value6", x => x == DateTime.MinValue, true);
39+
40+
Cookie? FindCookie(string name) =>response!.Cookies!.FirstOrDefault(p => p.Name == name);
41+
42+
void AssertCookie(string name, string value, Func<DateTime, bool> checkExpiration, bool httpOnly = false) {
43+
var c = FindCookie(name)!;
44+
c.Value.Should().Be(value);
45+
c.Path.Should().Be("/");
46+
c.Domain.Should().Be(_host);
47+
checkExpiration(c.Expires).Should().BeTrue();
48+
c.HttpOnly.Should().Be(httpOnly);
49+
}
50+
}
51+
}

test/RestSharp.Tests.Integrated/RequestFailureTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using RestSharp.Tests.Integrated.Fixtures;
32
using RestSharp.Tests.Integrated.Server;
43

54
namespace RestSharp.Tests.Integrated;
Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using RestSharp.Tests.Integrated.Fixtures;
32
using RestSharp.Tests.Integrated.Server;
43

54
namespace RestSharp.Tests.Integrated;
@@ -8,10 +7,12 @@ namespace RestSharp.Tests.Integrated;
87
public class AsyncTests {
98
readonly ITestOutputHelper _output;
109
readonly RestClient _client;
10+
readonly string _host;
1111

1212
public AsyncTests(TestServerFixture fixture, ITestOutputHelper output) {
13-
_output = output;
14-
_client = new RestClient(fixture.Server.Url);
13+
_output = output;
14+
_client = new RestClient(fixture.Server.Url);
15+
_host = _client.Options.BaseUrl!.Host;
1516
}
1617

1718
class Response {
@@ -51,63 +52,6 @@ public async Task Can_Perform_GET_Async() {
5152
response.Content.Should().Be(val);
5253
}
5354

54-
[Fact]
55-
public async Task Can_Perform_GET_Async_With_Request_Cookies() {
56-
var request = new RestRequest("get-cookies") {
57-
CookieContainer = new CookieContainer()
58-
};
59-
request.CookieContainer.Add(new Cookie("cookie", "value", null, _client.Options.BaseUrl.Host));
60-
request.CookieContainer.Add(new Cookie("cookie2", "value2", null, _client.Options.BaseUrl.Host));
61-
var response = await _client.ExecuteAsync(request);
62-
response.Content.Should().Be("[\"cookie=value\",\"cookie2=value2\"]");
63-
}
64-
65-
[Fact]
66-
public async Task Can_Perform_GET_Async_With_Response_Cookies() {
67-
var request = new RestRequest("set-cookies");
68-
var response = await _client.ExecuteAsync(request);
69-
response.Content.Should().Be("success");
70-
71-
// Check we got all our cookies
72-
var domain = _client.Options.BaseUrl.Host;
73-
var cookie = response.Cookies!.First(p => p.Name == "cookie1");
74-
Assert.Equal("value1", cookie.Value);
75-
Assert.Equal("/", cookie.Path);
76-
Assert.Equal(domain, cookie.Domain);
77-
Assert.Equal(DateTime.MinValue, cookie.Expires);
78-
Assert.False(cookie.HttpOnly);
79-
80-
// Cookie 2 should vanish as the path will not match
81-
cookie = response.Cookies!.FirstOrDefault(p => p.Name == "cookie2");
82-
Assert.Null(cookie);
83-
84-
// Check cookie3 has a valid expiration
85-
cookie = response.Cookies!.First(p => p.Name == "cookie3");
86-
Assert.Equal("value3", cookie.Value);
87-
Assert.Equal("/", cookie.Path);
88-
Assert.Equal(domain, cookie.Domain);
89-
Assert.True(cookie.Expires > DateTime.Now);
90-
91-
// Check cookie4 has a valid expiration
92-
cookie = response.Cookies!.First(p => p.Name == "cookie4");
93-
Assert.Equal("value4", cookie.Value);
94-
Assert.Equal("/", cookie.Path);
95-
Assert.Equal(domain, cookie.Domain);
96-
Assert.True(cookie.Expires > DateTime.Now);
97-
98-
// Cookie 5 should vanish as the request is not SSL
99-
cookie = response.Cookies!.FirstOrDefault(p => p.Name == "cookie5");
100-
Assert.Null(cookie);
101-
102-
// Check cookie6 should be http only
103-
cookie = response.Cookies!.First(p => p.Name == "cookie6");
104-
Assert.Equal("value6", cookie.Value);
105-
Assert.Equal("/", cookie.Path);
106-
Assert.Equal(domain, cookie.Domain);
107-
Assert.Equal(DateTime.MinValue, cookie.Expires);
108-
Assert.True(cookie.HttpOnly);
109-
}
110-
11155
[Fact]
11256
public async Task Can_Timeout_GET_Async() {
11357
var request = new RestRequest("timeout").AddBody("Body_Content");
@@ -136,4 +80,4 @@ public async Task Can_Delete_With_Response_Type_using_extension() {
13680

13781
response!.Message.Should().Be("Works!");
13882
}
139-
}
83+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<Nullable>disable</Nullable>
3+
<Nullable>enable</Nullable>
44
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
</PropertyGroup>
66
<ItemGroup>
7-
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj"/>
8-
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj"/>
9-
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj"/>
7+
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj" />
8+
<ProjectReference Include="..\..\src\RestSharp\RestSharp.csproj" />
9+
<ProjectReference Include="..\RestSharp.Tests.Shared\RestSharp.Tests.Shared.csproj" />
1010
</ItemGroup>
1111
<ItemGroup>
12-
<None Update="Assets\Koala.jpg" CopyToOutputDirectory="PreserveNewest"/>
13-
<None Update="Assets\TestFile.txt" CopyToOutputDirectory="PreserveNewest"/>
14-
<None Update="Assets\KoalaÄÖäö.jpg" CopyToOutputDirectory="PreserveNewest"/>
15-
<None Update="Assets\Teståæ.txt" CopyToOutputDirectory="PreserveNewest"/>
12+
<None Update="Assets\Koala.jpg" CopyToOutputDirectory="PreserveNewest" />
13+
<None Update="Assets\TestFile.txt" CopyToOutputDirectory="PreserveNewest" />
14+
<None Update="Assets\KoalaÄÖäö.jpg" CopyToOutputDirectory="PreserveNewest" />
15+
<None Update="Assets\Teståæ.txt" CopyToOutputDirectory="PreserveNewest" />
1616
</ItemGroup>
1717
<ItemGroup>
18-
<PackageReference Include="HttpTracer" Version="2.1.1"/>
19-
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.9"/>
20-
<PackageReference Include="Polly" Version="7.2.3"/>
21-
<PackageReference Include="Xunit.Extensions.Logging" Version="1.1.0"/>
18+
<PackageReference Include="HttpTracer" Version="2.1.1" />
19+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.9" />
20+
<PackageReference Include="Polly" Version="7.2.3" />
21+
<PackageReference Include="Xunit.Extensions.Logging" Version="1.1.0" />
2222
</ItemGroup>
2323
<ItemGroup>
24-
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest"/>
24+
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
2525
</ItemGroup>
2626
</Project>

0 commit comments

Comments
 (0)