Extract helper method.

This commit is contained in:
LukeMathWalker 2021-02-14 17:07:31 +00:00
parent e562b7df6f
commit 04c53d49da
2 changed files with 15 additions and 24 deletions

View file

@ -18,6 +18,18 @@ pub struct TestApp {
pub db_pool: PgPool, 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 { pub async fn spawn_app() -> TestApp {
// Randomise configuration to ensure test isolation // Randomise configuration to ensure test isolation
let configuration = { let configuration = {

View file

@ -4,17 +4,10 @@ use crate::helpers::spawn_app;
async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_200_for_valid_form_data() {
// Arrange // Arrange
let app = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new();
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
// Act // Act
let response = client let response = app.post_subscriptions(body.into()).await;
.post(&format!("{}/subscriptions", &app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!(200, response.status().as_u16()); 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() { async fn subscribe_returns_a_400_when_data_is_missing() {
// Arrange // Arrange
let app = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![ let test_cases = vec![
("name=le%20guin", "missing the email"), ("name=le%20guin", "missing the email"),
("email=ursula_le_guin%40gmail.com", "missing the name"), ("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 { for (invalid_body, error_message) in test_cases {
// Act // Act
let response = client let response = app.post_subscriptions(invalid_body.into()).await;
.post(&format!("{}/subscriptions", &app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!( 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() { async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
// Arrange // Arrange
let app = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![ let test_cases = vec![
("name=&email=ursula_le_guin%40gmail.com", "empty name"), ("name=&email=ursula_le_guin%40gmail.com", "empty name"),
("name=Ursula&email=", "empty email"), ("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 { for (body, description) in test_cases {
// Act // Act
let response = client let response = app.post_subscriptions(body.into()).await;
.post(&format!("{}/subscriptions", &app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!( assert_eq!(