Skip to content

Commit e21ec6b

Browse files
committed
Fix mock server to handle multiple Redis commands in single packet
1 parent 820f85e commit e21ec6b

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/app.test.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,53 @@ describe('Redis Proxy API', () => {
2424
const command = data.toString();
2525
console.log('Mock Redis received:', command.replace(/\r\n/g, '\\r\\n'));
2626

27+
// Count how many Redis commands are in this data packet
28+
// Each command starts with * followed by number of arguments
29+
const commandCount = (command.match(/\*\d+\r\n/g) || []).length;
30+
console.log('Command count:', commandCount);
31+
32+
// Send appropriate responses based on command content
33+
let responses = '';
34+
2735
// Handle HELLO command (Redis 6+ handshake)
2836
if (command.includes('HELLO')) {
29-
// Return Redis server info for HELLO command
30-
socket.write('*7\r\n$6\r\nserver\r\n$5\r\nredis\r\n$7\r\nversion\r\n$5\r\n7.2.0\r\n$5\r\nproto\r\n:3\r\n$2\r\nid\r\n:1\r\n');
37+
responses += '*7\r\n$6\r\nserver\r\n$5\r\nredis\r\n$7\r\nversion\r\n$5\r\n7.2.0\r\n$5\r\nproto\r\n:3\r\n$2\r\nid\r\n:1\r\n';
3138
}
32-
// Handle AUTH command
33-
else if (command.includes('AUTH')) {
34-
socket.write('+OK\r\n');
39+
40+
// Handle CLIENT SETINFO commands (send OK for each)
41+
if (command.includes('CLIENT')) {
42+
const clientCommands = (command.match(/\*4\r\n\$6\r\nCLIENT\r\n/g) || []).length;
43+
for (let i = 0; i < clientCommands; i++) {
44+
responses += '+OK\r\n';
45+
}
3546
}
36-
// Handle PING command
37-
else if (command.includes('PING')) {
38-
socket.write('+PONG\r\n');
47+
48+
// Handle other commands
49+
if (command.includes('AUTH') && !command.includes('CLIENT')) {
50+
responses += '+OK\r\n';
3951
}
40-
// Handle FOO command (our test command)
41-
else if (command.includes('FOO')) {
42-
socket.write('+BAR\r\n');
52+
if (command.includes('PING') && !command.includes('CLIENT')) {
53+
responses += '+PONG\r\n';
4354
}
44-
// Handle SELECT command (database selection)
45-
else if (command.includes('SELECT')) {
46-
socket.write('+OK\r\n');
55+
if (command.includes('FOO')) {
56+
responses += '+BAR\r\n';
4757
}
48-
// Handle INFO command
49-
else if (command.includes('INFO')) {
50-
socket.write('$23\r\n# Server\r\nredis_version:7.2.0\r\n');
58+
if (command.includes('SELECT') && !command.includes('CLIENT')) {
59+
responses += '+OK\r\n';
5160
}
52-
// Default response for any other command
53-
else {
54-
socket.write('+OK\r\n');
61+
if (command.includes('INFO') && !command.includes('CLIENT')) {
62+
responses += '$23\r\n# Server\r\nredis_version:7.2.0\r\n';
5563
}
64+
65+
// If no specific responses were generated, send OK for each command
66+
if (!responses) {
67+
for (let i = 0; i < commandCount; i++) {
68+
responses += '+OK\r\n';
69+
}
70+
}
71+
72+
console.log('Sending responses:', responses.replace(/\r\n/g, '\\r\\n'));
73+
socket.write(responses);
5674
},
5775
open(socket) {
5876
console.log('Mock Redis TCP connection opened');

0 commit comments

Comments
 (0)