feat: subscribe
API route skeleton
- Added `serde` crate for serialisation or deserialisation of data - Added test cases for the `subscribe` API route - Refactor the testing setup to another module `test_utils` - use random TCP port for testing
This commit is contained in:
5
tests/README.md
Normal file
5
tests/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Tests
|
||||
|
||||
## Notes
|
||||
|
||||
`tokio` spins up a new async runtime every time at the beginning of each test case and shutdown at the end of each test case the `spawn_app()` function therefore only survives as long as the runtime.
|
@@ -1,32 +1,19 @@
|
||||
use std::{fmt::format, net::TcpListener};
|
||||
// tokio spins up a new async runtime every time
|
||||
// at the beginning of each test case and shutdown at
|
||||
// the end of each test case
|
||||
// the spawn_app() function therefore only survives as long as the runtime
|
||||
mod test_utils;
|
||||
|
||||
use test_utils::spawn_app;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_check_works(){
|
||||
async fn health_check_works() {
|
||||
let server_address = spawn_app();
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let response = client.get(&format!("{}/health_check", &server_address)).send().await.expect("Failed to execute health_check request.");
|
||||
let response = client
|
||||
.get(&format!("{}/health_check", &server_address))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute health_check request.");
|
||||
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(Some(0), response.content_length());
|
||||
}
|
||||
|
||||
fn spawn_app() -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port");
|
||||
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
|
||||
let server = email_newsletter_api::run(listener).expect("Failed to bind address");
|
||||
|
||||
// run() returns an instance of HttpServer that will run forever.
|
||||
// We don't want this behavior
|
||||
// Therefore we want to spawn our server, run our test logic
|
||||
// and then tear down the entire test suite
|
||||
let _ = tokio::spawn(server);
|
||||
|
||||
format!("http://127.0.0.1:{}", port)
|
||||
}
|
||||
|
53
tests/subscribe.rs
Normal file
53
tests/subscribe.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
mod test_utils;
|
||||
|
||||
use test_utils::spawn_app;
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
let server_address = spawn_app();
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = "name=le%20test&email=le_test%40gmail.com";
|
||||
|
||||
let response = client
|
||||
.post(&format!("{}/subscribe", &server_address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute subscribe request.");
|
||||
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(Some(0), response.content_length());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_400_when_data_is_missing() {
|
||||
let server_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 {
|
||||
let response = client
|
||||
.post(&format!("{}/subscribe", &server_address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(invalid_body)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute subscribe request.");
|
||||
|
||||
assert_eq!(
|
||||
400,
|
||||
response.status().as_u16(),
|
||||
"The API failed with 400 Bad Request when the payload was {}.",
|
||||
error_message
|
||||
)
|
||||
}
|
||||
}
|
24
tests/test_utils.rs
Normal file
24
tests/test_utils.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::net::TcpListener;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(clippy::let_underscore_future)]
|
||||
pub fn spawn_app() -> String {
|
||||
/* Spawn a app server with a TcpListener bound to localhost:<random port>
|
||||
*
|
||||
* Returns a valid IPv4 string (i.e localhost:8080)
|
||||
*/
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port");
|
||||
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
|
||||
let server = email_newsletter_api::run(listener).expect("Failed to bind address");
|
||||
|
||||
/* `tokio::spawn(/*async task*/)` will spawn an async task to be run.
|
||||
We can continue executing other code concurrently while `task` runs in the background.
|
||||
If we need to wait for `task` to complete before proceeding,
|
||||
we can use `task.await`
|
||||
(which `#[tokio::test]` will take care for us in the mean time).*/
|
||||
let _ = tokio::spawn(server);
|
||||
|
||||
format!("http://127.0.0.1:{}", port)
|
||||
}
|
Reference in New Issue
Block a user