use ureq::http::{HeaderMap, HeaderValue};
use super::headers;
type TestResult = Result<(), Box<dyn std::error::Error>>;
fn protocol(accept: &str) -> Result<HeaderMap, ureq::http::header::InvalidHeaderValue> {
let mut headers = HeaderMap::new();
headers.insert("content-type", HeaderValue::from_static("application/json"));
headers.insert("accept", HeaderValue::from_str(accept)?);
Ok(headers)
}
#[test]
fn accepted_media_quality_parameters_do_not_refuse_the_request() -> TestResult {
for value in [
"application/json;q=0.5, text/event-stream;q=1.000",
"*/*;q=0.1",
"application/json, text/event-stream",
] {
headers::protocol(&protocol(value)?)?;
}
for value in [
"application/json;q=0, text/event-stream",
"application/json;q=2, text/event-stream",
"application/json;q=0.1234, text/event-stream",
"application/json;q=1;q=1, text/event-stream",
"application/json;q=0, */*;q=1",
"application/json, text/event-stream;q=0, */*;q=1",
] {
assert_eq!(
headers::protocol(&protocol(value)?)
.err()
.map(|error| error.code),
Some("mcp_http_accept_invalid")
);
}
Ok(())
}
#[test]
fn duplicate_authorization_is_refused_before_sign_in() {
let mut values = HeaderMap::new();
values.append(
"authorization",
HeaderValue::from_static("Bearer fixture-token"),
);
values.append(
"authorization",
HeaderValue::from_static("Bearer another-token"),
);
assert_eq!(
headers::bearer(&values).err().map(|error| error.code),
Some("mcp_sign_in_required")
);
}
#[test]
fn duplicate_content_type_headers_cannot_pick_a_permitted_first_value() -> TestResult {
let mut values = protocol("application/json, text/event-stream")?;
values.append("content-type", HeaderValue::from_static("text/plain"));
assert_eq!(
headers::protocol(&values).err().map(|error| error.code),
Some("mcp_http_content_type_invalid")
);
Ok(())
}