Skip to content

Commit

Permalink
test: ☑️ refactor route tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneylab committed Nov 28, 2024
1 parent 1b9d3f4 commit fb36a0f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 98 deletions.
98 changes: 0 additions & 98 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,101 +63,3 @@ pub(crate) async fn graphql_handler(
)
.into()
}

#[cfg(test)]
mod tests {
use axum::{
body::Body,
http::{header, Method, Request, StatusCode},
Router,
};
use http_body_util::BodyExt;
use serde_json::{json, Value};
use tower::util::ServiceExt;

use crate::startup::main_router;

async fn get_app() -> Router {
let database_url = "sqlite://:memory:";

main_router(database_url).await
}

#[tokio::test]
async fn graphql_endpoint_returns_200_ok() {
// arrange
let app = get_app().await;

// act
let response = app
.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 app = get_app().await;
let json_request_body: Value = json!(
{"operationName":"HelloQuery","variables":{},"query":"query HelloQuery { hello "
});

// act
let response = app
.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 app = get_app().await;

// act
let response = app
.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 }));
}
}
1 change: 1 addition & 0 deletions tests/api/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
mod helpers;
mod model;
mod observability;
mod routes;
mod startup;
88 changes: 88 additions & 0 deletions tests/api/routes/mod.rs
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 }));
}

0 comments on commit fb36a0f

Please sign in to comment.