Debian Codegraph @

pkg/util/url_test.go

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