|
| 1 | +import http, { IncomingMessage, Server, ServerResponse } from 'http'; |
| 2 | +import { AddressInfo } from 'net'; |
| 3 | +import { Readable } from 'stream'; |
| 4 | + |
| 5 | +import RpcCollection from '../src/collection'; |
| 6 | + |
| 7 | +type Handler = (req: IncomingMessage, res: ServerResponse) => void; |
| 8 | + |
| 9 | +async function startServer(handler: Handler): Promise<{ server: Server; uri: string }> { |
| 10 | + const server = http.createServer(handler); |
| 11 | + await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve)); |
| 12 | + const { address, port } = server.address() as AddressInfo; |
| 13 | + |
| 14 | + return { server, uri: `http://${address}:${port}` }; |
| 15 | +} |
| 16 | + |
| 17 | +async function stopServer(server: Server) { |
| 18 | + await new Promise<void>(resolve => server.close(() => resolve())); |
| 19 | +} |
| 20 | + |
| 21 | +function readBody(req: IncomingMessage): Promise<string> { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const chunks: Uint8Array[] = []; |
| 24 | + req.on('data', chunk => chunks.push(chunk)); |
| 25 | + req.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8'))); |
| 26 | + req.on('error', reject); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +function buildCollection(uri: string) { |
| 31 | + const logger = jest.fn(); |
| 32 | + const datasource = { collections: [], schema: { charts: [] } }; |
| 33 | + const options = { uri, authSecret: 'secret' }; |
| 34 | + const schema = { |
| 35 | + actions: {}, |
| 36 | + charts: [], |
| 37 | + fields: {}, |
| 38 | + segments: [], |
| 39 | + aggregationCapabilities: { supportedDateOperations: new Set(), supportGroups: false }, |
| 40 | + countable: false, |
| 41 | + searchable: false, |
| 42 | + }; |
| 43 | + |
| 44 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 45 | + return new RpcCollection(logger as any, datasource as any, options, 'books', schema as any); |
| 46 | +} |
| 47 | + |
| 48 | +describe('RpcCollection.execute', () => { |
| 49 | + let server: Server; |
| 50 | + let uri: string; |
| 51 | + |
| 52 | + afterEach(async () => { |
| 53 | + if (server) await stopServer(server); |
| 54 | + }); |
| 55 | + |
| 56 | + it('parses a JSON Success response and rebuilds invalidated as a Set', async () => { |
| 57 | + const received: { body?: unknown } = {}; |
| 58 | + |
| 59 | + ({ server, uri } = await startServer(async (req, res) => { |
| 60 | + received.body = JSON.parse(await readBody(req)); |
| 61 | + res.setHeader('Content-Type', 'application/json'); |
| 62 | + res.end( |
| 63 | + JSON.stringify({ |
| 64 | + type: 'Success', |
| 65 | + message: 'ok', |
| 66 | + invalidated: ['books'], |
| 67 | + response_headers: { 'x-foo': 'bar' }, |
| 68 | + }), |
| 69 | + ); |
| 70 | + })); |
| 71 | + |
| 72 | + const collection = buildCollection(uri); |
| 73 | + const result = await collection.execute({ id: 1 } as never, 'noop', { foo: 'bar' }, undefined); |
| 74 | + |
| 75 | + expect(received.body).toEqual({ action: 'noop', filter: undefined, data: { foo: 'bar' } }); |
| 76 | + expect(result).toEqual({ |
| 77 | + type: 'Success', |
| 78 | + message: 'ok', |
| 79 | + invalidated: new Set(['books']), |
| 80 | + responseHeaders: { 'x-foo': 'bar' }, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns a FileResult with a Readable stream when X-Forest-Action-Type=File', async () => { |
| 85 | + ({ server, uri } = await startServer(async (req, res) => { |
| 86 | + await readBody(req); |
| 87 | + res.setHeader('Content-Type', 'application/pdf'); |
| 88 | + res.setHeader('Content-Disposition', 'attachment; filename="report%20final.pdf"'); |
| 89 | + res.setHeader('X-Forest-Action-Type', 'File'); |
| 90 | + res.setHeader('X-Forest-Action-File-Name', 'report%20final.pdf'); |
| 91 | + res.setHeader( |
| 92 | + 'X-Forest-Action-Response-Headers', |
| 93 | + JSON.stringify({ 'set-cookie': 'token=xyz' }), |
| 94 | + ); |
| 95 | + Readable.from([Buffer.from('hello-pdf')]).pipe(res); |
| 96 | + })); |
| 97 | + |
| 98 | + const collection = buildCollection(uri); |
| 99 | + const result = await collection.execute({ id: 1 } as never, 'download', {}, undefined); |
| 100 | + |
| 101 | + expect(result.type).toBe('File'); |
| 102 | + if (result.type !== 'File') throw new Error('unreachable'); |
| 103 | + expect(result.mimeType).toBe('application/pdf'); |
| 104 | + expect(result.name).toBe('report final.pdf'); |
| 105 | + expect(result.responseHeaders).toEqual({ 'set-cookie': 'token=xyz' }); |
| 106 | + |
| 107 | + const chunks: Uint8Array[] = []; |
| 108 | + for await (const chunk of result.stream) chunks.push(chunk as Uint8Array); |
| 109 | + expect(Buffer.concat(chunks).toString('utf-8')).toBe('hello-pdf'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('omits responseHeaders when the header is absent', async () => { |
| 113 | + ({ server, uri } = await startServer(async (req, res) => { |
| 114 | + await readBody(req); |
| 115 | + res.setHeader('Content-Type', 'text/plain'); |
| 116 | + res.setHeader('X-Forest-Action-Type', 'File'); |
| 117 | + res.setHeader('X-Forest-Action-File-Name', 'note.txt'); |
| 118 | + res.end('hi'); |
| 119 | + })); |
| 120 | + |
| 121 | + const collection = buildCollection(uri); |
| 122 | + const result = await collection.execute({ id: 1 } as never, 'download', {}, undefined); |
| 123 | + |
| 124 | + expect(result.type).toBe('File'); |
| 125 | + if (result.type !== 'File') throw new Error('unreachable'); |
| 126 | + expect(result.responseHeaders).toBeUndefined(); |
| 127 | + expect(result.name).toBe('note.txt'); |
| 128 | + }); |
| 129 | +}); |
0 commit comments