@@ -3,7 +3,7 @@ import type { APIGatewayProxyEvent } from 'aws-lambda'
3
3
4
4
import { normalizeRequest } from '../transforms'
5
5
6
- export const createMockedEvent = (
6
+ export const createMockedLambdaEvent = (
7
7
httpMethod = 'POST' ,
8
8
body : any = undefined ,
9
9
isBase64Encoded = false
@@ -53,41 +53,78 @@ export const createMockedEvent = (
53
53
}
54
54
}
55
55
56
- test ( 'Normalizes an aws event with base64' , ( ) => {
57
- const corsEventB64 = createMockedEvent (
58
- 'POST' ,
59
- Buffer . from ( JSON . stringify ( { bazinga : 'hello_world' } ) , 'utf8' ) . toString (
60
- 'base64'
61
- ) ,
62
- true
63
- )
56
+ describe ( 'Lambda Request' , ( ) => {
57
+ it ( 'Normalizes an aws event with base64' , async ( ) => {
58
+ const corsEventB64 = createMockedLambdaEvent (
59
+ 'POST' ,
60
+ Buffer . from ( JSON . stringify ( { bazinga : 'hello_world' } ) , 'utf8' ) . toString (
61
+ 'base64'
62
+ ) ,
63
+ true
64
+ )
64
65
65
- expect ( normalizeRequest ( corsEventB64 ) ) . toEqual ( {
66
- headers : new Headers ( corsEventB64 . headers ) ,
67
- method : 'POST' ,
68
- query : null ,
69
- body : {
70
- bazinga : 'hello_world' ,
71
- } ,
66
+ expect ( await normalizeRequest ( corsEventB64 ) ) . toEqual ( {
67
+ headers : new Headers ( corsEventB64 . headers as Record < string , string > ) ,
68
+ method : 'POST' ,
69
+ query : null ,
70
+ jsonBody : {
71
+ bazinga : 'hello_world' ,
72
+ } ,
73
+ } )
72
74
} )
73
- } )
74
75
75
- test ( 'Handles CORS requests with and without b64 encoded' , ( ) => {
76
- const corsEventB64 = createMockedEvent ( 'OPTIONS' , undefined , true )
76
+ it ( 'Handles CORS requests with and without b64 encoded' , async ( ) => {
77
+ const corsEventB64 = createMockedLambdaEvent ( 'OPTIONS' , undefined , true )
78
+
79
+ expect ( await normalizeRequest ( corsEventB64 ) ) . toEqual ( {
80
+ headers : new Headers ( corsEventB64 . headers as Record < string , string > ) , // headers returned as symbol
81
+ method : 'OPTIONS' ,
82
+ query : null ,
83
+ jsonBody : undefined ,
84
+ } )
85
+
86
+ const corsEventWithoutB64 = createMockedLambdaEvent (
87
+ 'OPTIONS' ,
88
+ undefined ,
89
+ false
90
+ )
77
91
78
- expect ( normalizeRequest ( corsEventB64 ) ) . toEqual ( {
79
- headers : new Headers ( corsEventB64 . headers ) , // headers returned as symbol
80
- method : 'OPTIONS' ,
81
- query : null ,
82
- body : undefined ,
92
+ expect ( await normalizeRequest ( corsEventWithoutB64 ) ) . toEqual ( {
93
+ headers : new Headers ( corsEventB64 . headers as Record < string , string > ) , // headers returned as symbol
94
+ method : 'OPTIONS' ,
95
+ query : null ,
96
+ jsonBody : undefined ,
97
+ } )
83
98
} )
99
+ } )
100
+
101
+ describe ( 'Fetch API Request' , ( ) => {
102
+ it ( 'Normalizes a fetch event' , async ( ) => {
103
+ const fetchEvent = new Request (
104
+ 'http://localhost:9210/graphql?whatsup=doc&its=bugs' ,
105
+ {
106
+ method : 'POST' ,
107
+ headers : {
108
+ 'content-type' : 'application/json' ,
109
+ } ,
110
+ body : JSON . stringify ( { bazinga : 'kittens_purr_purr' } ) ,
111
+ }
112
+ )
113
+
114
+ const partial = await normalizeRequest ( fetchEvent )
84
115
85
- const corsEventWithoutB64 = createMockedEvent ( 'OPTIONS' , undefined , false )
116
+ expect ( partial ) . toMatchObject ( {
117
+ // headers: fetchEvent.headers,
118
+ method : 'POST' ,
119
+ query : {
120
+ whatsup : 'doc' ,
121
+ its : 'bugs' ,
122
+ } ,
123
+ jsonBody : {
124
+ bazinga : 'kittens_purr_purr' ,
125
+ } ,
126
+ } )
86
127
87
- expect ( normalizeRequest ( corsEventWithoutB64 ) ) . toEqual ( {
88
- headers : new Headers ( corsEventB64 . headers ) , // headers returned as symbol
89
- method : 'OPTIONS' ,
90
- query : null ,
91
- body : undefined ,
128
+ expect ( partial . headers . get ( 'content-type' ) ) . toEqual ( 'application/json' )
92
129
} )
93
130
} )
0 commit comments