|
| 1 | +import { strict as assert } from 'node:assert'; |
| 2 | +import { Buffer } from 'node:buffer'; |
| 3 | +import { testUtils, GLOBAL } from './test-utils'; |
| 4 | +import { RedisProxy } from './redis-proxy'; |
| 5 | +import type { RedisClientType } from '@redis/client/lib/client/index.js'; |
| 6 | + |
| 7 | +describe('RedisSocketProxy', function () { |
| 8 | + testUtils.testWithClient('basic proxy functionality', async (client: RedisClientType<any, any, any, any, any>) => { |
| 9 | + const socketOptions = client?.options?.socket; |
| 10 | + //@ts-ignore |
| 11 | + assert(socketOptions?.port, 'Test requires a TCP connection to Redis'); |
| 12 | + |
| 13 | + const proxyPort = 50000 + Math.floor(Math.random() * 10000); |
| 14 | + const proxy = new RedisProxy({ |
| 15 | + listenHost: '127.0.0.1', |
| 16 | + listenPort: proxyPort, |
| 17 | + //@ts-ignore |
| 18 | + targetPort: socketOptions.port, |
| 19 | + //@ts-ignore |
| 20 | + targetHost: socketOptions.host || '127.0.0.1', |
| 21 | + enableLogging: true |
| 22 | + }); |
| 23 | + |
| 24 | + const proxyEvents = { |
| 25 | + connections: [] as any[], |
| 26 | + dataTransfers: [] as any[] |
| 27 | + }; |
| 28 | + |
| 29 | + proxy.on('connection', (connectionInfo) => { |
| 30 | + proxyEvents.connections.push(connectionInfo); |
| 31 | + }); |
| 32 | + |
| 33 | + proxy.on('data', (connectionId, direction, data) => { |
| 34 | + proxyEvents.dataTransfers.push({ connectionId, direction, dataLength: data.length }); |
| 35 | + }); |
| 36 | + |
| 37 | + try { |
| 38 | + await proxy.start(); |
| 39 | + |
| 40 | + const proxyClient = client.duplicate({ |
| 41 | + socket: { |
| 42 | + port: proxyPort, |
| 43 | + host: '127.0.0.1' |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + await proxyClient.connect(); |
| 48 | + |
| 49 | + const stats = proxy.getStats(); |
| 50 | + assert.equal(stats.activeConnections, 1, 'Should have one active connection'); |
| 51 | + assert.equal(proxyEvents.connections.length, 1, 'Should have recorded one connection event'); |
| 52 | + |
| 53 | + const pingResult = await proxyClient.ping(); |
| 54 | + assert.equal(pingResult, 'PONG', 'Client should be able to communicate with Redis through the proxy'); |
| 55 | + |
| 56 | + const clientToServerTransfers = proxyEvents.dataTransfers.filter(t => t.direction === 'client->server'); |
| 57 | + const serverToClientTransfers = proxyEvents.dataTransfers.filter(t => t.direction === 'server->client'); |
| 58 | + |
| 59 | + assert(clientToServerTransfers.length > 0, 'Should have client->server data transfers'); |
| 60 | + assert(serverToClientTransfers.length > 0, 'Should have server->client data transfers'); |
| 61 | + |
| 62 | + const testKey = `test:proxy:${Date.now()}`; |
| 63 | + const testValue = 'proxy-test-value'; |
| 64 | + |
| 65 | + await proxyClient.set(testKey, testValue); |
| 66 | + const retrievedValue = await proxyClient.get(testKey); |
| 67 | + assert.equal(retrievedValue, testValue, 'Should be able to set and get values through proxy'); |
| 68 | + |
| 69 | + proxyClient.destroy(); |
| 70 | + |
| 71 | + |
| 72 | + } finally { |
| 73 | + await proxy.stop(); |
| 74 | + } |
| 75 | + }, GLOBAL.SERVERS.OPEN_RESP_3); |
| 76 | + |
| 77 | + testUtils.testWithProxiedClient('custom message injection via proxy client', |
| 78 | + async (proxiedClient: RedisClientType<any, any, any, any, any>, proxy: RedisProxy) => { |
| 79 | + const customMessageTransfers: any[] = []; |
| 80 | + |
| 81 | + proxy.on('data', (connectionId, direction, data) => { |
| 82 | + if (direction === 'server->client') { |
| 83 | + customMessageTransfers.push({ connectionId, dataLength: data.length, data }); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | + const stats = proxy.getStats(); |
| 89 | + assert.equal(stats.activeConnections, 1, 'Should have one active connection'); |
| 90 | + |
| 91 | + // Send a resp3 push |
| 92 | + const customMessage = Buffer.from('>4\r\n$6\r\nMOVING\r\n:1\r\n:2\r\n$6\r\nhost:3\r\n'); |
| 93 | + |
| 94 | + const sendResults = proxy.sendToAllClients(customMessage); |
| 95 | + assert.equal(sendResults.length, 1, 'Should send to one client'); |
| 96 | + assert.equal(sendResults[0].success, true, 'Custom message send should succeed'); |
| 97 | + |
| 98 | + |
| 99 | + const customMessageFound = customMessageTransfers.find(transfer => |
| 100 | + transfer.dataLength === customMessage.length |
| 101 | + ); |
| 102 | + assert(customMessageFound, 'Should have recorded the custom message transfer'); |
| 103 | + |
| 104 | + assert.equal(customMessageFound.dataLength, customMessage.length, |
| 105 | + 'Custom message length should match'); |
| 106 | + |
| 107 | + const pingResult = await proxiedClient.ping(); |
| 108 | + assert.equal(pingResult, 'PONG', 'Client should be able to communicate with Redis through the proxy'); |
| 109 | + |
| 110 | + }, GLOBAL.SERVERS.OPEN_RESP_3) |
| 111 | +}); |
0 commit comments