mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
finished chapter 3.7
This commit is contained in:
parent
ff20460c13
commit
c669e49c61
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1353,5 +1353,6 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -15,6 +15,7 @@ name = "zero2prod"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.7"
|
axum = "0.7"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|||||||
20
src/lib.rs
20
src/lib.rs
@ -1,10 +1,18 @@
|
|||||||
use axum::{extract::Path, http::StatusCode, response::IntoResponse, routing::get, Router};
|
use axum::{
|
||||||
|
extract::Path,
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
routing::{get, post},
|
||||||
|
Form, Router,
|
||||||
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
pub fn app() -> Router {
|
pub fn app() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
.route("/:name", get(greet))
|
.route("/:name", get(greet))
|
||||||
.route("/health_check", get(health_check))
|
.route("/health_check", get(health_check))
|
||||||
|
.route("/subscribtions", post(subscribe))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn root() -> impl IntoResponse {
|
async fn root() -> impl IntoResponse {
|
||||||
@ -17,3 +25,13 @@ async fn greet(Path(name): Path<String>) -> impl IntoResponse {
|
|||||||
async fn health_check() -> impl IntoResponse {
|
async fn health_check() -> impl IntoResponse {
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FormData {
|
||||||
|
name: String,
|
||||||
|
email: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn subscribe(Form(form): Form<FormData>) -> impl IntoResponse {
|
||||||
|
StatusCode::OK
|
||||||
|
}
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
|
use reqwest::Client;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn health_check() {
|
async fn health_check() {
|
||||||
let address = spawn_app().await;
|
let address = spawn_app().await;
|
||||||
let url = format!("{}/health_check", &address);
|
let url = format!("{}/health_check", &address);
|
||||||
let client = reqwest::Client::new();
|
let client = Client::new();
|
||||||
let response = client
|
let response = client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.send()
|
.send()
|
||||||
@ -15,6 +16,52 @@ async fn health_check() {
|
|||||||
assert_eq!(Some(0), response.content_length());
|
assert_eq!(Some(0), response.content_length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn subscribe_returns_200_for_valid_form_data() {
|
||||||
|
let address = spawn_app().await;
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
let body = "name=kristofers%20solo&email=dev%40kristofers.solo";
|
||||||
|
let response = client
|
||||||
|
.post(&format!("{}/subscribtions", &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_when_data_is_missing() {
|
||||||
|
let address = spawn_app().await;
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
let test_cases = vec![
|
||||||
|
("name=krisotfers%20solo", "missing the email"),
|
||||||
|
("email=dev%40kristofers.solo", "missing the name"),
|
||||||
|
("", "missing both name and email"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (invalid_body, error_message) in test_cases {
|
||||||
|
let response = client
|
||||||
|
.post(&format!("{}/subscribtions", &address))
|
||||||
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
.body(invalid_body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
422,
|
||||||
|
response.status().as_u16(),
|
||||||
|
"The API did not call with 400 Bad Request when the payload was {}.",
|
||||||
|
error_message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn spawn_app() -> String {
|
async fn spawn_app() -> String {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0")
|
let listener = TcpListener::bind("127.0.0.1:0")
|
||||||
.await
|
.await
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user