Skip to content

Commit

Permalink
Merge pull request #19 from alisinabh/setup-struct-type
Browse files Browse the repository at this point in the history
  • Loading branch information
alisinabh authored May 11, 2024
2 parents 0f67f98 + 2297da5 commit d7e3ddf
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions tests/lookup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
service: T,
app_data: Data<MaxmindDB>,
}

async fn setup() -> SetupResult<
impl Service<
actix_http::Request,
Error = actix_web::Error,
Response = ServiceResponse<impl MessageBody>,
>,
Data<MaxmindDB>,
) {
> {
let app_data = atlas_rs::init_db("tests-data/", "GeoIP2-City-Test")
.await
.unwrap();
Expand All @@ -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"],
Expand All @@ -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());
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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}"))
Expand All @@ -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(),
Expand Down

0 comments on commit d7e3ddf

Please sign in to comment.