From 41bff69dd5654d1e10f1584a80c1a9f4c5850061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hazel=20=7C=20=E3=81=B8=E3=81=84=E3=81=9C=E3=82=8B?= Date: Sun, 12 Mar 2023 11:03:13 -0500 Subject: [PATCH 1/2] Fixed a bug causing duplicate user-agent headers when using a shared `HttpClient`. (#2008) * Added product version to the user-agent check when configuring HttpClient. * Created a unit test to handle duplicate user agent scenario. --- src/RestSharp/RestClient.cs | 2 +- test/RestSharp.Tests/RestClientTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index f4fe00932..1dd953392 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); + } } From 620a5576525ac9eab70bcc00337a15aa2750716c Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Sun, 12 Mar 2023 17:04:26 +0100 Subject: [PATCH 2/2] Follow up fix of a compile error --- src/RestSharp/RestClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index 1dd953392..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}/{x.Product?.Version}" != 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); }