-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
mod helpers; | ||
mod model; | ||
mod observability; | ||
mod routes; | ||
mod startup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{header, Method, Request, StatusCode}, | ||
}; | ||
use axum_graphql::startup::ApplicationRouters; | ||
use http_body_util::BodyExt; | ||
use serde_json::{json, Value}; | ||
use tower::util::ServiceExt; | ||
|
||
use crate::helpers::TestApp; | ||
|
||
#[tokio::test] | ||
async fn graphql_endpoint_returns_200_ok() { | ||
// arrange | ||
let ApplicationRouters { main_router, .. } = TestApp::spawn_routers().await; | ||
|
||
// act | ||
let response = main_router | ||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap()) | ||
.await | ||
.unwrap(); | ||
|
||
// assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
} | ||
|
||
#[tokio::test] | ||
async fn graphql_endpoint_responds_to_invalid_query() { | ||
// arrange | ||
let ApplicationRouters { main_router, .. } = TestApp::spawn_routers().await; | ||
let json_request_body: Value = json!( | ||
{"operationName":"HelloQuery","variables":{},"query":"query HelloQuery { hello " | ||
}); | ||
|
||
// act | ||
let response = main_router | ||
.oneshot( | ||
Request::builder() | ||
.method(Method::POST) | ||
.uri("/") | ||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) | ||
.body(Body::from(json_request_body.to_string())) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
let body = response.into_body().collect().await.unwrap().to_bytes(); | ||
let body: Value = serde_json::from_slice(&body).unwrap(); | ||
assert_eq!( | ||
body, | ||
json!({ | ||
"data": None::<String>, | ||
"errors": [{ | ||
"locations": [{"column": 26,"line": 1}], | ||
"message": " --> 1:26\n |\n1 | query HelloQuery { hello \n | ^---\n |\n = expected selection_set, selection, directive, or arguments" | ||
}], | ||
"extensions": {"traceId": "00000000000000000000000000000000"} | ||
}) | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn health_check_returns_expected_json_response_with_200_ok() { | ||
// arrange | ||
let ApplicationRouters { main_router, .. } = TestApp::spawn_routers().await; | ||
|
||
// act | ||
let response = main_router | ||
.oneshot( | ||
Request::builder() | ||
.uri("/health") | ||
.body(Body::empty()) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
let body = response.into_body().collect().await.unwrap().to_bytes(); | ||
let body: Value = serde_json::from_slice(&body).unwrap(); | ||
assert_eq!(body, json!({ "healthy": true })); | ||
} |