File tree 2 files changed +25
-12
lines changed
2 files changed +25
-12
lines changed Original file line number Diff line number Diff line change @@ -12,18 +12,16 @@ import AggregateError from "aggregate-error";
12
12
type IncomingMessage = any ;
13
13
14
14
export function getPayload ( request : IncomingMessage ) : Promise < string > {
15
- if ( "body" in request ) {
16
- if (
17
- typeof request . body === "object" &&
18
- "rawBody" in request &&
19
- request . rawBody instanceof Buffer
20
- ) {
21
- // The body is already an Object and rawBody is a Buffer (e.g. GCF)
22
- return Promise . resolve ( request . rawBody . toString ( "utf8" ) ) ;
23
- } else {
24
- // The body is a String (e.g. Lambda)
25
- return Promise . resolve ( request . body ) ;
26
- }
15
+ if (
16
+ typeof request . body === "object" &&
17
+ "rawBody" in request &&
18
+ request . rawBody instanceof Buffer
19
+ ) {
20
+ // The body is already an Object and rawBody is a Buffer (e.g. GCF)
21
+ return Promise . resolve ( request . rawBody . toString ( "utf8" ) ) ;
22
+ } else if ( typeof request . body === "string" ) {
23
+ // The body is a String (e.g. Lambda)
24
+ return Promise . resolve ( request . body ) ;
27
25
}
28
26
29
27
// We need to load the payload from the request (normal case of Node.js server)
Original file line number Diff line number Diff line change @@ -74,4 +74,19 @@ describe("getPayload", () => {
74
74
75
75
expect ( await promise ) . toEqual ( "foo" ) ;
76
76
} ) ;
77
+
78
+ it ( "resolves with a string if the body key of the request is defined but value is undefined" , async ( ) => {
79
+ const request = new EventEmitter ( ) ;
80
+ // @ts -ignore body is not part of EventEmitter, which we are using
81
+ // to mock the request object
82
+ request . body = undefined ;
83
+
84
+ const promise = getPayload ( request ) ;
85
+
86
+ // we emit data, to ensure that the body attribute is preferred
87
+ request . emit ( "data" , "bar" ) ;
88
+ request . emit ( "end" ) ;
89
+
90
+ expect ( await promise ) . toEqual ( "bar" ) ;
91
+ } ) ;
77
92
} ) ;
You can’t perform that action at this time.
0 commit comments