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
This commit is contained in:
Stephen Halter 2016-06-06 15:10:41 -07:00
parent badbc7c8f7
commit c1dadbd723
3 changed files with 20 additions and 11 deletions

View File

@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.SendAllEnd(request); await connection.SendAllTryEnd(request);
await ReceiveBadRequestResponse(connection); await ReceiveBadRequestResponse(connection);
} }
} }
@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.Send(request); await connection.SendAll(request);
await ReceiveBadRequestResponse(connection); await ReceiveBadRequestResponse(connection);
} }
} }
@ -166,7 +166,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) 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); await ReceiveBadRequestResponse(connection);
} }
} }
@ -179,7 +179,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.SendEnd( await connection.SendAllTryEnd(
"GET / HTTP/1.1", "GET / HTTP/1.1",
"H\u00eb\u00e4d\u00ebr: value", "H\u00eb\u00e4d\u00ebr: value",
"", "",
@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) 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); await ReceiveBadRequestResponse(connection);
} }
} }

View File

@ -7,7 +7,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests namespace Microsoft.AspNetCore.Server.KestrelTests
@ -365,7 +364,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.Send( await connection.SendAll(
"POST / HTTP/1.1", "POST / HTTP/1.1",
"Transfer-Encoding: chunked", "Transfer-Encoding: chunked",
"", "",
@ -407,7 +406,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.Send( await connection.SendAll(
"POST / HTTP/1.1", "POST / HTTP/1.1",
"Transfer-Encoding: chunked", "Transfer-Encoding: chunked",
"", "",

View File

@ -46,16 +46,26 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
public async Task SendAll(params string[] lines) public async Task SendAll(params string[] lines)
{ {
var text = String.Join("\r\n", 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); await writer.WriteAsync(text);
writer.Flush(); writer.Flush();
_stream.Flush(); _stream.Flush();
} }
public async Task SendAllEnd(params string[] lines) public async Task SendAllTryEnd(params string[] lines)
{ {
await SendAll(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) public async Task Send(params string[] lines)