mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 05:56:35 +00:00
Extract helper method.
This commit is contained in:
parent
e562b7df6f
commit
04c53d49da
2 changed files with 15 additions and 24 deletions
|
@ -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 = {
|
||||
|
|
|
@ -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!(
|
||||
|
|
Loading…
Reference in a new issue