#![cfg(test)]
use proptest::prelude::*;
use serde_json::json;
use crate::cache::inspect_request;
use crate::layer::testing::terminal_response;
use mcp_usage_core::{Call, Method};
fn hostile_stream() -> impl Strategy<Value = Vec<u8>> {
prop_oneof![
any::<Vec<u8>>(),
prop::collection::vec(
prop_oneof![
Just(b"data: ".to_vec()),
Just(b"data:".to_vec()),
Just(b"\n".to_vec()),
Just(b"\r\n".to_vec()),
Just(b": keepalive".to_vec()),
Just(br#"{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete"}}"#.to_vec()),
Just(br#"{"error":{"code":-32020}}"#.to_vec()),
Just(b"{".to_vec()),
Just(b"\0".to_vec()),
".{0,16}".prop_map(String::into_bytes),
],
0..24,
)
.prop_map(|parts| parts.concat()),
]
}
fn content_type() -> impl Strategy<Value = String> {
prop_oneof![
Just("application/json".to_owned()),
Just("text/event-stream".to_owned()),
Just("text/event-stream; charset=utf-8".to_owned()),
Just("Application/JSON".to_owned()),
Just("application/jsonp".to_owned()),
Just(String::new()),
".{0,24}",
]
}
proptest! {
#[test]
fn reading_a_terminal_response_never_panics(
media in content_type(),
bytes in hostile_stream(),
) {
let terminal = terminal_response(&media, &bytes);
if let Some(value) = terminal {
prop_assert!(
value.get("result").is_some() || value.get("error").is_some(),
"a terminal response must carry a result or an error: {value}"
);
}
}
#[test]
fn unrecognized_media_types_are_never_terminal(bytes in hostile_stream()) {
for media in ["application/jsonp", "text/plain", "", "application/octet-stream"] {
prop_assert!(terminal_response(media, &bytes).is_none());
}
}
#[test]
fn inspecting_a_request_never_panics(
bytes in any::<Vec<u8>>(),
named in any::<bool>(),
) {
let call = Call::new(
Method::ToolsList,
named.then(|| "tool".to_owned()),
);
let metadata = inspect_request(&call, &bytes);
if metadata.is_continuation {
prop_assert!(metadata.cache_key.is_none());
}
}
#[test]
fn cache_keys_require_header_and_body_agreement(
header_name in "[a-z]{1,8}",
body_name in "[a-z]{1,8}",
) {
let call = Call::new(Method::ResourcesRead, Some(header_name.clone()));
let body = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "resources/read",
"params": { "uri": body_name },
});
let metadata = inspect_request(&call, body.to_string().as_bytes());
prop_assert_eq!(metadata.cache_key.is_some(), header_name == body_name);
}
}