Debian Codegraph @

pkg/util/url_test.go

raw
1 package util
2
3 import (
4 "net/url"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 //nolint:gosmopolitan // Unicode characters used intentionally for testing user/password handling
11 func TestURLUserPassword(t *testing.T) {
12 t.Parallel()
13
14 tests := []struct {
15 name string
16 user string
17 password string
18 wantUsername string
19 wantPassword string
20 wantNil bool
21 }{
22 {
23 name: "returns user with password when both provided",
24 user: "admin",
25 password: "secret",
26 wantUsername: "admin",
27 wantPassword: "secret",
28 wantNil: false,
29 },
30 {
31 name: "returns user without password when only user provided",
32 user: "admin",
33 password: "",
34 wantUsername: "admin",
35 wantPassword: "",
36 wantNil: false,
37 },
38 {
39 name: "returns nil when neither user nor password provided",
40 user: "",
41 password: "",
42 wantUsername: "",
43 wantPassword: "",
44 wantNil: true,
45 },
46 {
47 name: "returns user with password when user is empty but password is provided",
48 user: "",
49 password: "secret",
50 wantUsername: "",
51 wantPassword: "secret",
52 wantNil: false,
53 },
54 {
55 name: "handles special characters in credentials",
56 user: "[email protected]",
57 password: "p@ssw0rd!#$%",
58 wantUsername: "[email protected]",
59 wantPassword: "p@ssw0rd!#$%",
60 wantNil: false,
61 },
62 {
63 name: "handles unicode characters",
64 user: "用户",
65 password: "密码",
66 wantUsername: "用户",
67 wantPassword: "密码",
68 wantNil: false,
69 },
70 {
71 name: "handles very long credentials",
72 user: "averylongusernamethatcouldbeused",
73 password: "anextremelylongpasswordthatnobodyshouldusebutwestilltest",
74 wantUsername: "averylongusernamethatcouldbeused",
75 wantPassword: "anextremelylongpasswordthatnobodyshouldusebutwestilltest",
76 wantNil: false,
77 },
78 }
79
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 t.Parallel()
83
84 got := URLUserPassword(tt.user, tt.password)
85
86 if tt.wantNil {
87 assert.Nil(t, got, "URLUserPassword(%q, %q) should return nil", tt.user, tt.password)
88 } else {
89 assert.NotNil(t, got, "URLUserPassword(%q, %q) should not return nil", tt.user, tt.password)
90 assert.Equal(t, tt.wantUsername, got.Username(), "Username mismatch")
91 gotPassword, _ := got.Password()
92 assert.Equal(t, tt.wantPassword, gotPassword, "Password mismatch")
93 }
94 })
95 }
96 }
97
98 func TestURLUserPassword_Integration(t *testing.T) {
99 t.Parallel()
100
101 // Verify the returned Userinfo works correctly with url.URL
102 t.Run("works with url.URL", func(t *testing.T) {
103 t.Parallel()
104
105 userinfo := URLUserPassword("admin", "secret")
106 testURL := &url.URL{
107 Scheme: "https",
108 Host: "example.com",
109 User: userinfo,
110 Path: "/path",
111 }
112
113 expected := "https://admin:[email protected]/path"
114 assert.Equal(t, expected, testURL.String(), "URL with userinfo should serialize correctly")
115 })
116
117 t.Run("nil userinfo serializes to empty string", func(t *testing.T) {
118 t.Parallel()
119
120 userinfo := URLUserPassword("", "")
121 testURL := &url.URL{
122 Scheme: "https",
123 Host: "example.com",
124 User: userinfo,
125 Path: "/path",
126 }
127
128 expected := "https://example.com/path"
129 assert.Equal(t, expected, testURL.String(), "URL with nil userinfo should not include @")
130 })
131 }