|
| 1 | +import { describe, it, expect, afterEach } from '@jest/globals'; |
| 2 | +import http from 'node:http'; |
| 3 | +import { HttpStreamTransport } from '../../src/transports/http/server.js'; |
| 4 | +import { SSEServerTransport } from '../../src/transports/sse/server.js'; |
| 5 | + |
| 6 | +function getPort(): number { |
| 7 | + return 10000 + Math.floor(Math.random() * 50000); |
| 8 | +} |
| 9 | + |
| 10 | +function httpGet(port: number, path: string): Promise<{ status: number; body: string }> { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + const req = http.get(`http://127.0.0.1:${port}${path}`, (res) => { |
| 13 | + let body = ''; |
| 14 | + res.on('data', (chunk) => (body += chunk)); |
| 15 | + res.on('end', () => resolve({ status: res.statusCode!, body })); |
| 16 | + }); |
| 17 | + req.on('error', reject); |
| 18 | + req.setTimeout(2000, () => { |
| 19 | + req.destroy(new Error('timeout')); |
| 20 | + }); |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +describe('Health endpoint – HttpStreamTransport', () => { |
| 25 | + let transport: HttpStreamTransport; |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + if (transport?.isRunning()) await transport.close(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('serves /health by default with { ok: true }', async () => { |
| 32 | + const port = getPort(); |
| 33 | + transport = new HttpStreamTransport({ port }); |
| 34 | + await transport.start(); |
| 35 | + |
| 36 | + const res = await httpGet(port, '/health'); |
| 37 | + expect(res.status).toBe(200); |
| 38 | + expect(JSON.parse(res.body)).toEqual({ ok: true }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('serves a custom path when configured', async () => { |
| 42 | + const port = getPort(); |
| 43 | + transport = new HttpStreamTransport({ |
| 44 | + port, |
| 45 | + health: { path: '/healthz' }, |
| 46 | + }); |
| 47 | + await transport.start(); |
| 48 | + |
| 49 | + const res = await httpGet(port, '/healthz'); |
| 50 | + expect(res.status).toBe(200); |
| 51 | + expect(JSON.parse(res.body)).toEqual({ ok: true }); |
| 52 | + |
| 53 | + // Default /health should 404 |
| 54 | + const notFound = await httpGet(port, '/health'); |
| 55 | + expect(notFound.status).toBe(404); |
| 56 | + }); |
| 57 | + |
| 58 | + it('serves a custom response body when configured', async () => { |
| 59 | + const port = getPort(); |
| 60 | + const customResponse = { success: true, data: 'ok' }; |
| 61 | + transport = new HttpStreamTransport({ |
| 62 | + port, |
| 63 | + health: { path: '/healthz', response: customResponse }, |
| 64 | + }); |
| 65 | + await transport.start(); |
| 66 | + |
| 67 | + const res = await httpGet(port, '/healthz'); |
| 68 | + expect(res.status).toBe(200); |
| 69 | + expect(JSON.parse(res.body)).toEqual(customResponse); |
| 70 | + }); |
| 71 | + |
| 72 | + it('disables health endpoint when enabled is false', async () => { |
| 73 | + const port = getPort(); |
| 74 | + transport = new HttpStreamTransport({ |
| 75 | + port, |
| 76 | + health: { enabled: false }, |
| 77 | + }); |
| 78 | + await transport.start(); |
| 79 | + |
| 80 | + const res = await httpGet(port, '/health'); |
| 81 | + expect(res.status).toBe(404); |
| 82 | + }); |
| 83 | + |
| 84 | + it('does not respond to POST on health path', async () => { |
| 85 | + const port = getPort(); |
| 86 | + transport = new HttpStreamTransport({ port }); |
| 87 | + await transport.start(); |
| 88 | + |
| 89 | + const res = await new Promise<{ status: number }>((resolve, reject) => { |
| 90 | + const req = http.request( |
| 91 | + { hostname: '127.0.0.1', port, path: '/health', method: 'POST' }, |
| 92 | + (res) => resolve({ status: res.statusCode! }), |
| 93 | + ); |
| 94 | + req.on('error', reject); |
| 95 | + req.end(); |
| 96 | + }); |
| 97 | + |
| 98 | + // POST to /health should not match the health route |
| 99 | + expect(res.status).not.toBe(200); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('Health endpoint – SSEServerTransport', () => { |
| 104 | + let transport: SSEServerTransport; |
| 105 | + |
| 106 | + afterEach(async () => { |
| 107 | + if (transport?.isRunning()) await transport.close(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('serves /health by default with { ok: true }', async () => { |
| 111 | + const port = getPort(); |
| 112 | + transport = new SSEServerTransport({ port }); |
| 113 | + await transport.start(); |
| 114 | + |
| 115 | + const res = await httpGet(port, '/health'); |
| 116 | + expect(res.status).toBe(200); |
| 117 | + expect(JSON.parse(res.body)).toEqual({ ok: true }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('serves a custom path when configured', async () => { |
| 121 | + const port = getPort(); |
| 122 | + transport = new SSEServerTransport({ |
| 123 | + port, |
| 124 | + health: { path: '/healthz' }, |
| 125 | + }); |
| 126 | + await transport.start(); |
| 127 | + |
| 128 | + const res = await httpGet(port, '/healthz'); |
| 129 | + expect(res.status).toBe(200); |
| 130 | + expect(JSON.parse(res.body)).toEqual({ ok: true }); |
| 131 | + |
| 132 | + const notFound = await httpGet(port, '/health'); |
| 133 | + expect(notFound.status).toBe(404); |
| 134 | + }); |
| 135 | + |
| 136 | + it('serves a custom response body when configured', async () => { |
| 137 | + const port = getPort(); |
| 138 | + const customResponse = { success: true, data: 'ok' }; |
| 139 | + transport = new SSEServerTransport({ |
| 140 | + port, |
| 141 | + health: { path: '/healthz', response: customResponse }, |
| 142 | + }); |
| 143 | + await transport.start(); |
| 144 | + |
| 145 | + const res = await httpGet(port, '/healthz'); |
| 146 | + expect(res.status).toBe(200); |
| 147 | + expect(JSON.parse(res.body)).toEqual(customResponse); |
| 148 | + }); |
| 149 | + |
| 150 | + it('disables health endpoint when enabled is false', async () => { |
| 151 | + const port = getPort(); |
| 152 | + transport = new SSEServerTransport({ |
| 153 | + port, |
| 154 | + health: { enabled: false }, |
| 155 | + }); |
| 156 | + await transport.start(); |
| 157 | + |
| 158 | + const res = await httpGet(port, '/health'); |
| 159 | + expect(res.status).toBe(404); |
| 160 | + }); |
| 161 | +}); |
0 commit comments