Skip to content

Commit e2295c7

Browse files
test: refactor TestBasicCredentials using table-driven tests (#3406)
* test: refactor TestBasicCredentials using table-driven tests * Included additional edge cases: - Empty passwords - Special characters - Long strings - Unicode characters
1 parent eb40ac8 commit e2295c7

File tree

1 file changed

+84
-29
lines changed

1 file changed

+84
-29
lines changed

auth/auth_test.go

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"errors"
5+
"strings"
56
"sync"
67
"testing"
78
"time"
@@ -179,36 +180,90 @@ func TestStreamingCredentialsProvider(t *testing.T) {
179180
}
180181

181182
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-
})
183+
tests := []struct {
184+
name string
185+
username string
186+
password string
187+
expectedUser string
188+
expectedPass string
189+
expectedRaw string
190+
}{
191+
{
192+
name: "basic auth",
193+
username: "user1",
194+
password: "pass1",
195+
expectedUser: "user1",
196+
expectedPass: "pass1",
197+
expectedRaw: "user1:pass1",
198+
},
199+
{
200+
name: "empty username",
201+
username: "",
202+
password: "pass1",
203+
expectedUser: "",
204+
expectedPass: "pass1",
205+
expectedRaw: ":pass1",
206+
},
207+
{
208+
name: "empty password",
209+
username: "user1",
210+
password: "",
211+
expectedUser: "user1",
212+
expectedPass: "",
213+
expectedRaw: "user1:",
214+
},
215+
{
216+
name: "both username and password empty",
217+
username: "",
218+
password: "",
219+
expectedUser: "",
220+
expectedPass: "",
221+
expectedRaw: ":",
222+
},
223+
{
224+
name: "special characters",
225+
username: "user:1",
226+
password: "pa:ss@!#",
227+
expectedUser: "user:1",
228+
expectedPass: "pa:ss@!#",
229+
expectedRaw: "user:1:pa:ss@!#",
230+
},
231+
{
232+
name: "unicode characters",
233+
username: "ユーザー",
234+
password: "密碼123",
235+
expectedUser: "ユーザー",
236+
expectedPass: "密碼123",
237+
expectedRaw: "ユーザー:密碼123",
238+
},
239+
{
240+
name: "long credentials",
241+
username: strings.Repeat("u", 1000),
242+
password: strings.Repeat("p", 1000),
243+
expectedUser: strings.Repeat("u", 1000),
244+
expectedPass: strings.Repeat("p", 1000),
245+
expectedRaw: strings.Repeat("u", 1000) + ":" + strings.Repeat("p", 1000),
246+
},
247+
}
201248

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-
})
249+
for _, tt := range tests {
250+
t.Run(tt.name, func(t *testing.T) {
251+
creds := NewBasicCredentials(tt.username, tt.password)
252+
253+
user, pass := creds.BasicAuth()
254+
if user != tt.expectedUser {
255+
t.Errorf("BasicAuth() username = %q; want %q", user, tt.expectedUser)
256+
}
257+
if pass != tt.expectedPass {
258+
t.Errorf("BasicAuth() password = %q; want %q", pass, tt.expectedPass)
259+
}
260+
261+
raw := creds.RawCredentials()
262+
if raw != tt.expectedRaw {
263+
t.Errorf("RawCredentials() = %q; want %q", raw, tt.expectedRaw)
264+
}
265+
})
266+
}
212267
}
213268

214269
func TestReAuthCredentialsListener(t *testing.T) {

0 commit comments

Comments
 (0)