|
1 | 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
2 | | -import type { Server } from "node:http"; |
| 2 | +import { request, type Server } from "node:http"; |
| 3 | +import { gzipSync } from "node:zlib"; |
3 | 4 | import type { AddressInfo } from "node:net"; |
4 | 5 | import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client"; |
5 | 6 | import { createHttpApp } from "../src/server/http-app.js"; |
@@ -65,6 +66,75 @@ function post(options: { |
65 | 66 | } |
66 | 67 |
|
67 | 68 | describe("Streamable HTTP protocol boundary", () => { |
| 69 | + test("rejects unsupported media types before waiting for the request body", async () => { |
| 70 | + for (const contentType of [undefined, "text/plain", "text/plain; a=application/json"]) { |
| 71 | + for (const framing of ["length", "chunked"]) { |
| 72 | + const response = await new Promise<{ status: number; type?: string; body: string }>((resolve, reject) => { |
| 73 | + const req = request(endpoint, { |
| 74 | + method: "POST", |
| 75 | + headers: { |
| 76 | + "Accept": "application/json, text/event-stream", |
| 77 | + "MCP-Protocol-Version": MODERN_PROTOCOL_VERSION, |
| 78 | + "Mcp-Method": "server/discover", |
| 79 | + ...(contentType ? { "Content-Type": contentType } : {}), |
| 80 | + ...(framing === "length" ? { "Content-Length": 2 * 1024 * 1024 } : { "Transfer-Encoding": "chunked" }) |
| 81 | + } |
| 82 | + }); |
| 83 | + const deadline = setTimeout(() => { |
| 84 | + req.destroy(); |
| 85 | + reject(new Error("Server waited for an unsupported request body")); |
| 86 | + }, 2000); |
| 87 | + req.on("error", error => { |
| 88 | + clearTimeout(deadline); |
| 89 | + reject(error); |
| 90 | + }); |
| 91 | + req.on("response", res => { |
| 92 | + let body = ""; |
| 93 | + res.setEncoding("utf8"); |
| 94 | + res.on("data", chunk => { body += chunk; }); |
| 95 | + res.on("end", () => { |
| 96 | + clearTimeout(deadline); |
| 97 | + resolve({ status: res.statusCode!, type: res.headers["content-type"], body }); |
| 98 | + req.destroy(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + // Deliberately leave the upload unfinished: status-only tests miss buffering. |
| 102 | + req.write("{"); |
| 103 | + }); |
| 104 | + expect(response.status).toBe(415); |
| 105 | + expect(response.type).toContain("application/json"); |
| 106 | + expect(JSON.parse(response.body).error.code).toBe(-32600); |
| 107 | + } |
| 108 | + } |
| 109 | + }, 15000); |
| 110 | + |
| 111 | + test("limits chunked and decompressed JSON bodies", async () => { |
| 112 | + const body = JSON.stringify({ padding: "x".repeat(1100 * 1024) }); |
| 113 | + const chunked = await new Promise<{ status: number; body: string }>((resolve, reject) => { |
| 114 | + const req = request(endpoint, { |
| 115 | + method: "POST", |
| 116 | + headers: { "Content-Type": "application/json", "Transfer-Encoding": "chunked" } |
| 117 | + }, res => { |
| 118 | + let responseBody = ""; |
| 119 | + res.setEncoding("utf8"); |
| 120 | + res.on("data", chunk => { responseBody += chunk; }); |
| 121 | + res.on("end", () => resolve({ status: res.statusCode!, body: responseBody })); |
| 122 | + }); |
| 123 | + req.on("error", reject); |
| 124 | + req.end(body); |
| 125 | + }); |
| 126 | + expect(chunked.status).toBe(413); |
| 127 | + expect(JSON.parse(chunked.body).error.code).toBe(-32600); |
| 128 | + |
| 129 | + const compressed = await fetch(endpoint, { |
| 130 | + method: "POST", |
| 131 | + headers: { "Content-Type": "application/json", "Content-Encoding": "gzip" }, |
| 132 | + body: gzipSync(body) |
| 133 | + }); |
| 134 | + expect(compressed.status).toBe(413); |
| 135 | + expect((await compressed.json() as { error: { code: number } }).error.code).toBe(-32600); |
| 136 | + }); |
| 137 | + |
68 | 138 | test("serves an SDK client without protocol sessions", async () => { |
69 | 139 | const client = new Client({ name: "http-test", version: "1.0.0" }, { |
70 | 140 | versionNegotiation: { mode: { pin: MODERN_PROTOCOL_VERSION } } |
@@ -92,6 +162,22 @@ describe("Streamable HTTP protocol boundary", () => { |
92 | 162 | })); |
93 | 163 | }); |
94 | 164 |
|
| 165 | + test("returns JSON-RPC errors for empty bodies and unsupported encodings", async () => { |
| 166 | + const empty = await post({ rawBody: "" }); |
| 167 | + expect(empty.status).toBe(400); |
| 168 | + expect((await empty.json() as { error: { code: number } }).error.code).toBe(-32600); |
| 169 | + |
| 170 | + for (const headers of [ |
| 171 | + { "Content-Type": "application/json; charset=iso-8859-1" }, |
| 172 | + { "Content-Encoding": "unsupported" } |
| 173 | + ] as Record<string, string>[]) { |
| 174 | + const response = await post({ headers }); |
| 175 | + expect(response.status).toBe(415); |
| 176 | + expect(response.headers.get("Content-Type")).toContain("application/json"); |
| 177 | + expect((await response.json() as { error: { code: number } }).error.code).toBe(-32600); |
| 178 | + } |
| 179 | + }); |
| 180 | + |
95 | 181 | test("rejects non-JSON media types and accepts JSON parameters", async () => { |
96 | 182 | for (const type of ["text/plain", "text/plain; a=application/json"]) { |
97 | 183 | const response = await post({ headers: { "Content-Type": type } }); |
|
0 commit comments