import { EventEmitter } from 'events'; import { ClientRequest } from 'http'; import { requestToCurl } from './index'; class MockClientRequest extends EventEmitter { method = 'POST'; protocol = 'https:'; path = '/api/resource'; host = 'cli.example.com'; headers: Record = { host: 'cli.example.com', 'content-type': 'text/plain', }; outputData: Array<{ data: string | Buffer; encoding?: BufferEncoding }> = []; finished = false; writableFinished = false; getHeaders() { return this.headers; } private recordChunk(chunk: any, encoding?: BufferEncoding) { if (chunk === undefined || chunk === null || typeof chunk === 'function') { return; } this.outputData.push({ data: chunk, encoding }); } write(chunk: any, encoding?: BufferEncoding) { this.recordChunk(chunk, encoding); this.emit('wrote', chunk); return true; } end(chunk?: any) { this.recordChunk(chunk); if (chunk !== undefined && chunk !== null && typeof chunk !== 'function') { this.emit('wrote', chunk); } this.finished = true; this.writableFinished = true; this.emit('finish'); this.emit('close'); return this; } } describe('requestToCurl', () => { it('captures streamed data when chunks are written before ending', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const curlPromise = requestToCurl(req); req.write('hello'); req.end(' world'); await expect(curlPromise).resolves.toContain("--data-binary 'hello world'"); }); it('uses form encoding flags based on headers', async () => { const req = new MockClientRequest() as unknown as ClientRequest; (req as any).headers['content-type'] = 'application/x-www-form-urlencoded'; const curlPromise = requestToCurl(req); (req as any).end('foo=bar'); const curl = await curlPromise; expect(curl).toContain("--data 'foo=bar'"); expect(curl).not.toContain('content-length'); }); it('provides best effort even if the request already ended', async () => { const req = new MockClientRequest() as unknown as ClientRequest; (req as any).write('early'); (req as any).end(' chunks'); const curl = await requestToCurl(req); expect(curl).toContain("--data-binary 'early chunks'"); }); it('invokes the optional callback upon completion', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const callback = jest.fn(); requestToCurl(req, callback); (req as any).end('payload'); await new Promise(resolve => setTimeout(resolve, 10)); expect(callback).toHaveBeenCalledTimes(1); expect(callback.mock.calls[0][0]).toBe(null); expect(callback.mock.calls[0][1]).toContain("--data-binary 'payload'"); }); it('rejects and passes timeout errors to the callback', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const callback = jest.fn(); requestToCurl(req, callback, 20); (req as any).write('waiting'); await new Promise(resolve => setTimeout(resolve, 30)); expect(callback).toHaveBeenCalledTimes(1); expect(callback.mock.calls[0][0]).toMatchObject({ code: 'ETIMEDOUT' }); }); it('rejects when only a timeout argument is provided', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const promise = requestToCurl(req, 20); (req as any).write('partial data'); try { await promise; throw new Error('Expected requestToCurl to reject'); } catch (error) { expect(error).toMatchObject({ code: 'ETIMEDOUT' }); } }); it('supports disabling the timeout', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const promise = requestToCurl(req, false); (req as any).write('slow'); setTimeout(() => (req as any).end(' finish'), 20); await expect(promise).resolves.toContain("--data-binary 'slow finish'"); }); it('uses DEFAULT_TIMEOUT_MS when timeout is true', async () => { const req = new MockClientRequest() as unknown as ClientRequest; const originalDefault = requestToCurl.DEFAULT_TIMEOUT_MS; requestToCurl.DEFAULT_TIMEOUT_MS = 30; const start = Date.now(); const promise = requestToCurl(req, true); (req as any).write('late data'); await expect(promise).rejects.toMatchObject({ code: 'ETIMEDOUT' }); const elapsed = Date.now() - start; expect(elapsed).toBeGreaterThanOrEqual(25); requestToCurl.DEFAULT_TIMEOUT_MS = originalDefault; }); });