Repository navigation
Expand file tree
/
Copy pathurl_test.go
More file actions
162 lines (155 loc) · 3.82 KB
/
Copy pathurl_test.go
File metadata and controls
162 lines (155 loc) · 3.82 KB
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
package github
import (
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestParseGitPseudoURLSuccess(t *testing.T) {
testCases := []struct {
name string
inURL string
wantGU GitURL
}{
{
name: "valid SSH URL",
inURL: "git@github.com:Pix4D/go-kit.git",
wantGU: GitURL{
URL: &url.URL{
Scheme: "ssh",
User: url.User("git"),
Host: "github.com",
Path: "/Pix4D/go-kit.git",
},
Owner: "Pix4D",
Repo: "go-kit",
FullName: "Pix4D/go-kit",
},
},
{
name: "valid HTTPS URL",
inURL: "https://github.com/Pix4D/go-kit.git",
wantGU: GitURL{
URL: &url.URL{
Scheme: "https",
Host: "github.com",
Path: "/Pix4D/go-kit.git",
},
Owner: "Pix4D",
Repo: "go-kit",
FullName: "Pix4D/go-kit",
},
},
{
name: "valid HTTP URL",
inURL: "http://github.com/Pix4D/go-kit.git",
wantGU: GitURL{
URL: &url.URL{
Scheme: "http",
Host: "github.com",
Path: "/Pix4D/go-kit.git",
},
Owner: "Pix4D",
Repo: "go-kit",
FullName: "Pix4D/go-kit",
},
},
{
name: "valid HTTPS URL with username:password",
inURL: "https://username:password@github.com/Pix4D/go-kit.git",
wantGU: GitURL{
URL: &url.URL{
Scheme: "https",
User: url.UserPassword("username", "password"),
Host: "github.com",
Path: "/Pix4D/go-kit.git",
},
Owner: "Pix4D",
Repo: "go-kit",
FullName: "Pix4D/go-kit",
},
},
{
name: "valid HTTP URL with username:password",
inURL: "http://username:password@github.com/Pix4D/go-kit.git",
wantGU: GitURL{
URL: &url.URL{
Scheme: "http",
User: url.UserPassword("username", "password"),
Host: "github.com",
Path: "/Pix4D/go-kit.git",
},
Owner: "Pix4D",
Repo: "go-kit",
FullName: "Pix4D/go-kit",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gitUrl, err := ParseGitPseudoURL(tc.inURL)
if err != nil {
t.Fatalf("\nhave: %s\nwant: <no error>", err)
}
if diff := cmp.Diff(tc.wantGU, gitUrl, cmp.Comparer(
func(x, y *url.Userinfo) bool {
return x.String() == y.String()
})); diff != "" {
t.Errorf("gitURL: (-want +have):\n%s", diff)
}
})
}
}
func TestParseGitPseudoURLFailure(t *testing.T) {
testCases := []struct {
name string
inURL string
wantErr string
}{
{
name: "totally invalid URL",
inURL: "hello",
wantErr: "invalid git URL hello: missing scheme",
},
{
name: "invalid SSH URL",
inURL: "git@github.com/Pix4D/go-kit.git",
wantErr: "invalid git SSH URL git@github.com/Pix4D/go-kit.git: want exactly one ':'",
},
{
name: "invalid HTTPS URL",
inURL: "https://github.com:Pix4D/go-kit.git",
wantErr: `invalid port ":Pix4D" after host`,
},
{
name: "invalid HTTP URL",
inURL: "http://github.com:Pix4D/go-kit.git",
wantErr: `invalid port ":Pix4D" after host`,
},
{
name: "too few path components",
inURL: "http://github.com/go-kit.git",
wantErr: "invalid git URL: path: want: 3 components; have: 2 [ go-kit.git]",
},
{
name: "too many path components",
inURL: "http://github.com/1/2/go-kit.git",
wantErr: "invalid git URL: path: want: 3 components; have: 4 [ 1 2 go-kit.git]",
},
{
name: "No leaked password in invalid URL with username:password",
inURL: "http://username:password@github.com/Pix4D/go-kit.git\n",
wantErr: `net/url: invalid control character in URL`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := ParseGitPseudoURL(tc.inURL)
if err == nil {
t.Fatalf("\nhave: %v\nwant: %v", "<no error>", tc.wantErr)
}
if have, want := err.Error(), tc.wantErr; have != want {
t.Fatalf("\nhave: %v\nwant: %v", have, want)
}
})
}
}