2024-01-01 21:03:40 +01:00
|
|
|
use learn_axum::configuration::get_configuration;
|
|
|
|
|
use sqlx::{Connection, PgConnection};
|
2023-12-15 23:13:00 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
use tokio::net::TcpListener;
|
2023-11-12 14:11:13 +01:00
|
|
|
|
|
|
|
|
struct TestApp {
|
2024-01-01 21:02:31 +01:00
|
|
|
address: String,
|
2023-11-12 14:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn health_check_works() {
|
2023-11-25 08:48:09 +01:00
|
|
|
// Arrange
|
2024-01-01 21:02:31 +01:00
|
|
|
let TestApp { address, .. } = spawn_app().await;
|
2023-11-12 14:11:13 +01:00
|
|
|
|
2023-11-25 08:48:09 +01:00
|
|
|
// Act
|
2023-11-12 14:11:13 +01:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
let response = client
|
2024-01-01 21:02:31 +01:00
|
|
|
.get(format!("{address}/health_check"))
|
2023-11-12 14:11:13 +01:00
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to execute request.");
|
2023-11-25 08:48:09 +01:00
|
|
|
|
|
|
|
|
// Assert
|
2023-11-12 14:11:13 +01:00
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
assert_eq!(Some(0), response.content_length());
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 08:48:09 +01:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn subscribe_returns_a_200_for_valid_form_data() {
|
|
|
|
|
// Arrange
|
2024-01-01 21:02:31 +01:00
|
|
|
let TestApp { address } = spawn_app().await;
|
|
|
|
|
let configuration = get_configuration().expect("Failed to read configuration");
|
|
|
|
|
let connection_string = configuration.database.connection_string();
|
|
|
|
|
// The `Connection` trait MUST be in scope for us to invoke
|
|
|
|
|
// `PgConnection::connect` - it is not an inherent method of the struct!
|
|
|
|
|
let connection = PgConnection::connect(&connection_string)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to connect to Postgres.");
|
2023-11-25 08:48:09 +01:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
let response = client
|
2024-01-01 21:02:31 +01:00
|
|
|
.post(&format!("{}/subscriptions", &address))
|
2023-11-25 08:48:09 +01:00
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
.body(body)
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Failed to execute request.");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
assert_eq!(200, response.status().as_u16());
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 21:02:31 +01:00
|
|
|
// #[tokio::test]
|
|
|
|
|
// async fn subscribe_returns_a_400_when_data_is_missing() {
|
|
|
|
|
// // Arrange
|
|
|
|
|
// let TestApp { addr, .. } = spawn_app().await;
|
|
|
|
|
// let client = reqwest::Client::new();
|
|
|
|
|
// let test_cases = vec![
|
|
|
|
|
// ("name=le%20guin", "missing the email"),
|
|
|
|
|
// ("email=ursula_le_guin%40gmail.com", "missing the name"),
|
|
|
|
|
// ("", "missing both name and email"),
|
|
|
|
|
// ];
|
|
|
|
|
//
|
|
|
|
|
// for (invalid_body, error_message) in test_cases {
|
|
|
|
|
// // Act
|
|
|
|
|
// let response = client
|
|
|
|
|
// .post(&format!("http://{addr}/subscriptions"))
|
|
|
|
|
// .header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
// .body(invalid_body)
|
|
|
|
|
// .send()
|
|
|
|
|
// .await
|
|
|
|
|
// .expect("Failed to execute request.");
|
|
|
|
|
//
|
|
|
|
|
// // Assert
|
|
|
|
|
// assert_eq!(
|
|
|
|
|
// 400,
|
|
|
|
|
// response.status().as_u16(),
|
|
|
|
|
// "The API did not fail with 400 Bad Request when the payload was {}.",
|
|
|
|
|
// error_message
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2023-11-25 08:48:09 +01:00
|
|
|
|
2023-11-12 14:11:13 +01:00
|
|
|
async fn spawn_app() -> TestApp {
|
2023-12-15 23:13:00 +01:00
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
2024-01-01 21:02:31 +01:00
|
|
|
let address = format!("http://{}", listener.local_addr().unwrap());
|
|
|
|
|
|
2023-12-15 23:13:00 +01:00
|
|
|
tokio::spawn(async move {
|
2024-01-01 14:34:42 +01:00
|
|
|
axum::serve(listener, learn_axum::startup::app())
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
});
|
2024-01-01 21:03:40 +01:00
|
|
|
TestApp { address }
|
2023-11-12 14:11:13 +01:00
|
|
|
}
|