use super::TrustTask;
use crate::server::error::AppError;
pub fn parse_message_type(msg_type: &str) -> Result<TrustTask, AppError> {
if msg_type.is_empty() {
return Err(AppError::TrustTaskMissing);
}
TrustTask::new(msg_type)
}
pub fn assert_matches(actual: &TrustTask, expected: &TrustTask) -> Result<(), AppError> {
if actual.as_str() == expected.as_str() {
return Ok(());
}
Err(AppError::TrustTaskMismatch {
expected: expected.as_str().to_string(),
received: Some(actual.as_str().to_string()),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn task(url: &str) -> TrustTask {
TrustTask::new(url).expect("test URL must validate")
}
#[test]
fn parse_empty_yields_missing() {
let err = parse_message_type("").expect_err("empty must reject");
assert!(matches!(err, AppError::TrustTaskMissing));
}
#[test]
fn parse_non_https_yields_malformed() {
let err = parse_message_type("urn:example:not-https").expect_err("non-https must reject");
assert!(matches!(err, AppError::TrustTaskMalformed(_)));
}
#[test]
fn parse_well_formed_url_succeeds() {
let t = parse_message_type("https://trusttasks.org/did-hosting/did/request/1.0")
.expect("well-formed URL must parse");
assert_eq!(
t.as_str(),
"https://trusttasks.org/did-hosting/did/request/1.0"
);
}
#[test]
fn parse_accepts_legacy_msg_url_too() {
let t = parse_message_type("https://affinidi.com/webvh/1.0/did/request")
.expect("legacy MSG_* URL must also parse");
assert_eq!(t.as_str(), "https://affinidi.com/webvh/1.0/did/request");
}
#[test]
fn assert_matches_succeeds_on_exact_match() {
let a = task("https://trusttasks.org/did-hosting/did/request/1.0");
let b = task("https://trusttasks.org/did-hosting/did/request/1.0");
assert!(assert_matches(&a, &b).is_ok());
}
#[test]
fn assert_matches_byte_strict_on_version() {
let actual = task("https://trusttasks.org/did-hosting/did/request/1.1");
let expected = task("https://trusttasks.org/did-hosting/did/request/1.0");
let err = assert_matches(&actual, &expected).expect_err("version-skew must reject");
match err {
AppError::TrustTaskMismatch { expected, received } => {
assert_eq!(
expected,
"https://trusttasks.org/did-hosting/did/request/1.0"
);
assert_eq!(
received.as_deref(),
Some("https://trusttasks.org/did-hosting/did/request/1.1")
);
}
other => panic!("expected TrustTaskMismatch, got {other:?}"),
}
}
#[test]
fn assert_matches_byte_strict_on_path() {
let actual = task("https://trusttasks.org/did-hosting/did/publish/1.0");
let expected = task("https://trusttasks.org/did-hosting/did/request/1.0");
assert!(assert_matches(&actual, &expected).is_err());
}
}