2021-10-22 14:01:14 +00:00
|
|
|
use actix_http::Method;
|
2021-12-12 02:10:28 +00:00
|
|
|
use actix_web::dev::Service;
|
2021-10-22 14:01:14 +00:00
|
|
|
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-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
|
|
|
assert!(
|
|
|
|
std::str::from_utf8(&actix_web::body::to_bytes(body).await.unwrap())
|
|
|
|
.unwrap()
|
|
|
|
.contains("graphql")
|
|
|
|
);
|
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-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"add": 30}}).to_string().into_bytes()
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
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-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2020-10-02 13:17:47 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"hello": "Hello, world!"}})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes()
|
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-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"hello": "Hello, Foo!"}})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes()
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
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());
|
2021-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"count": 0}}).to_string().into_bytes()
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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());
|
2021-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"addCount": 10}}).to_string().into_bytes(),
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"subtractCount": 8}})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes()
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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());
|
2021-12-12 02:10:28 +00:00
|
|
|
let body = response.into_body();
|
2021-10-22 14:01:14 +00:00
|
|
|
assert_eq!(
|
2021-12-12 02:10:28 +00:00
|
|
|
actix_web::body::to_bytes(body).await.unwrap(),
|
|
|
|
json!({"data": {"subtractCount": 6}})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes()
|
2021-10-22 14:01:14 +00:00
|
|
|
);
|
2021-11-13 00:11:26 +00:00
|
|
|
}
|
2022-01-17 15:09:16 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "cbor")]
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_cbor() {
|
|
|
|
let srv = test::init_service(
|
|
|
|
App::new()
|
|
|
|
.app_data(Data::new(Schema::new(
|
|
|
|
AddQueryRoot,
|
|
|
|
EmptyMutation,
|
|
|
|
EmptySubscription,
|
|
|
|
)))
|
|
|
|
.service(
|
|
|
|
web::resource("/")
|
|
|
|
.guard(guard::Post())
|
|
|
|
.to(gql_handle_schema::<AddQueryRoot, EmptyMutation, EmptySubscription>),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let response = srv
|
|
|
|
.call(
|
|
|
|
test::TestRequest::with_uri("/")
|
|
|
|
.method(Method::POST)
|
|
|
|
.set_payload(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
|
|
|
|
.insert_header((actix_http::header::ACCEPT, "application/cbor"))
|
|
|
|
.to_request(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
#[derive(Debug, serde::Deserialize, PartialEq)]
|
|
|
|
struct Response {
|
|
|
|
data: ResponseInner,
|
|
|
|
}
|
|
|
|
#[derive(Debug, serde::Deserialize, PartialEq)]
|
|
|
|
struct ResponseInner {
|
|
|
|
add: i32,
|
|
|
|
}
|
|
|
|
let body = actix_web::body::to_bytes(response.into_body())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let response: Response = serde_cbor::from_slice(&body).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
response,
|
|
|
|
Response {
|
|
|
|
data: ResponseInner { add: 30 }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|