Add failure cases to http integration tests (#239)

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

View file

@ -168,6 +168,15 @@ pub mod tests {
println!(" - test_batched_post");
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 {
@ -281,4 +290,29 @@ pub mod tests {
).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)]
mod tests {
use iron::prelude::*;
use super::*;
use iron::Url;
use iron::{Handler, Headers};
use iron_test::{request, response};
@ -413,20 +413,19 @@ mod tests {
impl http_tests::HTTPIntegration for TestIronIntegration {
fn get(&self, url: &str) -> http_tests::TestResponse {
make_test_response(request::get(
&fixup_url(url),
Headers::new(),
&make_handler(),
))
let result = request::get(&fixup_url(url), Headers::new(), &make_handler());
match result {
Ok(response) => make_test_response(response),
Err(e) => make_test_error_response(e),
}
}
fn post(&self, url: &str, body: &str) -> http_tests::TestResponse {
make_test_response(request::post(
&fixup_url(url),
Headers::new(),
body,
&make_handler(),
))
let result = request::post(&fixup_url(url), Headers::new(), body, &make_handler());
match result {
Ok(response) => make_test_response(response),
Err(e) => make_test_error_response(e),
}
}
}
@ -441,8 +440,17 @@ mod tests {
Ok(Database::new())
}
fn make_test_response(response: IronResult<Response>) -> http_tests::TestResponse {
let response = response.expect("Error response from GraphQL handler");
fn make_test_error_response(_: IronError) -> http_tests::TestResponse {
// 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
.status
.expect("No status code returned from handler")