diff --git a/tests/lookup_service.rs b/tests/lookup_service.rs index 70c1e66..5a83ec1 100644 --- a/tests/lookup_service.rs +++ b/tests/lookup_service.rs @@ -3,14 +3,18 @@ use actix_web::dev::{Service, ServiceResponse}; use actix_web::{test, web::Data, App}; use atlas_rs::maxmind_db::MaxmindDB; -async fn setup() -> ( +struct SetupResult { + service: T, + app_data: Data, +} + +async fn setup() -> SetupResult< impl Service< actix_http::Request, Error = actix_web::Error, Response = ServiceResponse, >, - Data, -) { +> { let app_data = atlas_rs::init_db("tests-data/", "GeoIP2-City-Test") .await .unwrap(); @@ -21,17 +25,17 @@ async fn setup() -> ( ) .await; - (service, app_data) + SetupResult { service, app_data } } #[actix_web::test] async fn test_lookup_endpoint() { - let (service, app_data) = setup().await; + let setup = setup().await; let req = test::TestRequest::get() .uri("/geoip/lookup/city/214.78.120.1") .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; - let db = app_data.db.read().await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; + let db = setup.app_data.db.read().await; assert_eq!(resp["database_build_epoch"], db.build_epoch()); assert_eq!( resp["results"]["214.78.120.1"]["city"]["geoname_id"], @@ -45,12 +49,12 @@ async fn test_lookup_endpoint() { #[actix_web::test] async fn test_multiple_ips() { - let (service, _app_data) = setup().await; + let setup = setup().await; let req = test::TestRequest::get() .uri("/geoip/lookup/city/214.78.120.1,214.78.120.2,4.2.2.4") .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; assert!(resp["results"].get("214.78.120.1").is_some()); assert!(resp["results"].get("214.78.120.2").is_some()); @@ -59,24 +63,24 @@ async fn test_multiple_ips() { #[actix_web::test] async fn test_non_existing_ip() { - let (service, _app_data) = setup().await; + let setup = setup().await; let req = test::TestRequest::get() .uri("/geoip/lookup/city/1.1.1.1") .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; assert!(resp["results"].get("1.1.1.1").unwrap().is_null()); } #[actix_web::test] async fn test_special_ip() { - let (service, _app_data) = setup().await; + let setup = setup().await; let req = test::TestRequest::get() .uri("/geoip/lookup/city/192.168.1.1") .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; assert_eq!( resp["error"]["code"].as_str().unwrap(), @@ -86,12 +90,12 @@ async fn test_special_ip() { #[actix_web::test] async fn test_invalid_ip() { - let (service, _app_data) = setup().await; + let setup = setup().await; let req = test::TestRequest::get() .uri("/geoip/lookup/city/192.168.1.") .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; assert_eq!( resp["error"]["code"].as_str().unwrap(), @@ -101,7 +105,7 @@ async fn test_invalid_ip() { #[actix_web::test] async fn test_rejects_too_many_ips() { - let (service, _app_data) = setup().await; + let setup = setup().await; let ip_addresses = (1..=51) .map(|i| format!("1.1.1.{i}")) @@ -112,7 +116,7 @@ async fn test_rejects_too_many_ips() { .uri(format!("/geoip/lookup/city/{ip_addresses}").as_ref()) .to_request(); - let resp: serde_json::Value = test::call_and_read_body_json(&service, req).await; + let resp: serde_json::Value = test::call_and_read_body_json(&setup.service, req).await; assert_eq!( resp["error"]["code"].as_str().unwrap(),