Skip to content

Commit 6efb8a9

Browse files
qbootjoelwurtz
authored andcommitted
feat(sitemap): add tests
1 parent 7f56025 commit 6efb8a9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/redirectionio_router_test.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,6 +2852,46 @@ fn test_action_seo_override_title_4() {
28522852
}
28532853

28542854

2855+
fn setup_action_sitemap() -> Router<Rule> {
2856+
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");
2857+
let mut router = Router::<Rule>::from_config(config);
2858+
2859+
let route_1: Rule = serde_json::from_str(r#"{"body_filters":[{"action":"replace_text","content":"<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><sitemap><loc>https://redirection.io/sitemap_static.xml</loc></sitemap><sitemap><loc>https://redirection.io/features/sitemap/</loc></sitemap><sitemap><loc>https://redirection.io/news/sitemap/</loc></sitemap></sitemapindex>"}],"id":"action-sitemap","rank":0,"source":{"path":"/sitemap.xml"},"status_code":200}"#).expect("cannot deserialize");
2860+
router.insert(route_1.into_route(&router.config));
2861+
2862+
router
2863+
}
2864+
2865+
2866+
#[test]
2867+
fn test_action_sitemap_1() {
2868+
let router = setup_action_sitemap();
2869+
let default_config = RouterConfig::default();
2870+
let request = Request::new(PathAndQueryWithSkipped::from_config(&default_config, r#"/sitemap.xml"#), r#"/sitemap.xml"#.to_string(),None,None,None,None,None);
2871+
let request_configured = Request::rebuild_with_config(&router.config, &request);
2872+
let matched = router.match_request(&request_configured);
2873+
let traces = router.trace_request(&request_configured);
2874+
let routes_traces = Trace::<Rule>::get_routes_from_traces(&traces);
2875+
2876+
assert_eq!(!matched.is_empty(), true);
2877+
assert_eq!(!routes_traces.is_empty(), true);
2878+
2879+
let mut action = Action::from_routes_rule(matched, &request_configured);
2880+
let response_status_code = 0;
2881+
2882+
let action_status_code = action.get_status_code(response_status_code);
2883+
assert_eq!(action_status_code, 200);
2884+
let body_filter_opt = action.create_filter_body(response_status_code, &[]);
2885+
assert_eq!(body_filter_opt.is_some(), true);
2886+
2887+
let mut body_filter = body_filter_opt.unwrap();
2888+
let mut new_body = body_filter.filter(r#""#.as_bytes().to_vec());
2889+
new_body.extend(body_filter.end());
2890+
assert_eq!(new_body, r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://redirection.io/sitemap_static.xml</loc></sitemap><sitemap><loc>https://redirection.io/features/sitemap/</loc></sitemap><sitemap><loc>https://redirection.io/news/sitemap/</loc></sitemap></sitemapindex>"#.as_bytes().to_vec());
2891+
assert_eq!(action.should_log_request(true, response_status_code), true);
2892+
}
2893+
2894+
28552895
fn setup_action_stop() -> Router<Rule> {
28562896
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");
28572897
let mut router = Router::<Rule>::from_config(config);

0 commit comments

Comments
 (0)