@@ -17,6 +17,10 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
1717 var httpRequestMock = new Mock < HttpRequest > ( ) ;
1818 httpRequestMock . Setup ( r => r . Path ) . Returns ( new PathString ( "" ) ) ;
1919 httpRequestMock . Setup ( r => r . ContentType ) . Returns ( "application/vnd.api+json" ) ;
20+ httpRequestMock . Setup ( r => r . ContentLength ) . Returns ( 0 ) ;
21+ var headers = new HeaderDictionary ( ) ;
22+ headers . Add ( "Accept" , "application/vnd.api+json" ) ;
23+ httpRequestMock . Setup ( r => r . Headers ) . Returns ( headers ) ;
2024
2125 var httpContextMock = new Mock < HttpContext > ( ) ;
2226 httpContextMock . Setup ( c => c . Request ) . Returns ( httpRequestMock . Object ) ;
@@ -32,13 +36,50 @@ public async void Invoke_CallsHandleJsonApiRequest_OnRouter()
3236 Assert . True ( router . DidHandleRoute ) ;
3337 }
3438
39+ [ Fact ]
40+ public async void Invoke_SetsStatusCode_To415_ForInvalidAcceptType ( )
41+ {
42+ // arrange
43+ var httpRequestMock = new Mock < HttpRequest > ( ) ;
44+ httpRequestMock . Setup ( r => r . Path ) . Returns ( new PathString ( "" ) ) ;
45+ httpRequestMock . Setup ( r => r . ContentType ) . Returns ( "application/vnd.api+json" ) ;
46+ httpRequestMock . Setup ( r => r . ContentLength ) . Returns ( 0 ) ;
47+ var headers = new HeaderDictionary ( ) ;
48+ headers . Add ( "Accept" , "" ) ;
49+ httpRequestMock . Setup ( r => r . Headers ) . Returns ( headers ) ;
50+
51+ var httpResponsMock = new Mock < HttpResponse > ( ) ;
52+ httpResponsMock . SetupAllProperties ( ) ;
53+ httpResponsMock . Setup ( r => r . Body ) . Returns ( new MemoryStream ( ) ) ;
54+
55+ var httpContextMock = new Mock < HttpContext > ( ) ;
56+ httpContextMock . Setup ( c => c . Request ) . Returns ( httpRequestMock . Object ) ;
57+ httpContextMock . Setup ( c => c . Response ) . Returns ( httpResponsMock . Object ) ;
58+
59+ var requestDelegateMock = new Mock < RequestDelegate > ( ) ;
60+
61+ var router = new TestRouter ( ) ;
62+ var loggerMock = new Mock < ILogger < JsonApiMiddleware > > ( ) ;
63+ var middleware = new JsonApiMiddleware ( requestDelegateMock . Object , loggerMock . Object , router , null ) ;
64+
65+ // act
66+ await middleware . Invoke ( httpContextMock . Object ) ;
67+
68+ // assert
69+ Assert . Equal ( 415 , httpResponsMock . Object . StatusCode ) ;
70+ }
71+
3572 [ Fact ]
3673 public async void Invoke_SetsStatusCode_To415_ForInvalidContentType ( )
3774 {
3875 // arrange
3976 var httpRequestMock = new Mock < HttpRequest > ( ) ;
4077 httpRequestMock . Setup ( r => r . Path ) . Returns ( new PathString ( "" ) ) ;
4178 httpRequestMock . Setup ( r => r . ContentType ) . Returns ( "" ) ;
79+ httpRequestMock . Setup ( r => r . ContentLength ) . Returns ( 1 ) ;
80+ var headers = new HeaderDictionary ( ) ;
81+ headers . Add ( "Accept" , "application/vnd.api+json" ) ;
82+ httpRequestMock . Setup ( r => r . Headers ) . Returns ( headers ) ;
4283
4384 var httpResponsMock = new Mock < HttpResponse > ( ) ;
4485 httpResponsMock . SetupAllProperties ( ) ;
0 commit comments