|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gotest.tools/v3/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestClientSetName(t *testing.T) { |
| 11 | + conn := getLocalConnection() |
| 12 | + defer conn.Close() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + command string |
| 17 | + expected string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Set valid name without spaces", |
| 21 | + command: "K", |
| 22 | + expected: "OK", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Set valid name with trailing space", |
| 26 | + command: "K ", |
| 27 | + expected: "OK", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Too many arguments for SETNAME", |
| 31 | + command: "K K", |
| 32 | + expected: "ERR wrong number of arguments for 'client|setname' command", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Name with space between characters", |
| 36 | + command: "\"K K\"", |
| 37 | + expected: "ERR Client names cannot contain spaces, newlines or special characters.", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Empty name argument", |
| 41 | + command: " ", |
| 42 | + expected: "ERR wrong number of arguments for 'client|setname' command", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Missing name argument", |
| 46 | + command: "", |
| 47 | + expected: "ERR wrong number of arguments for 'client|setname' command", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Name with newline character", |
| 51 | + command: "\n", |
| 52 | + expected: "ERR Client names cannot contain spaces, newlines or special characters.", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Name with valid character followed by newline", |
| 56 | + command: "K\n", |
| 57 | + expected: "ERR Client names cannot contain spaces, newlines or special characters.", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Name with special character", |
| 61 | + command: "K%", |
| 62 | + expected: "ERR Client names cannot contain spaces, newlines or special characters.", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tc := range tests { |
| 67 | + t.Run(tc.name, func(t *testing.T) { |
| 68 | + result := FireCommand(conn, "CLIENT SETNAME "+tc.command) |
| 69 | + assert.DeepEqual(t, tc.expected, result) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestClientGetName(t *testing.T) { |
| 75 | + conn := getLocalConnection() |
| 76 | + defer conn.Close() |
| 77 | + |
| 78 | + tests := []struct { |
| 79 | + name string |
| 80 | + command string |
| 81 | + expected string |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "GetName with invalid argument", |
| 85 | + command: "CLIENT GETNAME invalid-arg", |
| 86 | + expected: "ERR wrong number of arguments for 'client|getname' command", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "GetName with no name set", |
| 90 | + command: "CLIENT GETNAME", |
| 91 | + expected: "(nil)", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "SetName with invalid name containing space", |
| 95 | + command: "CLIENT SETNAME \"K K\"; CLIENT GETNAME", |
| 96 | + expected: "(nil)", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "SetName with valid name", |
| 100 | + command: "CLIENT SETNAME K; CLIENT GETNAME", |
| 101 | + expected: "K", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + // Split multiple commands (like "CLIENT SETNAME" followed by "CLIENT GETNAME") if needed |
| 108 | + commands := strings.Split(tt.command, "; ") |
| 109 | + var result interface{} |
| 110 | + for _, cmd := range commands { |
| 111 | + result = FireCommand(conn, strings.TrimSpace(cmd)) |
| 112 | + } |
| 113 | + assert.DeepEqual(t, tt.expected, result) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
0 commit comments