1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package api
import (
"testing"
)
func TestScheme(t *testing.T) {
tests := []struct {
name string
scheme string
expected string
}{
{"http scheme", "http", "http"},
{"https scheme", "https", "https"},
{"empty scheme", "", ""},
{"custom scheme", "ftp", "ftp"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL()
result := u.Scheme(tt.scheme)
if u.URL.Scheme != tt.expected {
t.Errorf("Expected scheme %q, got %q", tt.expected, u.URL.Scheme)
}
// Test chaining - should return the same instance
if result != u {
t.Error("Scheme() should return the same instance for chaining")
}
})
}
}
func TestHost(t *testing.T) {
tests := []struct {
name string
host string
expected string
}{
{"simple host", "example.com", "example.com"},
{"host with port", "example.com:8080", "example.com:8080"},
{"empty host", "", ""},
{"IP address", "192.168.1.1", "192.168.1.1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL().Host(tt.host)
if u.URL.Host != tt.expected {
t.Errorf("Expected host %q, got %q", tt.expected, u.URL.Host)
}
})
}
}
func TestPath(t *testing.T) {
tests := []struct {
name string
pathParts []string
expectedPath string
expectedRaw string
}{
{
name: "empty path parts",
pathParts: []string{},
expectedPath: "",
expectedRaw: "",
},
{
name: "single empty path part",
pathParts: []string{""},
expectedPath: "/",
expectedRaw: "/",
},
{
name: "single path part",
pathParts: []string{"networks"},
expectedPath: "/networks",
expectedRaw: "/networks",
},
{
name: "multiple path parts",
pathParts: []string{"1.0", "networks", "my-network"},
expectedPath: "/1.0/networks/my-network",
expectedRaw: "/1.0/networks/my-network",
},
{
name: "path with slash",
pathParts: []string{"networks", "name-with-/-in-it"},
expectedPath: "/networks/name-with-/-in-it",
expectedRaw: "/networks/name-with-%2F-in-it",
},
{
name: "path with percent",
pathParts: []string{"networks", "name-with-%-in-it"},
expectedPath: "/networks/name-with-%-in-it",
expectedRaw: "/networks/name-with-%25-in-it",
},
{
name: "path with space",
pathParts: []string{"networks", "my network"},
expectedPath: "/networks/my network",
expectedRaw: "/networks/my%20network",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL().Path(tt.pathParts...)
if u.URL.Path != tt.expectedPath {
t.Errorf("Expected path %q, got %q", tt.expectedPath, u.URL.Path)
}
if u.RawPath != tt.expectedRaw {
t.Errorf("Expected raw path %q, got %q", tt.expectedRaw, u.RawPath)
}
})
}
}
func TestProject(t *testing.T) {
tests := []struct {
name string
projectName string
shouldHaveQuery bool
expectedValue string
}{
{
name: "empty project name",
projectName: "",
shouldHaveQuery: false,
},
{
name: "default project",
projectName: "default",
shouldHaveQuery: false,
},
{
name: "custom project",
projectName: "my-project",
shouldHaveQuery: true,
expectedValue: "my-project",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL().Project(tt.projectName)
query := u.Query()
if tt.shouldHaveQuery {
if !query.Has("project") {
t.Error("Expected query to have 'project' parameter")
}
if query.Get("project") != tt.expectedValue {
t.Errorf("Expected project value %q, got %q", tt.expectedValue, query.Get("project"))
}
} else {
if query.Has("project") {
t.Errorf("Expected query to not have 'project' parameter, but got %q", query.Get("project"))
}
}
})
}
}
func TestTarget(t *testing.T) {
tests := []struct {
name string
clusterMember string
shouldHaveQuery bool
expectedValue string
}{
{
name: "empty target",
clusterMember: "",
shouldHaveQuery: false,
},
{
name: "none target",
clusterMember: "none",
shouldHaveQuery: false,
},
{
name: "custom target",
clusterMember: "member1",
shouldHaveQuery: true,
expectedValue: "member1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL().Target(tt.clusterMember)
query := u.Query()
if tt.shouldHaveQuery {
if !query.Has("target") {
t.Error("Expected query to have 'target' parameter")
}
if query.Get("target") != tt.expectedValue {
t.Errorf("Expected target value %q, got %q", tt.expectedValue, query.Get("target"))
}
} else {
if query.Has("target") {
t.Errorf("Expected query to not have 'target' parameter, but got %q", query.Get("target"))
}
}
})
}
}
func TestWithQuery(t *testing.T) {
tests := []struct {
name string
key string
value string
expectedKey string
expectedValue string
}{
{
name: "simple query",
key: "foo",
value: "bar",
expectedKey: "foo",
expectedValue: "bar",
},
{
name: "empty value",
key: "empty",
value: "",
expectedKey: "empty",
expectedValue: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := NewURL().WithQuery(tt.key, tt.value)
query := u.Query()
if !query.Has(tt.expectedKey) {
t.Errorf("Expected query to have key %q", tt.expectedKey)
}
if query.Get(tt.expectedKey) != tt.expectedValue {
t.Errorf("Expected value %q for key %q, got %q", tt.expectedValue, tt.expectedKey, query.Get(tt.expectedKey))
}
})
}
// Test multiple query parameters
t.Run("multiple query parameters", func(t *testing.T) {
u := NewURL().
WithQuery("foo", "bar").
WithQuery("recursion", "1")
query := u.Query()
if query.Get("foo") != "bar" {
t.Errorf("Expected foo=bar, got %q", query.Get("filter"))
}
if query.Get("recursion") != "1" {
t.Errorf("Expected recursion=1, got %q", query.Get("recursion"))
}
})
}
func TestURLString(t *testing.T) {
tests := []struct {
name string
setup func() *URL
expected string
}{
{
name: "full URL with all components",
setup: func() *URL {
return NewURL().
Scheme("https").
Host("example.com").
Path("1.0", "networks", "test-network").
Project("my-project").
Target("member1")
},
expected: "https://example.com/1.0/networks/test-network?project=my-project&target=member1",
},
{
name: "URL with encoded path",
setup: func() *URL {
return NewURL().
Scheme("https").
Host("example.com").
Path("1.0", "networks", "name-with-/-in-it")
},
expected: "https://example.com/1.0/networks/name-with-%2F-in-it",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := tt.setup()
actual := u.String()
if actual != tt.expected {
t.Errorf("Expected URL %q, got %q", tt.expected, actual)
}
})
}
}
|