Update tests for actix-web v4.0.0-beta.5
This commit is contained in:
parent
816aeb635f
commit
b8762953a4
@ -24,5 +24,5 @@ serde_urlencoded = "0.7.0"
|
|||||||
futures-channel = "0.3.13"
|
futures-channel = "0.3.13"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2.0.0"
|
actix-rt = "2.2.0"
|
||||||
async-mutex = "1.4.0"
|
async-mutex = "1.4.0"
|
||||||
|
@ -2,10 +2,9 @@ use std::future::Future;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use actix::ActorFutureExt;
|
|
||||||
use actix::{
|
use actix::{
|
||||||
Actor, ActorContext, ActorStreamExt, AsyncContext, ContextFutureSpawner, StreamHandler,
|
Actor, ActorContext, ActorFutureExt, ActorStreamExt, AsyncContext, ContextFutureSpawner,
|
||||||
WrapFuture, WrapStream,
|
StreamHandler, WrapFuture, WrapStream,
|
||||||
};
|
};
|
||||||
use actix_http::error::PayloadError;
|
use actix_http::error::PayloadError;
|
||||||
use actix_http::{ws, Error};
|
use actix_http::{ws, Error};
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
mod test_utils;
|
mod test_utils;
|
||||||
|
use actix_http::Request;
|
||||||
|
use actix_web::dev::{MessageBody, Service, ServiceResponse};
|
||||||
use actix_web::{guard, test, web, App};
|
use actix_web::{guard, test, web, App};
|
||||||
use async_graphql::*;
|
use async_graphql::*;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
@ -6,43 +8,50 @@ use test_utils::*;
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_playground() {
|
async fn test_playground() {
|
||||||
let srv = test::start(|| {
|
let app = test::init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.guard(guard::Get())
|
.guard(guard::Get())
|
||||||
.to(test_utils::gql_playgound),
|
.to(test_utils::gql_playgound),
|
||||||
)
|
),
|
||||||
});
|
)
|
||||||
let mut response = srv.get("/").send().await.unwrap();
|
.await;
|
||||||
assert!(response.status().is_success());
|
|
||||||
let body = response.body().await.unwrap();
|
let req = test::TestRequest::with_uri("/").to_request();
|
||||||
|
|
||||||
|
let resp = app.call(req).await.unwrap();
|
||||||
|
assert!(resp.status().is_success());
|
||||||
|
let body = test::read_body(resp).await;
|
||||||
assert!(std::str::from_utf8(&body).unwrap().contains("graphql"));
|
assert!(std::str::from_utf8(&body).unwrap().contains("graphql"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_add() {
|
async fn test_add() {
|
||||||
let srv = test::start(|| {
|
let app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(Schema::new(AddQueryRoot, EmptyMutation, EmptySubscription))
|
.data(Schema::new(AddQueryRoot, EmptyMutation, EmptySubscription))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/")
|
web::resource("/")
|
||||||
.guard(guard::Post())
|
.guard(guard::Post())
|
||||||
.to(gql_handle_schema::<AddQueryRoot, EmptyMutation, EmptySubscription>),
|
.to(gql_handle_schema::<AddQueryRoot, EmptyMutation, EmptySubscription>),
|
||||||
)
|
),
|
||||||
});
|
)
|
||||||
let mut response = srv
|
.await;
|
||||||
.post("/")
|
|
||||||
.send_body(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
|
let resp = test::TestRequest::post()
|
||||||
.await
|
.uri("/")
|
||||||
.unwrap();
|
.set_payload(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
|
||||||
assert!(response.status().is_success());
|
.send_request(&app)
|
||||||
let body = response.body().await.unwrap();
|
.await;
|
||||||
|
|
||||||
|
assert!(resp.status().is_success());
|
||||||
|
let body = test::read_body(resp).await;
|
||||||
assert_eq!(body, json!({"data": {"add": 30}}).to_string());
|
assert_eq!(body, json!({"data": {"add": 30}}).to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_hello() {
|
async fn test_hello() {
|
||||||
let srv = test::start(|| {
|
let app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(Schema::new(
|
.data(Schema::new(
|
||||||
HelloQueryRoot,
|
HelloQueryRoot,
|
||||||
@ -53,16 +62,18 @@ async fn test_hello() {
|
|||||||
web::resource("/")
|
web::resource("/")
|
||||||
.guard(guard::Post())
|
.guard(guard::Post())
|
||||||
.to(gql_handle_schema::<HelloQueryRoot, EmptyMutation, EmptySubscription>),
|
.to(gql_handle_schema::<HelloQueryRoot, EmptyMutation, EmptySubscription>),
|
||||||
)
|
),
|
||||||
});
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
let mut response = srv
|
let resp = test::TestRequest::post()
|
||||||
.post("/")
|
.uri("/")
|
||||||
.send_body(r#"{"query":"{ hello }"}"#)
|
.set_payload(r#"{"query":"{ hello }"}"#)
|
||||||
.await
|
.send_request(&app)
|
||||||
.unwrap();
|
.await;
|
||||||
assert!(response.status().is_success());
|
|
||||||
let body = response.body().await.unwrap();
|
assert!(resp.status().is_success());
|
||||||
|
let body = test::read_body(resp).await;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
body,
|
body,
|
||||||
json!({"data": {"hello": "Hello, world!"}}).to_string()
|
json!({"data": {"hello": "Hello, world!"}}).to_string()
|
||||||
@ -71,7 +82,7 @@ async fn test_hello() {
|
|||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_hello_header() {
|
async fn test_hello_header() {
|
||||||
let srv = test::start(|| {
|
let app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(Schema::new(
|
.data(Schema::new(
|
||||||
HelloQueryRoot,
|
HelloQueryRoot,
|
||||||
@ -82,23 +93,25 @@ async fn test_hello_header() {
|
|||||||
web::resource("/")
|
web::resource("/")
|
||||||
.guard(guard::Post())
|
.guard(guard::Post())
|
||||||
.to(gql_handle_schema_with_header::<HelloQueryRoot>),
|
.to(gql_handle_schema_with_header::<HelloQueryRoot>),
|
||||||
)
|
),
|
||||||
});
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
let mut response = srv
|
let resp = test::TestRequest::post()
|
||||||
.post("/")
|
.uri("/")
|
||||||
.header("Name", "Foo")
|
.append_header(("Name", "Foo"))
|
||||||
.send_body(r#"{"query":"{ hello }"}"#)
|
.set_payload(r#"{"query":"{ hello }"}"#)
|
||||||
.await
|
.send_request(&app)
|
||||||
.unwrap();
|
.await;
|
||||||
assert!(response.status().is_success());
|
|
||||||
let body = response.body().await.unwrap();
|
assert!(resp.status().is_success());
|
||||||
|
let body = test::read_body(resp).await;
|
||||||
assert_eq!(body, json!({"data": {"hello": "Hello, Foo!"}}).to_string());
|
assert_eq!(body, json!({"data": {"hello": "Hello, Foo!"}}).to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_count() {
|
async fn test_count() {
|
||||||
let srv = test::start(|| {
|
let app = test::init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.data(
|
.data(
|
||||||
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
|
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
|
||||||
@ -109,28 +122,40 @@ async fn test_count() {
|
|||||||
web::resource("/")
|
web::resource("/")
|
||||||
.guard(guard::Post())
|
.guard(guard::Post())
|
||||||
.to(gql_handle_schema::<CountQueryRoot, CountMutation, EmptySubscription>),
|
.to(gql_handle_schema::<CountQueryRoot, CountMutation, EmptySubscription>),
|
||||||
)
|
),
|
||||||
});
|
)
|
||||||
count_action_helper(0, r#"{"query":"{ count }"}"#, &srv).await;
|
.await;
|
||||||
count_action_helper(10, r#"{"query":"mutation{ addCount(count: 10) }"}"#, &srv).await;
|
|
||||||
|
count_action_helper(0, r#"{"query":"{ count }"}"#, &app).await;
|
||||||
|
count_action_helper(10, r#"{"query":"mutation{ addCount(count: 10) }"}"#, &app).await;
|
||||||
count_action_helper(
|
count_action_helper(
|
||||||
8,
|
8,
|
||||||
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
|
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
|
||||||
&srv,
|
&app,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
count_action_helper(
|
count_action_helper(
|
||||||
6,
|
6,
|
||||||
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
|
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
|
||||||
&srv,
|
&app,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn count_action_helper(expected: i32, body: &'static str, srv: &test::TestServer) {
|
async fn count_action_helper<S, B, E>(expected: i32, payload: &'static str, app: &S)
|
||||||
let mut response = srv.post("/").send_body(body).await.unwrap();
|
where
|
||||||
assert!(response.status().is_success());
|
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
|
||||||
let body = response.body().await.unwrap();
|
B: MessageBody + Unpin,
|
||||||
|
E: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let resp = test::TestRequest::post()
|
||||||
|
.uri("/")
|
||||||
|
.set_payload(payload)
|
||||||
|
.send_request(app)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(resp.status().is_success());
|
||||||
|
let body = test::read_body(resp).await;
|
||||||
assert!(std::str::from_utf8(&body)
|
assert!(std::str::from_utf8(&body)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.contains(&expected.to_string()));
|
.contains(&expected.to_string()));
|
||||||
|
Loading…
Reference in New Issue
Block a user