From b8762953a44e13e0ebc7e26bc3d367717ac165ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Libor=20Va=C5=A1=C3=AD=C4=8Dek?= Date: Sat, 3 Apr 2021 11:47:22 +0200 Subject: [PATCH] Update tests for actix-web v4.0.0-beta.5 --- integrations/actix-web/Cargo.toml | 2 +- integrations/actix-web/src/subscription.rs | 5 +- integrations/actix-web/tests/graphql.rs | 121 +++++++++++++-------- 3 files changed, 76 insertions(+), 52 deletions(-) diff --git a/integrations/actix-web/Cargo.toml b/integrations/actix-web/Cargo.toml index 1b4ca5b6..4b605128 100644 --- a/integrations/actix-web/Cargo.toml +++ b/integrations/actix-web/Cargo.toml @@ -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" diff --git a/integrations/actix-web/src/subscription.rs b/integrations/actix-web/src/subscription.rs index e4dd2b9a..152a49ec 100644 --- a/integrations/actix-web/src/subscription.rs +++ b/integrations/actix-web/src/subscription.rs @@ -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}; diff --git a/integrations/actix-web/tests/graphql.rs b/integrations/actix-web/tests/graphql.rs index dc75676a..f53111ea 100644 --- a/integrations/actix-web/tests/graphql.rs +++ b/integrations/actix-web/tests/graphql.rs @@ -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::), - ) - }); - 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::), - ) - }); + ), + ) + .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::), - ) - }); + ), + ) + .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::), - ) - }); - 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(expected: i32, payload: &'static str, app: &S) +where + S: Service, 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()));