From c1dadbd7234ed4023adfaf8e1d859d8d6bf6d35e Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 6 Jun 2016 15:10:41 -0700 Subject: [PATCH] Make Bad Request tests more reliable - Avoid calling write again after the request is already rejected - Don't try to close a socket from the client if we already expect the server to forcefully close the socket --- .../BadHttpRequestTests.cs | 10 +++++----- .../ChunkedRequestTests.cs | 5 ++--- .../TestConnection.cs | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/BadHttpRequestTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/BadHttpRequestTests.cs index 591fac9c22..1849541e23 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/BadHttpRequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/BadHttpRequestTests.cs @@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.SendAllEnd(request); + await connection.SendAllTryEnd(request); await ReceiveBadRequestResponse(connection); } } @@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.Send(request); + await connection.SendAll(request); await ReceiveBadRequestResponse(connection); } } @@ -166,7 +166,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.SendAllEnd($"GET / HTTP/1.1\r\n{rawHeaders}"); + await connection.SendAllTryEnd($"GET / HTTP/1.1\r\n{rawHeaders}"); await ReceiveBadRequestResponse(connection); } } @@ -179,7 +179,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.SendEnd( + await connection.SendAllTryEnd( "GET / HTTP/1.1", "H\u00eb\u00e4d\u00ebr: value", "", @@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.SendEnd($"GET {path} HTTP/1.1\r\n"); + await connection.SendAllTryEnd($"GET {path} HTTP/1.1\r\n"); await ReceiveBadRequestResponse(connection); } } diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs index 173912d594..e3ef2109df 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/ChunkedRequestTests.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Server.Kestrel; using Xunit; namespace Microsoft.AspNetCore.Server.KestrelTests @@ -365,7 +364,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.Send( + await connection.SendAll( "POST / HTTP/1.1", "Transfer-Encoding: chunked", "", @@ -407,7 +406,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests { using (var connection = server.CreateConnection()) { - await connection.Send( + await connection.SendAll( "POST / HTTP/1.1", "Transfer-Encoding: chunked", "", diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs index 7bae6b888b..41ed1501f6 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/TestConnection.cs @@ -46,16 +46,26 @@ namespace Microsoft.AspNetCore.Server.KestrelTests public async Task SendAll(params string[] lines) { var text = String.Join("\r\n", lines); - var writer = new StreamWriter(_stream, Encoding.ASCII); + var writer = new StreamWriter(_stream, Encoding.GetEncoding("iso-8859-1")); await writer.WriteAsync(text); writer.Flush(); _stream.Flush(); } - public async Task SendAllEnd(params string[] lines) + public async Task SendAllTryEnd(params string[] lines) { await SendAll(lines); - _socket.Shutdown(SocketShutdown.Send); + + try + { + _socket.Shutdown(SocketShutdown.Send); + } + catch (IOException) + { + // The server may forcefully close the connection (usually due to a bad request), + // so an IOException: "An existing connection was forcibly closed by the remote host" + // isn't guaranteed but not unexpected. + } } public async Task Send(params string[] lines)