File tree 2 files changed +34
-2
lines changed
2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -94,18 +94,27 @@ export function parseUrl(url) {
94
94
* @param {Request } request The request to read the body from.
95
95
* @returns {Promise<string|any|FormData|null> } The body of the request.
96
96
*/
97
- export function getBody ( request ) {
97
+ export async function getBody ( request ) {
98
+
98
99
// first get the content type
99
100
const contentType = request . headers . get ( "content-type" ) ;
100
101
101
102
// if there's no content type, there's no body
102
103
if ( ! contentType ) {
103
104
return Promise . resolve ( null ) ;
105
+ }
106
+
107
+ // next try to read the body as text to see if there's a body
108
+ const text = await request . clone ( ) . text ( ) ;
109
+
110
+ // if there's no body, return null
111
+ if ( ! text ) {
112
+ return Promise . resolve ( null ) ;
104
113
}
105
114
106
115
// if it's a text format then return a string
107
116
if ( contentType . startsWith ( "text" ) ) {
108
- return request . text ( ) ;
117
+ return text ;
109
118
}
110
119
111
120
// if the content type is JSON, parse the body as JSON
Original file line number Diff line number Diff line change @@ -563,6 +563,29 @@ describe("MockServer", () => {
563
563
"application/json" ,
564
564
) ;
565
565
} ) ;
566
+
567
+ it ( "should return response when JSON request is sent without a body" , async ( ) => {
568
+ server . post ( "/test" , {
569
+ status : 200 ,
570
+ body : { foo : "bar" } ,
571
+ } ) ;
572
+
573
+ const request = createRequest ( {
574
+ method : "POST" ,
575
+ url : `${ BASE_URL } /test` ,
576
+ headers : {
577
+ "content-type" : "application/json" ,
578
+ } ,
579
+ } ) ;
580
+
581
+ const { response } = await server . traceReceive ( request ) ;
582
+ assert . strictEqual ( response . status , 200 ) ;
583
+ assert . strictEqual ( response . statusText , "OK" ) ;
584
+ assert . strictEqual (
585
+ response . headers . get ( "content-type" ) ,
586
+ "application/json" ,
587
+ ) ;
588
+ } ) ;
566
589
} ) ;
567
590
568
591
describe ( "Query Strings" , ( ) => {
You can’t perform that action at this time.
0 commit comments