diff --git a/test/RestSharp.Tests.Integrated/HttpHeadersTests.cs b/test/RestSharp.Tests.Integrated/HttpHeadersTests.cs index 778cff395..e2d2408fb 100644 --- a/test/RestSharp.Tests.Integrated/HttpHeadersTests.cs +++ b/test/RestSharp.Tests.Integrated/HttpHeadersTests.cs @@ -1,7 +1,9 @@ namespace RestSharp.Tests.Integrated; public sealed class HttpHeadersTests(WireMockTestServer server) : IClassFixture, 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() { @@ -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); } @@ -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); } @@ -56,17 +58,28 @@ public async Task Should_use_both_default_and_request_headers() { .AddHeader(requestHeader.Name, requestHeader.Value); var response = await _client.ExecuteAsync(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(request); + var h = FindHeader(response, "User-Agent"); + h.Should().NotBeNull(); + h.Value.Should().Be(UserAgent); + } + + static void CheckHeader(RestResponse response, Header header) { + var h = FindHeader(response, header.Name); + h.Should().NotBeNull(); + h.Value.Should().Be(header.Value); + } + + static TestServerResponse FindHeader(RestResponse response, string headerName) + => response.Data!.First(x => x.Name == headerName); + record Header(string Name, string Value); class HeaderInterceptor(string headerName, string headerValue) : Interceptors.Interceptor { diff --git a/test/RestSharp.Tests/RestClientTests.cs b/test/RestSharp.Tests/RestClientTests.cs index 2824ef4cd..9d575db4c 100644 --- a/test/RestSharp.Tests/RestClientTests.cs +++ b/test/RestSharp.Tests/RestClientTests.cs @@ -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 ); @@ -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 );