Skip to content

Commit

Permalink
tests: refactor subscriptions tests with endpoint client
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsavio committed Apr 3, 2024
1 parent c80bbab commit fc68c93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
12 changes: 12 additions & 0 deletions tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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.")
}
}

static TRACING: Lazy<()> = Lazy::new(|| {
let default_filter_level = "info".to_string();
let subscriber_name = "test".to_string();
Expand Down
27 changes: 3 additions & 24 deletions tests/api/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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"),
Expand All @@ -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!(
Expand All @@ -65,7 +51,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"),
Expand All @@ -74,13 +59,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!(
Expand Down

0 comments on commit fc68c93

Please sign in to comment.