Skip to content

Commit

Permalink
fix: not modify option in newPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Jun 25, 2022
1 parent 51a3e48 commit 458749d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pipe.go
Expand Up @@ -74,9 +74,8 @@ func newPipe(conn net.Conn, option *ClientOption) (p *pipe, err error) {

helloCmd := []string{"HELLO", "3"}
if option.Password != "" && option.Username == "" {
option.Username = "default"
}
if option.Username != "" {
helloCmd = append(helloCmd, "AUTH", "default", option.Password)
} else if option.Username != "" {
helloCmd = append(helloCmd, "AUTH", option.Username, option.Password)
}
if option.ClientName != "" {
Expand Down
31 changes: 30 additions & 1 deletion pipe_test.go
Expand Up @@ -161,7 +161,7 @@ func ExpectOK(t *testing.T, result RedisResult) {
}

func TestNewPipe(t *testing.T) {
t.Run("Auth", func(t *testing.T) {
t.Run("Auth without Username", func(t *testing.T) {
n1, n2 := net.Pipe()
mock := &redisMock{buf: bufio.NewReader(n2), conn: n2}
go func() {
Expand Down Expand Up @@ -189,6 +189,35 @@ func TestNewPipe(t *testing.T) {
n1.Close()
n2.Close()
})
t.Run("Auth with Username", func(t *testing.T) {
n1, n2 := net.Pipe()
mock := &redisMock{buf: bufio.NewReader(n2), conn: n2}
go func() {
mock.Expect("HELLO", "3", "AUTH", "ua", "pa", "SETNAME", "cn").
Reply(RedisMessage{
typ: '%',
values: []RedisMessage{{typ: '+', string: "key"}, {typ: '+', string: "value"}},
})
mock.Expect("CLIENT", "TRACKING", "ON", "OPTIN").
ReplyString("OK")
mock.Expect("SELECT", "1").
ReplyString("OK")
}()
p, err := newPipe(n1, &ClientOption{
SelectDB: 1,
Username: "ua",
Password: "pa",
ClientName: "cn",
})
if err != nil {
t.Fatalf("pipe setup failed: %v", err)
}
go func() { mock.Expect("QUIT").ReplyString("OK") }()
p.Close()
mock.Close()
n1.Close()
n2.Close()
})
t.Run("Network Error", func(t *testing.T) {
n1, n2 := net.Pipe()
n1.Close()
Expand Down

0 comments on commit 458749d

Please sign in to comment.