diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index f4fe00932..00f8b9353 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -210,7 +210,7 @@ public RestClient( static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) { if (options.MaxTimeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(options.MaxTimeout); - if (options.UserAgent != null && httpClient.DefaultRequestHeaders.UserAgent.All(x => x.Product?.Name != options.UserAgent)) { + if (options.UserAgent != null && httpClient.DefaultRequestHeaders.UserAgent.All(x => $"{x.Product?.Name}/{x.Product?.Version}" != options.UserAgent)) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation(KnownHeaders.UserAgent, options.UserAgent); } diff --git a/test/RestSharp.Tests/RestClientTests.cs b/test/RestSharp.Tests/RestClientTests.cs index 7dd4fe618..c99e2fc88 100644 --- a/test/RestSharp.Tests/RestClientTests.cs +++ b/test/RestSharp.Tests/RestClientTests.cs @@ -116,4 +116,19 @@ public void Should_use_new_httpClient_instance() { client1.HttpClient.Should().NotBeSameAs(client2.HttpClient); } + + [Fact] + public void ConfigureHttpClient_does_not_duplicate_user_agent_for_same_client() { + // arrange + var httpClient = new HttpClient(); + var clientOptions = new RestClientOptions(); + + // act + var restClient1 = new RestClient(httpClient, clientOptions); + var restClient2 = new RestClient(httpClient, clientOptions); + + // assert + Assert.Contains(httpClient.DefaultRequestHeaders.UserAgent, agent => $"{agent.Product.Name}/{agent.Product.Version}" == clientOptions.UserAgent); + Assert.Single(httpClient.DefaultRequestHeaders.UserAgent); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy