From bbcc3936bc984f1284a92aa1ebd91c230e7fbbd3 Mon Sep 17 00:00:00 2001 From: Amir Salehi Date: Sat, 14 Jun 2025 18:16:19 +0330 Subject: [PATCH 1/2] test: refactor TestBasicCredentials using table-driven tests --- auth/auth_test.go | 72 ++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index be762a854..bf3ca5321 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -179,36 +179,50 @@ 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", + }, + } - 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) { From 3b963241e7161dca5a16e440596e343085bf76a5 Mon Sep 17 00:00:00 2001 From: Amir Salehi Date: Sat, 14 Jun 2025 22:22:57 +0330 Subject: [PATCH 2/2] Included additional edge cases: - Empty passwords - Special characters - Long strings - Unicode characters --- auth/auth_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/auth/auth_test.go b/auth/auth_test.go index bf3ca5321..73984e331 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -2,6 +2,7 @@ package auth import ( "errors" + "strings" "sync" "testing" "time" @@ -203,6 +204,46 @@ func TestBasicCredentials(t *testing.T) { 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), + }, } for _, tt := range tests {