From cb2b0b19ceeb8a8e6605eb015a7d401b1773c693 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 ac8abebe5d4bb2..3e157c785a476b 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1378,6 +1378,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 1860fef4c56e9d14f292074023fd60788695b5fa 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 3e157c785a476b..b2da776cd15f45 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1385,6 +1385,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 cd4d5682c89fa5f8b5971c430281d2973b5697c7 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 f7e6fc759c0dd5..e6dda2c132d053 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 b2da776cd15f45..ac8abebe5d4bb2 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1378,16 +1378,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 2dfac00450c223364bfcde41360a8d131b883933 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 e6dda2c132d053..310e0c801e593a 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"); @@ -106,6 +106,7 @@ public static Uri[] GetEchoServerList() 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 ac8abebe5d4bb2..3d7dfe9db0dc30 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" @@ -1365,7 +1366,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