|
| 1 | +const http = require('http'); |
| 2 | +const { io } = require('socket.io-client'); |
| 3 | +const jwt = require('jsonwebtoken'); |
| 4 | + |
| 5 | +// Import server pieces |
| 6 | +const { httpServer } = require('../server'); |
| 7 | +// Use the client-side tunnel classes for the simulated client |
| 8 | +const { TunnelRequest, TunnelResponse } = require('../../proxy-client/lib'); |
| 9 | + |
| 10 | +// Helper to create a local target HTTP server |
| 11 | +function createLocalTarget(handler) { |
| 12 | + return new Promise((resolve) => { |
| 13 | + const server = http.createServer(handler); |
| 14 | + server.listen(0, () => { |
| 15 | + const { port } = server.address(); |
| 16 | + resolve({ server, port }); |
| 17 | + }); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +// Start the proxy server on ephemeral port |
| 22 | +function ensureProxyListening() { |
| 23 | + return new Promise((resolve) => { |
| 24 | + if (httpServer.listening) { |
| 25 | + return resolve(httpServer.address().port); |
| 26 | + } |
| 27 | + httpServer.listen(0, () => { |
| 28 | + resolve(httpServer.address().port); |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +describe('Integration: HTTP tunnel flow', () => { |
| 34 | + let localTarget; |
| 35 | + let proxyPort; |
| 36 | + let clientSocket; |
| 37 | + |
| 38 | + beforeAll(async () => { |
| 39 | + // Create a simple echo JSON endpoint |
| 40 | + localTarget = await createLocalTarget((req, res) => { |
| 41 | + if (req.url === '/ping' && req.method === 'GET') { |
| 42 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 43 | + res.end(JSON.stringify({ ok: true })); |
| 44 | + return; |
| 45 | + } |
| 46 | + res.writeHead(404); |
| 47 | + res.end('NF'); |
| 48 | + }); |
| 49 | + proxyPort = await ensureProxyListening(); |
| 50 | + |
| 51 | + // Set auth envs expected by the server for Socket.IO auth middleware |
| 52 | + process.env.SECRET_KEY = 'SECRET'; |
| 53 | + process.env.VERIFY_TOKEN = 'VERIFY'; |
| 54 | + |
| 55 | + // Fake client socket to receive tunneled requests (simulate real tunnel client logic minimal subset) |
| 56 | + clientSocket = io(`http://localhost:${proxyPort}`, { |
| 57 | + path: '/$web_tunnel', |
| 58 | + transports: ['websocket'], |
| 59 | + auth: { token: jwt.sign({ token: 'VERIFY' }, 'SECRET') }, |
| 60 | + autoConnect: false, |
| 61 | + }); |
| 62 | + await new Promise((resolve, reject) => { |
| 63 | + clientSocket.on('connect_error', reject); |
| 64 | + clientSocket.on('connect', resolve); |
| 65 | + clientSocket.connect(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + afterAll(async () => { |
| 70 | + if (clientSocket && clientSocket.connected) clientSocket.disconnect(); |
| 71 | + await new Promise((r) => httpServer.close(() => r())); |
| 72 | + await new Promise((r) => localTarget.server.close(() => r())); |
| 73 | + }); |
| 74 | + |
| 75 | + test('GET /ping proxied successfully', async () => { |
| 76 | + // Implement minimal client-side handling of tunnel events |
| 77 | + clientSocket.on('request', (requestId, request) => { |
| 78 | + // rewrite target |
| 79 | + request.port = localTarget.port; |
| 80 | + request.hostname = 'localhost'; |
| 81 | + const localReq = http.request(request, (localRes) => { |
| 82 | + const tunnelResponse = new TunnelResponse({ socket: clientSocket, responseId: requestId }); |
| 83 | + tunnelResponse.writeHead(localRes.statusCode, localRes.statusMessage, localRes.headers, localRes.httpVersion); |
| 84 | + localRes.pipe(tunnelResponse); |
| 85 | + }); |
| 86 | + const tunnelRequest = new TunnelRequest({ socket: clientSocket, requestId }); |
| 87 | + tunnelRequest.pipe(localReq); |
| 88 | + }); |
| 89 | + |
| 90 | + const body = await new Promise((resolve, reject) => { |
| 91 | + const req = http.request({ |
| 92 | + host: 'localhost', |
| 93 | + port: proxyPort, |
| 94 | + path: '/ping', |
| 95 | + method: 'GET', |
| 96 | + headers: { Host: `localhost:${proxyPort}` }, |
| 97 | + }, (res) => { |
| 98 | + let data = ''; |
| 99 | + res.setEncoding('utf8'); |
| 100 | + res.on('data', (chunk) => (data += chunk)); |
| 101 | + res.on('end', () => { |
| 102 | + try { |
| 103 | + resolve({ status: res.statusCode, json: JSON.parse(data) }); |
| 104 | + } catch (e) { |
| 105 | + reject(e); |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + req.on('error', reject); |
| 110 | + req.end(); |
| 111 | + }); |
| 112 | + |
| 113 | + expect(body.status).toBe(200); |
| 114 | + expect(body.json).toEqual({ ok: true }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments