Skip to content

Commit bbcc393

Browse files
committed
test: refactor TestBasicCredentials using table-driven tests
1 parent eb40ac8 commit bbcc393

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

auth/auth_test.go

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,50 @@ func TestStreamingCredentialsProvider(t *testing.T) {
179179
}
180180

181181
func TestBasicCredentials(t *testing.T) {
182-
t.Run("basic auth", func(t *testing.T) {
183-
creds := NewBasicCredentials("user1", "pass1")
184-
username, password := creds.BasicAuth()
185-
if username != "user1" {
186-
t.Fatalf("expected username 'user1', got '%s'", username)
187-
}
188-
if password != "pass1" {
189-
t.Fatalf("expected password 'pass1', got '%s'", password)
190-
}
191-
})
192-
193-
t.Run("raw credentials", func(t *testing.T) {
194-
creds := NewBasicCredentials("user1", "pass1")
195-
raw := creds.RawCredentials()
196-
expected := "user1:pass1"
197-
if raw != expected {
198-
t.Fatalf("expected raw credentials '%s', got '%s'", expected, raw)
199-
}
200-
})
182+
tests := []struct {
183+
name string
184+
username string
185+
password string
186+
expectedUser string
187+
expectedPass string
188+
expectedRaw string
189+
}{
190+
{
191+
name: "basic auth",
192+
username: "user1",
193+
password: "pass1",
194+
expectedUser: "user1",
195+
expectedPass: "pass1",
196+
expectedRaw: "user1:pass1",
197+
},
198+
{
199+
name: "empty username",
200+
username: "",
201+
password: "pass1",
202+
expectedUser: "",
203+
expectedPass: "pass1",
204+
expectedRaw: ":pass1",
205+
},
206+
}
201207

202-
t.Run("empty username", func(t *testing.T) {
203-
creds := NewBasicCredentials("", "pass1")
204-
username, password := creds.BasicAuth()
205-
if username != "" {
206-
t.Fatalf("expected empty username, got '%s'", username)
207-
}
208-
if password != "pass1" {
209-
t.Fatalf("expected password 'pass1', got '%s'", password)
210-
}
211-
})
208+
for _, tt := range tests {
209+
t.Run(tt.name, func(t *testing.T) {
210+
creds := NewBasicCredentials(tt.username, tt.password)
211+
212+
user, pass := creds.BasicAuth()
213+
if user != tt.expectedUser {
214+
t.Errorf("BasicAuth() username = %q; want %q", user, tt.expectedUser)
215+
}
216+
if pass != tt.expectedPass {
217+
t.Errorf("BasicAuth() password = %q; want %q", pass, tt.expectedPass)
218+
}
219+
220+
raw := creds.RawCredentials()
221+
if raw != tt.expectedRaw {
222+
t.Errorf("RawCredentials() = %q; want %q", raw, tt.expectedRaw)
223+
}
224+
})
225+
}
212226
}
213227

214228
func TestReAuthCredentialsListener(t *testing.T) {

0 commit comments

Comments
 (0)