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

Commit 5b79e7e

Browse files
author
Cesar Blum Silveira
committed
Add test to verify app can write its own error response.
1 parent 56ec330 commit 5b79e7e

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/ResponseTests.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.IO;
65
using System.Linq;
76
using System.Net;
87
using System.Net.Http;
@@ -762,14 +761,11 @@ await connection.Receive(
762761
[Fact]
763762
public async Task HeadResponseCanContainContentLengthHeader()
764763
{
765-
var testLogger = new TestApplicationErrorLogger();
766-
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) };
767-
768764
using (var server = new TestServer(httpContext =>
769765
{
770766
httpContext.Response.ContentLength = 42;
771767
return TaskCache.CompletedTask;
772-
}, serviceContext))
768+
}, new TestServiceContext()))
773769
{
774770
using (var connection = server.CreateConnection())
775771
{
@@ -778,7 +774,7 @@ await connection.SendEnd(
778774
"",
779775
"");
780776
await connection.ReceiveEnd(
781-
$"HTTP/1.1 200 OK",
777+
"HTTP/1.1 200 OK",
782778
$"Date: {server.Context.DateHeaderValue}",
783779
"Content-Length: 42",
784780
"",
@@ -790,14 +786,11 @@ await connection.ReceiveEnd(
790786
[Fact]
791787
public async Task HeadResponseCanContainContentLengthHeaderButBodyNotWritten()
792788
{
793-
var testLogger = new TestApplicationErrorLogger();
794-
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) };
795-
796789
using (var server = new TestServer(async httpContext =>
797790
{
798791
httpContext.Response.ContentLength = 12;
799792
await httpContext.Response.WriteAsync("hello, world");
800-
}, serviceContext))
793+
}, new TestServiceContext()))
801794
{
802795
using (var connection = server.CreateConnection())
803796
{
@@ -806,7 +799,7 @@ await connection.SendEnd(
806799
"",
807800
"");
808801
await connection.ReceiveEnd(
809-
$"HTTP/1.1 200 OK",
802+
"HTTP/1.1 200 OK",
810803
$"Date: {server.Context.DateHeaderValue}",
811804
"Content-Length: 12",
812805
"",
@@ -815,6 +808,46 @@ await connection.ReceiveEnd(
815808
}
816809
}
817810

811+
[Fact]
812+
public async Task AppCanWriteOwnBadRequestResponse()
813+
{
814+
var expectedResponse = string.Empty;
815+
var responseWrittenTcs = new TaskCompletionSource<object>();
816+
817+
using (var server = new TestServer(async httpContext =>
818+
{
819+
try
820+
{
821+
await httpContext.Request.Body.ReadAsync(new byte[1], 0, 1);
822+
}
823+
catch (BadHttpRequestException ex)
824+
{
825+
expectedResponse = ex.Message;
826+
httpContext.Response.StatusCode = 400;
827+
httpContext.Response.ContentLength = ex.Message.Length;
828+
await httpContext.Response.WriteAsync(ex.Message);
829+
responseWrittenTcs.SetResult(null);
830+
}
831+
}, new TestServiceContext()))
832+
{
833+
using (var connection = server.CreateConnection())
834+
{
835+
await connection.SendEnd(
836+
"POST / HTTP/1.1",
837+
"Transfer-Encoding: chunked",
838+
"",
839+
"bad");
840+
await responseWrittenTcs.Task;
841+
await connection.ReceiveEnd(
842+
"HTTP/1.1 400 Bad Request",
843+
$"Date: {server.Context.DateHeaderValue}",
844+
$"Content-Length: {expectedResponse.Length}",
845+
"",
846+
expectedResponse);
847+
}
848+
}
849+
}
850+
818851
public static TheoryData<string, StringValues, string> NullHeaderData
819852
{
820853
get

0 commit comments

Comments
 (0)