Skip to content

Commit 3184776

Browse files
authored
test(e2e): unix domain socket (uds/ipc) (#1208)
1 parent 4be9fbd commit 3184776

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { once } from 'node:events';
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+
await once(targetServer, 'listening');
23+
24+
return {
25+
async [Symbol.asyncDispose]() {
26+
await new Promise<void>((resolve, reject) => {
27+
targetServer.close((error) => (error ? reject(error) : resolve()));
28+
});
29+
},
30+
};
31+
};
32+
33+
const startProxyServer = async (socketPath: string) => {
34+
const proxyMiddleware = createProxyMiddleware({
35+
target: {
36+
socketPath,
37+
},
38+
});
39+
40+
const proxyServer = createApp(proxyMiddleware).listen(0);
41+
await once(proxyServer, 'listening');
42+
43+
return {
44+
server: proxyServer,
45+
async [Symbol.asyncDispose]() {
46+
await new Promise<void>((resolve, reject) => {
47+
proxyServer.close((error) => (error ? reject(error) : resolve()));
48+
});
49+
},
50+
};
51+
};
52+
53+
it('proxies HTTP request to unix socket target', async () => {
54+
// ARRANGE
55+
const UNIX_DOMAIN_SOCKET_PATH = join(tmpdir(), `hpm-test-app-${randomUUID()}.sock`);
56+
57+
await using targetServer = await startTargetServer(UNIX_DOMAIN_SOCKET_PATH);
58+
await using proxyServer = await startProxyServer(UNIX_DOMAIN_SOCKET_PATH);
59+
60+
void targetServer;
61+
62+
// ACT
63+
const response = await request(proxyServer.server).get('/hello').expect(200);
64+
65+
// ASSERT
66+
expect(response.body).toEqual({ ok: true });
67+
});
68+
});

0 commit comments

Comments
 (0)