Skip to content

Commit

Permalink
deal with tx and some more error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebob committed Sep 27, 2023
1 parent ae80f40 commit 7b7a51c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
29 changes: 19 additions & 10 deletions cmd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ func (m *Miniredis) cmdClient(c *server.Peer, cmd string, args []string) {
return
}

switch strings.ToUpper(args[0]) {
case "SETNAME":
m.cmdClientSetName(c, args[1:])
case "GETNAME":
m.cmdClientGetName(c, args[1:])
default:
setDirty(c)
c.WriteError(fmt.Sprintf("ERR 'CLIENT %s' not supported", strings.Join(args, " ")))
}
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
switch cmd := strings.ToUpper(args[0]); cmd {
case "SETNAME":
m.cmdClientSetName(c, args[1:])
case "GETNAME":
m.cmdClientGetName(c, args[1:])
default:
setDirty(c)
c.WriteError(fmt.Sprintf("ERR unknown subcommand '%s'. Try CLIENT HELP.", cmd))
}
})
}

// CLIENT SETNAME
Expand All @@ -39,7 +41,14 @@ func (m *Miniredis) cmdClientSetName(c *server.Peer, args []string) {
return
}

c.ClientName = args[0]
name := args[0]
if strings.ContainsAny(name, " \n") {
setDirty(c)
c.WriteError("ERR Client names cannot contain spaces, newlines or special characters.")
return

}
c.ClientName = name
c.WriteOK()
}

Expand Down
27 changes: 21 additions & 6 deletions integration/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,31 @@ func TestCopy(t *testing.T) {
func TestClient(t *testing.T) {
skip(t)
testRaw(t, func(c *client) {
// Set the client name
c.Do("CLIENT", "SETNAME", "miniredis-tests")

// Get the client name
// Try to get the client name without setting it first
c.Do("CLIENT", "GETNAME")

// Try to get the client name without setting it first
c.Do("CLIENT", "SETNAME", "miniredis-tests")
c.Do("CLIENT", "GETNAME")
c.Do("CLIENT", "SETNAME", "miniredis-tests2")
c.Do("CLIENT", "GETNAME")
c.Do("CLIENT", "SETNAME", "")
c.Do("CLIENT", "GETNAME")

// Try to execute the CLIENT command with no arguments
c.Error("wrong number", "CLIENT")
c.Error("unknown subcommand", "CLIENT", "FOOBAR")
c.Error("wrong number", "CLIENT", "GETNAME", "foo")
c.Error("contain spaces", "CLIENT", "SETNAME", "miniredis tests")
c.Error("contain spaces", "CLIENT", "SETNAME", "miniredis\ntests")
})

testRaw2(t, func(c1, c2 *client) {
c1.Do("MULTI")
c1.Do("CLIENT", "SETNAME", "conn-c1")
c1.Do("CLIENT", "GETNAME")
c2.Do("CLIENT", "GETNAME") // not set yet
c1.Do("EXEC")
c1.Do("CLIENT", "GETNAME")
c2.Do("CLIENT", "GETNAME")
})

}

0 comments on commit 7b7a51c

Please sign in to comment.