Update tests for actix-web v4.0.0-beta.5

This commit is contained in:
Libor Vašíček 2021-04-03 11:47:22 +02:00 committed by Sunli
parent c97b634303
commit 80406ddab2
3 changed files with 76 additions and 52 deletions

View File

@ -24,5 +24,5 @@ serde_urlencoded = "0.7.0"
futures-channel = "0.3.13"
[dev-dependencies]
actix-rt = "2.0.0"
actix-rt = "2.2.0"
async-mutex = "1.4.0"

View File

@ -2,10 +2,9 @@ use std::future::Future;
use std::str::FromStr;
use std::time::{Duration, Instant};
use actix::ActorFutureExt;
use actix::{
Actor, ActorContext, ActorStreamExt, AsyncContext, ContextFutureSpawner, StreamHandler,
WrapFuture, WrapStream,
Actor, ActorContext, ActorFutureExt, ActorStreamExt, AsyncContext, ContextFutureSpawner,
StreamHandler, WrapFuture, WrapStream,
};
use actix_http::error::PayloadError;
use actix_http::{ws, Error};

View File

@ -1,4 +1,6 @@
mod test_utils;
use actix_http::Request;
use actix_web::dev::{MessageBody, Service, ServiceResponse};
use actix_web::{guard, test, web, App};
use async_graphql::*;
use serde_json::json;
@ -6,43 +8,50 @@ use test_utils::*;
#[actix_rt::test]
async fn test_playground() {
let srv = test::start(|| {
let app = test::init_service(
App::new().service(
web::resource("/")
.guard(guard::Get())
.to(test_utils::gql_playgound),
)
});
let mut response = srv.get("/").send().await.unwrap();
assert!(response.status().is_success());
let body = response.body().await.unwrap();
),
)
.await;
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"));
}
#[actix_rt::test]
async fn test_add() {
let srv = test::start(|| {
let app = test::init_service(
App::new()
.data(Schema::new(AddQueryRoot, EmptyMutation, EmptySubscription))
.service(
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<AddQueryRoot, EmptyMutation, EmptySubscription>),
)
});
let mut response = srv
.post("/")
.send_body(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.body().await.unwrap();
),
)
.await;
let resp = test::TestRequest::post()
.uri("/")
.set_payload(r#"{"query":"{ add(a: 10, b: 20) }"}"#)
.send_request(&app)
.await;
assert!(resp.status().is_success());
let body = test::read_body(resp).await;
assert_eq!(body, json!({"data": {"add": 30}}).to_string());
}
#[actix_rt::test]
async fn test_hello() {
let srv = test::start(|| {
let app = test::init_service(
App::new()
.data(Schema::new(
HelloQueryRoot,
@ -53,16 +62,18 @@ async fn test_hello() {
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<HelloQueryRoot, EmptyMutation, EmptySubscription>),
)
});
),
)
.await;
let mut response = srv
.post("/")
.send_body(r#"{"query":"{ hello }"}"#)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.body().await.unwrap();
let resp = test::TestRequest::post()
.uri("/")
.set_payload(r#"{"query":"{ hello }"}"#)
.send_request(&app)
.await;
assert!(resp.status().is_success());
let body = test::read_body(resp).await;
assert_eq!(
body,
json!({"data": {"hello": "Hello, world!"}}).to_string()
@ -71,7 +82,7 @@ async fn test_hello() {
#[actix_rt::test]
async fn test_hello_header() {
let srv = test::start(|| {
let app = test::init_service(
App::new()
.data(Schema::new(
HelloQueryRoot,
@ -82,23 +93,25 @@ async fn test_hello_header() {
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema_with_header::<HelloQueryRoot>),
)
});
),
)
.await;
let mut response = srv
.post("/")
.header("Name", "Foo")
.send_body(r#"{"query":"{ hello }"}"#)
.await
.unwrap();
assert!(response.status().is_success());
let body = response.body().await.unwrap();
let resp = test::TestRequest::post()
.uri("/")
.append_header(("Name", "Foo"))
.set_payload(r#"{"query":"{ hello }"}"#)
.send_request(&app)
.await;
assert!(resp.status().is_success());
let body = test::read_body(resp).await;
assert_eq!(body, json!({"data": {"hello": "Hello, Foo!"}}).to_string());
}
#[actix_rt::test]
async fn test_count() {
let srv = test::start(|| {
let app = test::init_service(
App::new()
.data(
Schema::build(CountQueryRoot, CountMutation, EmptySubscription)
@ -109,28 +122,40 @@ async fn test_count() {
web::resource("/")
.guard(guard::Post())
.to(gql_handle_schema::<CountQueryRoot, CountMutation, EmptySubscription>),
)
});
count_action_helper(0, r#"{"query":"{ count }"}"#, &srv).await;
count_action_helper(10, r#"{"query":"mutation{ addCount(count: 10) }"}"#, &srv).await;
),
)
.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(
8,
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
&srv,
&app,
)
.await;
count_action_helper(
6,
r#"{"query":"mutation{ subtractCount(count: 2) }"}"#,
&srv,
&app,
)
.await;
}
async fn count_action_helper(expected: i32, body: &'static str, srv: &test::TestServer) {
let mut response = srv.post("/").send_body(body).await.unwrap();
assert!(response.status().is_success());
let body = response.body().await.unwrap();
async fn count_action_helper<S, B, E>(expected: i32, payload: &'static str, app: &S)
where
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
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)
.unwrap()
.contains(&expected.to_string()));