1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Text ;
3
4
using System . Threading . Tasks ;
4
5
using Amazon . Lambda . APIGatewayEvents ;
5
6
using Amazon . Lambda . Core ;
@@ -41,7 +42,7 @@ public async Task ProcessTheJsonRequest()
41
42
[ Fact ]
42
43
public async Task ErrorWhenJsonNotMapsToObject ( )
43
44
{
44
- var source = "Not Mapped object " + serializedExpectation ;
45
+ var source = "Make it broken " + serializedExpectation ;
45
46
var request = new APIGatewayProxyRequest ( )
46
47
{
47
48
Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } } ,
@@ -50,7 +51,7 @@ public async Task ErrorWhenJsonNotMapsToObject()
50
51
var middleware = new HttpJsonBodyParserMiddleware < TestObject > ( ) ;
51
52
Action action = ( ) => middleware . Before ( request , context ) ;
52
53
53
- action . Should ( ) . Throw < Exception > ( ) . WithMessage ( $ "Error parsing \" { source } \" to type { typeof ( TestObject ) } ") ;
54
+ action . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Content type defined as JSON but an invalid JSON was provided ") ;
54
55
}
55
56
56
57
[ Fact ]
@@ -66,5 +67,43 @@ public async Task NotProcessTheBodyIfNoHeaderIsPassed()
66
67
context . AdditionalContext . ContainsKey ( "Body" ) . Should ( ) . BeTrue ( ) ;
67
68
context . AdditionalContext [ "Body" ] . Should ( ) . Be ( serializedExpectation ) ;
68
69
}
70
+
71
+ [ Fact ]
72
+ public async Task HandleABase64Body ( )
73
+ {
74
+ string base64Serialized = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( serializedExpectation ) ) ;
75
+
76
+ var request = new APIGatewayProxyRequest ( )
77
+ {
78
+ Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } } ,
79
+ IsBase64Encoded = true ,
80
+ Body = base64Serialized
81
+ } ;
82
+
83
+ var middleware = new HttpJsonBodyParserMiddleware < TestObject > ( ) ;
84
+ await middleware . Before ( request , context ) ;
85
+
86
+ context . AdditionalContext . ContainsKey ( "Body" ) . Should ( ) . BeTrue ( ) ;
87
+ context . AdditionalContext [ "Body" ] . Should ( ) . BeEquivalentTo ( expectation ) ;
88
+ }
89
+
90
+ [ Fact ]
91
+ public async Task HandleInvalidBase64Body ( )
92
+ {
93
+ var source = "Make it broken" + serializedExpectation ;
94
+ string base64Serialized = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( source ) ) ;
95
+
96
+ var request = new APIGatewayProxyRequest ( )
97
+ {
98
+ Headers = new Dictionary < string , string > { { "Content-Type" , "application/json" } } ,
99
+ IsBase64Encoded = true ,
100
+ Body = base64Serialized
101
+ } ;
102
+
103
+ var middleware = new HttpJsonBodyParserMiddleware < TestObject > ( ) ;
104
+ Action action = ( ) => middleware . Before ( request , context ) ;
105
+
106
+ action . Should ( ) . Throw < Exception > ( ) . WithMessage ( "Content type defined as JSON but an invalid JSON was provided" ) ;
107
+ }
69
108
}
70
109
}
0 commit comments