feat: add sanitization check, refactor error returns

This commit is contained in:
Sandro Eiler 2024-02-10 21:33:01 +01:00
parent 13162f6470
commit d7d37341ba
6 changed files with 106 additions and 7 deletions

View file

@ -70,7 +70,7 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
assert_eq!(
422,
response.status().as_u16(),
"The API did not fail with 400 Bad Request when the payload was {}.",
"The API did not fail with 422 when the payload was {}.",
error_message
);
}
@ -104,6 +104,37 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
assert_eq!(saved.name, "le guin");
}
#[tokio::test]
async fn subscribe_returns_a_200_when_fields_are_present_but_empty() {
// 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"),
("name=Ursula&email=definitely-not-an-email", "invalid email"),
];
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.");
// Assert
assert_eq!(
200,
response.status().as_u16(),
"The API did not return a 200 OK when the payload was {}.",
description
);
}
}
async fn spawn_app() -> TestApp {
// The first time `initialize` is invoked the code in `TRACING` is executed.
// All other invocations will instead skip execution.