-
Notifications
You must be signed in to change notification settings - Fork 11
Add route_request dispatch coverage #689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ use std::sync::Arc; | |
|
|
||
| use edgezero_core::key_value_store::NoopKvStore; | ||
| use error_stack::Report; | ||
| use fastly::http::StatusCode; | ||
| use fastly::Request; | ||
| use fastly::http::{header, StatusCode}; | ||
| use fastly::{Request, Response}; | ||
| use trusted_server_core::auction::build_orchestrator; | ||
| use trusted_server_core::integrations::IntegrationRegistry; | ||
| use trusted_server_core::platform::{ | ||
|
|
@@ -118,7 +118,21 @@ impl PlatformGeo for NoopGeo { | |
| } | ||
|
|
||
| fn create_test_settings() -> Settings { | ||
| let settings = Settings::from_toml( | ||
| create_test_settings_with( | ||
| Some("missing-consent-store"), | ||
| "https://origin.test-publisher.com", | ||
| ) | ||
| } | ||
|
|
||
| fn create_test_settings_without_consent_store() -> Settings { | ||
| create_test_settings_with(None, "https://origin.test-publisher.com") | ||
| } | ||
|
|
||
| fn create_test_settings_with(consent_store: Option<&str>, origin_url: &str) -> Settings { | ||
| let consent_config = consent_store | ||
| .map(|store| format!("\n [consent]\n consent_store = \"{store}\"\n")) | ||
| .unwrap_or_default(); | ||
| let settings = Settings::from_toml(&format!( | ||
| r#" | ||
| [[handlers]] | ||
| path = "^/admin" | ||
|
|
@@ -128,7 +142,7 @@ fn create_test_settings() -> Settings { | |
| [publisher] | ||
| domain = "test-publisher.com" | ||
| cookie_domain = ".test-publisher.com" | ||
| origin_url = "https://origin.test-publisher.com" | ||
| origin_url = "{origin_url}" | ||
| proxy_secret = "unit-test-proxy-secret" | ||
|
|
||
| [edge_cookie] | ||
|
|
@@ -138,10 +152,7 @@ fn create_test_settings() -> Settings { | |
| enabled = false | ||
| config_store_id = "test-config-store-id" | ||
| secret_store_id = "test-secret-store-id" | ||
|
|
||
| [consent] | ||
| consent_store = "missing-consent-store" | ||
|
|
||
| {consent_config} | ||
| [integrations.prebid] | ||
| enabled = true | ||
| server_url = "https://test-prebid.com/openrtb2/auction" | ||
|
|
@@ -151,7 +162,7 @@ fn create_test_settings() -> Settings { | |
| providers = ["prebid"] | ||
| timeout_ms = 2000 | ||
| "#, | ||
| ) | ||
| )) | ||
| .expect("should parse adapter route test settings"); | ||
|
|
||
| assert_eq!( | ||
|
|
@@ -162,6 +173,21 @@ fn create_test_settings() -> Settings { | |
| settings | ||
| } | ||
|
|
||
| fn route_with_settings(settings: &Settings, req: Request) -> Option<Response> { | ||
| let orchestrator = build_orchestrator(settings).expect("should build auction orchestrator"); | ||
| let integration_registry = | ||
| IntegrationRegistry::new(settings).expect("should create integration registry"); | ||
| let runtime_services = test_runtime_services(&req); | ||
|
|
||
| futures::executor::block_on(route_request( | ||
| settings, | ||
| &orchestrator, | ||
| &integration_registry, | ||
| &runtime_services, | ||
| req, | ||
| )) | ||
| } | ||
|
|
||
| fn test_runtime_services(req: &Request) -> RuntimeServices { | ||
| RuntimeServices::builder() | ||
| .config_store(Arc::new(StubJwksConfigStore)) | ||
|
|
@@ -179,73 +205,137 @@ fn test_runtime_services(req: &Request) -> RuntimeServices { | |
| } | ||
|
|
||
| #[test] | ||
| fn configured_missing_consent_store_only_breaks_consent_routes() { | ||
| fn static_tsjs_route_serves_unified_bundle() { | ||
| let settings = create_test_settings(); | ||
| let orchestrator = build_orchestrator(&settings).expect("should build auction orchestrator"); | ||
| let integration_registry = | ||
| IntegrationRegistry::new(&settings).expect("should create integration registry"); | ||
| let req = Request::get("https://test.com/static/tsjs=tsjs-unified.min.js"); | ||
|
|
||
| let mut resp = route_with_settings(&settings, req).expect("should route static tsjs request"); | ||
|
|
||
| let discovery_req = Request::get("https://test.com/.well-known/trusted-server.json"); | ||
| let discovery_services = test_runtime_services(&discovery_req); | ||
| let discovery_resp = futures::executor::block_on(route_request( | ||
| &settings, | ||
| &orchestrator, | ||
| &integration_registry, | ||
| &discovery_services, | ||
| discovery_req, | ||
| )) | ||
| .expect("should route discovery request"); | ||
| assert_eq!( | ||
| discovery_resp.get_status(), | ||
| resp.get_status(), | ||
| StatusCode::OK, | ||
| "should keep discovery available when the consent store is unavailable" | ||
| "should serve the unified static bundle" | ||
| ); | ||
| assert_eq!( | ||
| resp.get_header_str(header::CONTENT_TYPE), | ||
| Some("application/javascript; charset=utf-8"), | ||
| "should serve the unified bundle as JavaScript" | ||
| ); | ||
| let body = resp.take_body_str(); | ||
|
|
||
| assert!( | ||
| body.contains("requestAds"), | ||
| "should serve unified bundle content with the core requestAds API" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn static_tsjs_route_returns_not_found_for_unknown_tsjs_bundle() { | ||
| let settings = create_test_settings(); | ||
| let req = Request::get("https://test.com/static/tsjs=tsjs-doesnotexist.min.js"); | ||
|
|
||
| let resp = route_with_settings(&settings, req).expect("should route static tsjs request"); | ||
|
|
||
| assert_eq!( | ||
| resp.get_status(), | ||
| StatusCode::NOT_FOUND, | ||
| "should let the static tsjs branch own unknown bundle paths" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn discovery_route_is_public() { | ||
| let settings = create_test_settings(); | ||
| let req = Request::get("https://test.com/.well-known/trusted-server.json"); | ||
|
|
||
| let resp = route_with_settings(&settings, req).expect("should route discovery request"); | ||
|
|
||
| let admin_req = Request::post("https://test.com/admin/keys/rotate"); | ||
| let admin_services = test_runtime_services(&admin_req); | ||
| let admin_resp = futures::executor::block_on(route_request( | ||
| &settings, | ||
| &orchestrator, | ||
| &integration_registry, | ||
| &admin_services, | ||
| admin_req, | ||
| )) | ||
| .expect("should route admin request"); | ||
| assert_eq!( | ||
| admin_resp.get_status(), | ||
| resp.get_status(), | ||
| StatusCode::OK, | ||
| "should keep discovery available without authentication" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn admin_route_rejects_unauthenticated_request() { | ||
| let settings = create_test_settings(); | ||
| let req = Request::post("https://test.com/admin/keys/rotate"); | ||
|
|
||
| let resp = route_with_settings(&settings, req).expect("should route admin request"); | ||
|
|
||
| assert_eq!( | ||
| resp.get_status(), | ||
| StatusCode::UNAUTHORIZED, | ||
| "should keep admin auth behavior unchanged when the consent store is unavailable" | ||
| "should reject unauthenticated admin requests before handler dispatch" | ||
| ); | ||
| assert!( | ||
| resp.get_header_str(header::WWW_AUTHENTICATE).is_some(), | ||
| "should advertise the Basic auth challenge" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn auction_route_dispatches_to_auction_handler() { | ||
| let settings = create_test_settings_without_consent_store(); | ||
|
ChristianPavilonis marked this conversation as resolved.
|
||
| let req = Request::post("https://test.com/auction").with_body( | ||
| r#"{"adUnits":[{"code":"slot","mediaTypes":{"banner":{"sizes":[[300]]}},"bids":[]}],"config":null}"#, | ||
| ); | ||
|
|
||
| let mut resp = route_with_settings(&settings, req).expect("should route auction request"); | ||
|
|
||
| let auction_req = Request::post("https://test.com/auction").with_body(r#"{"adUnits":[]}"#); | ||
| let auction_services = test_runtime_services(&auction_req); | ||
| let auction_resp = futures::executor::block_on(route_request( | ||
| &settings, | ||
| &orchestrator, | ||
| &integration_registry, | ||
| &auction_services, | ||
| auction_req, | ||
| )) | ||
| .expect("should return an error response for auction requests"); | ||
| assert_eq!( | ||
| auction_resp.get_status(), | ||
| resp.get_status(), | ||
| StatusCode::BAD_REQUEST, | ||
| "should reach the auction handler and reject invalid banner sizes" | ||
| ); | ||
| assert!( | ||
| resp.take_body_str().contains("Invalid banner size"), | ||
| "should return the auction validation error" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn auction_route_enforces_consent_store_when_configured() { | ||
| let settings = create_test_settings(); | ||
| let req = Request::post("https://test.com/auction").with_body(r#"{"adUnits":[]}"#); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏ nitpick — Inert request body reads as if it tests empty ad units The // Body is irrelevant: the consent gate (runtime_services_for_consent_route)
// fails before the auction handler parses the request.
let req = Request::post("https://test.com/auction").with_body(r#"{"adUnits":[]}"#); |
||
|
|
||
| let resp = route_with_settings(&settings, req).expect("should route auction request"); | ||
|
|
||
| assert_eq!( | ||
|
ChristianPavilonis marked this conversation as resolved.
|
||
| resp.get_status(), | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| "should fail auction requests when consent persistence is configured but unavailable" | ||
| "should fail auction when consent persistence is configured but unavailable" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unknown_route_falls_back_to_publisher_proxy_path() { | ||
| let settings = create_test_settings_with(None, "https://publisher-origin.invalid"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 note — Expected stderr noise, no action needed This test intentionally prints |
||
| let req = Request::get("https://test.com/articles/example"); | ||
|
|
||
| let resp = route_with_settings(&settings, req).expect("should route publisher fallback"); | ||
|
ChristianPavilonis marked this conversation as resolved.
|
||
|
|
||
| // The .invalid origin intentionally drives the publisher proxy req.send() failure; | ||
| // no other 502-mapping branch should run for /articles/example in this test. | ||
| assert_eq!( | ||
| resp.get_status(), | ||
| StatusCode::BAD_GATEWAY, | ||
| "should reach publisher proxy fallback and fail as an origin proxy error" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn publisher_fallback_uses_consent_dependent_services() { | ||
| let settings = create_test_settings(); | ||
| let req = Request::get("https://test.com/articles/example"); | ||
|
|
||
| let resp = route_with_settings(&settings, req) | ||
| .expect("should return an error response for publisher fallback"); | ||
|
|
||
| let publisher_req = Request::get("https://test.com/articles/example"); | ||
| let publisher_services = test_runtime_services(&publisher_req); | ||
| let publisher_resp = futures::executor::block_on(route_request( | ||
| &settings, | ||
| &orchestrator, | ||
| &integration_registry, | ||
| &publisher_services, | ||
| publisher_req, | ||
| )) | ||
| .expect("should return an error response for publisher fallback"); | ||
| assert_eq!( | ||
| publisher_resp.get_status(), | ||
| resp.get_status(), | ||
| StatusCode::SERVICE_UNAVAILABLE, | ||
| "should scope consent store failures to the consent-dependent routes" | ||
| "should fail publisher fallback when consent persistence is configured but unavailable" | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.