-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{collections::HashMap, fs::read_to_string}; | ||
|
||
use anyhow::Result; | ||
use millennium_falcon::{ | ||
application_services::{into_galaxy_routes_and_planet_id, MillenniumFalconData}, | ||
infrastructure_services::{actix::run, get_routes_from_db}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_health_check() { | ||
spawn_app("127.0.0.1:8080").await.unwrap(); | ||
|
||
let client = reqwest::Client::new(); | ||
let response = client | ||
.get("http://127.0.0.1:8080/health_check") | ||
.send() | ||
.await | ||
.expect("Failed to execute the request"); | ||
|
||
println!("{response:?}"); | ||
|
||
assert!(response.status().is_success()); | ||
assert_eq!(response.content_length(), Some(0)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_proba_endpoint() { | ||
spawn_app("127.0.0.1:8081").await.unwrap(); | ||
|
||
let client = reqwest::Client::new(); | ||
|
||
let responses = [(1, "0%"), (2, "81%"), (3, "90%"), (4, "100%")] | ||
.into_iter() | ||
.collect::<HashMap<_, _>>(); | ||
|
||
for example_id in 1..5 { | ||
let response = client | ||
.post("http://127.0.0.1:8081/proba") | ||
.body(read_to_string(format!("examples/example{example_id}/empire.json")).unwrap()) | ||
.send() | ||
.await | ||
.expect("Failed to execute the request"); | ||
|
||
println!("{response:?}"); | ||
|
||
assert!(response.status().is_success()); | ||
let text = response.text().await.unwrap(); | ||
assert_eq!(text, responses.get(&example_id).unwrap().to_string()); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_index() { | ||
spawn_app("127.0.0.1:8082").await.unwrap(); | ||
|
||
let client = reqwest::Client::new(); | ||
let response = client | ||
.get("http://127.0.0.1:8082/") | ||
.send() | ||
.await | ||
.expect("Failed to execute the request"); | ||
|
||
println!("{response:?}"); | ||
|
||
assert!(response.status().is_success()); | ||
let text = response.text().await.unwrap(); | ||
assert_eq!(text, read_to_string("front/index.html").unwrap()); | ||
} | ||
|
||
async fn spawn_app(address: &str) -> Result<()> { | ||
let millennium_falcon_data_path = "examples/millennium-falcon.json"; | ||
let millennium_falcon_data = MillenniumFalconData::read(millennium_falcon_data_path)?; | ||
let routes = get_routes_from_db(&millennium_falcon_data.routes_db).await?; | ||
let (galaxy_routes, planet_ids) = into_galaxy_routes_and_planet_id(routes); | ||
let server = run(address, galaxy_routes, planet_ids, millennium_falcon_data)?; | ||
|
||
tokio::spawn(server); | ||
Ok(()) | ||
} |