Update gitignore

This commit is contained in:
Nick Bland 2021-11-05 14:17:36 +10:00
parent 0b6fd3cd43
commit 33c5cf0adc
No known key found for this signature in database
GPG Key ID: B46CF88E4DAB4A2C
4 changed files with 125 additions and 124 deletions

View File

@ -1,16 +1,16 @@
kind: pipeline kind: pipeline
type: docker type: docker
name: mailApp name: mailApp
trigger: trigger:
branch: branch:
- master - master
event: event:
- push - push
steps: steps:
- name: testBuild - name: testBuild
image: rust:1.56 image: rust:1.56
commands: commands:
- cargo build --verbose --all - cargo build --verbose --all
- cargo test --verbose --all - cargo test --verbose --all

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
.vscode

View File

@ -1,28 +1,28 @@
use actix_web::{web, App, HttpResponse, HttpServer}; use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web::dev::Server; use actix_web::dev::Server;
use std::net::TcpListener; use std::net::TcpListener;
async fn health_check() -> HttpResponse { async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish() HttpResponse::Ok().finish()
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
struct FormData { struct FormData {
email: String, email: String,
name: String name: String
} }
async fn subscribe(_form: web::Form<FormData>) -> HttpResponse { async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish() 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)) .route("/subscriptions", web::post().to(subscribe))
}) })
.listen(listener)? .listen(listener)?
.run(); .run();
Ok(server) Ok(server)
} }

View File

@ -1,83 +1,83 @@
use std::net::TcpListener; use std::net::TcpListener;
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`] // Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
fn spawn_app() -> String { fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
let port = listener.local_addr().unwrap().port(); let port = listener.local_addr().unwrap().port();
let server = mail_app::run(listener).expect("Failed to bind address"); let server = mail_app::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server); let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port) 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
let address = spawn_app(); let address = spawn_app();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
// Perform a 'reqwest' against endpoint // Perform a 'reqwest' against endpoint
let response = client let response = client
.get(&format!("{}/health_check", &address)) .get(&format!("{}/health_check", &address))
.send() .send()
.await .await
.expect("Failed to execute request."); .expect("Failed to execute request.");
// Assert our test // Assert our test
assert!(response.status().is_success()); assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length()); assert_eq!(Some(0), response.content_length());
} }
#[actix_rt::test] #[actix_rt::test]
async fn subscribe_returns_200_for_valid_form_data() { async fn subscribe_returns_200_for_valid_form_data() {
// Arrange // Arrange
let app_address = spawn_app(); let app_address = spawn_app();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let body = "name=le%20guin&email=usrula_le_guin%40gmail.com"; let body = "name=le%20guin&email=usrula_le_guin%40gmail.com";
// Act // Act
let response = client let response = client
.post(&format!("{}/subscriptions", &app_address)) .post(&format!("{}/subscriptions", &app_address))
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.body(body) .body(body)
.send() .send()
.await .await
.expect("Failed to execute request."); .expect("Failed to execute request.");
// Assert test // Assert test
assert_eq!(200, response.status().as_u16()); assert_eq!(200, response.status().as_u16());
} }
#[actix_rt::test] #[actix_rt::test]
async fn subscrib_returns_400_for_missing_form_data() { async fn subscrib_returns_400_for_missing_form_data() {
//Arrange //Arrange
let app_address = spawn_app(); let app_address = spawn_app();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let test_cases = vec![ let test_cases = vec![
("name=le%20guin", "missing the email"), ("name=le%20guin", "missing the email"),
("email=ursula_le_guin%40gmail.com", "missing the name"), ("email=ursula_le_guin%40gmail.com", "missing the name"),
("", "missing both name and email") ("", "missing both name and email")
]; ];
for (invalid_body, error_message) in test_cases { for (invalid_body, error_message) in test_cases {
// Act // Act
let response = client let response = client
.post(&format!("{}/subscriptions", &app_address)) .post(&format!("{}/subscriptions", &app_address))
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body) .body(invalid_body)
.send() .send()
.await .await
.expect("Failed to execute request."); .expect("Failed to execute request.");
// Assert // Assert
assert_eq!( assert_eq!(
400, 400,
response.status().as_u16(), response.status().as_u16(),
// Customised error message on test failure // Customised error message on test failure
"The API did not fail with 400 Bad Request when the payload was {}.", error_message "The API did not fail with 400 Bad Request when the payload was {}.", error_message
); );
} }
} }