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

@ -1,12 +1,23 @@
use axum::Form;
use axum::http::StatusCode;
use axum::routing::post;
use axum::Router;
// use serde::Deserialize;
use serde::Deserialize;
// #[derive(Deserialize)]
// struct FormData {
// email: String,
// name: String,
// }
pub fn routes_subscriptions() -> Router {
Router::new().route("/subscriptions", post(|| async {}))
#[derive(Debug, Deserialize)]
struct FormData {
email: String,
name: String,
}
async fn subscribe(Form(params): Form<FormData>) {
// println!("Params are: {params:?}");
// if params.name.is_empty() || params.email.is_empty() {
// return StatusCode::NOT_ACCEPTABLE;
// }
// StatusCode::OK
}
pub fn routes_subscriptions() -> Router {
Router::new().route("/subscriptions", post(subscribe))
}

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();