thegraph-hyper: Add tests for GraphQLService
@@ -77,3 +77,65 @@ impl Service for GraphQLService {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use futures::prelude::*;
+ use futures::sync::mpsc::channel;
+ use http::status::StatusCode;
+ use hyper::{Body, Method, Request};
+ use hyper::service::Service;
+ use tokio_core::reactor::Core;
+
+ use thegraph::common::query::QueryResult;
+ use super::GraphQLService;
+
+ #[test]
+ fn posting_invalid_query_yields_error_response() {
+ let mut core = Core::new().unwrap();
+
+ let (query_sink, _) = channel(1);
+ let mut service = GraphQLService::new(query_sink);
+
+ let request = Request::builder()
+ .method(Method::POST)
+ .uri("http://localhost:8000/")
+ .body(Body::from(""))
+ .unwrap();
+
+ let response = core.run(service.call(request)).unwrap();
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
+ }
+
+ #[test]
+ fn posting_valid_queries_yields_result_response() {
+ let mut core = Core::new().unwrap();
+
+ let (query_sink, query_stream) = channel(1);
+ let mut service = GraphQLService::new(query_sink);
+
+ core.handle().spawn(
+ query_stream
+ .for_each(move |query| {
+ query.result_sender.send(QueryResult {}).unwrap();
+ Ok(())
+ })
+ .fuse(),
+ );
+
+ let request = Request::builder()
+ .method(Method::POST)
+ .uri("http://localhost:8000/")
+ .body(Body::from("{\"query\": \"{ users { name } }\"}"))
+ .unwrap();
+
+ // Assert the response indicates success
+ let response = core.run(service.call(request)).unwrap();
+ assert_eq!(response.status(), StatusCode::OK);
+
+ // Assert that the query result was the one generated by the query stream handler task
+ let body = core.run(response.into_body().concat2().map(|chunk| chunk.to_vec()))
+ .unwrap();
+ assert_eq!(body, "QueryResult".as_bytes());
+ }
+}
