Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

feat: add a /__test_loc__ dockerflow endpoint #282

Merged
merged 6 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/adm/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ pub fn filtered_dma(exclude: &Option<Vec<u16>>, dma: &u16) -> String {
}

/// Main handler for the User Agent HTTP request
///
pub async fn get_tiles(
state: &ServerState,
location: &Location,
Expand Down
18 changes: 17 additions & 1 deletion src/web/dockerflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;
use actix_web::{dev::Payload, web, FromRequest, HttpRequest, HttpResponse};
use actix_web_location::Location;
use serde::Deserialize;
use serde_json::Value;
use serde_json::{json, Value};

use crate::{error::HandlerError, server::ServerState};

Expand All @@ -29,6 +29,7 @@ pub fn service(config: &mut web::ServiceConfig) {
.service(web::resource("/__heartbeat__").route(web::get().to(heartbeat)))
.service(web::resource("/__version__").route(web::get().to(version)))
.service(web::resource("/__error__").route(web::get().to(test_error)))
.service(web::resource("/__loc_test__").route(web::get().to(loc_test)))
.service(web::resource("").route(web::get().to(document_boot)));
}

Expand Down Expand Up @@ -83,6 +84,21 @@ async fn test_error(
Err(err)
}

async fn loc_test(req: HttpRequest, location: Location) -> Result<HttpResponse, HandlerError> {
let ip = req.headers().get("X-FORWARDED-FOR").map(|val| {
val.to_str()
.map(ToOwned::to_owned)
.unwrap_or_else(|val| format!("{:?}", val))
});
let empty_to_null = |val: String| if val.is_empty() { None } else { Some(val) };
Ok(HttpResponse::Ok().json(json!({
"country": empty_to_null(location.country()),
"region": empty_to_null(location.region()),
"provider": empty_to_null(location.provider),
"ip": ip,
})))
}

async fn document_boot(state: web::Data<ServerState>) -> Result<HttpResponse, HandlerError> {
let settings = &state.settings;
return Ok(HttpResponse::Found()
Expand Down
19 changes: 19 additions & 0 deletions src/web/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,22 @@ async fn include_regions() {
assert_eq!(tiles.len(), 1);
assert_eq!(&tiles[0]["name"], "Acme");
}

#[actix_rt::test]
async fn test_loc() {
let mut settings = get_test_settings();

let mut app = init_app!(settings).await;

let req = test::TestRequest::get()
.uri("/__loc_test__")
.header("X-FORWARDED-FOR", TEST_ADDR)
.to_request();

let resp = test::call_service(&mut app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let result: Value = test::read_body_json(resp).await;
assert_eq!(result["country"], "US");
assert_eq!(result["region"], "WA");
}