refactor: consolidate nvim crates, extract server library, and update workspace dependencies
This commit is contained in:
1 parent
a083719cf1
commit
533adfd41b
53 files changed
+5967
-1230
No files matched your search
+59
-1
@@ -29,7 +29,9 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
|
||||
}
|
||||
}
|
||||
|
||||
if length == 0 {
|
||||
const MAX_MESSAGE_BYTES: usize = 50 * 1024 * 1024; // 50MB safety cap
|
||||
|
||||
if length == 0 || length > MAX_MESSAGE_BYTES {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -40,3 +42,59 @@ pub async fn read_mcp_message<R: tokio::io::AsyncRead + Unpin>(
|
||||
|
||||
String::from_utf8(buffer).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_ndjson_message() {
|
||||
let input = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n";
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, Some("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}".to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_content_length_message() {
|
||||
let payload = "{\"jsonrpc\":\"2.0\",\"id\":2}";
|
||||
let input = format!("Content-Length: {}\r\n\r\n{}", payload.len(), payload);
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, Some(payload.to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_mcp_message_eof() {
|
||||
let input = "";
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_mcp_message_zero_content_length() {
|
||||
let input = "Content-Length: 0\r\n\r\n";
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_mcp_message_exceeds_max_bytes() {
|
||||
let input = "Content-Length: 60000000\r\n\r\n";
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_mcp_message_read_exact_error() {
|
||||
let input = "Content-Length: 1000\r\n\r\nshort";
|
||||
let mut reader = BufReader::new(Cursor::new(input));
|
||||
let msg = read_mcp_message(&mut reader).await;
|
||||
assert_eq!(msg, None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user