|
| 1 | +//! Integration test for HTTP server with MCP protocol |
| 2 | +#![cfg(feature = "server-http")] |
| 3 | + |
| 4 | +use codegraph_mcp::{ |
| 5 | + http_config::HttpServerConfig, |
| 6 | + http_server::start_http_server, |
| 7 | + official_server::CodeGraphMCPServer, |
| 8 | +}; |
| 9 | +use serde_json::json; |
| 10 | +use tokio::time::{sleep, Duration}; |
| 11 | + |
| 12 | +#[tokio::test] |
| 13 | +async fn test_http_server_health_check() { |
| 14 | + // Start server in background |
| 15 | + let server = CodeGraphMCPServer::new(); |
| 16 | + let config = HttpServerConfig { |
| 17 | + host: "127.0.0.1".to_string(), |
| 18 | + port: 13000, // Use non-standard port for testing |
| 19 | + keep_alive_seconds: 5, |
| 20 | + }; |
| 21 | + |
| 22 | + let server_handle = tokio::spawn(async move { |
| 23 | + let _ = start_http_server(server, config).await; |
| 24 | + }); |
| 25 | + |
| 26 | + // Wait for server to start |
| 27 | + sleep(Duration::from_millis(100)).await; |
| 28 | + |
| 29 | + // Test health endpoint |
| 30 | + let client = reqwest::Client::new(); |
| 31 | + let response = client |
| 32 | + .get("http://127.0.0.1:13000/health") |
| 33 | + .send() |
| 34 | + .await |
| 35 | + .expect("Failed to send health check request"); |
| 36 | + |
| 37 | + assert_eq!(response.status(), 200); |
| 38 | + assert_eq!(response.text().await.unwrap(), "OK"); |
| 39 | + |
| 40 | + // Cleanup |
| 41 | + server_handle.abort(); |
| 42 | +} |
| 43 | + |
| 44 | +#[tokio::test] |
| 45 | +async fn test_http_mcp_initialize_request() { |
| 46 | + // Start server in background |
| 47 | + let server = CodeGraphMCPServer::new(); |
| 48 | + let config = HttpServerConfig { |
| 49 | + host: "127.0.0.1".to_string(), |
| 50 | + port: 13001, |
| 51 | + keep_alive_seconds: 5, |
| 52 | + }; |
| 53 | + |
| 54 | + let server_handle = tokio::spawn(async move { |
| 55 | + let _ = start_http_server(server, config).await; |
| 56 | + }); |
| 57 | + |
| 58 | + sleep(Duration::from_millis(100)).await; |
| 59 | + |
| 60 | + // Send initialize request |
| 61 | + let client = reqwest::Client::new(); |
| 62 | + let initialize_request = json!({ |
| 63 | + "jsonrpc": "2.0", |
| 64 | + "id": 1, |
| 65 | + "method": "initialize", |
| 66 | + "params": { |
| 67 | + "protocolVersion": "2025-06-18", |
| 68 | + "capabilities": {}, |
| 69 | + "clientInfo": { |
| 70 | + "name": "test-client", |
| 71 | + "version": "1.0.0" |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + let response = client |
| 77 | + .post("http://127.0.0.1:13001/mcp") |
| 78 | + .header("Content-Type", "application/json") |
| 79 | + .json(&initialize_request) |
| 80 | + .send() |
| 81 | + .await |
| 82 | + .expect("Failed to send initialize request"); |
| 83 | + |
| 84 | + // Should get SSE stream with session ID |
| 85 | + assert!(response.headers().contains_key("mcp-session-id")); |
| 86 | + assert_eq!(response.headers().get("content-type").unwrap(), "text/event-stream"); |
| 87 | + |
| 88 | + // Cleanup |
| 89 | + server_handle.abort(); |
| 90 | +} |
0 commit comments