From 5ec9910b1f8b95fd197e5819881a2e5e4596f31a Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Thu, 6 Feb 2025 13:20:18 +0100 Subject: [PATCH 1/4] Trying to add user agent header. --- .../System/Net/Http/HttpClientHandlerTest.RemoteServer.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 6596a330ddfa68..f12b6cac5531e6 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1381,6 +1381,11 @@ public async Task SendAsync_RequestVersion20_ResponseVersion20(Uri server) var request = new HttpRequestMessage(HttpMethod.Get, server); request.Version = new Version(2, 0); + request.Headers.TryAddWithoutValidation("User-Agent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + + "AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/90.0.4430.93 Safari/537.36"); + using (HttpClient client = CreateHttpClient()) { using (HttpResponseMessage response = await client.SendAsync(TestAsync, request)) From 63e45f767570f3b594b7014421079eddaa856fae Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Thu, 6 Feb 2025 15:15:08 +0100 Subject: [PATCH 2/4] Test another way --- .../System/Net/Http/HttpClientHandlerTest.RemoteServer.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index f12b6cac5531e6..2d1409ee383852 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1388,6 +1388,9 @@ public async Task SendAsync_RequestVersion20_ResponseVersion20(Uri server) using (HttpClient client = CreateHttpClient()) { + client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + + "(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"); + using (HttpResponseMessage response = await client.SendAsync(TestAsync, request)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); From 029e1ccecdecb41024949f36c7a412abe68d60ea Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Thu, 6 Feb 2025 16:31:36 +0100 Subject: [PATCH 3/4] Change server to httpbin --- .../Common/tests/System/Net/Configuration.Http.cs | 2 +- .../System/Net/Http/HttpClientHandlerTest.RemoteServer.cs | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Configuration.Http.cs b/src/libraries/Common/tests/System/Net/Configuration.Http.cs index 846aa340a9dd58..3ae47fe61a7610 100644 --- a/src/libraries/Common/tests/System/Net/Configuration.Http.cs +++ b/src/libraries/Common/tests/System/Net/Configuration.Http.cs @@ -21,7 +21,7 @@ public static partial class Http // This server doesn't use HTTP/2 server push (push promise) feature. Some HttpClient implementations // don't support servers that use push right now. - public static string Http2NoPushHost => GetValue("DOTNET_TEST_HTTP2NOPUSHHOST", "www.microsoft.com"); + public static string Http2NoPushHost => GetValue("DOTNET_TEST_HTTP2NOPUSHHOST", "httpbin.org/get"); // Domain server environment. public static string DomainJoinedHttpHost => GetValue("DOTNET_TEST_DOMAINJOINED_HTTPHOST"); diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 2d1409ee383852..6596a330ddfa68 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1381,16 +1381,8 @@ public async Task SendAsync_RequestVersion20_ResponseVersion20(Uri server) var request = new HttpRequestMessage(HttpMethod.Get, server); request.Version = new Version(2, 0); - request.Headers.TryAddWithoutValidation("User-Agent", - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + - "AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/90.0.4430.93 Safari/537.36"); - using (HttpClient client = CreateHttpClient()) { - client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + - "(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"); - using (HttpResponseMessage response = await client.SendAsync(TestAsync, request)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); From 5c23714659f13ecab60d5ba9f8ba77ade8430646 Mon Sep 17 00:00:00 2001 From: Roman Konecny Date: Fri, 7 Feb 2025 15:27:47 +0100 Subject: [PATCH 4/4] Update Http2NoPushHost and add Http2NoPushGetUris for improved testing --- src/libraries/Common/tests/System/Net/Configuration.Http.cs | 3 ++- .../System/Net/Http/HttpClientHandlerTest.RemoteServer.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Configuration.Http.cs b/src/libraries/Common/tests/System/Net/Configuration.Http.cs index 3ae47fe61a7610..d4aee66d4f439d 100644 --- a/src/libraries/Common/tests/System/Net/Configuration.Http.cs +++ b/src/libraries/Common/tests/System/Net/Configuration.Http.cs @@ -21,7 +21,7 @@ public static partial class Http // This server doesn't use HTTP/2 server push (push promise) feature. Some HttpClient implementations // don't support servers that use push right now. - public static string Http2NoPushHost => GetValue("DOTNET_TEST_HTTP2NOPUSHHOST", "httpbin.org/get"); + public static string Http2NoPushHost => GetValue("DOTNET_TEST_HTTP2NOPUSHHOST", "httpbin.org"); // Domain server environment. public static string DomainJoinedHttpHost => GetValue("DOTNET_TEST_DOMAINJOINED_HTTPHOST"); @@ -95,6 +95,7 @@ public static partial class Http public static readonly object[][] Http2Servers = { new object[] { new Uri("https://" + Http2Host) } }; public static readonly object[][] Http2NoPushServers = { new object[] { new Uri("https://" + Http2NoPushHost) } }; + public static readonly object[][] Http2NoPushGetUris = { new object[] { new Uri("https://" + Http2NoPushHost + "/get") } }; public static readonly RemoteServer RemoteHttp11Server = new RemoteServer(new Uri("http://" + Host + "/"), HttpVersion.Version11); public static readonly RemoteServer RemoteSecureHttp11Server = new RemoteServer(new Uri("https://" + SecureHost + "/"), HttpVersion.Version11); diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 6596a330ddfa68..aef5df1ec02804 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -31,6 +31,7 @@ public sealed class HttpClientHandler_RemoteServerTest : HttpClientHandlerTestBa public static readonly object[][] Http2Servers = Configuration.Http.Http2Servers; public static readonly object[][] Http2NoPushServers = Configuration.Http.Http2NoPushServers; + public static readonly object[][] Http2NoPushGetUris = Configuration.Http.Http2NoPushGetUris; // Standard HTTP methods defined in RFC7231: http://tools.ietf.org/html/rfc7231#section-4.3 // "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE" @@ -1368,7 +1369,7 @@ public async Task SendAsync_RequestVersion20_ResponseVersion20IfHttp2Supported(U } [OuterLoop("Uses external servers")] - [ConditionalTheory(nameof(IsWindows10Version1607OrGreater)), MemberData(nameof(Http2NoPushServers))] + [ConditionalTheory(nameof(IsWindows10Version1607OrGreater)), MemberData(nameof(Http2NoPushGetUris))] public async Task SendAsync_RequestVersion20_ResponseVersion20(Uri server) { // Sync API supported only up to HTTP/1.1