|
| 1 | +import http from 'node:http'; |
1 | 2 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 3 | import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
3 | 4 | import { registerEchoTool, EchoSchema } from '../tools/echo.js'; |
@@ -1217,5 +1218,66 @@ describe('Tools', () => { |
1217 | 1218 | handler!({ name: 'test.gz', data: 'ftp://example.com/file.txt', outputType: 'resource' }) |
1218 | 1219 | ).rejects.toThrow('Unsupported URL protocol'); |
1219 | 1220 | }); |
| 1221 | + |
| 1222 | + it('should reject HTTP error responses instead of compressing the error page', async () => { |
| 1223 | + const mockServer = { |
| 1224 | + registerTool: vi.fn(), |
| 1225 | + registerResource: vi.fn(), |
| 1226 | + } as unknown as McpServer; |
| 1227 | + |
| 1228 | + let handler: Function | null = null; |
| 1229 | + (mockServer.registerTool as any).mockImplementation( |
| 1230 | + (name: string, config: any, h: Function) => { |
| 1231 | + handler = h; |
| 1232 | + } |
| 1233 | + ); |
| 1234 | + |
| 1235 | + registerGZipFileAsResourceTool(mockServer); |
| 1236 | + |
| 1237 | + const httpServer = http.createServer((req, res) => { |
| 1238 | + if (req.url === '/ok.txt') { |
| 1239 | + res.writeHead(200, { 'content-type': 'text/plain' }); |
| 1240 | + res.end('real file contents'); |
| 1241 | + return; |
| 1242 | + } |
| 1243 | + const status = req.url === '/boom.txt' ? 500 : 404; |
| 1244 | + res.writeHead(status, { 'content-type': 'text/plain' }); |
| 1245 | + res.end(`error body for ${status}`); |
| 1246 | + }); |
| 1247 | + await new Promise<void>((resolve) => httpServer.listen(0, '127.0.0.1', resolve)); |
| 1248 | + const address = httpServer.address(); |
| 1249 | + if (!address || typeof address === 'string') { |
| 1250 | + throw new Error('expected a TCP address'); |
| 1251 | + } |
| 1252 | + |
| 1253 | + try { |
| 1254 | + const ok = await handler!({ |
| 1255 | + name: 'ok.txt.gz', |
| 1256 | + data: `http://127.0.0.1:${address.port}/ok.txt`, |
| 1257 | + outputType: 'resource', |
| 1258 | + }); |
| 1259 | + expect(ok.content[0].type).toBe('resource'); |
| 1260 | + |
| 1261 | + await expect( |
| 1262 | + handler!({ |
| 1263 | + name: 'missing.txt.gz', |
| 1264 | + data: `http://127.0.0.1:${address.port}/missing.txt`, |
| 1265 | + outputType: 'resource', |
| 1266 | + }) |
| 1267 | + ).rejects.toThrow('status 404'); |
| 1268 | + |
| 1269 | + await expect( |
| 1270 | + handler!({ |
| 1271 | + name: 'boom.txt.gz', |
| 1272 | + data: `http://127.0.0.1:${address.port}/boom.txt`, |
| 1273 | + outputType: 'resource', |
| 1274 | + }) |
| 1275 | + ).rejects.toThrow('status 500'); |
| 1276 | + } finally { |
| 1277 | + await new Promise<void>((resolve, reject) => |
| 1278 | + httpServer.close((err) => (err ? reject(err) : resolve())) |
| 1279 | + ); |
| 1280 | + } |
| 1281 | + }); |
1220 | 1282 | }); |
1221 | 1283 | }); |
0 commit comments