async-graphql/integrations/actix-web/tests/graphql.rs

245 lines
6.5 KiB
Rust
Raw Normal View History

2021-10-22 14:01:14 +00:00
use actix_http::Method;
use actix_web::dev::{AnyBody, Service};
use actix_web::{guard, test, web, web::Data, App};
2020-10-02 13:17:47 +00:00
use serde_json::json;
2021-10-22 14:01:14 +00:00
use async_graphql::*;
2020-10-02 13:17:47 +00:00
use test_utils::*;
2021-10-22 14:01:14 +00:00
mod test_utils;
2020-10-02 13:17:47 +00:00
#[actix_rt::test]
async fn test_playground() {
2021-10-22 14:01:14 +00:00
let srv = test::init_service(
2020-10-02 13:17:47 +00:00
App::new().service(
web::resource("/")
.guard(guard::Get())
.to(test_utils::gql_playgound),
2021-10-22 14:01:14 +00:00
),
)
.await;
let req = test::TestRequest::with_uri("/").to_request();
let response = srv.call(req).await.unwrap();
2020-10-02 13:17:47 +00:00
assert!(response.status().is_success());
2021-10-22 14:01:14 +00:00
let body = response.response().body();
if let AnyBody::Bytes(bytes) = body {
assert!(std::str::from_utf8(&bytes).unwrap().contains("graphql"));
} else {
panic!("response body must be Bytes {:?}", body);
}
2020-10-02 13:17:47 +00:00
}
#[actix_rt::test]
async fn test_add() {
2021-10-22 14:01:14 +00:00
let srv = test::init_service(
2020-10-02 13:17:47 +00:00
App::new()
2021-10-22 14:01:14 +00:00
.app_data(Data::new(Schema::new(
AddQueryRoot,
EmptyMutation,
EmptySubscription,
)))
2020-10-02 13:17:47 +00:00
.service(
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<AddQueryRoot, EmptyMutation, EmptySubscription>),
2021-10-22 14:01:14 +00:00
),
)
.await;
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
.to_request(),
)
2020-10-02 13:17:47 +00:00
.await
.unwrap();
assert!(response.status().is_success());
2021-10-22 14:01:14 +00:00
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(json!({"data": {"add": 30}}).to_string().into_bytes().into())
);
2020-10-02 13:17:47 +00:00
}
#[actix_rt::test]
async fn test_hello() {
2021-10-22 14:01:14 +00:00
let srv = test::init_service(
2020-10-02 13:17:47 +00:00
App::new()
2021-10-22 14:01:14 +00:00
.app_data(Data::new(Schema::new(
2020-10-02 13:17:47 +00:00
HelloQueryRoot,
EmptyMutation,
EmptySubscription,
2021-10-22 14:01:14 +00:00
)))
2020-10-02 13:17:47 +00:00
.service(
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<HelloQueryRoot, EmptyMutation, EmptySubscription>),
2021-10-22 14:01:14 +00:00
),
)
.await;
2020-10-02 13:17:47 +00:00
2021-10-22 14:01:14 +00:00
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"{ hello }"}"#)
.to_request(),
)
2020-10-02 13:17:47 +00:00
.await
.unwrap();
assert!(response.status().is_success());
2021-10-22 14:01:14 +00:00
let body = response.response().body();
2020-10-02 13:17:47 +00:00
assert_eq!(
body,
2021-10-22 14:01:14 +00:00
&AnyBody::Bytes(
json!({"data": {"hello": "Hello, world!"}})
.to_string()
.into_bytes()
.into()
)
2020-10-02 13:17:47 +00:00
);
}
#[actix_rt::test]
async fn test_hello_header() {
2021-10-22 14:01:14 +00:00
let srv = test::init_service(
2020-10-02 13:17:47 +00:00
App::new()
2021-10-22 14:01:14 +00:00
.app_data(Data::new(Schema::new(
2020-10-02 13:17:47 +00:00
HelloQueryRoot,
EmptyMutation,
EmptySubscription,
2021-10-22 14:01:14 +00:00
)))
2020-10-02 13:17:47 +00:00
.service(
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema_with_header::<HelloQueryRoot>),
2021-10-22 14:01:14 +00:00
),
)
.await;
2020-10-02 13:17:47 +00:00
2021-10-22 14:01:14 +00:00
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.insert_header(("Name", "Foo"))
.set_payload(r#"{"query":"{ hello }"}"#)
.to_request(),
)
2020-10-02 13:17:47 +00:00
.await
.unwrap();
assert!(response.status().is_success());
2021-10-22 14:01:14 +00:00
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(
json!({"data": {"hello": "Hello, Foo!"}})
.to_string()
.into_bytes()
.into()
)
);
2020-10-02 13:17:47 +00:00
}
#[actix_rt::test]
async fn test_count() {
2021-10-22 14:01:14 +00:00
let srv = test::init_service(
2020-10-02 13:17:47 +00:00
App::new()
2021-10-22 14:01:14 +00:00
.app_data(Data::new(
2020-10-02 13:17:47 +00:00
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
.data(Count::default())
.finish(),
2021-10-22 14:01:14 +00:00
))
2020-10-02 13:17:47 +00:00
.service(
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<CountQueryRoot, CountMutation, EmptySubscription>),
2021-10-22 14:01:14 +00:00
),
2020-10-02 13:17:47 +00:00
)
.await;
2021-10-22 14:01:14 +00:00
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"{ count }"}"#)
.to_request(),
)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(
json!({"data": {"count": 0}})
.to_string()
.into_bytes()
.into()
)
);
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"mutation{ addCount(count: 10) }"}"#)
.to_request(),
)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(
json!({"data": {"addCount": 10}})
.to_string()
.into_bytes()
.into()
)
);
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"mutation{ subtractCount(count: 2) }"}"#)
.to_request(),
)
.await
.unwrap();
2020-10-02 13:17:47 +00:00
assert!(response.status().is_success());
2021-10-22 14:01:14 +00:00
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(
json!({"data": {"subtractCount": 8}})
.to_string()
.into_bytes()
.into()
)
);
let response = srv
.call(
test::TestRequest::with_uri("/")
.method(Method::POST)
.set_payload(r#"{"query":"mutation{ subtractCount(count: 2) }"}"#)
.to_request(),
)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.response().body();
assert_eq!(
body,
&AnyBody::Bytes(
json!({"data": {"subtractCount": 6}})
.to_string()
.into_bytes()
.into()
)
);
2021-11-13 00:11:26 +00:00
}