Skip to content

Commit

Permalink
feat(router): use authority instead of host to allow matching on port
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Mar 7, 2023
1 parent b64498f commit 425f308
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl FromStr for Request {
Ok(Request::new(
PathAndQueryWithSkipped::from_config(&config, path_and_query_str),
path_and_query_str.to_string(),
http_request.uri().host().map(|s| s.to_string()),
http_request.uri().authority().map(|s| s.to_string()),
http_request.uri().scheme_str().map(|s| s.to_string()),
None,
None,
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Request {
let mut request = Request::from_config(
router_config,
path_and_query_str.to_string(),
http_request.uri().host().map(|s| s.to_string()),
http_request.uri().authority().map(|s| s.to_string()),
http_request.uri().scheme_str().map(|s| s.to_string()),
example.method.clone(),
None,
Expand Down
68 changes: 68 additions & 0 deletions tests/redirectionio_router_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6983,6 +6983,74 @@ fn test_rule_header_regex_3() {
assert_eq!(action.should_log_request(true, response_status_code, None), true);
}

fn setup_rule_host_port() -> Router<Rule> {
let config: RouterConfig = serde_json::from_str(r#"{"always_match_any_host":false,"ignore_header_case":false,"ignore_host_case":false,"ignore_marketing_query_params":true,"ignore_path_and_query_case":false,"marketing_query_params":["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],"pass_marketing_query_params_to_target":true}"#).expect("cannot deserialize");
let mut router = Router::<Rule>::from_config(config);

let route_1: Rule = serde_json::from_str(r#"{"id":"rule-with-host-port","rank":0,"source":{"host":"example.org:8080","path":"/foo"},"status_code":302,"target":"http://example.org:8081/bar"}"#).expect("cannot deserialize");
router.insert(route_1.into_route(&router.config));

router
}


#[test]
fn test_rule_host_port_1() {
let router = setup_rule_host_port();
let default_config = RouterConfig::default();
let request = Request::new(PathAndQueryWithSkipped::from_config(&default_config, r#"/foo"#), r#"/foo"#.to_string(),Some(r#"example.org"#.to_string()),Some(r#"http"#.to_string()),None,None,None);
let request_configured = Request::rebuild_with_config(&router.config, &request);
let matched = router.match_request(&request_configured);
let traces = router.trace_request(&request_configured);
let routes_traces = Trace::<Rule>::get_routes_from_traces(&traces);

assert_eq!(!matched.is_empty(), false);
assert_eq!(!routes_traces.is_empty(), false);

}

#[test]
fn test_rule_host_port_2() {
let router = setup_rule_host_port();
let default_config = RouterConfig::default();
let request = Request::new(PathAndQueryWithSkipped::from_config(&default_config, r#"/foo"#), r#"/foo"#.to_string(),Some(r#"example.org:4040"#.to_string()),Some(r#"http"#.to_string()),None,None,None);
let request_configured = Request::rebuild_with_config(&router.config, &request);
let matched = router.match_request(&request_configured);
let traces = router.trace_request(&request_configured);
let routes_traces = Trace::<Rule>::get_routes_from_traces(&traces);

assert_eq!(!matched.is_empty(), false);
assert_eq!(!routes_traces.is_empty(), false);

}

#[test]
fn test_rule_host_port_3() {
let router = setup_rule_host_port();
let default_config = RouterConfig::default();
let request = Request::new(PathAndQueryWithSkipped::from_config(&default_config, r#"/foo"#), r#"/foo"#.to_string(),Some(r#"example.org:8080"#.to_string()),Some(r#"http"#.to_string()),None,None,None);
let request_configured = Request::rebuild_with_config(&router.config, &request);
let matched = router.match_request(&request_configured);
let traces = router.trace_request(&request_configured);
let routes_traces = Trace::<Rule>::get_routes_from_traces(&traces);

assert_eq!(!matched.is_empty(), true);
assert_eq!(!routes_traces.is_empty(), true);

let mut action = Action::from_routes_rule(matched, &request_configured, None);
let response_status_code = 0;

let action_status_code = action.get_status_code(response_status_code, None);
assert_eq!(action_status_code, 302);
let headers = action.filter_headers(Vec::new(), response_status_code, false, None);
assert_eq!(headers.len(), 1);

let target_header = headers.first().unwrap();
assert_eq!(target_header.name, "Location");
assert_eq!(target_header.value, r#"http://example.org:8081/bar"#);
assert_eq!(action.should_log_request(true, response_status_code, None), true);
}

fn setup_rule_ip_trigger() -> Router<Rule> {
let config: RouterConfig = serde_json::from_str(r#"{"always_match_any_host":false,"ignore_header_case":false,"ignore_host_case":false,"ignore_marketing_query_params":true,"ignore_path_and_query_case":false,"marketing_query_params":["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],"pass_marketing_query_params_to_target":true}"#).expect("cannot deserialize");
let mut router = Router::<Rule>::from_config(config);
Expand Down

0 comments on commit 425f308

Please sign in to comment.