|
| 1 | +const http = require('http'); |
| 2 | +const httpRequest = require('../../../../utils/async/httpRequest'); |
| 3 | + |
| 4 | +// Spin up a minimal HTTP server, run the callback, then close it. |
| 5 | +async function withServer(handler, fn) { |
| 6 | + const server = http.createServer(handler); |
| 7 | + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); |
| 8 | + const { port } = server.address(); |
| 9 | + try { |
| 10 | + return await fn(port); |
| 11 | + } finally { |
| 12 | + await new Promise(resolve => server.close(resolve)); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +describe('httpRequest', () => { |
| 17 | + test('GET returns status code and buffered body', () => |
| 18 | + withServer((req, res) => { |
| 19 | + res.writeHead(200); |
| 20 | + res.end(`${req.method} hello world`); |
| 21 | + }, async port => { |
| 22 | + const res = await httpRequest('GET', `http://127.0.0.1:${port}/`); |
| 23 | + expect(res.statusCode).toBe(200); |
| 24 | + expect(res.body).toBe('GET hello world'); |
| 25 | + })); |
| 26 | + |
| 27 | + test('GET passes query string and path to the server', () => |
| 28 | + withServer((req, res) => { |
| 29 | + res.writeHead(200); |
| 30 | + res.end(`${req.method} ${req.url}`); |
| 31 | + }, async port => { |
| 32 | + const res = await httpRequest('GET', `http://127.0.0.1:${port}/foo?bar=baz`); |
| 33 | + expect(res.statusCode).toBe(200); |
| 34 | + expect(res.body).toBe('GET /foo?bar=baz'); |
| 35 | + })); |
| 36 | + |
| 37 | + test('GET resolves on 404 (non-5xx is not an error)', () => |
| 38 | + withServer((req, res) => { |
| 39 | + res.writeHead(404); |
| 40 | + res.end(`${req.method} not found`); |
| 41 | + }, async port => { |
| 42 | + const res = await httpRequest('GET', `http://127.0.0.1:${port}/`); |
| 43 | + expect(res.statusCode).toBe(404); |
| 44 | + expect(res.body).toBe('GET not found'); |
| 45 | + })); |
| 46 | + |
| 47 | + test('GET rejects on 5xx response', () => |
| 48 | + withServer((req, res) => { |
| 49 | + res.writeHead(500); |
| 50 | + res.end(`${req.method} oops`); |
| 51 | + }, async port => { |
| 52 | + await expect( |
| 53 | + httpRequest('GET', `http://127.0.0.1:${port}/`) |
| 54 | + ).rejects.toThrow('500'); |
| 55 | + })); |
| 56 | + |
| 57 | + test('DELETE sends the correct method to the server', () => |
| 58 | + withServer((req, res) => { |
| 59 | + res.writeHead(200); |
| 60 | + res.end(`${req.method} ok`); |
| 61 | + }, async port => { |
| 62 | + const res = await httpRequest('DELETE', `http://127.0.0.1:${port}/resource`); |
| 63 | + expect(res.statusCode).toBe(200); |
| 64 | + expect(res.body).toBe('DELETE ok'); |
| 65 | + })); |
| 66 | + |
| 67 | + test('GET retries on 5xx when retryParams provided', () => { |
| 68 | + let calls = 0; |
| 69 | + return withServer((req, res) => { |
| 70 | + calls += 1; |
| 71 | + if (calls < 3) { |
| 72 | + res.writeHead(500); |
| 73 | + res.end(`${req.method} not yet`); |
| 74 | + } else { |
| 75 | + res.writeHead(200); |
| 76 | + res.end(`${req.method} ok`); |
| 77 | + } |
| 78 | + }, async port => { |
| 79 | + const res = await httpRequest( |
| 80 | + 'GET', |
| 81 | + `http://127.0.0.1:${port}/`, |
| 82 | + { times: 5, interval: 0 }, |
| 83 | + ); |
| 84 | + expect(res.statusCode).toBe(200); |
| 85 | + expect(res.body).toBe('GET ok'); |
| 86 | + expect(calls).toBe(3); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments