GitLabgitlab-org
cli
main
1package api
3import (
4 "io"
5 "net"
6 "net/http"
7 "net/http/httptest"
8 "slices"
9 "strings"
10 "sync"
11 "sync/atomic"
12 "testing"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18)
20type receivedRequest struct {
21 method string
22 body string
23}
25// redirectingServer answers every request under /moved with a redirect of the
26// given status to the same path without that prefix, the way a proxy in front
27// of an instance moves http to https, and records what reaches the target.
28func redirectingServer(t *testing.T, status int) (*httptest.Server, func() []receivedRequest) {
29 t.Helper()
31 var (
32 mu sync.Mutex
33 received []receivedRequest
34 )
35 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36 if target, moved := strings.CutPrefix(r.URL.RequestURI(), "/moved"); moved {
37 http.Redirect(w, r, "http://"+r.Host+target, status)
38 return
39 }
41 body, err := io.ReadAll(r.Body)
42 if err != nil {
43 t.Errorf("reading the request body: %v", err)
44 }
45 mu.Lock()
46 received = append(received, receivedRequest{method: r.Method, body: string(body)})
47 mu.Unlock()
49 w.Header().Set("Content-Type", "application/json")
50 if r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/releases") {
51 _, _ = io.WriteString(w, `[]`)
52 return
53 }
54 _, _ = io.WriteString(w, `{"tag_name":"v1.0.0","name":"v1.0.0"}`)
55 }))
56 t.Cleanup(server.Close)
58 return server, func() []receivedRequest {
59 mu.Lock()
60 defer mu.Unlock()
61 return slices.Clone(received)
62 }
63}
65func newRedirectTestClient(t *testing.T, baseURL string) *Client {
66 t.Helper()
68 client, err := NewClient(
69 func(*http.Client) (gitlab.AuthSource, error) {
70 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
71 },
72 WithBaseURL(baseURL),
73 )
74 require.NoError(t, err)
75 return client
76}
78func TestHTTPClient_RefusesRedirectThatTurnsCreateIntoGet(t *testing.T) {
79 t.Parallel()
81 server, received := redirectingServer(t, http.StatusMovedPermanently)
82 client := newRedirectTestClient(t, server.URL+"/moved/api/v4")
84 _, _, err := client.Lab().Releases.CreateRelease("group/project", &gitlab.CreateReleaseOptions{
85 TagName: new("v1.0.0"),
86 }, gitlab.WithContext(t.Context()))
88 require.ErrorContains(t, err, "refusing to follow the 301 redirect from "+server.URL+"/moved/api/v4/projects/group%2Fproject/releases")
89 require.ErrorContains(t, err, "would resend this POST request as a GET without its body")
90 assert.Empty(t, received())
91}
93func TestHTTPClient_RefusesRedirectThatTurnsUpdateIntoGet(t *testing.T) {
94 t.Parallel()
96 server, received := redirectingServer(t, http.StatusFound)
97 client := newRedirectTestClient(t, server.URL+"/moved/api/v4")
99 // Without the policy the GET reaches the release, which decodes into the
100 // expected type, so the update reports success without having been made.
101 _, _, err := client.Lab().Releases.UpdateRelease("group/project", "v1.0.0", &gitlab.UpdateReleaseOptions{
102 Name: new("renamed"),
103 }, gitlab.WithContext(t.Context()))
105 require.ErrorContains(t, err, "refusing to follow the 302 redirect")
106 require.ErrorContains(t, err, "would resend this PUT request as a GET without its body")
107 assert.Empty(t, received())
108}
110func TestHTTPClient_FollowsRedirectOfGet(t *testing.T) {
111 t.Parallel()
113 server, received := redirectingServer(t, http.StatusMovedPermanently)
114 client := newRedirectTestClient(t, server.URL+"/moved/api/v4")
116 release, _, err := client.Lab().Releases.GetRelease("group/project", "v1.0.0", gitlab.WithContext(t.Context()))
118 require.NoError(t, err)
119 assert.Equal(t, "v1.0.0", release.TagName)
120 assert.Equal(t, []receivedRequest{{method: http.MethodGet, body: ""}}, received())
121}
123func TestHTTPClient_FollowsRedirectThatKeepsTheMethod(t *testing.T) {
124 t.Parallel()
126 server, received := redirectingServer(t, http.StatusPermanentRedirect)
127 client := newRedirectTestClient(t, server.URL+"/moved/api/v4")
129 release, _, err := client.Lab().Releases.CreateRelease("group/project", &gitlab.CreateReleaseOptions{
130 TagName: new("v1.0.0"),
131 }, gitlab.WithContext(t.Context()))
133 require.NoError(t, err)
134 assert.Equal(t, "v1.0.0", release.TagName)
135 got := received()
136 require.Len(t, got, 1)
137 assert.Equal(t, http.MethodPost, got[0].method)
138 assert.JSONEq(t, `{"tag_name":"v1.0.0"}`, got[0].body)
139}
141func TestHTTPClient_StopsAfterTenRedirects(t *testing.T) {
142 t.Parallel()
144 var hits atomic.Int32
145 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146 hits.Add(1)
147 http.Redirect(w, r, r.URL.Path, http.StatusFound)
148 }))
149 t.Cleanup(server.Close)
150 client := newRedirectTestClient(t, server.URL+"/api/v4")
152 req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, server.URL+"/loop", http.NoBody)
153 require.NoError(t, err)
154 resp, err := client.HTTPClient().Do(req)
155 if resp != nil {
156 require.NoError(t, resp.Body.Close())
157 }
159 require.ErrorContains(t, err, "stopped after 10 redirects")
160 assert.Equal(t, int32(10), hits.Load())
161}
163func TestHTTPClient_MethodChangingRedirectNamesTheAPIProtocol(t *testing.T) {
164 const redirectsToHTTPS = "the server redirects http to https"
166 sameHostHTTPS := func(serverHost string) string { return "https://" + serverHost + "/api/v4/projects" }
168 // The refused location is never dialed, so the https targets need no server.
169 tests := []struct {
170 name string
171 autologin bool
172 location func(serverHost string) string
173 wantHint func(serverHost string) string
174 }{
175 {
176 name: "http to https on the same host",
177 location: sameHostHTTPS,
178 wantHint: func(serverHost string) string {
179 return "; the server redirects http to https, so set api_protocol to https with `glab config set api_protocol https --host " + serverHost + "` or GLAB_API_PROTOCOL=https"
180 },
181 },
182 {
183 name: "http to https on the same host with CI auto-login",
184 autologin: true,
185 location: sameHostHTTPS,
186 wantHint: func(string) string {
187 return "; the server redirects http to https, and CI auto-login reads api_protocol from CI_SERVER_PROTOCOL, so give the instance an https external_url, or set CI_SERVER_PROTOCOL to https in the job"
188 },
189 },
190 {
191 name: "http to https on another host and the same port",
192 location: func(serverHost string) string {
193 _, port, _ := net.SplitHostPort(serverHost)
194 return "https://" + net.JoinHostPort("gitlab.example.com", port) + "/api/v4/projects"
195 },
196 },
197 {
198 name: "http to https on the same host and another port",
199 location: func(serverHost string) string {
200 hostname, _, _ := net.SplitHostPort(serverHost)
201 return "https://" + hostname + "/api/v4/projects"
202 },
203 },
204 {
205 name: "same scheme and host",
206 location: func(serverHost string) string { return "http://" + serverHost + "/users/sign_in" },
207 },
208 }
210 for _, tc := range tests {
211 t.Run(tc.name, func(t *testing.T) {
212 autologin := ""
213 if tc.autologin {
214 autologin = "true"
215 }
216 t.Setenv("GLAB_ENABLE_CI_AUTOLOGIN", autologin)
217 t.Setenv("GITLAB_CI", autologin)
219 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
220 http.Redirect(w, r, tc.location(r.Host), http.StatusMovedPermanently)
221 }))
222 t.Cleanup(server.Close)
223 client := newRedirectTestClient(t, server.URL+"/api/v4")
225 req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, server.URL+"/api/v4/projects", strings.NewReader(`{}`))
226 require.NoError(t, err)
227 resp, err := client.HTTPClient().Do(req)
228 if resp != nil {
229 require.NoError(t, resp.Body.Close())
230 }
232 require.ErrorContains(t, err, "would resend this POST request as a GET")
233 if tc.wantHint == nil {
234 assert.NotContains(t, err.Error(), redirectsToHTTPS)
235 return
236 }
237 assert.True(t, strings.HasSuffix(err.Error(), tc.wantHint(server.Listener.Addr().String())), "the error does not end with the hint: %v", err)
238 })
239 }
240}
242// A host without a port reaches 443 over https, so the hint is given whether
243// the redirect leaves the port out or writes it, as Omnibus GitLab does with
244// https://<host>:443.
245func TestRefuseMethodChangingRedirect_NamesTheAPIProtocolForTheDefaultPort(t *testing.T) {
246 t.Setenv("GLAB_ENABLE_CI_AUTOLOGIN", "")
248 tests := []struct {
249 name string
250 location string
251 }{
252 {name: "port left out", location: "https://gitlab.example.com/api/v4/projects"},
253 {name: "port 443 written out", location: "https://gitlab.example.com:443/api/v4/projects"},
254 }
256 for _, tc := range tests {
257 t.Run(tc.name, func(t *testing.T) {
258 original, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "http://gitlab.example.com/api/v4/projects", http.NoBody)
259 require.NoError(t, err)
260 req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, tc.location, http.NoBody)
261 require.NoError(t, err)
262 req.Response = &http.Response{StatusCode: http.StatusMovedPermanently}
264 err = refuseMethodChangingRedirect(req, []*http.Request{original})
266 require.ErrorContains(t, err, "`glab config set api_protocol https --host gitlab.example.com` or GLAB_API_PROTOCOL=https")
267 })
268 }
269}
Diagnostics6 git calls · 24 ms
CallStatusSizeTime024 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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=3 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 · hit3 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=3 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:02001.1 KiB · 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=1165 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb 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=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetch2002.6 KiB · hit16 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=16 git.response.bytes=2705 git.response.cache=HIT git.response.duration_ms=16 git.fetch.wants=dfcb77596441973098dcb1e3d30b4be248eae9c7 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=2705 git.pkt.payload.max=2678 git.sideband.band1.bytes=2678 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
- a5e13a0b887b18aa
- Request
- 975c4e0a…
- 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