@@ -12,16 +12,24 @@ namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests
12
12
{
13
13
public class HttpJsonBodyParserMiddlewareShould
14
14
{
15
+ private MiddyNetContext context ;
16
+ private TestObject expectation ;
17
+ private string serializedExpectation ;
18
+
19
+ public HttpJsonBodyParserMiddlewareShould ( )
20
+ {
21
+ context = new MiddyNetContext ( Substitute . For < ILambdaContext > ( ) ) ;
22
+ expectation = new TestObject ( "bar" ) ;
23
+ serializedExpectation = JsonConvert . SerializeObject ( expectation ) ;
24
+ }
25
+
15
26
[ Fact ]
16
27
public async Task ProcessTheJsonRequest ( )
17
28
{
18
- var context = new MiddyNetContext ( Substitute . For < ILambdaContext > ( ) ) ;
19
- var expectation = new TestObject ( "bar" ) ;
20
- var source = JsonConvert . SerializeObject ( expectation ) ;
21
29
var request = new APIGatewayProxyRequest ( )
22
30
{
23
31
Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } } ,
24
- Body = source
32
+ Body = serializedExpectation
25
33
} ;
26
34
var middleware = new HttpJsonBodyParserMiddleware < TestObject > ( ) ;
27
35
await middleware . Before ( request , context ) ;
@@ -33,9 +41,7 @@ public async Task ProcessTheJsonRequest()
33
41
[ Fact ]
34
42
public async Task ErrorWhenJsonNotMapsToObject ( )
35
43
{
36
- var context = new MiddyNetContext ( Substitute . For < ILambdaContext > ( ) ) ;
37
- var expectation = new TestObject ( "bar" ) ;
38
- var source = "Not Mapped object" + JsonConvert . SerializeObject ( expectation ) ;
44
+ var source = "Not Mapped object" + serializedExpectation ;
39
45
var request = new APIGatewayProxyRequest ( )
40
46
{
41
47
Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } } ,
@@ -46,5 +52,19 @@ public async Task ErrorWhenJsonNotMapsToObject()
46
52
47
53
action . Should ( ) . Throw < Exception > ( ) . WithMessage ( $ "Error parsing \" { source } \" to type { typeof ( TestObject ) } ") ;
48
54
}
55
+
56
+ [ Fact ]
57
+ public async Task NotProcessTheBodyIfNoHeaderIsPassed ( )
58
+ {
59
+ var request = new APIGatewayProxyRequest ( )
60
+ {
61
+ Body = serializedExpectation
62
+ } ;
63
+ var middleware = new HttpJsonBodyParserMiddleware < TestObject > ( ) ;
64
+ await middleware . Before ( request , context ) ;
65
+
66
+ context . AdditionalContext . ContainsKey ( "Body" ) . Should ( ) . BeTrue ( ) ;
67
+ context . AdditionalContext [ "Body" ] . Should ( ) . Be ( serializedExpectation ) ;
68
+ }
49
69
}
50
70
}
0 commit comments