Skip to content

Commit

Permalink
bug: Add explicit endpoint_url setting (#270)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrconlin committed Mar 24, 2021
1 parent 8d732b7 commit 5649d96
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion autoendpoint/src/extractors/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
34 changes: 32 additions & 2 deletions autoendpoint/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 5649d96

Please sign in to comment.