First import
This commit is contained in:
22
template/api/tests/api/default.rs
Normal file
22
template/api/tests/api/default.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::helpers::app_address;
|
||||
use ::{{crate_name}}::Object;
|
||||
use rstest::*;
|
||||
|
||||
mod get_object {
|
||||
use super::*;
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_hello(app_address: &str) {
|
||||
let object: Object = reqwest::Client::new()
|
||||
.get(&format!("{}/hello", &app_address))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request")
|
||||
.json()
|
||||
.await
|
||||
.expect("Cannot parse JSON result");
|
||||
|
||||
assert_eq!(object.name, "test");
|
||||
}
|
||||
}
|
||||
15
template/api/tests/api/health_check.rs
Normal file
15
template/api/tests/api/health_check.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use crate::helpers::app_address;
|
||||
use rstest::*;
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn health_check_works(app_address: &str) {
|
||||
let response = reqwest::Client::new()
|
||||
.get(&format!("{}/ping", &app_address))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.");
|
||||
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(Some(0), response.content_length());
|
||||
}
|
||||
31
template/api/tests/api/helpers.rs
Normal file
31
template/api/tests/api/helpers.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use {{crate_name}}_api::configuration::Settings;
|
||||
use {{crate_name}}_api::observability::{get_subscriber, init_subscriber};
|
||||
use rstest::*;
|
||||
use std::net::TcpListener;
|
||||
use tracing::info;
|
||||
|
||||
fn setup_tracing(settings: &Settings) {
|
||||
info!("Setting up tracing");
|
||||
let subscriber = get_subscriber(&settings.application.log_directive);
|
||||
init_subscriber(subscriber);
|
||||
}
|
||||
|
||||
fn setup_server(settings: &Settings) -> String {
|
||||
info!("Setting up server");
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
|
||||
let server = {{crate_name}}_api::run(listener, settings).expect("Failed to bind address");
|
||||
let _ = tokio::spawn(server);
|
||||
format!("http://127.0.0.1:{}", port)
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
#[once]
|
||||
pub fn app_address() -> String {
|
||||
let settings = Settings::new_from_file(Some("config/test".to_string()))
|
||||
.expect("Cannot load test configuration");
|
||||
setup_tracing(&settings);
|
||||
let address = setup_server(&settings);
|
||||
address
|
||||
}
|
||||
3
template/api/tests/api/main.rs
Normal file
3
template/api/tests/api/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod default;
|
||||
mod health_check;
|
||||
mod helpers;
|
||||
Reference in New Issue
Block a user