From c5a102b01e25631069041a267d2faab94858b7de Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Sat, 26 Aug 2023 23:00:45 +0200 Subject: [PATCH] feat: subscription post answer --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 13 ++++++++++-- tests/health_check.rs | 47 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e24304f..de244b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1681,6 +1681,7 @@ version = "0.1.0" dependencies = [ "actix-web", "reqwest", + "serde", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index b05ed2a..071b3fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ poath = "src/lib.rs" [dependencies] actix-web = "4.3.1" reqwest = "0.11.20" +serde = { version = "1.0.188", features = ["derive"] } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } [[bin]] diff --git a/src/lib.rs b/src/lib.rs index df91374..4807f51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,12 +10,21 @@ async fn health_check() -> impl Responder { HttpResponse::Ok() } +#[derive(serde::Deserialize)] +struct FormData { + email: String, + name: String, +} + +async fn subscribe(_form: web::Form) -> HttpResponse { + HttpResponse::Ok().finish() +} + pub fn run(listener: TcpListener) -> Result { let server = HttpServer::new(|| { App::new() - .route("/", web::get().to(greet)) .route("/health_check", web::get().to(health_check)) - .route("/{name}", web::get().to(greet)) + .route("/subscriptions", web::post().to(subscribe)) }) .listen(listener)? .run(); diff --git a/tests/health_check.rs b/tests/health_check.rs index 5ef520a..cdecaf1 100644 --- a/tests/health_check.rs +++ b/tests/health_check.rs @@ -23,3 +23,50 @@ async fn health_check_works() { assert!(response.status().is_success()); assert_eq!(Some(0), response.content_length()); } + +#[tokio::test] +async fn subscribe_returns_a_200_for_valid_form_data() { + // Arrange + let app_address = spawn_app(); + let client = reqwest::Client::new(); + let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; + // 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()); +} + +#[tokio::test] +async fn subscribe_returns_a_400_when_data_is_missing() { + // Arrange + let app_address = spawn_app(); + 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!("{}/subscriptions", app_address)) + .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 + ); + } +}