refactor: change query to form for subscription api

This commit is contained in:
Sandro Eiler 2024-01-02 14:39:30 +01:00
parent 08561d48a2
commit 476eed4559
2 changed files with 49 additions and 38 deletions

View file

@ -52,36 +52,36 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
assert_eq!(200, response.status().as_u16());
}
// #[tokio::test]
// async fn subscribe_returns_a_400_when_data_is_missing() {
// // Arrange
// let TestApp { address, .. } = 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"),
// ("", "missing both name and email"),
// ];
//
// for (invalid_body, error_message) in test_cases {
// // Act
// let response = client
// .post(&format!("{address}/subscriptions"))
// .header("Content-Type", "application/x-www-form-urlencoded")
// .body(invalid_body)
// .send()
// .await
// .expect("Failed to execute request.");
//
// // Assert
// assert_eq!(
// 400,
// response.status().as_u16(),
// "The API did not fail with 400 Bad Request when the payload was {}.",
// error_message
// );
// }
// }
#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
// Arrange
let TestApp { address, .. } = 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"),
("", "missing both name and email"),
];
for (invalid_body, error_message) in test_cases {
// Act
let response = client
.post(&format!("{address}/subscriptions"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request.");
// Assert
assert_eq!(
422,
response.status().as_u16(),
"The API did not fail with 400 Bad Request when the payload was {}.",
error_message
);
}
}
async fn spawn_app() -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();