GitLabgitlab-org

cli

main
ply6 git calls367 ms97 KiB3 cachedprotocol v2
267 lines5.8 KiBRaw
1//go:build !integration
3package git
5import (
6 "testing"
7)
9func TestIsValidURL(t *testing.T) {
10 tests := []struct {
11 name string
12 url string
13 want bool
14 }{
15 {
16 name: "scp-like",
17 url: "git@example.com:owner/repo",
18 want: true,
19 },
20 {
21 name: "scp-like with no user",
22 url: "example.com:owner/repo",
23 want: false,
24 },
25 {
26 name: "ssh",
27 url: "ssh://git@example.com/owner/repo",
28 want: true,
29 },
30 {
31 name: "git",
32 url: "git://example.com/owner/repo",
33 want: true,
34 },
35 {
36 name: "https",
37 url: "https://example.com/owner/repo.git",
38 want: true,
39 },
40 {
41 // URL schemes are case-insensitive (RFC 3986).
42 name: "uppercase HTTPS scheme",
43 url: "HTTPS://example.com/owner/repo.git",
44 want: true,
45 },
46 {
47 name: "no protocol",
48 url: "example.com/owner/repo",
49 want: false,
50 },
51 }
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 if got := IsValidURL(tt.url); got != tt.want {
55 t.Errorf("IsValidURL() = %v, want %v", got, tt.want)
56 }
57 })
58 }
61func TestParseURL(t *testing.T) {
62 type url struct {
63 Scheme string
64 User string
65 Host string
66 Path string
67 }
68 tests := []struct {
69 name string
70 url string
71 want url
72 wantErr string
73 }{
74 {
75 name: "HTTPS",
76 url: "https://example.com/owner/repo.git",
77 want: url{
78 Scheme: "https",
79 User: "",
80 Host: "example.com",
81 Path: "/owner/repo.git",
82 },
83 },
84 {
85 name: "HTTP",
86 url: "http://example.com/owner/repo.git",
87 want: url{
88 Scheme: "http",
89 User: "",
90 Host: "example.com",
91 Path: "/owner/repo.git",
92 },
93 },
94 {
95 // An uppercase scheme is valid (RFC 3986) and must not be mistaken
96 // for scp-style SSH, which would make the host "HTTPS".
97 name: "uppercase HTTPS scheme",
98 url: "HTTPS://example.com/owner/repo.git",
99 want: url{
100 Scheme: "https",
101 User: "",
102 Host: "example.com",
103 Path: "/owner/repo.git",
104 },
105 },
106 {
107 name: "git",
108 url: "git://example.com/owner/repo.git",
109 want: url{
110 Scheme: "git",
111 User: "",
112 Host: "example.com",
113 Path: "/owner/repo.git",
114 },
115 },
116 {
117 name: "ssh",
118 url: "ssh://git@example.com/owner/repo.git",
119 want: url{
120 Scheme: "ssh",
121 User: "git",
122 Host: "example.com",
123 Path: "/owner/repo.git",
124 },
125 },
126 {
127 name: "ssh with port",
128 url: "ssh://git@example.com:443/owner/repo.git",
129 want: url{
130 Scheme: "ssh",
131 User: "git",
132 Host: "example.com",
133 Path: "/owner/repo.git",
134 },
135 },
136 {
137 name: "git+ssh",
138 url: "git+ssh://example.com/owner/repo.git",
139 want: url{
140 Scheme: "ssh",
141 User: "",
142 Host: "example.com",
143 Path: "/owner/repo.git",
144 },
145 },
146 {
147 name: "scp-like",
148 url: "git@example.com:owner/repo.git",
149 want: url{
150 Scheme: "ssh",
151 User: "git",
152 Host: "example.com",
153 Path: "/owner/repo.git",
154 },
155 },
156 {
157 name: "scp-like, leading slash",
158 url: "git@example.com:/owner/repo.git",
159 want: url{
160 Scheme: "ssh",
161 User: "git",
162 Host: "example.com",
163 Path: "/owner/repo.git",
164 },
165 },
166 {
167 name: "file protocol",
168 url: "file:///example.com/owner/repo.git",
169 want: url{
170 Scheme: "file",
171 User: "",
172 Host: "",
173 Path: "/example.com/owner/repo.git",
174 },
175 },
176 {
177 name: "file path",
178 url: "/example.com/owner/repo.git",
179 want: url{
180 Scheme: "",
181 User: "",
182 Host: "",
183 Path: "/example.com/owner/repo.git",
184 },
185 },
186 {
187 name: "Windows file path",
188 url: "C:\\example.com\\owner\\repo.git",
189 want: url{
190 Scheme: "c",
191 User: "",
192 Host: "",
193 Path: "",
194 },
195 },
196 {
197 name: "Invalid URL",
198 url: `git@example.com/%/url`,
199 wantErr: `parse "git@example.com/%/url": invalid URL escape "%/u"`,
200 },
201 }
202 for _, tt := range tests {
203 t.Run(tt.name, func(t *testing.T) {
204 u, err := ParseURL(tt.url)
205 if tt.wantErr != "" {
206 if err.Error() != tt.wantErr {
207 t.Errorf("expected error %s, got %s", tt.wantErr, err.Error())
208 }
209 } else {
210 if err != nil {
211 t.Errorf("unexpected error %s", err)
212 }
213 if u.Scheme != tt.want.Scheme {
214 t.Errorf("expected scheme %q, got %q", tt.want.Scheme, u.Scheme)
215 }
216 if u.User.Username() != tt.want.User {
217 t.Errorf("expected user %q, got %q", tt.want.User, u.User.Username())
218 }
219 if u.Host != tt.want.Host {
220 t.Errorf("expected host %q, got %q", tt.want.Host, u.Host)
221 }
222 if u.Path != tt.want.Path {
223 t.Errorf("expected path %q, got %q", tt.want.Path, u.Path)
224 }
225 }
226 })
227 }
230func TestValidateRemoteURL(t *testing.T) {
231 tests := []struct {
232 url string
233 wantErr bool
234 }{
235 {url: "", wantErr: true},
236 {url: "ext::touch x", wantErr: true},
237 {url: "ext::sh -c touch% /tmp/x", wantErr: true},
238 {url: "fd::3", wantErr: true},
239 {url: "--upload-pack=touch x", wantErr: true},
240 {url: "-oProxyCommand=x", wantErr: true},
241 {url: "file:///tmp/x", wantErr: true},
242 {url: "/tmp/x", wantErr: true},
243 {url: "-host:g/p.git", wantErr: true},
244 {url: "git@-host:g/p.git", wantErr: true},
245 {url: "http::http://x/p.git", wantErr: true},
246 {url: "git@host:g/p.git\n--upload-pack=x", wantErr: true},
247 {url: "https://gitlab.com/g/p.git ", wantErr: true},
248 {url: "ssh://git@host/g\n", wantErr: true},
249 {url: "gitlab_web:g/p.git"},
250 {url: "git@my_host.local:g/p.git"},
251 {url: "git@gitlab.com:g/p.git"},
252 {url: "gitlab.example.com:g/p.git"},
253 {url: "gitlab@host.example.com:g/p.git"},
254 {url: "[git@2001:db8::1]:g/p.git"},
255 {url: "https://gitlab.com/g/p.git"},
256 {url: "HTTPS://gitlab.com/g/p.git"},
257 {url: "ssh://git@host:2222/g/p.git"},
258 {url: "http://localhost:3000/g/p.git"},
259 }
260 for _, tt := range tests {
261 t.Run(tt.url, func(t *testing.T) {
262 if err := ValidateRemoteURL(tt.url); (err != nil) != tt.wantErr {
263 t.Errorf("ValidateRemoteURL(%q) error = %v, wantErr %v", tt.url, err, tt.wantErr)
264 }
265 })
266 }
Diagnostics6 git calls · 367 ms
CallStatusSizeTime0367 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB161 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=159
git.response.bytes=59849
git.response.cache=EXPIRED
git.response.duration_ms=161
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchtree:0200510 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=510
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=a3147172443266d011121808a125248c5c915972
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=510
git.pkt.payload.max=483
git.sideband.band1.bytes=483
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=483
git.pack.trailer=4ef234d86a93490a6a3da3549aa13a474a5c9eac
Open pack map
fetch2001.3 KiB200 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=200
git.response.bytes=1335
git.response.cache=MISS
git.response.duration_ms=200
git.fetch.wants=4165fe9a0a01ab349d94892a12602adee4ddb222
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1335
git.pkt.payload.max=1308
git.sideband.band1.bytes=1308
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e195b67c14b86e
Request
ed269f75…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format