-
Hi. I can't find any testing steps in https://docs.rs/poem/latest/poem/index.html. I need to dig into the codebase to find any "real world" test. Below are the tests for my Health endpoint under a different framework.
#[test]
fn health() -> Result<()> {
let client = Client::tracked(adr::rocket()).context("failed to create rocket test client")?;
let query = HealthQuery::build(());
let resp = client
.post("/graphql")
.header(ContentType::JSON)
.json(&query)
.dispatch();
assert_eq!(resp.status(), Status::Ok);
let health_response = resp
.into_json::<HealthResponse>()
.context("failed to deserialize response")?;
assert_eq!(health_response.data.health.status, "running");
Ok(())
}
#[tokio::test]
async fn health() -> Result<()> {
dotenv().ok();
let client = jdw::server();
let query = HealthQuery::build(());
let resp = request()
.method("POST")
.path("/graphql")
.json(&query)
.reply(&client)
.await;
assert_eq!(resp.status(), StatusCode::OK);
let health_response: HealthResponse =
from_slice(resp.body()).context("failed to deserialize response body")?;
assert_eq!(health_response.data.health.status, "running");
Ok(())
} I am having a hard time making the same integration test for Health resources in Poem. Any help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Alright, I will provide more examples to show how to use |
Beta Was this translation helpful? Give feedback.
-
I've added some examples for the https://docs.rs/poem/1.2.45/poem/test/index.html https://docs.rs/poem/1.2.45/poem/test/struct.TestClient.html#method.default_header https://docs.rs/poem/1.2.45/poem/test/struct.TestRequestBuilder.html#method.query https://docs.rs/poem/1.2.45/poem/test/struct.TestRequestBuilder.html#method.data |
Beta Was this translation helpful? Give feedback.
-
That was blazingly fast. I thought it would take more than a day. For the context: I hear so many complements about Poem, so I create a repo that compares each of the Rust frameworks. It is still in the early stage thought. https://github.com/azzamsa/rust-backend-zoo I got : running 1 test
resp: Ok("data of type `async_graphql::schema::Schema<adp::Query, adp::Mutation, async_graphql::types::empty_subscription::EmptySubscription>` was not found.")
test health::tests::health ... ok #[tokio::test]
async fn health() -> Result<()> {
let app = Route::new().at("/", graphql_handler);
let client = TestClient::new(app);
// let query = HealthQuery::build(());
// let resp = client.post("/graphql").body_json(&query).send().await;
let resp = client
.post("/")
.body(r#"{"query": "{ health { status } }" }"#)
.send()
.await;
let resp_ = resp.into_body().into_string().await;
println!("resp: {:?}", resp_);
//resp.assert_status_is_ok();
Ok(())
} #[handler]
pub async fn graphql_handler(schema: Data<&AppSchema>, req: Json<Request>) -> Json<Response> {
Json(schema.execute(req.0).await)
} Am I missing something? |
Beta Was this translation helpful? Give feedback.
-
You forgot to add let resp = client
.post("/")
.data(schema) // <<<----- here
.body(r#"{"query": "{ health { status } }" }"#)
.send()
.await; |
Beta Was this translation helpful? Give feedback.
You forgot to add
schema
. 🙂