-
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.
refactor: moving the integration tests to tests folder, and create st…
…artup.rs containing all the boilerplate for spawning server
- Loading branch information
Showing
6 changed files
with
160 additions
and
136 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
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,3 @@ | ||
pub mod config; | ||
pub mod handlers; | ||
pub 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
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,32 @@ | ||
use axum::{ | ||
extract::{connect_info::IntoMakeServiceWithConnectInfo, ConnectInfo}, | ||
middleware::AddExtension, | ||
serve::Serve, | ||
Router, | ||
}; | ||
use sqlx::{PgPool, Pool, Postgres}; | ||
use std::net::SocketAddr; | ||
use tokio::net::TcpListener; | ||
|
||
use crate::{config, handlers}; | ||
|
||
fn app(pool: Pool<Postgres>, geocoding_api_key: String) -> Router { | ||
handlers::router(pool, geocoding_api_key) | ||
} | ||
|
||
type Server = Serve< | ||
IntoMakeServiceWithConnectInfo<Router, SocketAddr>, | ||
AddExtension<Router, ConnectInfo<SocketAddr>>, | ||
>; | ||
|
||
pub fn run( | ||
listener: TcpListener, | ||
db_pool: PgPool, | ||
config: config::Config, | ||
) -> Result<Server, std::io::Error> { | ||
let server = axum::serve( | ||
listener, | ||
app(db_pool, config.geocoding_api_key).into_make_service_with_connect_info::<SocketAddr>(), | ||
); | ||
Ok(server) | ||
} |
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,100 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{header, Method, Request, StatusCode}, | ||
}; | ||
use cch23_challenge::handlers::day22::router; | ||
use http_body_util::BodyExt; | ||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready` | ||
|
||
// TODO: remove the coupling between the tests and axum service | ||
// TODO: like this this example https://github.com/LukeMathWalker/zero-to-production/blob/root-chapter-03-part1/tests/health_check.rs | ||
#[tokio::test] | ||
async fn health_check() { | ||
// Arrange | ||
let app = router(); | ||
|
||
// Act | ||
let response = app | ||
.oneshot( | ||
Request::builder() | ||
.uri("/22/health") | ||
.body(Body::empty()) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// Assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
} | ||
|
||
#[tokio::test] | ||
async fn day22_integers() { | ||
// Arrange | ||
let app = router(); | ||
|
||
// Act | ||
let response = app | ||
.oneshot( | ||
Request::builder() | ||
.uri("/22/integers") | ||
.method(Method::POST) | ||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) | ||
.body(Body::from( | ||
r#"888 | ||
77 | ||
888 | ||
22 | ||
77"#, | ||
)) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// Assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
let body = response.into_body().collect().await.unwrap().to_bytes(); | ||
let body = String::from_utf8_lossy(&body[..]); | ||
let expected = "🎁".repeat(22); | ||
assert_eq!(body, expected); | ||
} | ||
|
||
#[tokio::test] | ||
async fn day22_rocket() { | ||
// Arrange | ||
let app = router(); | ||
|
||
// Act | ||
let response = app | ||
.oneshot( | ||
Request::builder() | ||
.uri("/22/rocket") | ||
.method(Method::POST) | ||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON.as_ref()) | ||
.body(Body::from( | ||
r#"5 | ||
0 1 0 | ||
-2 2 3 | ||
3 -3 -5 | ||
1 1 5 | ||
4 3 5 | ||
4 | ||
0 1 | ||
2 4 | ||
3 4 | ||
1 2 | ||
"#, | ||
)) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// Assert | ||
assert_eq!(response.status(), StatusCode::OK); | ||
let body = response.into_body().collect().await.unwrap().to_bytes(); | ||
let expected = "3 26.123"; | ||
let body = String::from_utf8_lossy(&body[..]); | ||
assert_eq!(body, expected); | ||
} |