From 04c53d49da7e6e3fb09845a1654ca07fa15e9a96 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 14 Feb 2021 17:07:31 +0000 Subject: [PATCH] Extract helper method. --- tests/api/helpers.rs | 12 ++++++++++++ tests/api/subscriptions.rs | 27 +++------------------------ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index dbe09a9..661824d 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -18,6 +18,18 @@ pub struct TestApp { pub db_pool: PgPool, } +impl TestApp { + 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 { // Randomise configuration to ensure test isolation let configuration = { diff --git a/tests/api/subscriptions.rs b/tests/api/subscriptions.rs index 803b299..e866dc3 100644 --- a/tests/api/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -4,17 +4,10 @@ use crate::helpers::spawn_app; 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 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()); @@ -32,7 +25,6 @@ async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_400_when_data_is_missing() { // Arrange let app = 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"), @@ -41,13 +33,7 @@ async fn subscribe_returns_a_400_when_data_is_missing() { for (invalid_body, error_message) in test_cases { // Act - let response = client - .post(&format!("{}/subscriptions", &app.address)) - .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!( @@ -64,7 +50,6 @@ async fn subscribe_returns_a_400_when_data_is_missing() { 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"), @@ -73,13 +58,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!(