diff --git a/tests/api/health_check.rs b/tests/api/health_check.rs index 76c77a3..85e9648 100644 --- a/tests/api/health_check.rs +++ b/tests/api/health_check.rs @@ -1,17 +1,12 @@ -use crate::helpers::{spawn_app, TestApp}; +use crate::helpers::spawn_app; #[tokio::test] async fn health_check_works() { // Arrange - let TestApp { address, .. } = spawn_app().await; + let app = spawn_app().await; // Act - let client = reqwest::Client::new(); - let response = client - .get(format!("{address}/health_check")) - .send() - .await - .expect("Failed to execute request."); + let response = app.get_health_check().await; // Assert assert!(response.status().is_success()); diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index c80a827..ccee1ce 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -27,6 +27,25 @@ pub struct TestApp { pub db_pool: PgPool, } +impl TestApp { + pub async fn get_health_check(&self) -> reqwest::Response { + reqwest::Client::new() + .get(&format!("{}/health_check", &self.address)) + .send() + .await + .expect("Failed to execute request.") + } + pub async fn post_subscriptions(&self, body: String) -> reqwest::Response { + reqwest::Client::new() + .post(&format!("{}/subscriptions", &self.address)) + .header("Content-Type", "application/x-www-form-urlencoded") + .body(body) + .send() + .await + .expect("Failed to execute request.") + } +} + pub async fn spawn_app() -> TestApp { // The first time `initialize` is invoked the code in `TRACING` is executed. // All other invocations will instead skip execution. @@ -60,7 +79,6 @@ pub async fn spawn_app() -> TestApp { TestApp { address, - // address: format!("http://localhost:{}", application_port), // port: application_port, db_pool: connection_pool, // email_server, diff --git a/tests/api/subscriptions.rs b/tests/api/subscriptions.rs index 4ef4942..276fd09 100644 --- a/tests/api/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -3,8 +3,7 @@ use crate::helpers::{spawn_app, TestApp}; #[tokio::test] async fn subscribe_returns_a_422_when_data_is_missing() { // Arrange - let TestApp { address, .. } = spawn_app().await; - let client = reqwest::Client::new(); + let app = spawn_app().await; let test_cases = vec![ ("name=le%20guin", "missing the email"), ("email=ursula_le_guin%40gmail.com", "missing the name"), @@ -13,13 +12,7 @@ async fn subscribe_returns_a_422_when_data_is_missing() { for (invalid_body, error_message) in test_cases { // Act - let response = client - .post(&format!("{address}/subscriptions")) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(invalid_body) - .send() - .await - .expect("Failed to execute request."); + let response = app.post_subscriptions(invalid_body.into()).await; // Assert assert_eq!( @@ -35,17 +28,10 @@ async fn subscribe_returns_a_422_when_data_is_missing() { async fn subscribe_returns_a_200_for_valid_form_data() { // Arrange let app = spawn_app().await; - let client = reqwest::Client::new(); + let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; // Act - let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(body) - .send() - .await - .expect("Failed to execute request."); + let response = app.post_subscriptions(body.into()).await; // Assert assert_eq!(200, response.status().as_u16()); @@ -63,7 +49,6 @@ async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() { // Arrange let app = spawn_app().await; - let client = reqwest::Client::new(); let test_cases = vec![ ("name=&email=ursula_le_guin%40gmail.com", "empty name"), ("name=Ursula&email=", "empty email"), @@ -72,13 +57,7 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() { for (body, description) in test_cases { // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .header("Content-Type", "application/x-www-form-urlencoded") - .body(body) - .send() - .await - .expect("Failed to execute request."); + let response = app.post_subscriptions(body.into()).await; // Assert assert_eq!(