From 476eed4559e4683c4128e78f1efde8026525ee39 Mon Sep 17 00:00:00 2001 From: Sandro Eiler Date: Tue, 2 Jan 2024 14:39:30 +0100 Subject: [PATCH] refactor: change query to form for subscription api --- src/routes/subscriptions.rs | 27 ++++++++++++----- tests/health_check.rs | 60 ++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs index d7f8451..ebb1c5f 100644 --- a/src/routes/subscriptions.rs +++ b/src/routes/subscriptions.rs @@ -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) { + // 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)) } diff --git a/tests/health_check.rs b/tests/health_check.rs index a60e846..5095778 100644 --- a/tests/health_check.rs +++ b/tests/health_check.rs @@ -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();