From 5649d966e9eade5526efe240c52a84724d3a1020 Mon Sep 17 00:00:00 2001 From: JR Conlin Date: Wed, 24 Mar 2021 16:43:47 -0700 Subject: [PATCH] bug: Add explicit `endpoint_url` setting (#270) This will add an explicit setting to specify the endpoint URL Note that this will include `AUTOEND_ENDPOINT_URL` as a new setting, the URL must be a fully qualified string (e.g. `https://updates.push.services.mozilla.com/`) Closes #269 --- autoendpoint/src/extractors/subscription.rs | 2 +- autoendpoint/src/settings.rs | 34 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/autoendpoint/src/extractors/subscription.rs b/autoendpoint/src/extractors/subscription.rs index 9f58dfe51..37be3ead7 100644 --- a/autoendpoint/src/extractors/subscription.rs +++ b/autoendpoint/src/extractors/subscription.rs @@ -242,7 +242,7 @@ fn validate_vapid_jwt(vapid: &VapidHeaderWithKey, domain: &Url) -> ApiResult<()> }; if domain != &aud { - error!("Bad Aud: I am{:?}, asked for {:?} ", domain, aud); + error!("Bad Aud: I am <{:?}>, asked for <{:?}> ", domain, aud); return Err(VapidError::InvalidAudience.into()); } diff --git a/autoendpoint/src/settings.rs b/autoendpoint/src/settings.rs index a1d806a00..86bc8d5fe 100644 --- a/autoendpoint/src/settings.rs +++ b/autoendpoint/src/settings.rs @@ -17,6 +17,7 @@ pub struct Settings { pub scheme: String, pub host: String, pub port: u16, + pub endpoint_url: String, pub router_table_name: String, pub message_table_name: String, @@ -40,6 +41,7 @@ impl Default for Settings { Settings { scheme: "http".to_string(), host: "127.0.0.1".to_string(), + endpoint_url: "".to_string(), port: 8000, router_table_name: "router".to_string(), message_table_name: "message".to_string(), @@ -125,8 +127,12 @@ impl Settings { /// Get the URL for this endpoint server pub fn endpoint_url(&self) -> Url { - Url::parse(&format!("{}://{}:{}", self.scheme, self.host, self.port)) - .expect("Invalid endpoint URL") + let endpoint = if self.endpoint_url.is_empty() { + format!("{}://{}:{}", self.scheme, self.host, self.port) + } else { + self.endpoint_url.clone() + }; + Url::parse(&endpoint).expect("Invalid endpoint URL") } } @@ -158,4 +164,28 @@ mod tests { assert_eq!(result, success); Ok(()) } + + #[test] + fn test_endpoint_url() -> ApiResult<()> { + let example = "https://example.org/"; + let settings = Settings { + endpoint_url: example.to_owned(), + ..Default::default() + }; + + assert_eq!(settings.endpoint_url(), url::Url::parse(example).unwrap()); + let settings = Settings { + ..Default::default() + }; + + assert_eq!( + settings.endpoint_url(), + url::Url::parse(&format!( + "{}://{}:{}", + settings.scheme, settings.host, settings.port + )) + .unwrap() + ); + Ok(()) + } }