Skip to content

test: refactor TestBasicCredentials using table-driven tests #3406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
Merged
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
113 changes: 84 additions & 29 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"errors"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -179,36 +180,90 @@ func TestStreamingCredentialsProvider(t *testing.T) {
}

func TestBasicCredentials(t *testing.T) {
t.Run("basic auth", func(t *testing.T) {
creds := NewBasicCredentials("user1", "pass1")
username, password := creds.BasicAuth()
if username != "user1" {
t.Fatalf("expected username 'user1', got '%s'", username)
}
if password != "pass1" {
t.Fatalf("expected password 'pass1', got '%s'", password)
}
})

t.Run("raw credentials", func(t *testing.T) {
creds := NewBasicCredentials("user1", "pass1")
raw := creds.RawCredentials()
expected := "user1:pass1"
if raw != expected {
t.Fatalf("expected raw credentials '%s', got '%s'", expected, raw)
}
})
tests := []struct {
name string
username string
password string
expectedUser string
expectedPass string
expectedRaw string
}{
{
name: "basic auth",
username: "user1",
password: "pass1",
expectedUser: "user1",
expectedPass: "pass1",
expectedRaw: "user1:pass1",
},
{
name: "empty username",
username: "",
password: "pass1",
expectedUser: "",
expectedPass: "pass1",
expectedRaw: ":pass1",
},
{
name: "empty password",
username: "user1",
password: "",
expectedUser: "user1",
expectedPass: "",
expectedRaw: "user1:",
},
{
name: "both username and password empty",
username: "",
password: "",
expectedUser: "",
expectedPass: "",
expectedRaw: ":",
},
{
name: "special characters",
username: "user:1",
password: "pa:ss@!#",
expectedUser: "user:1",
expectedPass: "pa:ss@!#",
expectedRaw: "user:1:pa:ss@!#",
},
{
name: "unicode characters",
username: "ユーザー",
password: "密碼123",
expectedUser: "ユーザー",
expectedPass: "密碼123",
expectedRaw: "ユーザー:密碼123",
},
{
name: "long credentials",
username: strings.Repeat("u", 1000),
password: strings.Repeat("p", 1000),
expectedUser: strings.Repeat("u", 1000),
expectedPass: strings.Repeat("p", 1000),
expectedRaw: strings.Repeat("u", 1000) + ":" + strings.Repeat("p", 1000),
},
}

t.Run("empty username", func(t *testing.T) {
creds := NewBasicCredentials("", "pass1")
username, password := creds.BasicAuth()
if username != "" {
t.Fatalf("expected empty username, got '%s'", username)
}
if password != "pass1" {
t.Fatalf("expected password 'pass1', got '%s'", password)
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
creds := NewBasicCredentials(tt.username, tt.password)

user, pass := creds.BasicAuth()
if user != tt.expectedUser {
t.Errorf("BasicAuth() username = %q; want %q", user, tt.expectedUser)
}
if pass != tt.expectedPass {
t.Errorf("BasicAuth() password = %q; want %q", pass, tt.expectedPass)
}

raw := creds.RawCredentials()
if raw != tt.expectedRaw {
t.Errorf("RawCredentials() = %q; want %q", raw, tt.expectedRaw)
}
})
}
}

func TestReAuthCredentialsListener(t *testing.T) {
Expand Down
Loading