From e78eb85245a5ee315b3e8d7fe0530ddc476bf503 Mon Sep 17 00:00:00 2001 From: David Shulman Date: Mon, 21 May 2018 18:08:01 -0700 Subject: [PATCH] Fix HttpWebRequest Connection header test (dotnet/corefx#29833) Change the test to use a loopback server instead of an external server. And make sure to not use a proxy since it isn't needed anyway. These changes will improve the test reliability. Fixes dotnet/corefx#19225 Fixes dotnet/corefx#28437 Commit migrated from https://github.com/dotnet/corefx/commit/6fc873aee0d80622e12aed29d4e6fffd065afcfc --- .../tests/HttpWebRequestTest.cs | 37 ++++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Net.Requests/tests/HttpWebRequestTest.cs b/src/libraries/System.Net.Requests/tests/HttpWebRequestTest.cs index 2fa91f6..d3effb6 100644 --- a/src/libraries/System.Net.Requests/tests/HttpWebRequestTest.cs +++ b/src/libraries/System.Net.Requests/tests/HttpWebRequestTest.cs @@ -455,32 +455,35 @@ namespace System.Net.Tests [InlineData(null)] [InlineData(false)] [InlineData(true)] - [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "dotnet/corefx #19225")] - public void KeepAlive_CorrectConnectionHeaderSent(bool? keepAlive) + public async Task KeepAlive_CorrectConnectionHeaderSent(bool? keepAlive) { - HttpWebRequest request = WebRequest.CreateHttp(Test.Common.Configuration.Http.RemoteEchoServer); - - if (keepAlive.HasValue) + await LoopbackServer.CreateServerAsync(async (server, url) => { - request.KeepAlive = keepAlive.Value; - } + HttpWebRequest request = WebRequest.CreateHttp(url); + request.Proxy = null; // Don't use a proxy since it might interfere with the Connection: headers. + if (keepAlive.HasValue) + { + request.KeepAlive = keepAlive.Value; + } - using (var response = (HttpWebResponse)request.GetResponse()) - using (var body = new StreamReader(response.GetResponseStream())) - { - string content = body.ReadToEnd(); + Task getResponseTask = request.GetResponseAsync(); + Task> serverTask = server.AcceptConnectionSendResponseAndCloseAsync(); + + await TaskTimeoutExtensions.WhenAllOrAnyFailed(new Task[] { getResponseTask, serverTask }); + + List requestLines = await serverTask; if (!keepAlive.HasValue || keepAlive.Value) { - // Validate that the request doesn't contain Connection: "close", but we can't validate - // that it does contain Connection: "keep-alive", as that's optional as of HTTP 1.1. - Assert.DoesNotContain("\"Connection\": \"close\"", content, StringComparison.OrdinalIgnoreCase); + // Validate that the request doesn't contain "Connection: close", but we can't validate + // that it does contain "Connection: Keep-Alive", as that's optional as of HTTP 1.1. + Assert.DoesNotContain("Connection: close", requestLines, StringComparer.OrdinalIgnoreCase); } else { - Assert.Contains("\"Connection\": \"close\"", content, StringComparison.OrdinalIgnoreCase); - Assert.DoesNotContain("\"Keep-Alive\"", content, StringComparison.OrdinalIgnoreCase); + Assert.Contains("Connection: close", requestLines, StringComparer.OrdinalIgnoreCase); + Assert.DoesNotContain("Keep-Alive", requestLines, StringComparer.OrdinalIgnoreCase); } - } + }); } [Theory, MemberData(nameof(EchoServers))] -- 2.7.4