|
| 1 | +/** |
| 2 | + * Unit tests for OPTIONS preflight handling and notification responses |
| 3 | + * |
| 4 | + * Tests that: |
| 5 | + * - OPTIONS requests receive 204 No Content with CORS headers |
| 6 | + * - JSON-RPC notifications receive HTTP 202 Accepted response |
| 7 | + */ |
| 8 | + |
| 9 | +#include <string> |
| 10 | + |
| 11 | +#include <gtest/gtest.h> |
| 12 | + |
| 13 | +namespace mcp { |
| 14 | +namespace filter { |
| 15 | +namespace { |
| 16 | + |
| 17 | +class OptionsNotificationTest : public ::testing::Test {}; |
| 18 | + |
| 19 | +// Test OPTIONS preflight response format |
| 20 | +TEST_F(OptionsNotificationTest, OptionsPreflightResponseFormat) { |
| 21 | + // Build OPTIONS preflight response like http_sse_filter_chain_factory.cc does |
| 22 | + std::ostringstream response; |
| 23 | + response << "HTTP/1.1 204 No Content\r\n"; |
| 24 | + response << "Access-Control-Allow-Origin: *\r\n"; |
| 25 | + response << "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"; |
| 26 | + response << "Access-Control-Allow-Headers: Content-Type, Authorization, " |
| 27 | + "Accept, Mcp-Session-Id, Mcp-Protocol-Version\r\n"; |
| 28 | + response << "Access-Control-Max-Age: 86400\r\n"; |
| 29 | + response << "Content-Length: 0\r\n"; |
| 30 | + response << "\r\n"; |
| 31 | + |
| 32 | + std::string preflight_response = response.str(); |
| 33 | + |
| 34 | + // Verify 204 No Content for preflight |
| 35 | + EXPECT_TRUE(preflight_response.find("HTTP/1.1 204 No Content") != |
| 36 | + std::string::npos) |
| 37 | + << "Preflight should return 204 No Content"; |
| 38 | + |
| 39 | + // Verify all CORS headers present |
| 40 | + EXPECT_TRUE(preflight_response.find("Access-Control-Allow-Origin: *") != |
| 41 | + std::string::npos); |
| 42 | + EXPECT_TRUE(preflight_response.find("Access-Control-Allow-Methods:") != |
| 43 | + std::string::npos); |
| 44 | + EXPECT_TRUE(preflight_response.find("Access-Control-Allow-Headers:") != |
| 45 | + std::string::npos); |
| 46 | + |
| 47 | + // Verify max-age for caching preflight results |
| 48 | + EXPECT_TRUE(preflight_response.find("Access-Control-Max-Age: 86400") != |
| 49 | + std::string::npos) |
| 50 | + << "Should cache preflight for 24 hours"; |
| 51 | + |
| 52 | + // Verify empty body |
| 53 | + EXPECT_TRUE(preflight_response.find("Content-Length: 0") != std::string::npos) |
| 54 | + << "Preflight response should have empty body"; |
| 55 | +} |
| 56 | + |
| 57 | +// Test OPTIONS response includes required MCP headers |
| 58 | +TEST_F(OptionsNotificationTest, OptionsAllowsMcpHeaders) { |
| 59 | + std::string allowed_headers = |
| 60 | + "Content-Type, Authorization, Accept, Mcp-Session-Id, Mcp-Protocol-Version"; |
| 61 | + |
| 62 | + // MCP Inspector uses these headers |
| 63 | + EXPECT_TRUE(allowed_headers.find("Mcp-Session-Id") != std::string::npos) |
| 64 | + << "Should allow Mcp-Session-Id header"; |
| 65 | + EXPECT_TRUE(allowed_headers.find("Mcp-Protocol-Version") != std::string::npos) |
| 66 | + << "Should allow Mcp-Protocol-Version header"; |
| 67 | + EXPECT_TRUE(allowed_headers.find("Authorization") != std::string::npos) |
| 68 | + << "Should allow Authorization header for OAuth"; |
| 69 | +} |
| 70 | + |
| 71 | +// Test HTTP 202 notification response format |
| 72 | +TEST_F(OptionsNotificationTest, NotificationResponseFormat) { |
| 73 | + // Build notification response like http_sse_filter_chain_factory.cc does |
| 74 | + std::string http_response = |
| 75 | + "HTTP/1.1 202 Accepted\r\n" |
| 76 | + "Content-Length: 0\r\n" |
| 77 | + "Access-Control-Allow-Origin: *\r\n" |
| 78 | + "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n" |
| 79 | + "Access-Control-Allow-Headers: Content-Type, Authorization, Accept, " |
| 80 | + "Mcp-Session-Id, Mcp-Protocol-Version\r\n" |
| 81 | + "Connection: keep-alive\r\n" |
| 82 | + "\r\n"; |
| 83 | + |
| 84 | + // Verify 202 Accepted for notifications |
| 85 | + EXPECT_TRUE(http_response.find("HTTP/1.1 202 Accepted") != std::string::npos) |
| 86 | + << "Notification response should return 202 Accepted"; |
| 87 | + |
| 88 | + // Verify CORS headers present |
| 89 | + EXPECT_TRUE(http_response.find("Access-Control-Allow-Origin: *") != |
| 90 | + std::string::npos); |
| 91 | + |
| 92 | + // Verify empty body |
| 93 | + EXPECT_TRUE(http_response.find("Content-Length: 0") != std::string::npos) |
| 94 | + << "Notification response should have empty body"; |
| 95 | +} |
| 96 | + |
| 97 | +// Test that notifications don't return JSON-RPC response |
| 98 | +TEST_F(OptionsNotificationTest, NotificationNoJsonRpcResponse) { |
| 99 | + // JSON-RPC notifications should NOT have a JSON body |
| 100 | + // Only HTTP response headers with 202 status |
| 101 | + |
| 102 | + std::string notification_response = |
| 103 | + "HTTP/1.1 202 Accepted\r\n" |
| 104 | + "Content-Length: 0\r\n" |
| 105 | + "Connection: keep-alive\r\n" |
| 106 | + "\r\n"; |
| 107 | + |
| 108 | + // Should NOT contain JSON-RPC fields |
| 109 | + EXPECT_TRUE(notification_response.find("\"jsonrpc\"") == std::string::npos) |
| 110 | + << "Notification response should not contain JSON-RPC body"; |
| 111 | + EXPECT_TRUE(notification_response.find("\"result\"") == std::string::npos) |
| 112 | + << "Notification response should not contain result field"; |
| 113 | + EXPECT_TRUE(notification_response.find("\"id\"") == std::string::npos) |
| 114 | + << "Notification response should not contain id field"; |
| 115 | +} |
| 116 | + |
| 117 | +// Test OPTIONS for common MCP paths |
| 118 | +TEST_F(OptionsNotificationTest, OptionsRegisteredPaths) { |
| 119 | + // These paths should all handle OPTIONS requests |
| 120 | + std::vector<std::string> mcp_paths = { |
| 121 | + "/mcp", |
| 122 | + "/mcp/events", |
| 123 | + "/rpc", |
| 124 | + "/health", |
| 125 | + "/info" |
| 126 | + }; |
| 127 | + |
| 128 | + for (const auto& path : mcp_paths) { |
| 129 | + // Each path should be registered for OPTIONS |
| 130 | + EXPECT_FALSE(path.empty()) << "Path should not be empty"; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace |
| 135 | +} // namespace filter |
| 136 | +} // namespace mcp |
0 commit comments