|
| 1 | +import { randomUUID } from 'node:crypto'; |
| 2 | +import type { AddressInfo } from 'node:net'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | + |
| 6 | +import express from 'express'; |
| 7 | +import request from 'supertest'; |
| 8 | +import { describe, expect, it } from 'vitest'; |
| 9 | + |
| 10 | +import { createApp, createProxyMiddleware } from './test-kit.js'; |
| 11 | + |
| 12 | +// E2E test of: https://github.com/chimurai/http-proxy-middleware/issues/434#issuecomment-1172896375 |
| 13 | + |
| 14 | +describe('E2E Unix Domain Socket proxy (UDS, or IPC sockets)', () => { |
| 15 | + const startTargetServer = async (socketPath: string) => { |
| 16 | + const app = express(); |
| 17 | + app.get('/hello', (req, res) => { |
| 18 | + res.status(200).json({ ok: true }); |
| 19 | + }); |
| 20 | + |
| 21 | + const targetServer = app.listen(socketPath); |
| 22 | + |
| 23 | + return { |
| 24 | + async [Symbol.asyncDispose]() { |
| 25 | + await new Promise<void>((resolve, reject) => { |
| 26 | + targetServer.close(() => resolve()); |
| 27 | + }); |
| 28 | + }, |
| 29 | + }; |
| 30 | + }; |
| 31 | + |
| 32 | + const startProxyServer = async (socketPath: string) => { |
| 33 | + const proxyMiddleware = createProxyMiddleware({ |
| 34 | + target: { |
| 35 | + socketPath, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + const proxyServer = createApp(proxyMiddleware).listen(0); |
| 40 | + |
| 41 | + return { |
| 42 | + server: proxyServer, |
| 43 | + async [Symbol.asyncDispose]() { |
| 44 | + await new Promise<void>((resolve, reject) => { |
| 45 | + proxyServer.close(() => resolve()); |
| 46 | + }); |
| 47 | + }, |
| 48 | + }; |
| 49 | + }; |
| 50 | + |
| 51 | + it('proxies HTTP request to unix socket target', async () => { |
| 52 | + // ARRANGE |
| 53 | + const UNIX_DOMAIN_SOCKET_PATH = join(tmpdir(), `hpm-test-app-${randomUUID()}.sock`); |
| 54 | + |
| 55 | + await using targetServer = await startTargetServer(UNIX_DOMAIN_SOCKET_PATH); |
| 56 | + await using proxyServer = await startProxyServer(UNIX_DOMAIN_SOCKET_PATH); |
| 57 | + |
| 58 | + void targetServer; |
| 59 | + const address = proxyServer.server.address() as AddressInfo; |
| 60 | + |
| 61 | + // ACT |
| 62 | + const response = await request(`http://[${address.address}]:${address.port}`) |
| 63 | + .get('/hello') |
| 64 | + .expect(200); |
| 65 | + |
| 66 | + // ASSERT |
| 67 | + expect(response.body).toEqual({ ok: true }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments