use url::Url;
use super::error::InvalidInput;
const SEARCH: &str = "Patient/_search";
const MATCH: &str = "Patient/$match";
pub(super) const FORM: &str = "application/x-www-form-urlencoded";
pub(super) fn base(base: Url) -> Result<Url, InvalidInput> {
ihe_atna::fhir_base::under_base(base, "").ok_or(InvalidInput::Base)
}
pub(super) fn endpoint(base: Url) -> Result<Url, InvalidInput> {
ihe_atna::fhir_base::under_base(base, SEARCH).ok_or(InvalidInput::Base)
}
pub(super) fn match_endpoint(base: Url) -> Result<Url, InvalidInput> {
ihe_atna::fhir_base::under_base(base, MATCH).ok_or(InvalidInput::Base)
}
#[cfg(test)]
mod tests {
use url::Url;
use super::{base, endpoint};
use crate::pdqm::error::InvalidInput;
#[test]
fn the_endpoint_is_the_type_level_search_under_the_base() {
for given in [
"https://pdq.example.org/fhir",
"https://pdq.example.org/fhir/",
] {
let url = endpoint(Url::parse(given).expect("a URL")).expect("an endpoint");
assert_eq!(
url.as_str(),
"https://pdq.example.org/fhir/Patient/_search",
"{given}"
);
let url = base(Url::parse(given).expect("a URL")).expect("a base");
assert_eq!(url.as_str(), "https://pdq.example.org/fhir/", "{given}");
}
}
#[test]
fn a_base_with_a_query_or_another_scheme_is_refused() {
for base in [
"https://pdq.example.org/fhir?x=1",
"https://pdq.example.org/fhir#top",
"ftp://pdq.example.org/fhir",
"urn:oid:2.999.1",
] {
assert_eq!(
endpoint(Url::parse(base).expect("a URL")).err(),
Some(InvalidInput::Base),
"{base} is no FHIR base"
);
}
}
}