Skip to content

Commit 0f33f0f

Browse files
committed
server: Add test helper function to ensure CORS headers are set right
1 parent a711610 commit 0f33f0f

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

server/http/src/test_utils.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use graph::prelude::serde_json;
22
use graph::prelude::*;
33
use http::StatusCode;
4-
use hyper::{Body, Response};
4+
use hyper::{header::ACCESS_CONTROL_ALLOW_ORIGIN, Body, Response};
55

66
/// Asserts that the response is a successful GraphQL response; returns its `"data"` field.
77
pub fn assert_successful_response(
88
response: Response<Body>,
99
) -> serde_json::Map<String, serde_json::Value> {
1010
assert_eq!(response.status(), StatusCode::OK);
11-
11+
assert_expected_headers(&response);
1212
futures03::executor::block_on(
1313
hyper::body::to_bytes(response.into_body())
1414
.map_ok(|chunk| {
@@ -35,7 +35,7 @@ pub fn assert_error_response(
3535
graphql_response: bool,
3636
) -> Vec<serde_json::Value> {
3737
assert_eq!(response.status(), expected_status);
38-
38+
assert_expected_headers(&response);
3939
let body = String::from_utf8(
4040
futures03::executor::block_on(hyper::body::to_bytes(response.into_body()))
4141
.unwrap()
@@ -59,3 +59,13 @@ pub fn assert_error_response(
5959
.expect("GraphQL \"errors\" field must be a vector")
6060
.clone()
6161
}
62+
63+
pub fn assert_expected_headers(response: &Response<Body>) {
64+
assert_eq!(
65+
response
66+
.headers()
67+
.get(ACCESS_CONTROL_ALLOW_ORIGIN)
68+
.expect("Missing CORS Header"),
69+
&"*"
70+
);
71+
}

server/http/tests/response.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ use std::collections::BTreeMap;
88
fn generates_200_for_query_results() {
99
let data = BTreeMap::new();
1010
let query_result = QueryResults::from(data).as_http_response();
11+
test_utils::assert_expected_headers(&query_result);
1112
test_utils::assert_successful_response(query_result);
1213
}
1314

1415
#[test]
1516
fn generates_valid_json_for_an_empty_result() {
1617
let data = BTreeMap::new();
1718
let query_result = QueryResults::from(data).as_http_response();
19+
test_utils::assert_expected_headers(&query_result);
1820
let data = test_utils::assert_successful_response(query_result);
1921
assert!(data.is_empty());
2022
}

0 commit comments

Comments
 (0)