@@ -266,6 +266,23 @@ where
266
266
}
267
267
. boxed ( )
268
268
}
269
+ /// Handles requests without body.
270
+ fn handle_requests_without_body ( & self ) -> GraphQLServiceResponse {
271
+ async {
272
+ let response_obj = json ! ( {
273
+ "message" : "Body is required"
274
+ } ) ;
275
+ let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
276
+
277
+ Ok ( Response :: builder ( )
278
+ . status ( StatusCode :: BAD_REQUEST )
279
+ . header ( CONTENT_TYPE , "application/json" )
280
+ . header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
281
+ . body ( Body :: from ( response_str) )
282
+ . unwrap ( ) )
283
+ }
284
+ . boxed ( )
285
+ }
269
286
270
287
fn has_request_body ( & self , req : & Request < Body > ) -> bool {
271
288
if let Some ( length) = req. headers ( ) . get ( hyper:: header:: CONTENT_LENGTH ) {
@@ -294,10 +311,14 @@ where
294
311
let headers = req. headers ( ) ;
295
312
let content_type = headers. get ( "content-type" ) ;
296
313
297
- if method == Method :: POST && ( content_type. is_none ( ) || ! self . has_request_body ( & req ) ) {
314
+ if method == Method :: POST && ( content_type. is_none ( ) ) {
298
315
return self . handle_requests_without_content_type ( ) . boxed ( ) ;
299
316
}
300
317
318
+ if method == Method :: POST && !self . has_request_body ( & req) {
319
+ return self . handle_requests_without_body ( ) . boxed ( ) ;
320
+ }
321
+
301
322
match ( method, path_segments. as_slice ( ) ) {
302
323
( Method :: GET , [ "" ] ) => self . index ( ) . boxed ( ) ,
303
324
( Method :: GET , & [ "subgraphs" , "id" , _, "graphql" ] )
@@ -410,7 +431,8 @@ where
410
431
411
432
#[ cfg( test) ]
412
433
mod tests {
413
- use graph:: data:: value:: { Object , Word } ;
434
+ use graph:: data:: value:: Object ;
435
+ use graph:: prelude:: serde_json:: json;
414
436
use http:: header:: CONTENT_TYPE ;
415
437
use http:: status:: StatusCode ;
416
438
use hyper:: service:: Service ;
@@ -533,14 +555,15 @@ mod tests {
533
555
534
556
let response =
535
557
futures03:: executor:: block_on ( service. call ( request) ) . expect ( "Should return a response" ) ;
536
- let errors = test_utils:: assert_error_response ( response, StatusCode :: BAD_REQUEST , false ) ;
558
+ let errors = test_utils:: assert_error_response ( response, StatusCode :: OK , false ) ;
537
559
538
560
let message = errors[ 0 ] . as_str ( ) . expect ( "Error message is not a string" ) ;
539
561
540
- assert_eq ! (
541
- message,
542
- "GraphQL server error (client error): The \" query\" field is missing in request data"
543
- ) ;
562
+ let response = json ! ( {
563
+ "error" : "GraphQL server error (client error): The \" query\" field is missing in request data" . to_string( )
564
+ } ) ;
565
+
566
+ assert_eq ! ( message, response. to_string( ) ) ;
544
567
}
545
568
546
569
#[ tokio:: test( flavor = "multi_thread" ) ]
0 commit comments