Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion redis/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package redis_test

import (
"fmt"

netURL "net/url"

. "github.com/onsi/ginkgo"
Expand Down
6 changes: 3 additions & 3 deletions redis/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (m *hosts) Get(host string) bool {
}

func generateConnection(url *netURL.URL) (redigo.Conn, error) {
// Then we expec the server to not ask for a password
// Then we expect the server to not ask for a password
if hostsNotUsingAuth.Get(url.Host) {
url.User = nil
conn, err := redisurl.ConnectToURL(url.String())
if err == redigoErrNoAuth {
if errors.Is(err, redigoErrNoAuth) {
hostsNotUsingAuth.Remove(url.Host)
return generateConnection(url)
}
Expand All @@ -63,7 +63,7 @@ func generateConnection(url *netURL.URL) (redigo.Conn, error) {

// Then we expect the server to potentially ask for a password
conn, err := redisurl.ConnectToURL(url.String())
if err == redigoErrSentAuth {
if errors.Is(err, redigoErrSentAuth) || errors.Is(err, redigoErrSentAuth2) {
hostsNotUsingAuth.Add(url.Host)
return generateConnection(url)
}
Expand Down
5 changes: 3 additions & 2 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
var (
ErrNil = redigo.ErrNil

redigoErrNoAuth = redigo.Error("NOAUTH Authentication required.")
redigoErrSentAuth = redigo.Error("ERR Client sent AUTH, but no password is set")
redigoErrNoAuth = redigo.Error("NOAUTH Authentication required.")
redigoErrSentAuth = redigo.Error("ERR Client sent AUTH, but no password is set")
redigoErrSentAuth2 = redigo.Error("ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?")
)

type Z struct {
Expand Down
17 changes: 8 additions & 9 deletions redis/redisurl/redisurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,38 @@ func Connect() (redis.Conn, error) {

func ConnectToURL(s string) (c redis.Conn, err error) {
redisURL, err := url.Parse(s)

if err != nil {
return
return nil, err
}

auth := ""

if redisURL.User != nil {
if password, ok := redisURL.User.Password(); ok {
auth = password
}
}

c, err = redis.Dial("tcp", redisURL.Host)

if err != nil {
fmt.Println(err)
return
return nil, err
}

if len(auth) > 0 {
_, err = c.Do("AUTH", auth)

if err != nil {
fmt.Println(err)
return
return c, err
}
}

if len(redisURL.Path) > 1 {
db := strings.TrimPrefix(redisURL.Path, "/")
c.Do("SELECT", db)
_, err = c.Do("SELECT", db)
if err != nil {
return c, err
}
}

return
return c, err
}
47 changes: 45 additions & 2 deletions redis/redisurl/redisurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (

func TestConnect(t *testing.T) {
c, err := redisurl.Connect()

if err != nil {
t.Errorf("Error returned")
t.Fatalf("Error returned: %v", err)
}

if c == nil {
t.Fatal("Connection is nil")
}
defer c.Close()

pong, err := redis.String(c.Do("PING"))

Expand All @@ -24,3 +28,42 @@ func TestConnect(t *testing.T) {
t.Errorf("Wanted PONG, got %v\n", pong)
}
}

func TestConnectToURL_InvalidURL(t *testing.T) {
c, err := redisurl.ConnectToURL("://invalid url")
if err == nil {
t.Error("Expected error for invalid URL, got nil")
}

if c != nil {
t.Error("Expected nil connection for invalid URL")
}
}

func TestConnectToURL_InvalidHost(t *testing.T) {
c, err := redisurl.ConnectToURL("redis://nonexistent-host-12345:6379")
if err == nil {
t.Error("Expected error for invalid host, got nil")
}

// This is the critical check - connection should be nil on error
if c != nil {
t.Error("Expected nil connection when dial fails")
}
}

func TestConnectToURL_WithAuth(t *testing.T) {
c, err := redisurl.ConnectToURL("redis://:password@localhost:6379/10")
// When AUTH fails, connection is returned so caller can check error type and retry
if err != nil {
if c == nil {
t.Error("Connection should be returned even when AUTH fails (for fallback logic)")
} else {
c.Close() // Caller's responsibility to close on error
}
}

if c != nil && err == nil {
defer c.Close()
}
}