|
| 1 | +import * as http from 'node:http'; |
| 2 | +import { getIntrospectionQuery, buildSchema, introspectionFromSchema } from 'graphql'; |
| 3 | + |
| 4 | +import { fetchEndpointSchemaSDL } from '../src/fetch-endpoint-schema'; |
| 5 | + |
| 6 | +const TEST_SDL = ` |
| 7 | + type Query { |
| 8 | + hello: String |
| 9 | + version: Int |
| 10 | + } |
| 11 | +`; |
| 12 | + |
| 13 | +function createMockServer(handler: (req: http.IncomingMessage, res: http.ServerResponse) => void): Promise<{ server: http.Server; port: number }> { |
| 14 | + return new Promise((resolve) => { |
| 15 | + const server = http.createServer(handler); |
| 16 | + server.listen(0, '127.0.0.1', () => { |
| 17 | + const addr = server.address() as { port: number }; |
| 18 | + resolve({ server, port: addr.port }); |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +function introspectionHandler(_req: http.IncomingMessage, res: http.ServerResponse) { |
| 24 | + const schema = buildSchema(TEST_SDL); |
| 25 | + const introspection = introspectionFromSchema(schema); |
| 26 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 27 | + res.end(JSON.stringify({ data: introspection })); |
| 28 | +} |
| 29 | + |
| 30 | +describe('fetchEndpointSchemaSDL', () => { |
| 31 | + let server: http.Server; |
| 32 | + let port: number; |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + ({ server, port } = await createMockServer(introspectionHandler)); |
| 36 | + }); |
| 37 | + |
| 38 | + afterAll(() => { |
| 39 | + server.close(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('fetches and returns SDL from a live endpoint', async () => { |
| 43 | + const sdl = await fetchEndpointSchemaSDL(`http://127.0.0.1:${port}/graphql`); |
| 44 | + |
| 45 | + expect(sdl).toContain('type Query'); |
| 46 | + expect(sdl).toContain('hello'); |
| 47 | + expect(sdl).toContain('version'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('throws on HTTP error responses', async () => { |
| 51 | + const { server: errServer, port: errPort } = await createMockServer((_req, res) => { |
| 52 | + res.writeHead(500); |
| 53 | + res.end('Internal Server Error'); |
| 54 | + }); |
| 55 | + |
| 56 | + await expect( |
| 57 | + fetchEndpointSchemaSDL(`http://127.0.0.1:${errPort}/graphql`), |
| 58 | + ).rejects.toThrow('HTTP 500'); |
| 59 | + |
| 60 | + errServer.close(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('throws on invalid JSON response', async () => { |
| 64 | + const { server: badServer, port: badPort } = await createMockServer((_req, res) => { |
| 65 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 66 | + res.end('<html>not json</html>'); |
| 67 | + }); |
| 68 | + |
| 69 | + await expect( |
| 70 | + fetchEndpointSchemaSDL(`http://127.0.0.1:${badPort}/graphql`), |
| 71 | + ).rejects.toThrow('Failed to parse response'); |
| 72 | + |
| 73 | + badServer.close(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('throws when introspection returns errors', async () => { |
| 77 | + const { server: errServer, port: errPort } = await createMockServer((_req, res) => { |
| 78 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 79 | + res.end(JSON.stringify({ errors: [{ message: 'Not allowed' }] })); |
| 80 | + }); |
| 81 | + |
| 82 | + await expect( |
| 83 | + fetchEndpointSchemaSDL(`http://127.0.0.1:${errPort}/graphql`), |
| 84 | + ).rejects.toThrow('Introspection returned errors'); |
| 85 | + |
| 86 | + errServer.close(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('passes custom headers to the endpoint', async () => { |
| 90 | + let receivedHeaders: http.IncomingHttpHeaders = {}; |
| 91 | + |
| 92 | + const { server: headerServer, port: headerPort } = await createMockServer((req, res) => { |
| 93 | + receivedHeaders = req.headers; |
| 94 | + introspectionHandler(req, res); |
| 95 | + }); |
| 96 | + |
| 97 | + await fetchEndpointSchemaSDL(`http://127.0.0.1:${headerPort}/graphql`, { |
| 98 | + auth: 'Bearer test-token', |
| 99 | + headerHost: 'custom.host.io', |
| 100 | + headers: { 'X-Custom': 'value123' }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(receivedHeaders['authorization']).toBe('Bearer test-token'); |
| 104 | + expect(receivedHeaders['host']).toBe('custom.host.io'); |
| 105 | + expect(receivedHeaders['x-custom']).toBe('value123'); |
| 106 | + |
| 107 | + headerServer.close(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments