@@ -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" ] )
@@ -411,6 +432,7 @@ where
411
432
#[ cfg( test) ]
412
433
mod tests {
413
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 ;
@@ -537,14 +559,15 @@ mod tests {
537
559
538
560
let response =
539
561
futures03:: executor:: block_on ( service. call ( request) ) . expect ( "Should return a response" ) ;
540
- let errors = test_utils:: assert_error_response ( response, StatusCode :: BAD_REQUEST , false ) ;
562
+ let errors = test_utils:: assert_error_response ( response, StatusCode :: OK , false ) ;
541
563
542
564
let message = errors[ 0 ] . as_str ( ) . expect ( "Error message is not a string" ) ;
543
565
544
- assert_eq ! (
545
- message,
546
- "GraphQL server error (client error): The \" query\" field is missing in request data"
547
- ) ;
566
+ let response = json ! ( {
567
+ "error" : "GraphQL server error (client error): The \" query\" field is missing in request data" . to_string( )
568
+ } ) ;
569
+
570
+ assert_eq ! ( message, response. to_string( ) ) ;
548
571
}
549
572
550
573
#[ tokio:: test( flavor = "multi_thread" ) ]
0 commit comments