-
Notifications
You must be signed in to change notification settings - Fork 13
/
credentials_test.go
84 lines (77 loc) · 2.08 KB
/
credentials_test.go
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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"net/url"
"testing"
"github.com/saucelabs/forwarder/log/stdlog"
)
func TestUserInfoMatcherMatch(t *testing.T) {
tests := []struct {
name string
input []string
hostport string
expected *url.Userinfo
}{
{
name: "Matcher is empty",
hostport: "abc:80",
},
{
name: "Matches hostport",
input: []string{"user:pass@abc:80", "foo:pass@*:80", "bar:pass@abc:0", "baz:pass@*:0"},
hostport: "abc:80",
expected: url.UserPassword("user", "pass"),
},
{
name: "Matches host wildcard",
input: []string{"user:pass@abc:80", "foo:pass@*:80", "bar:pass@abc:0", "baz:pass@*:0"},
hostport: "xxx:80",
expected: url.UserPassword("foo", "pass"),
},
{
name: "Matches port wildcard",
input: []string{"user:pass@abc:80", "foo:pass@*:80", "bar:pass@abc:0", "baz:pass@*:0"},
hostport: "abc:90",
expected: url.UserPassword("bar", "pass"),
},
{
name: "Matches global wildcard",
input: []string{"user:pass@abc:80", "foo:pass@*:80", "bar:pass@abc:0", "baz:pass@*:0"},
hostport: "xxx:443",
expected: url.UserPassword("baz", "pass"),
},
{
name: "Matches port '*'",
input: []string{"user:pass@abc:*"},
hostport: "abc:80",
expected: url.UserPassword("user", "pass"),
},
}
for i := range tests {
tc := tests[i]
t.Run(tc.name, func(t *testing.T) {
var (
credentials = make([]*HostPortUser, len(tc.input))
err error
)
for i := range tc.input {
credentials[i], err = ParseHostPortUser(tc.input[i])
if err != nil {
t.Fatal(err)
}
}
m, err := NewCredentialsMatcher(credentials, stdlog.Default())
if err != nil {
t.Fatal(err)
}
u := m.Match(tc.hostport)
if u.String() != tc.expected.String() {
t.Fatalf("expected %s, got %s", tc.expected, u)
}
})
}
}