Skip to content

Commit b29fad3

Browse files
committed
ci: fix tests
1 parent 40fbbda commit b29fad3

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

server/http/src/service.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,23 @@ where
266266
}
267267
.boxed()
268268
}
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+
}
269286

270287
fn has_request_body(&self, req: &Request<Body>) -> bool {
271288
if let Some(length) = req.headers().get(hyper::header::CONTENT_LENGTH) {
@@ -294,10 +311,14 @@ where
294311
let headers = req.headers();
295312
let content_type = headers.get("content-type");
296313

297-
if method == Method::POST && (content_type.is_none() || !self.has_request_body(&req)) {
314+
if method == Method::POST && (content_type.is_none()) {
298315
return self.handle_requests_without_content_type().boxed();
299316
}
300317

318+
if method == Method::POST && !self.has_request_body(&req) {
319+
return self.handle_requests_without_body().boxed();
320+
}
321+
301322
match (method, path_segments.as_slice()) {
302323
(Method::GET, [""]) => self.index().boxed(),
303324
(Method::GET, &["subgraphs", "id", _, "graphql"])
@@ -411,6 +432,7 @@ where
411432
#[cfg(test)]
412433
mod tests {
413434
use graph::data::value::Object;
435+
use graph::prelude::serde_json::json;
414436
use http::header::CONTENT_TYPE;
415437
use http::status::StatusCode;
416438
use hyper::service::Service;
@@ -537,14 +559,15 @@ mod tests {
537559

538560
let response =
539561
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);
541563

542564
let message = errors[0].as_str().expect("Error message is not a string");
543565

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());
548571
}
549572

550573
#[tokio::test(flavor = "multi_thread")]

0 commit comments

Comments
 (0)