GitLabgitlab-org
cli
main
1//go:build !integration
3package glinstance
5import (
6 "fmt"
7 "testing"
9 "github.com/stretchr/testify/assert"
10)
12func TestIsSelfHosted(t *testing.T) {
13 type args struct {
14 h string
15 }
16 tests := []struct {
17 name string
18 args args
19 want bool
20 }{
21 {
22 name: "self_hosted subdomain",
23 args: args{h: "gitlab.example.com"},
24 want: true,
25 },
26 {
27 name: "gitlab.com",
28 args: args{h: "gitlab.com"},
29 want: false,
30 },
31 {
32 name: "is a gitlab.com subdomain",
33 args: args{h: "example.gitlab.com"},
34 want: true,
35 },
36 {
37 name: "is a gitlab.com staging",
38 args: args{h: "staging.gitlab.com"},
39 want: true,
40 },
41 {
42 name: "self hosted",
43 args: args{h: "example.com"},
44 want: true,
45 },
46 }
47 for _, tt := range tests {
48 t.Run(tt.name, func(t *testing.T) {
49 if got := IsSelfHosted(tt.args.h); got != tt.want {
50 t.Errorf("IsSelfHosted() = %v, want %v", got, tt.want)
51 }
52 })
53 }
54}
56func TestNormalizeHostname(t *testing.T) {
57 tests := []struct {
58 host string
59 want string
60 }{
61 {
62 host: "GitLab.com",
63 want: "gitlab.com",
64 },
65 {
66 host: "subdomain.gitlab.com",
67 want: "subdomain.gitlab.com",
68 },
69 {
70 host: "ssh.gitlab.com",
71 want: "ssh.gitlab.com",
72 },
73 {
74 host: "upload.gitlab.com",
75 want: "upload.gitlab.com",
76 },
77 {
78 host: "staging.gitlab.com",
79 want: "staging.gitlab.com",
80 },
81 {
82 host: "EXAMPLE.COM",
83 want: "example.com",
84 },
85 {
86 host: "gitlab.my.org",
87 want: "gitlab.my.org",
88 },
89 }
90 for _, tt := range tests {
91 t.Run(tt.host, func(t *testing.T) {
92 if got := NormalizeHostname(tt.host); got != tt.want {
93 t.Errorf("NormalizeHostname() = %v, want %v", got, tt.want)
94 }
95 })
96 }
97}
99func TestAPIEndpoint(t *testing.T) {
100 tests := []struct {
101 host string
102 apiHost string
103 subfolder string
104 protocol string
105 want string
106 }{
107 {
108 host: "gitlab.com",
109 protocol: "https",
110 want: "https://gitlab.com/api/v4/",
111 },
112 {
113 host: "fake-host.com",
114 protocol: "https",
115 apiHost: "api.fake-host.com",
116 want: "https://api.fake-host.com/api/v4/",
117 },
118 {
119 host: "staging.gitlab.com",
120 protocol: "https",
121 want: "https://staging.gitlab.com/api/v4/",
122 },
123 {
124 host: "ghe.io",
125 protocol: "https",
126 want: "https://ghe.io/api/v4/",
127 },
128 {
129 host: "ghe.io",
130 want: "https://ghe.io/api/v4/",
131 },
132 {
133 host: "salsa.debian.com",
134 protocol: "http",
135 want: "http://salsa.debian.com/api/v4/",
136 },
137 {
138 host: "salsa.debian.com",
139 protocol: "http",
140 apiHost: "api.salsa.debian.com",
141 want: "http://api.salsa.debian.com/api/v4/",
142 },
143 {
144 host: "myserver.net",
145 protocol: "http",
146 apiHost: "myserver.net/gitlab",
147 want: "http://myserver.net/gitlab/api/v4/",
148 },
149 }
150 for i, tt := range tests {
151 t.Run(fmt.Sprintf("%d - %s", i, tt.host), func(t *testing.T) {
152 if got := APIEndpoint(tt.host, tt.protocol, tt.apiHost, tt.subfolder); got != tt.want {
153 t.Errorf("APIEndpoint() = %v, want %v", got, tt.want)
154 }
155 })
156 }
157}
159func TestDefault(t *testing.T) {
160 tests := []struct {
161 name string
162 want string
163 }{
164 {
165 name: "default_hostname",
166 want: "gitlab.com",
167 },
168 }
169 for _, tt := range tests {
170 t.Run(tt.name, func(t *testing.T) {
171 if got := DefaultHostname; got != tt.want {
172 t.Errorf("Default() = %v, want %v", got, tt.want)
173 }
174 })
175 }
176}
178func TestStripHostProtocol(t *testing.T) {
179 tests := []struct {
180 name string
181 hostname string
182 wantHostname string
183 wantProtocol string
184 }{
185 {
186 name: "url with https protocol",
187 hostname: "https://gitlab.com",
188 wantHostname: "gitlab.com",
189 wantProtocol: "https",
190 },
191 {
192 name: "https url with ending slash",
193 hostname: "https://gitlab.com/",
194 wantHostname: "gitlab.com",
195 wantProtocol: "https",
196 },
197 {
198 name: "url with http protocol",
199 hostname: "http://gitlab.com/",
200 wantHostname: "gitlab.com",
201 wantProtocol: "http",
202 },
203 {
204 name: "http url with ending slash",
205 hostname: "http://gitlab.com/",
206 wantHostname: "gitlab.com",
207 wantProtocol: "http",
208 },
209 }
210 for _, tt := range tests {
211 t.Run(tt.name, func(t *testing.T) {
212 gotHostname, gotProtocol := StripHostProtocol(tt.hostname)
213 if gotHostname != tt.wantHostname {
214 t.Errorf("StripHostProtocol() gotHostname = %v, want %v", gotHostname, tt.wantHostname)
215 }
216 if gotProtocol != tt.wantProtocol {
217 t.Errorf("StripHostProtocol() gotProtocol = %v, want %v", gotProtocol, tt.wantProtocol)
218 }
219 })
220 }
221}
223func TestHostnameValidator(t *testing.T) {
224 testCases := []struct {
225 name string
226 hostname any
227 expected string
228 }{
229 {
230 name: "valid",
231 hostname: "localhost",
232 },
233 {
234 name: "invalid/not-string",
235 hostname: 1,
236 expected: "hostname is not a string",
237 },
238 {
239 name: "invalid/empty-string",
240 hostname: "",
241 expected: "a value is required",
242 },
243 {
244 name: "invalid/has-forward-slash",
245 hostname: "local/host",
246 expected: "invalid hostname",
247 },
248 {
249 name: "invalid/has-colon",
250 hostname: "local:host",
251 expected: "invalid hostname",
252 },
253 }
254 for _, tC := range testCases {
255 t.Run(tC.name, func(t *testing.T) {
256 err := HostnameValidator(tC.hostname)
257 if tC.expected == "" {
258 assert.NoError(t, err)
259 } else {
260 assert.Error(t, err, tC.expected)
261 }
262 })
263 }
264}
266func Test_GraphQLEndpoint(t *testing.T) {
267 testCases := []struct {
268 name string
269 protocol string
270 hostname string
271 apiHost string
272 subfolder string
273 output string
274 }{
275 {
276 name: "OfficialInstance/https",
277 protocol: "https",
278 hostname: "gitlab.com",
279 output: "https://gitlab.com/api/graphql/",
280 },
281 {
282 name: "OfficialInstance/any-protocol-is-https",
283 protocol: "NoExistProtocol",
284 hostname: "gitlab.com",
285 output: "https://gitlab.com/api/graphql/",
286 },
287 {
288 name: "OfficialInstance/no-protocol-default-to-https",
289 protocol: "",
290 hostname: "gitlab.com",
291 output: "https://gitlab.com/api/graphql/",
292 },
293 {
294 name: "SelfHosted/https",
295 protocol: "https",
296 hostname: "gitlab.alpinelinux.org",
297 output: "https://gitlab.alpinelinux.org/api/graphql/",
298 },
299 {
300 name: "SelfHost/http",
301 protocol: "http",
302 hostname: "gitlab.alpinelinux.org",
303 output: "http://gitlab.alpinelinux.org/api/graphql/",
304 },
305 {
306 name: "SelfHosted/no-protocol-default-to-https",
307 protocol: "",
308 hostname: "gitlab.alpinelinux.org",
309 output: "https://gitlab.alpinelinux.org/api/graphql/",
310 },
311 }
312 for _, tC := range testCases {
313 t.Run(tC.name, func(t *testing.T) {
314 got := GraphQLEndpoint(tC.hostname, tC.protocol, tC.apiHost, tC.subfolder)
315 assert.Equal(t, tC.output, got)
316 })
317 }
318}
320func TestAuthEndpoint(t *testing.T) {
321 t.Parallel()
323 assert.Equal(t, "https://gitlab.example.com", AuthEndpoint("gitlab.example.com", "", ""))
324 assert.Equal(t, "http://gitlab.example.com/gitlab", AuthEndpoint("gitlab.example.com", "http", "/gitlab/"))
325 assert.Equal(t, "https://gitlab.example.com/apps/gitlab", AuthEndpoint("gitlab.example.com", "https", "apps/gitlab"))
326}
Diagnostics6 git calls · 30 ms
CallStatusSizeTime030 ms
discover200185 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 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=44Open pack map
fetchtree:0200134 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=134 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=be5e976e967e5e96675c5ec40a6a21baa01a7554 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=134 git.pkt.payload.max=107 git.sideband.band1.bytes=107 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=107 git.pack.trailer=43619d0d925e615aae4d175fe8dbb5b1616686c7Open pack map
fetch2001.4 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1476 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=7459152a78aeaa084b34c9b8c631299fe59d9673 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=1476 git.pkt.payload.max=1449 git.sideband.band1.bytes=1449 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
- a5e228f098c9a151
- Request
- 4a804bf0…
- 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