Skip to content

Commit

Permalink
Completed 3.7.3.1
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony M. Bonafide <AnthonyMBonafide@gmail.com>
  • Loading branch information
AnthonyMBonafide committed Mar 20, 2024
1 parent acb36fa commit 57982d0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ name = "zero2prod"
[dependencies]
actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
reqwest = "0.11"
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
use std::net::TcpListener;

use actix_web::{dev::Server, web, App, HttpResponse, HttpServer, Responder};
use actix_web::{dev::Server, web, App, HttpResponse, HttpServer};
#[derive(serde::Deserialize, serde::Serialize)]
struct FormData {
name: String,
email: String,
}

async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}

async fn health_check() -> impl Responder {
async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}

pub fn run(tcp_listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| App::new().route("/health_check", web::get().to(health_check)))
.listen(tcp_listener)?
.run();
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
.route("/subscribe", web::post().to(subscribe))
})
.listen(tcp_listener)?
.run();

Ok(server)
}
43 changes: 43 additions & 0 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,49 @@ async fn health_check_works() {
assert_eq!(Some(0), response.content_length());
}

#[tokio::test]
async fn subscribe_returns_200_for_valid_form_data() {
let address = spawn_app();
let client = reqwest::Client::new();

let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
let response = client
.post(&format!("{}/subscribe", &address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request");

assert_eq!(200, response.status().as_u16())
}
#[tokio::test]
async fn subscribe_returns_400_for_missing_form_data() {
let address = spawn_app();
let client = reqwest::Client::new();
let test_cases = vec![
("name=le%20guin", "missing email"),
("email=ursla_le_guin%40gmail.com", "missing name"),
("", "missing both name and email"),
];

for (invalid_body, error_message) in test_cases {
let response = client
.post(&format!("{}/subscribe", &address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request");

assert_eq!(
400,
response.status().as_u16(),
"The API did not fail with a 400 when the payload was {}",
error_message
)
}
}
fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind port");
let port = listener.local_addr().unwrap().port();
Expand Down

0 comments on commit 57982d0

Please sign in to comment.