Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Add regression tests for 'Connection: keep-alive, upgrade' request he…
Browse files Browse the repository at this point in the history
…ader (#1171).
  • Loading branch information
Cesar Blum Silveira committed Nov 3, 2016
1 parent 25d647c commit a4398aa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IHttpUpgradeFeature>().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()
Expand Down
21 changes: 21 additions & 0 deletions test/Microsoft.AspNetCore.Server.KestrelTests/MessageBodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(buffer, 0, 5));
}
}

private void AssertASCII(string expected, ArraySegment<byte> actual)
{
var encoding = Encoding.ASCII;
Expand Down

0 comments on commit a4398aa

Please sign in to comment.