From a4398aa8b39756dcb66a824d4521058301600720 Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Thu, 3 Nov 2016 15:34:55 -0700 Subject: [PATCH] Add regression tests for 'Connection: keep-alive, upgrade' request header (#1171). --- .../RequestTests.cs | 42 +++++++++++++++++++ .../MessageBodyTests.cs | 21 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs index a01b84f7b..2c3aabf5e 100644 --- a/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/RequestTests.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Testing.xunit; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -184,6 +185,47 @@ public async Task StreamsAreNotPersistedAcrossRequests() } } + [Fact] + public void CanUpgradeRequestWithConnectionKeepAliveUpgradeHeader() + { + var dataRead = false; + var builder = new WebHostBuilder() + .UseKestrel() + .UseUrls($"http://127.0.0.1:0") + .Configure(app => + { + app.Run(async context => + { + var stream = await context.Features.Get().UpgradeAsync(); + var data = new byte[3]; + var bytesRead = 0; + + while (bytesRead < 3) + { + bytesRead += await stream.ReadAsync(data, bytesRead, data.Length - bytesRead); + } + + dataRead = Encoding.ASCII.GetString(data, 0, 3) == "abc"; + }); + }); + + using (var host = builder.Build()) + { + host.Start(); + + using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + socket.Connect(new IPEndPoint(IPAddress.Loopback, host.GetPort())); + socket.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nConnection: keep-alive, upgrade\r\n\r\n")); + socket.Send(Encoding.ASCII.GetBytes("abc")); + + while (socket.Receive(new byte[1024]) > 0) ; + } + } + + Assert.True(dataRead); + } + private async Task TestRemoteIPAddress(string registerAddress, string requestAddress, string expectAddress) { var builder = new WebHostBuilder() diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs index f8dba124e..96a1239ee 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs @@ -88,6 +88,27 @@ public async Task CanHandleLargeBlocks() } } + [Theory] + [InlineData("keep-alive, upgrade")] + [InlineData("Keep-Alive, Upgrade")] + [InlineData("upgrade, keep-alive")] + [InlineData("Upgrade, Keep-Alive")] + public void ConnectionUpgradeKeepAlive(string headerConnection) + { + using (var input = new TestInput()) + { + var body = MessageBody.For("HTTP/1.1", new FrameRequestHeaders { HeaderConnection = headerConnection }, input.FrameContext); + var stream = new FrameRequestStream(); + stream.StartAcceptingReads(body); + + input.Add("Hello", true); + + var buffer = new byte[1024]; + Assert.Equal(5, stream.Read(buffer, 0, 1024)); + AssertASCII("Hello", new ArraySegment(buffer, 0, 5)); + } + } + private void AssertASCII(string expected, ArraySegment actual) { var encoding = Encoding.ASCII;