Skip to content

Commit

Permalink
Add test for user agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed May 22, 2024
1 parent 799c88a commit 873ecde
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
37 changes: 25 additions & 12 deletions test/RestSharp.Tests.Integrated/HttpHeadersTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace RestSharp.Tests.Integrated;

public sealed class HttpHeadersTests(WireMockTestServer server) : IClassFixture<WireMockTestServer>, IDisposable {
readonly RestClient _client = new(new RestClientOptions(server.Url!) { ThrowOnAnyError = true });
const string UserAgent = "RestSharp/test";

readonly RestClient _client = new(new RestClientOptions(server.Url!) { ThrowOnAnyError = true, UserAgent = UserAgent });

[Fact]
public async Task Ensure_headers_correctly_set_in_the_interceptor() {
Expand All @@ -17,7 +19,7 @@ public async Task Ensure_headers_correctly_set_in_the_interceptor() {

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var header = response.Data!.First(x => x.Name == headerName);
var header = FindHeader(response, headerName);
header.Should().NotBeNull();
header.Value.Should().Be(headerValue);
}
Expand All @@ -40,7 +42,7 @@ public async Task Ensure_headers_correctly_set_in_the_hook() {
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Data.Should().NotBeNull();
var header = response.Data!.First(x => x.Name == headerName);
var header = FindHeader(response, headerName);
header.Should().NotBeNull();
header.Value.Should().Be(headerValue);
}
Expand All @@ -56,17 +58,28 @@ public async Task Should_use_both_default_and_request_headers() {
.AddHeader(requestHeader.Name, requestHeader.Value);

var response = await _client.ExecuteAsync<TestServerResponse[]>(request);
CheckHeader(defaultHeader);
CheckHeader(requestHeader);
return;

void CheckHeader(Header header) {
var h = response.Data!.First(x => x.Name == header.Name);
h.Should().NotBeNull();
h.Value.Should().Be(header.Value);
}
CheckHeader(response, defaultHeader);
CheckHeader(response, requestHeader);
}

[Fact]
public async Task Should_sent_custom_UserAgent() {
var request = new RestRequest("/headers");
var response = await _client.ExecuteAsync<TestServerResponse[]>(request);
var h = FindHeader(response, "User-Agent");
h.Should().NotBeNull();
h.Value.Should().Be(UserAgent);
}

static void CheckHeader(RestResponse<TestServerResponse[]> response, Header header) {
var h = FindHeader(response, header.Name);
h.Should().NotBeNull();
h.Value.Should().Be(header.Value);
}

static TestServerResponse FindHeader(RestResponse<TestServerResponse[]> response, string headerName)
=> response.Data!.First(x => x.Name == headerName);

record Header(string Name, string Value);

class HeaderInterceptor(string headerName, string headerValue) : Interceptors.Interceptor {
Expand Down
8 changes: 2 additions & 6 deletions test/RestSharp.Tests/RestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public void ConfigureDefaultParameters_sets_user_agent_new_httpClient_instance()
//assert
Assert.Single(
restClient.DefaultParameters,
parameter => parameter.Type == ParameterType.HttpHeader &&
parameter.Name == KnownHeaders.UserAgent &&
parameter.Value is string valueAsString &&
parameter => parameter is { Type: ParameterType.HttpHeader, Name: KnownHeaders.UserAgent, Value: string valueAsString } &&
valueAsString == clientOptions.UserAgent
);

Expand All @@ -151,9 +149,7 @@ public void ConfigureDefaultParameters_sets_user_agent_given_httpClient_instance
//assert
Assert.Single(
restClient.DefaultParameters,
parameter => parameter.Type == ParameterType.HttpHeader &&
parameter.Name == KnownHeaders.UserAgent &&
parameter.Value is string valueAsString &&
parameter => parameter is { Type: ParameterType.HttpHeader, Name: KnownHeaders.UserAgent, Value: string valueAsString } &&
valueAsString == clientOptions.UserAgent
);

Expand Down

0 comments on commit 873ecde

Please sign in to comment.