2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . IO ;
6
5
using System . Linq ;
7
6
using System . Net ;
8
7
using System . Net . Http ;
@@ -762,14 +761,11 @@ await connection.Receive(
762
761
[ Fact ]
763
762
public async Task HeadResponseCanContainContentLengthHeader ( )
764
763
{
765
- var testLogger = new TestApplicationErrorLogger ( ) ;
766
- var serviceContext = new TestServiceContext { Log = new TestKestrelTrace ( testLogger ) } ;
767
-
768
764
using ( var server = new TestServer ( httpContext =>
769
765
{
770
766
httpContext . Response . ContentLength = 42 ;
771
767
return TaskCache . CompletedTask ;
772
- } , serviceContext ) )
768
+ } , new TestServiceContext ( ) ) )
773
769
{
774
770
using ( var connection = server . CreateConnection ( ) )
775
771
{
@@ -778,7 +774,7 @@ await connection.SendEnd(
778
774
"" ,
779
775
"" ) ;
780
776
await connection . ReceiveEnd (
781
- $ "HTTP/1.1 200 OK",
777
+ "HTTP/1.1 200 OK" ,
782
778
$ "Date: { server . Context . DateHeaderValue } ",
783
779
"Content-Length: 42" ,
784
780
"" ,
@@ -790,14 +786,11 @@ await connection.ReceiveEnd(
790
786
[ Fact ]
791
787
public async Task HeadResponseCanContainContentLengthHeaderButBodyNotWritten ( )
792
788
{
793
- var testLogger = new TestApplicationErrorLogger ( ) ;
794
- var serviceContext = new TestServiceContext { Log = new TestKestrelTrace ( testLogger ) } ;
795
-
796
789
using ( var server = new TestServer ( async httpContext =>
797
790
{
798
791
httpContext . Response . ContentLength = 12 ;
799
792
await httpContext . Response . WriteAsync ( "hello, world" ) ;
800
- } , serviceContext ) )
793
+ } , new TestServiceContext ( ) ) )
801
794
{
802
795
using ( var connection = server . CreateConnection ( ) )
803
796
{
@@ -806,7 +799,7 @@ await connection.SendEnd(
806
799
"" ,
807
800
"" ) ;
808
801
await connection . ReceiveEnd (
809
- $ "HTTP/1.1 200 OK",
802
+ "HTTP/1.1 200 OK" ,
810
803
$ "Date: { server . Context . DateHeaderValue } ",
811
804
"Content-Length: 12" ,
812
805
"" ,
@@ -815,6 +808,46 @@ await connection.ReceiveEnd(
815
808
}
816
809
}
817
810
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
+
818
851
public static TheoryData < string , StringValues , string > NullHeaderData
819
852
{
820
853
get
0 commit comments