From b55bef20aa8ca7e5c14b06a4d89d7ba192029891 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 4 Mar 2016 14:47:35 -0800 Subject: [PATCH] Allow the server to forcefully close socket it tests with connection failures - This is a delayed reaction to 54caf30 which causes connections closed from the server to be closed less gracefully due to no longer waiting for a FIN from the client --- .../ChunkedRequestTests.cs | 4 ++-- .../ChunkedResponseTests.cs | 4 ++-- .../EngineTests.cs | 10 +++++----- .../TestConnection.cs | 19 +++++++++++++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs index f741539480..8b34273913 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs @@ -373,7 +373,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "HTTP/1.1 400 Bad Request", ""); await connection.ReceiveStartsWith("Date:"); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "Content-Length: 0", "Server: Kestrel", "", @@ -417,7 +417,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "HTTP/1.1 400 Bad Request", ""); await connection.ReceiveStartsWith("Date:"); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "Content-Length: 0", "Server: Kestrel", "", diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedResponseTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedResponseTests.cs index 106959057a..59b2e16e10 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedResponseTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedResponseTests.cs @@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "GET / HTTP/1.1", "", ""); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Transfer-Encoding: chunked", "", @@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests ""); // Headers are sent before connection is closed, but chunked body terminator isn't sent - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Transfer-Encoding: chunked", "", diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs index 2a4e788c58..9c42dac2af 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs @@ -263,7 +263,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = new TestConnection(server.Port)) { - await connection.Send( + await connection.SendEnd( "POST / HTTP/1.0", "Content-Length: 11", "", @@ -638,7 +638,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "GET / HTTP/1.1", "", ""); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Content-Length: 11", "", @@ -680,7 +680,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "GET / HTTP/1.1", "", ""); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Content-Length: 11", "", @@ -870,7 +870,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "GET / HTTP/1.1", "", ""); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Content-Length: 11", "", @@ -941,7 +941,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests "", ""); - await connection.ReceiveEnd( + await connection.ReceiveForcedEnd( "HTTP/1.1 200 OK", "Content-Length: 5", "", diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs index f7f3e36b4e..1c929bcb57 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs @@ -126,6 +126,25 @@ namespace Microsoft.AspNetCore.Server.KestrelTests Assert.Equal("", text); } + public async Task ReceiveForcedEnd(params string[] lines) + { + await Receive(lines); + + try + { + var ch = new char[128]; + var count = await _reader.ReadAsync(ch, 0, 128); + var text = new string(ch, 0, count); + Assert.Equal("", text); + } + catch (IOException) + { + // The server is forcefully closing the connection so an IOException: + // "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." + // isn't guaranteed but not unexpected. + } + } + public static Socket CreateConnectedLoopbackSocket(int port) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);