Add in framework for post requests for user subscriptions
Complete up to page 45
This commit is contained in:
parent
23e1a3f8cb
commit
78311c8cb3
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -754,6 +754,7 @@ dependencies = [
|
|||||||
"actix-rt",
|
"actix-rt",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.0.0-beta.8"
|
actix-web = "4.0.0-beta.8"
|
||||||
|
serde = { version = "1", features = ["derive"]}
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "2"
|
actix-rt = "2"
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -6,10 +6,21 @@ async fn health_check() -> HttpResponse {
|
|||||||
HttpResponse::Ok().finish()
|
HttpResponse::Ok().finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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> {
|
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||||
let server = HttpServer::new(|| {
|
let server = HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.route("/health_check", web::get().to(health_check))
|
.route("/health_check", web::get().to(health_check))
|
||||||
|
.route("/subscriptions", web::post().to(subscribe))
|
||||||
})
|
})
|
||||||
.listen(listener)?
|
.listen(listener)?
|
||||||
.run();
|
.run();
|
||||||
|
@ -1,5 +1,17 @@
|
|||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
|
||||||
|
fn spawn_app() -> String {
|
||||||
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
|
||||||
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
|
||||||
|
let server = mail_app::run(listener).expect("Failed to bind address");
|
||||||
|
|
||||||
|
let _ = tokio::spawn(server);
|
||||||
|
|
||||||
|
format!("http://127.0.0.1:{}", port)
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn health_check_works() {
|
async fn health_check_works() {
|
||||||
// Arrange
|
// Arrange
|
||||||
@ -19,13 +31,53 @@ async fn health_check_works() {
|
|||||||
assert_eq!(Some(0), response.content_length());
|
assert_eq!(Some(0), response.content_length());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_app() -> String {
|
#[actix_rt::test]
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
|
async fn subscribe_returns_200_for_valid_form_data() {
|
||||||
let port = listener.local_addr().unwrap().port();
|
// Arrange
|
||||||
|
let app_address = spawn_app();
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let body = "name=le%20guin&email=usrula_le_guin%40gmail.com";
|
||||||
|
|
||||||
let server = mail_app::run(listener).expect("Failed to bind address");
|
// 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.");
|
||||||
|
|
||||||
let _ = tokio::spawn(server);
|
// Assert test
|
||||||
|
assert_eq!(200, response.status().as_u16());
|
||||||
format!("http://127.0.0.1:{}", port)
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn subscrib_returns_400_for_missing_form_data() {
|
||||||
|
//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(),
|
||||||
|
// Customised error message on test failure
|
||||||
|
"The API did not fail with 400 Bad Request when the payload was {}.", error_message
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user