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::routing::post;
use axum::Router; use axum::Router;
// use serde::Deserialize; use serde::Deserialize;
// #[derive(Deserialize)] #[derive(Debug, Deserialize)]
// struct FormData { struct FormData {
// email: String, email: String,
// name: String, name: String,
// } }
pub fn routes_subscriptions() -> Router {
Router::new().route("/subscriptions", post(|| async {})) 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()); assert_eq!(200, response.status().as_u16());
} }
// #[tokio::test] #[tokio::test]
// async fn subscribe_returns_a_400_when_data_is_missing() { async fn subscribe_returns_a_400_when_data_is_missing() {
// // Arrange // Arrange
// let TestApp { address, .. } = spawn_app().await; let TestApp { address, .. } = spawn_app().await;
// let client = reqwest::Client::new(); let client = reqwest::Client::new();
// let test_cases = vec![ let test_cases = vec![
// ("name=le%20guin", "missing the email"), ("name=le%20guin", "missing the email"),
// ("email=ursula_le_guin%40gmail.com", "missing the name"), ("email=ursula_le_guin%40gmail.com", "missing the name"),
// ("", "missing both name and email"), ("", "missing both name and email"),
// ]; ];
//
// for (invalid_body, error_message) in test_cases { for (invalid_body, error_message) in test_cases {
// // Act // Act
// let response = client let response = client
// .post(&format!("{address}/subscriptions")) .post(&format!("{address}/subscriptions"))
// .header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
// .body(invalid_body) .body(invalid_body)
// .send() .send()
// .await .await
// .expect("Failed to execute request."); .expect("Failed to execute request.");
//
// // Assert // Assert
// assert_eq!( assert_eq!(
// 400, 422,
// response.status().as_u16(), response.status().as_u16(),
// "The API did not fail with 400 Bad Request when the payload was {}.", "The API did not fail with 400 Bad Request when the payload was {}.",
// error_message error_message
// ); );
// } }
// } }
async fn spawn_app() -> TestApp { async fn spawn_app() -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();