chore: first working version with integration test

This commit is contained in:
Sandro Eiler 2023-11-12 14:11:13 +01:00
parent f869b0f067
commit 122c38a0da
19 changed files with 266 additions and 1214 deletions

34
tests/health_check.rs Normal file
View file

@ -0,0 +1,34 @@
use std::net::{SocketAddr, TcpListener};
struct TestApp {
addr: SocketAddr,
}
#[tokio::test]
async fn health_check_works() {
let TestApp { addr, .. } = spawn_app().await;
let client = reqwest::Client::new();
let response = client
.get(format!("http://{addr}/health_check"))
.send()
.await
.expect("Failed to execute request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
// fn spawn_app() {
// let server = learn_axum::run().expect("Failed to bind address.");
// tokio::spawn(server);
// }
async fn spawn_app() -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
let addr = listener.local_addr().unwrap();
let server = learn_axum::run(listener).expect("Failed to bind to address");
tokio::spawn(server);
TestApp { addr }
}

View file

@ -1,71 +0,0 @@
#![allow(unused_imports)]
use anyhow::Result;
use serde_json::json;
#[tokio::test]
async fn test_quick_dev() -> Result<()> {
let hc = httpc_test::new_client("http://localhost:3000")?;
hc.do_get("/hello?name=jen").await?.print().await?;
let hc = httpc_test::new_client("http://localhost:3000")?;
hc.do_get("/hello2/mike").await?.print().await?;
hc.do_get("/src/main.rs").await?.print().await?;
hc.do_get("/src/blub.rs").await?.print().await?;
let req_login = hc.do_post(
"/api/login",
json!(
{
"username": "demo1",
"password": "demowrong"
}
),
);
req_login.await?.print().await?;
let req_login = hc.do_post(
"/api/login",
json!(
{
"username": "demo1",
"password": "demo1"
}
),
);
req_login.await?.print().await?;
hc.do_get("/hello2/mike").await?.print().await?;
let req_create_property = hc.do_post(
"/api/properties",
json!(
{
"address": "Lolilat Street 1",
"contact": "01234 567890"
}
),
);
req_create_property.await?.print().await?;
let req_create_property = hc.do_post(
"/api/properties",
json!(
{
"address": "Lolilat Street 2",
"contact": "01243 217890"
}
),
);
req_create_property.await?.print().await?;
let req_get_properties = hc.do_get("/api/properties").await?;
req_get_properties.print().await?;
let req_delete_property = hc.do_delete("/api/properties/1").await?;
req_delete_property.print().await?;
let req_get_properties = hc.do_get("/api/properties").await?;
req_get_properties.print().await?;
let req_delete_property = hc.do_delete("/api/properties/0").await?;
req_delete_property.print().await?;
Ok(())
}