diff --git a/async-graphql-tide/Cargo.toml b/async-graphql-tide/Cargo.toml index 3655a566..d4c7531a 100644 --- a/async-graphql-tide/Cargo.toml +++ b/async-graphql-tide/Cargo.toml @@ -18,5 +18,6 @@ tide = "0.8" [dev-dependencies] async-std = "1.5.0" -surf = "2.0.0-alpha.1" serde_json = "1.0.51" +smol = { version = "0.1", features = ["tokio02"] } +reqwest = "0.10.4" diff --git a/async-graphql-tide/tests/graphql.rs b/async-graphql-tide/tests/graphql.rs index f2e2dcce..1824df40 100644 --- a/async-graphql-tide/tests/graphql.rs +++ b/async-graphql-tide/tests/graphql.rs @@ -1,7 +1,6 @@ mod test_utils; -use async_std::prelude::*; -use async_std::task; use serde_json::json; +use smol::{Task, Timer}; use std::time::Duration; use tide::Request; @@ -11,10 +10,10 @@ type Result = std::result::Result #[test] fn quickstart() -> Result<()> { - task::block_on(async { + smol::run(async { let listen_addr = test_utils::find_listen_addr().await; - let server: task::JoinHandle> = task::spawn(async move { + let server = Task::>::spawn(async move { struct QueryRoot; #[Object] impl QueryRoot { @@ -34,21 +33,27 @@ fn quickstart() -> Result<()> { Ok(()) }); - let client: task::JoinHandle> = task::spawn(async move { - task::sleep(Duration::from_millis(300)).await; + let client = Task::>::spawn(async move { + Timer::after(Duration::from_millis(300)).await; - let string = surf::post(format!("http://{}", listen_addr)) - .body_bytes(r#"{"query":"{ add(a: 10, b: 20) }"}"#) - .set_header("Content-Type".parse().unwrap(), "application/json") - .recv_string() + let resp = reqwest::Client::new() + .post(format!("http://{}", listen_addr).as_str()) + .body(r#"{"query":"{ add(a: 10, b: 20) }"}"#) + .header(reqwest::header::CONTENT_TYPE, "application/json") + .send() .await?; + assert_eq!(resp.status(), reqwest::StatusCode::OK); + let string = resp.text().await?; + println!("{}", string); + assert_eq!(string, json!({"data": {"add": 30}}).to_string()); Ok(()) }); - server.race(client).await?; + client.await?; + server.cancel().await; Ok(()) }) @@ -56,10 +61,10 @@ fn quickstart() -> Result<()> { #[test] fn hello() -> Result<()> { - task::block_on(async { + smol::run(async { let listen_addr = test_utils::find_listen_addr().await; - let server: task::JoinHandle> = task::spawn(async move { + let server = Task::>::spawn(async move { struct Hello(String); struct QueryRoot; #[Object] @@ -98,24 +103,34 @@ fn hello() -> Result<()> { Ok(()) }); - let client: task::JoinHandle> = task::spawn(async move { - task::sleep(Duration::from_millis(300)).await; + let client = Task::>::spawn(async move { + Timer::after(Duration::from_millis(300)).await; - let string = surf::post(format!("http://{}", listen_addr)) - .body_bytes(r#"{"query":"{ hello }"}"#) - .set_header("Content-Type".parse().unwrap(), "application/json") - .set_header("Name".parse().unwrap(), "Foo") - .recv_string() + let resp = reqwest::Client::new() + .post(format!("http://{}", listen_addr).as_str()) + .body(r#"{"query":"{ hello }"}"#) + .header(reqwest::header::CONTENT_TYPE, "application/json") + .header("Name", "Foo") + .send() .await?; + assert_eq!(resp.status(), reqwest::StatusCode::OK); + let string = resp.text().await?; + println!("{}", string); + assert_eq!(string, json!({"data":{"hello":"Hello, Foo!"}}).to_string()); - let string = surf::post(format!("http://{}", listen_addr)) - .body_bytes(r#"{"query":"{ hello }"}"#) - .set_header("Content-Type".parse().unwrap(), "application/json") - .recv_string() + let resp = reqwest::Client::new() + .post(format!("http://{}", listen_addr).as_str()) + .body(r#"{"query":"{ hello }"}"#) + .header(reqwest::header::CONTENT_TYPE, "application/json") + .send() .await?; + assert_eq!(resp.status(), reqwest::StatusCode::OK); + let string = resp.text().await?; + println!("{}", string); + assert_eq!( string, json!({"data":{"hello":"Hello, world!"}}).to_string() @@ -124,7 +139,8 @@ fn hello() -> Result<()> { Ok(()) }); - server.race(client).await?; + client.await?; + server.cancel().await; Ok(()) }) diff --git a/async-graphql-tide/tests/test_utils.rs b/async-graphql-tide/tests/test_utils.rs index 017229de..6fa289a0 100644 --- a/async-graphql-tide/tests/test_utils.rs +++ b/async-graphql-tide/tests/test_utils.rs @@ -1,6 +1,5 @@ -pub async fn find_listen_addr() -> async_std::net::SocketAddr { - async_std::net::TcpListener::bind("localhost:0") - .await +pub async fn find_listen_addr() -> std::net::SocketAddr { + std::net::TcpListener::bind("localhost:0") .unwrap() .local_addr() .unwrap()