Add failure cases to http integration tests ()

This commit is contained in:
Christian Legnitto 2018-09-02 10:59:54 -07:00 committed by GitHub
parent fc10b5e8f5
commit f2f68f868a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 14 deletions
juniper/src/http
juniper_iron/src

View file

@ -168,6 +168,15 @@ pub mod tests {
println!(" - test_batched_post"); println!(" - test_batched_post");
test_batched_post(integration); test_batched_post(integration);
println!(" - test_invalid_json");
test_invalid_json(integration);
println!(" - test_invalid_field");
test_invalid_field(integration);
println!(" - test_duplicate_keys");
test_duplicate_keys(integration);
} }
fn unwrap_json_response(response: &TestResponse) -> Json { fn unwrap_json_response(response: &TestResponse) -> Json {
@ -281,4 +290,29 @@ pub mod tests {
).expect("Invalid JSON constant in test") ).expect("Invalid JSON constant in test")
); );
} }
fn test_invalid_json<T: HTTPIntegration>(integration: &T) {
let response = integration.get("/?query=blah");
assert_eq!(response.status_code, 400);
let response = integration.post("/", r#"blah"#);
assert_eq!(response.status_code, 400);
}
fn test_invalid_field<T: HTTPIntegration>(integration: &T) {
// {hero{blah}}
let response = integration.get("/?query=%7Bhero%blah%7D%7D");
assert_eq!(response.status_code, 400);
let response = integration.post("/", r#"{"query": "{hero{blah}}"}"#);
assert_eq!(response.status_code, 400);
}
fn test_duplicate_keys<T: HTTPIntegration>(integration: &T) {
// {hero{name}}
let response = integration.get("/?query=%7B%22query%22%3A%20%22%7Bhero%7Bname%7D%7D%22%2C%20%22query%22%3A%20%22%7Bhero%7Bname%7D%7D%22%7D");
assert_eq!(response.status_code, 400);
let response = integration.post("/", r#"
{"query": "{hero{name}}", "query": "{hero{name}}"}
"#);
assert_eq!(response.status_code, 400);
}
} }

View file

@ -381,7 +381,7 @@ impl From<GraphQLIronError> for IronError {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use iron::prelude::*; use super::*;
use iron::Url; use iron::Url;
use iron::{Handler, Headers}; use iron::{Handler, Headers};
use iron_test::{request, response}; use iron_test::{request, response};
@ -413,20 +413,19 @@ mod tests {
impl http_tests::HTTPIntegration for TestIronIntegration { impl http_tests::HTTPIntegration for TestIronIntegration {
fn get(&self, url: &str) -> http_tests::TestResponse { fn get(&self, url: &str) -> http_tests::TestResponse {
make_test_response(request::get( let result = request::get(&fixup_url(url), Headers::new(), &make_handler());
&fixup_url(url), match result {
Headers::new(), Ok(response) => make_test_response(response),
&make_handler(), Err(e) => make_test_error_response(e),
)) }
} }
fn post(&self, url: &str, body: &str) -> http_tests::TestResponse { fn post(&self, url: &str, body: &str) -> http_tests::TestResponse {
make_test_response(request::post( let result = request::post(&fixup_url(url), Headers::new(), body, &make_handler());
&fixup_url(url), match result {
Headers::new(), Ok(response) => make_test_response(response),
body, Err(e) => make_test_error_response(e),
&make_handler(), }
))
} }
} }
@ -441,8 +440,17 @@ mod tests {
Ok(Database::new()) Ok(Database::new())
} }
fn make_test_response(response: IronResult<Response>) -> http_tests::TestResponse { fn make_test_error_response(_: IronError) -> http_tests::TestResponse {
let response = response.expect("Error response from GraphQL handler"); // For now all errors return the same status code.
// `juniper_iron` users can choose to do something different if desired.
http_tests::TestResponse {
status_code: 400,
body: None,
content_type: "application/json".to_string(),
}
}
fn make_test_response(response: Response) -> http_tests::TestResponse {
let status_code = response let status_code = response
.status .status
.expect("No status code returned from handler") .expect("No status code returned from handler")