Skip to content

Commit

Permalink
feat: subscription post answer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 26, 2023
1 parent 636e421 commit c5a102b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 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 @@ -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]]
Expand Down
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}

pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
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();
Expand Down
47 changes: 47 additions & 0 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit c5a102b

Please sign in to comment.