GitLabgitlab-org

cli

main
ply7 git calls36 ms116 KiB5 cachedprotocol v2
3024 lines97 KiBRaw
1//go:build !integration
3package api
5import (
6 "bytes"
7 "cmp"
8 "encoding/json"
9 "errors"
10 "fmt"
11 "io"
12 "net/http"
13 "net/url"
14 "os"
15 "slices"
16 "strings"
17 "testing"
19 "github.com/google/shlex"
20 "github.com/stretchr/testify/assert"
21 "github.com/stretchr/testify/require"
23 "gitlab.com/gitlab-org/cli/internal/api"
24 "gitlab.com/gitlab-org/cli/internal/cmdutils"
25 "gitlab.com/gitlab-org/cli/internal/git"
26 "gitlab.com/gitlab-org/cli/internal/glinstance"
27 "gitlab.com/gitlab-org/cli/internal/glrepo"
28 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
31// rawFields and magicFields build the ordered field list the flags produce;
32// slices.Concat of the two spells out an order they were interleaved in.
33func rawFields(specs ...string) []fieldFlag { return fieldFlags(specs, true) }
35func magicFields(specs ...string) []fieldFlag { return fieldFlags(specs, false) }
37func fieldFlags(specs []string, raw bool) []fieldFlag {
38 flags := make([]fieldFlag, len(specs))
39 for i, spec := range specs {
40 flags[i] = fieldFlag{spec: spec, raw: raw}
41 }
42 return flags
45// flagLine spells a field list the way it was typed, so a table whose cases are
46// its flags can name its subtests with them.
47func flagLine(fields []fieldFlag) string {
48 parts := make([]string, len(fields))
49 for i, f := range fields {
50 flag := "-F"
51 if f.raw {
52 flag = "-f"
53 }
54 parts[i] = flag + " '" + f.spec + "'"
55 }
56 return strings.Join(parts, " ")
59// sent is what one api run put on the wire.
60type sent struct {
61 req *http.Request
62 body []byte
65// parseFieldsAt runs a field list through the seam apiRun reaches parseFields
66// through: the method and --input together decide whether the fields become a
67// JSON body, which is what makes a name holding a bracket an error. An empty
68// method leaves --method unset, as apiRun sees it with no -X. The returned flag
69// is that decision, so a case can assert which path its fields took.
70func parseFieldsAt(t *testing.T, method, input string, fields []fieldFlag) (map[string]any, bool, error) {
71 t.Helper()
73 ios, stdin, _, _ := cmdtest.TestIOStreams()
74 _, _ = stdin.WriteString("RAW BODY")
76 opts := options{io: ios, requestMethod: http.MethodGet, requestInputFile: input, fields: fields}
77 if method != "" {
78 opts.requestMethod, opts.requestMethodPassed = method, true
79 }
80 jsonBody := opts.fieldsBecomeJSONBody(opts.methodForRequest())
81 params, _, err := parseFields(&opts, jsonBody)
82 return params, jsonBody, err
85// runAPIArgv runs the command from an argv string, so cobra's flag parsing is
86// part of what the case exercises: the order two field flags interleave in, and
87// which of them overrides the other, are decided nowhere else. Stdin holds
88// "RAW BODY" so a case can pass --input -. tr answers the request.
89func runAPIArgv(t *testing.T, cli string, tr roundTripFunc) error {
90 t.Helper()
92 ios, stdin, _, _ := cmdtest.TestIOStreams()
93 _, _ = stdin.WriteString("RAW BODY")
94 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
95 cmd := NewCmdApi(cmdtest.NewTestFactory(ios, cmdtest.WithApiClient(a)), nil)
97 argv, err := shlex.Split(cli)
98 require.NoError(t, err)
99 cmd.SetArgs(argv)
100 cmd.SetOut(&bytes.Buffer{})
101 cmd.SetErr(&bytes.Buffer{})
102 _, err = cmd.ExecuteC()
103 return err
106// runAPIArgvRecording records the request and its body, and answers 204.
107func runAPIArgvRecording(t *testing.T, cli string) (sent, error) {
108 t.Helper()
110 var got sent
111 err := runAPIArgv(t, cli, func(req *http.Request) (*http.Response, error) {
112 if req.Body != nil {
113 var readErr error
114 if got.body, readErr = io.ReadAll(req.Body); readErr != nil {
115 return nil, readErr
116 }
117 }
118 got.req = req
119 return &http.Response{StatusCode: http.StatusNoContent, Request: req}, nil
120 })
121 return got, err
124// runAPIArgvGuarded runs against a transport that fails the test if any request
125// is issued at all. That is the whole assertion for a case proving the error
126// fires before anything leaves; a canned response would hide it.
127func runAPIArgvGuarded(t *testing.T, cli string) error {
128 t.Helper()
130 return runAPIArgv(t, cli, func(req *http.Request) (*http.Response, error) {
131 t.Error("no request should be made")
132 return nil, fmt.Errorf("not supposed to be called")
133 })
136func Test_fieldFlagValue_Set(t *testing.T) {
137 newPair := func(seed []fieldFlag) (*[]fieldFlag, *fieldFlagValue, *fieldFlagValue) {
138 fields := seed
139 list := &fieldFlagList{fields: &fields}
140 return &fields, &fieldFlagValue{list: list}, &fieldFlagValue{list: list, raw: true}
141 }
143 t.Run("the first value replaces a default and later values append", func(t *testing.T) {
144 fields, magic, raw := newPair(rawFields("default=1"))
145 require.NoError(t, magic.Set("a=1"))
146 require.NoError(t, raw.Set("b=2"))
147 require.NoError(t, magic.Set("c=3"))
148 assert.Equal(t, slices.Concat(magicFields("a=1"), rawFields("b=2"), magicFields("c=3")), *fields)
149 })
151 t.Run("either flag may be the one that replaces the default", func(t *testing.T) {
152 fields, magic, raw := newPair(magicFields("default=1"))
153 require.NoError(t, raw.Set("a=1"))
154 require.NoError(t, magic.Set("b=2"))
155 assert.Equal(t, slices.Concat(rawFields("a=1"), magicFields("b=2")), *fields)
156 })
158 t.Run("String reports only its own flag's values", func(t *testing.T) {
159 _, magic, raw := newPair(nil)
160 assert.Empty(t, magic.String())
161 assert.Empty(t, raw.String())
162 require.NoError(t, magic.Set("a=1"))
163 require.NoError(t, raw.Set("b=2"))
164 require.NoError(t, magic.Set("c=3"))
165 assert.Equal(t, "[a=1,c=3]", magic.String())
166 assert.Equal(t, "[b=2]", raw.String())
167 assert.Equal(t, "stringArray", magic.Type())
168 })
171func Test_NewCmdApi(t *testing.T) {
172 ios, _, _, _ := cmdtest.TestIOStreams()
173 f := cmdtest.NewTestFactory(ios)
175 tests := []struct {
176 name string
177 cli string
178 wants options
179 wantsErr bool
180 }{
181 {
182 name: "no flags",
183 cli: "graphql",
184 wants: options{
185 hostname: "",
186 requestMethod: http.MethodGet,
187 requestMethodPassed: false,
188 requestPath: "graphql",
189 requestInputFile: "",
190 fields: nil,
191 requestHeaders: []string(nil),
192 showResponseHeaders: false,
193 paginate: false,
194 silent: false,
195 },
196 wantsErr: false,
197 },
198 {
199 name: "override method",
200 cli: "projects/octocat%2FSpoon-Knife -XDELETE",
201 wants: options{
202 hostname: "",
203 requestMethod: http.MethodDelete,
204 requestMethodPassed: true,
205 requestPath: "projects/octocat%2FSpoon-Knife",
206 requestInputFile: "",
207 fields: nil,
208 requestHeaders: []string(nil),
209 showResponseHeaders: false,
210 paginate: false,
211 silent: false,
212 },
213 wantsErr: false,
214 },
215 {
216 // Array element order depends on this reaching the parser intact.
217 name: "with fields interleaved between the two flags",
218 cli: "graphql -F a=1 -f b=2 -F c=3",
219 wants: options{
220 hostname: "",
221 requestMethod: http.MethodGet,
222 requestMethodPassed: false,
223 requestPath: "graphql",
224 requestInputFile: "",
225 fields: slices.Concat(magicFields("a=1"), rawFields("b=2"), magicFields("c=3")),
226 requestHeaders: []string(nil),
227 showResponseHeaders: false,
228 paginate: false,
229 silent: false,
230 },
231 wantsErr: false,
232 },
233 {
234 name: "with fields",
235 cli: "graphql -f query=QUERY -F body=@file.txt",
236 wants: options{
237 hostname: "",
238 requestMethod: http.MethodGet,
239 requestMethodPassed: false,
240 requestPath: "graphql",
241 requestInputFile: "",
242 fields: slices.Concat(rawFields("query=QUERY"), magicFields("body=@file.txt")),
243 requestHeaders: []string(nil),
244 showResponseHeaders: false,
245 paginate: false,
246 silent: false,
247 },
248 wantsErr: false,
249 },
250 {
251 name: "with headers",
252 cli: "user -H 'accept: text/plain' -i",
253 wants: options{
254 hostname: "",
255 requestMethod: http.MethodGet,
256 requestMethodPassed: false,
257 requestPath: "user",
258 requestInputFile: "",
259 fields: nil,
260 requestHeaders: []string{"accept: text/plain"},
261 showResponseHeaders: true,
262 paginate: false,
263 silent: false,
264 },
265 wantsErr: false,
266 },
267 {
268 name: "with pagination",
269 cli: "projects/OWNER%2FREPO/issues --paginate",
270 wants: options{
271 hostname: "",
272 requestMethod: http.MethodGet,
273 requestMethodPassed: false,
274 requestPath: "projects/OWNER%2FREPO/issues",
275 requestInputFile: "",
276 fields: nil,
277 requestHeaders: []string(nil),
278 showResponseHeaders: false,
279 paginate: true,
280 silent: false,
281 },
282 wantsErr: false,
283 },
284 {
285 // validate reads the --method default, so fields with no -X reach
286 // run() and are only then inferred to POST.
287 name: "pagination with fields",
288 cli: "projects --paginate -f a=b",
289 wants: options{
290 hostname: "",
291 requestMethod: http.MethodGet,
292 requestMethodPassed: false,
293 requestPath: "projects",
294 requestInputFile: "",
295 fields: rawFields("a=b"),
296 requestHeaders: []string(nil),
297 showResponseHeaders: false,
298 paginate: true,
299 silent: false,
300 },
301 wantsErr: false,
302 },
303 {
304 name: "with silenced output",
305 cli: "projects/OWNER%2FREPO/issues --silent",
306 wants: options{
307 hostname: "",
308 requestMethod: http.MethodGet,
309 requestMethodPassed: false,
310 requestPath: "projects/OWNER%2FREPO/issues",
311 requestInputFile: "",
312 fields: nil,
313 requestHeaders: []string(nil),
314 showResponseHeaders: false,
315 paginate: false,
316 silent: true,
317 },
318 wantsErr: false,
319 },
320 {
321 name: "POST pagination",
322 cli: "-XPOST projects/OWNER%2FREPO/issues --paginate",
323 wantsErr: true,
324 },
325 {
326 name: "GraphQL pagination",
327 cli: "-XPOST graphql --paginate",
328 wants: options{
329 hostname: "",
330 requestMethod: http.MethodPost,
331 requestMethodPassed: true,
332 requestPath: "graphql",
333 requestInputFile: "",
334 fields: nil,
335 requestHeaders: []string(nil),
336 showResponseHeaders: false,
337 paginate: true,
338 silent: false,
339 },
340 wantsErr: false,
341 },
342 {
343 name: "input pagination",
344 cli: "--input projects/OWNER%2FREPO/issues --paginate",
345 wantsErr: true,
346 },
347 {
348 name: "with request body from file",
349 cli: "user --input myfile",
350 wants: options{
351 hostname: "",
352 requestMethod: http.MethodGet,
353 requestMethodPassed: false,
354 requestPath: "user",
355 requestInputFile: "myfile",
356 fields: nil,
357 requestHeaders: []string(nil),
358 showResponseHeaders: false,
359 paginate: false,
360 silent: false,
361 },
362 wantsErr: false,
363 },
364 {
365 name: "no arguments",
366 cli: "",
367 wantsErr: true,
368 },
369 {
370 name: "with hostname",
371 cli: "graphql --hostname tom.petty",
372 wants: options{
373 hostname: "tom.petty",
374 requestMethod: http.MethodGet,
375 requestMethodPassed: false,
376 requestPath: "graphql",
377 requestInputFile: "",
378 fields: nil,
379 requestHeaders: []string(nil),
380 showResponseHeaders: false,
381 paginate: false,
382 silent: false,
383 },
384 wantsErr: false,
385 },
386 {
387 name: "invalid output format",
388 cli: "projects --output xml",
389 wantsErr: true,
390 },
391 {
392 name: "valid ndjson output format",
393 cli: "projects --output ndjson",
394 wants: options{
395 hostname: "",
396 requestMethod: http.MethodGet,
397 requestMethodPassed: false,
398 requestPath: "projects",
399 requestInputFile: "",
400 fields: nil,
401 requestHeaders: []string(nil),
402 showResponseHeaders: false,
403 paginate: false,
404 silent: false,
405 outputFormat: "ndjson",
406 },
407 wantsErr: false,
408 },
409 {
410 name: "with form fields",
411 cli: `projects/:fullpath/wikis/attachments --form "file=@image.png" --form "branch=main"`,
412 wants: options{
413 requestMethod: http.MethodGet,
414 requestMethodPassed: false,
415 requestPath: "projects/:fullpath/wikis/attachments",
416 fields: nil,
417 formFields: []string{"file=@image.png", "branch=main"},
418 requestHeaders: []string(nil),
419 },
420 wantsErr: false,
421 },
422 {
423 name: "form and field are mutually exclusive",
424 cli: `projects --form "file=@foo.png" --field "name=test"`,
425 wantsErr: true,
426 },
427 {
428 name: "form and raw-field are mutually exclusive",
429 cli: `projects --form "file=@foo.png" --raw-field "name=test"`,
430 wantsErr: true,
431 },
432 {
433 name: "form and input are mutually exclusive",
434 cli: `projects --form "file=@foo.png" --input body.json`,
435 wantsErr: true,
436 },
437 {
438 name: "form stdin used twice is invalid",
439 cli: `projects --form "file=@-" --form "other=@-"`,
440 wantsErr: true,
441 },
442 }
443 for _, tt := range tests {
444 t.Run(tt.name, func(t *testing.T) {
445 cmd := NewCmdApi(f, func(o *options) error {
446 assert.Equal(t, tt.wants.hostname, o.hostname)
447 assert.Equal(t, tt.wants.requestMethod, o.requestMethod)
448 assert.Equal(t, tt.wants.requestMethodPassed, o.requestMethodPassed)
449 assert.Equal(t, tt.wants.requestPath, o.requestPath)
450 assert.Equal(t, tt.wants.requestInputFile, o.requestInputFile)
451 assert.Equal(t, tt.wants.fields, o.fields)
452 assert.Equal(t, tt.wants.formFields, o.formFields)
453 assert.Equal(t, tt.wants.requestHeaders, o.requestHeaders)
454 assert.Equal(t, tt.wants.showResponseHeaders, o.showResponseHeaders)
455 return nil
456 })
458 argv, err := shlex.Split(tt.cli)
459 require.NoError(t, err)
460 cmd.SetArgs(argv)
461 cmd.SetIn(&bytes.Buffer{})
462 cmd.SetOut(&bytes.Buffer{})
463 cmd.SetErr(&bytes.Buffer{})
464 _, err = cmd.ExecuteC()
465 if tt.wantsErr {
466 assert.Error(t, err)
467 return
468 }
469 assert.NoError(t, err)
470 })
471 }
474func Test_apiRun(t *testing.T) {
475 tests := []struct {
476 name string
477 options options
478 httpResponse *http.Response
479 err error
480 stdout string
481 stderr string
482 }{
483 {
484 name: "success",
485 httpResponse: &http.Response{
486 StatusCode: http.StatusOK,
487 Body: io.NopCloser(bytes.NewBufferString(`bam!`)),
488 },
489 err: nil,
490 stdout: `bam!`,
491 stderr: ``,
492 },
493 {
494 name: "show response headers",
495 options: options{
496 showResponseHeaders: true,
497 },
498 httpResponse: &http.Response{
499 Proto: "HTTP/1.1",
500 Status: "200 Okey-dokey",
501 StatusCode: http.StatusOK,
502 Body: io.NopCloser(bytes.NewBufferString(`body`)),
503 Header: http.Header{"Content-Type": []string{"text/plain"}},
504 },
505 err: nil,
506 stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\nbody",
507 stderr: ``,
508 },
509 {
510 name: "success 204",
511 httpResponse: &http.Response{
512 StatusCode: http.StatusNoContent,
513 Body: nil,
514 },
515 err: nil,
516 stdout: ``,
517 stderr: ``,
518 },
519 {
520 name: "REST error",
521 httpResponse: &http.Response{
522 StatusCode: http.StatusBadRequest,
523 Body: io.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)),
524 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
525 },
526 err: cmdutils.SilentError,
527 stdout: `{"message": "THIS IS FINE"}`,
528 stderr: "glab: THIS IS FINE (HTTP 400)\n",
529 },
530 {
531 name: "REST string errors",
532 httpResponse: &http.Response{
533 StatusCode: http.StatusBadRequest,
534 Body: io.NopCloser(bytes.NewBufferString(`{"errors": ["ALSO", "FINE"]}`)),
535 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
536 },
537 err: cmdutils.SilentError,
538 stdout: `{"errors": ["ALSO", "FINE"]}`,
539 stderr: "glab: ALSO\nFINE\n",
540 },
541 {
542 name: "REST string errors and we can't unmarshal",
543 httpResponse: &http.Response{
544 StatusCode: http.StatusBadRequest,
545 Body: io.NopCloser(bytes.NewBufferString(`{"message": {"password": ["is too short (minimum is 8 characters)"] } }`)),
546 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
547 },
548 err: cmdutils.SilentError,
549 stdout: `{"message": {"password": ["is too short (minimum is 8 characters)"] } }`,
550 stderr: "glab: map[message:map[password:[is too short (minimum is 8 characters)]]]\n",
551 },
552 {
553 name: "GraphQL error",
554 options: options{
555 requestPath: "graphql",
556 },
557 httpResponse: &http.Response{
558 StatusCode: http.StatusOK,
559 Body: io.NopCloser(bytes.NewBufferString(`{"errors": [{"message":"AGAIN"}, {"message":"FINE"}]}`)),
560 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
561 },
562 err: cmdutils.SilentError,
563 stdout: `{"errors": [{"message":"AGAIN"}, {"message":"FINE"}]}`,
564 stderr: "glab: AGAIN\nFINE\n",
565 },
566 {
567 name: "failure",
568 httpResponse: &http.Response{
569 StatusCode: http.StatusBadGateway,
570 Body: io.NopCloser(bytes.NewBufferString(`gateway timeout`)),
571 },
572 err: cmdutils.SilentError,
573 stdout: `gateway timeout`,
574 stderr: "glab: HTTP 502\n",
575 },
576 {
577 name: "endpoint rate limit",
578 httpResponse: &http.Response{
579 StatusCode: http.StatusTooManyRequests,
580 Body: io.NopCloser(bytes.NewBufferString(`{"message": "This endpoint has been requested too many times. Try again later."}`)),
581 Header: http.Header{
582 "Content-Type": []string{"application/json"},
583 "Retry-After": []string{"60"},
584 "Ratelimit-Name": []string{"search_rate_limit"},
585 },
586 },
587 err: cmdutils.SilentError,
588 stdout: `{"message": "This endpoint has been requested too many times. Try again later."}`,
589 stderr: "glab: This endpoint has been requested too many times. Try again later. (HTTP 429)\nglab: rate limited by search_rate_limit, retry after 1m0s\n",
590 },
591 {
592 name: "Rack::Attack rate limit",
593 httpResponse: &http.Response{
594 StatusCode: http.StatusTooManyRequests,
595 Body: io.NopCloser(bytes.NewBufferString("Retry later\n")),
596 Header: http.Header{
597 "Content-Type": []string{"text/plain"},
598 "Retry-After": []string{"17"},
599 "Ratelimit-Name": []string{"throttle_authenticated_api"},
600 },
601 },
602 err: cmdutils.SilentError,
603 stdout: "Retry later\n",
604 stderr: "glab: HTTP 429\nglab: rate limited by throttle_authenticated_api, retry after 17s\n",
605 },
606 {
607 name: "rate limit without a wait",
608 httpResponse: &http.Response{
609 StatusCode: http.StatusTooManyRequests,
610 Body: io.NopCloser(bytes.NewBufferString("Retry later\n")),
611 },
612 err: cmdutils.SilentError,
613 stdout: "Retry later\n",
614 stderr: "glab: HTTP 429\n",
615 },
616 {
617 name: "silent",
618 options: options{
619 silent: true,
620 },
621 httpResponse: &http.Response{
622 StatusCode: http.StatusOK,
623 Body: io.NopCloser(bytes.NewBufferString(`body`)),
624 },
625 err: nil,
626 stdout: ``,
627 stderr: ``,
628 },
629 {
630 name: "show response headers even when silent",
631 options: options{
632 showResponseHeaders: true,
633 silent: true,
634 },
635 httpResponse: &http.Response{
636 Proto: "HTTP/1.1",
637 Status: "200 Okey-dokey",
638 StatusCode: http.StatusOK,
639 Body: io.NopCloser(bytes.NewBufferString(`body`)),
640 Header: http.Header{"Content-Type": []string{"text/plain"}},
641 },
642 err: nil,
643 stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n",
644 stderr: ``,
645 },
646 {
647 name: "REST empty array errors",
648 httpResponse: &http.Response{
649 StatusCode: http.StatusBadRequest,
650 Body: io.NopCloser(bytes.NewBufferString(`{"errors": []}`)),
651 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
652 },
653 err: cmdutils.SilentError,
654 stdout: `{"errors": []}`,
655 stderr: "glab: HTTP 400\n",
656 },
657 {
658 name: "REST nested array errors",
659 httpResponse: &http.Response{
660 StatusCode: http.StatusBadRequest,
661 Body: io.NopCloser(bytes.NewBufferString(`{"errors": [["nested", "error"]]}`)),
662 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
663 },
664 err: cmdutils.SilentError,
665 stdout: `{"errors": [["nested", "error"]]}`,
666 stderr: "glab: HTTP 400\n",
667 },
668 {
669 name: "GraphQL error with empty errors array",
670 options: options{
671 requestPath: "graphql",
672 },
673 httpResponse: &http.Response{
674 StatusCode: http.StatusOK,
675 Body: io.NopCloser(bytes.NewBufferString(`{"errors": []}`)),
676 Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
677 },
678 err: nil,
679 stdout: `{"errors": []}`,
680 stderr: "",
681 },
682 }
684 for _, tt := range tests {
685 t.Run(tt.name, func(t *testing.T) {
686 ios, _, stdout, stderr := cmdtest.TestIOStreams()
688 tt.options.io = ios
689 tt.options.baseRepo = func() (glrepo.Interface, error) {
690 return nil, fmt.Errorf("not supposed to be called")
691 }
692 tt.options.apiClient = func(repoHost string) (*api.Client, error) {
693 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
694 resp := tt.httpResponse
695 resp.Request = req
696 return resp, nil
697 }
698 return cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com"), nil
699 }
701 err := tt.options.run(t.Context())
702 if !errors.Is(err, tt.err) {
703 t.Errorf("expected error %v, got %v", tt.err, err)
704 }
706 if stdout.String() != tt.stdout {
707 t.Errorf("expected output %q, got %q", tt.stdout, stdout.String())
708 }
709 if stderr.String() != tt.stderr {
710 t.Errorf("expected error output %q, got %q", tt.stderr, stderr.String())
711 }
712 })
713 }
716func Test_apiRun_paginationREST(t *testing.T) {
717 ios, _, stdout, stderr := cmdtest.TestIOStreams()
719 requestCount := 0
720 responses := []*http.Response{
721 {
722 StatusCode: http.StatusOK,
723 Body: io.NopCloser(bytes.NewBufferString(`{"page":1}`)),
724 Header: http.Header{
725 "Link": []string{`<https://gitlab.com/api/v4/projects/1227/issues?page=2>; rel="next", <https://gitlab.com/api/v4/projects/1227/issues?page=3>; rel="last"`},
726 },
727 },
728 {
729 StatusCode: http.StatusOK,
730 Body: io.NopCloser(bytes.NewBufferString(`{"page":2}`)),
731 Header: http.Header{
732 "Link": []string{`<https://gitlab.com/api/v4/projects/1227/issues?page=3>; rel="next", <https://gitlab.com/api/v4/projects/1227/issues?page=3>; rel="last"`},
733 },
734 },
735 {
736 StatusCode: http.StatusOK,
737 Body: io.NopCloser(bytes.NewBufferString(`{"page":3}`)),
738 Header: http.Header{},
739 },
740 }
742 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
743 resp := responses[requestCount]
744 resp.Request = req
745 requestCount++
746 return resp, nil
747 }
748 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
749 options := options{
750 io: ios,
751 baseRepo: func() (glrepo.Interface, error) {
752 return nil, fmt.Errorf("not supposed to be called")
753 },
754 apiClient: func(repoHost string) (*api.Client, error) {
755 return a, nil
756 },
758 requestPath: "issues",
759 paginate: true,
760 }
762 err := options.run(t.Context())
763 require.NoError(t, err)
765 assert.Equal(t, `{"page":1}{"page":2}{"page":3}`, stdout.String(), "stdout")
766 assert.Empty(t, stderr.String(), "stderr")
768 assert.Equal(t, "https://gitlab.com/api/v4/issues?per_page=100", responses[0].Request.URL.String())
769 assert.Equal(t, "https://gitlab.com/api/v4/projects/1227/issues?page=2", responses[1].Request.URL.String())
770 assert.Equal(t, "https://gitlab.com/api/v4/projects/1227/issues?page=3", responses[2].Request.URL.String())
773func Test_apiRun_paginationREST_rateLimitedPage(t *testing.T) {
774 ios, _, stdout, stderr := cmdtest.TestIOStreams()
776 responses := []*http.Response{
777 {
778 StatusCode: http.StatusOK,
779 Body: io.NopCloser(bytes.NewBufferString(`{"page":1}`)),
780 Header: http.Header{
781 "Link": []string{`<https://gitlab.com/api/v4/projects/1227/issues?page=2>; rel="next"`},
782 },
783 },
784 {
785 StatusCode: http.StatusTooManyRequests,
786 Body: io.NopCloser(bytes.NewBufferString("Retry later\n")),
787 Header: http.Header{"Retry-After": []string{"42"}},
788 },
789 }
791 requestCount := 0
792 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
793 resp := responses[requestCount]
794 resp.Request = req
795 requestCount++
796 return resp, nil
797 }
798 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
799 options := options{
800 io: ios,
801 baseRepo: func() (glrepo.Interface, error) {
802 return nil, fmt.Errorf("not supposed to be called")
803 },
804 apiClient: func(repoHost string) (*api.Client, error) {
805 return a, nil
806 },
807 requestPath: "issues",
808 paginate: true,
809 }
811 err := options.run(t.Context())
812 require.ErrorIs(t, err, cmdutils.SilentError)
814 assert.Equal(t, 2, requestCount)
815 assert.Equal(t, "{\"page\":1}Retry later\n", stdout.String())
816 assert.Equal(t, "glab: HTTP 429\nglab: rate limited, retry after 42s\n", stderr.String())
819// Test_apiRun_paginationREST_followsLinkURLVerbatim pins the Link header
820// contract for --paginate: every page after the first is requested at exactly
821// the URL the server advertised as rel="next". Re-applying the caller's fields
822// duplicates array values and can override the page the server asked for. A
823// method that carries its fields in the body instead of the query keeps sending
824// them, so the same loop must not drop that body.
825func Test_apiRun_paginationREST_followsLinkURLVerbatim(t *testing.T) {
826 t.Parallel()
828 tests := []struct {
829 name string
830 fields []fieldFlag
831 // requestMethodPassed false leaves the --method default in place, which
832 // validate accepts and method inference then turns into a POST.
833 requestMethodPassed bool
834 outputFormat string
835 // wantFirstQuery: each caller parameter exactly once, plus per_page.
836 wantFirstQuery url.Values
837 // nextPageURLs: rel="next" targets requests two and three must match.
838 nextPageURLs [2]string
839 // wantMethod, wantBody and wantContentType hold for all three requests,
840 // not only the first: whichever of the query or the body carries the
841 // caller's fields must carry them on every page.
842 wantMethod string
843 wantBody string
844 wantContentType string
845 // wantStdout: every page's payload, in the order the pages arrived.
846 wantStdout string
847 }{
848 {
849 name: "array field",
850 fields: magicFields("ids=[1,2]"),
851 requestMethodPassed: true,
852 outputFormat: "json",
853 wantFirstQuery: url.Values{"ids[]": {"1", "2"}, "per_page": {"100"}},
854 nextPageURLs: [2]string{
855 "https://gitlab.com/api/v4/issues?ids%5B%5D=1&ids%5B%5D=2&page=2&per_page=100",
856 "https://gitlab.com/api/v4/issues?ids%5B%5D=1&ids%5B%5D=2&page=3&per_page=100",
857 },
858 wantMethod: http.MethodGet,
859 wantStdout: `[{"id":1}][{"id":2}][{"id":3}]`,
860 },
861 {
862 name: "array field with ndjson output",
863 fields: magicFields("ids=[1,2]"),
864 requestMethodPassed: true,
865 outputFormat: "ndjson",
866 wantFirstQuery: url.Values{"ids[]": {"1", "2"}, "per_page": {"100"}},
867 nextPageURLs: [2]string{
868 "https://gitlab.com/api/v4/issues?ids%5B%5D=1&ids%5B%5D=2&page=2&per_page=100",
869 "https://gitlab.com/api/v4/issues?ids%5B%5D=1&ids%5B%5D=2&page=3&per_page=100",
870 },
871 wantMethod: http.MethodGet,
872 wantStdout: "{\"id\":1}\n{\"id\":2}\n{\"id\":3}\n",
873 },
874 {
875 name: "scalar field",
876 fields: rawFields("foo=bar"),
877 requestMethodPassed: true,
878 outputFormat: "json",
879 wantFirstQuery: url.Values{"foo": {"bar"}, "per_page": {"100"}},
880 nextPageURLs: [2]string{
881 "https://gitlab.com/api/v4/issues?foo=bar&page=2&per_page=100",
882 "https://gitlab.com/api/v4/issues?foo=bar&page=3&per_page=100",
883 },
884 wantMethod: http.MethodGet,
885 wantStdout: `[{"id":1}][{"id":2}][{"id":3}]`,
886 },
887 {
888 name: "caller-supplied page",
889 fields: rawFields("page=1"),
890 requestMethodPassed: true,
891 outputFormat: "json",
892 wantFirstQuery: url.Values{"page": {"1"}, "per_page": {"100"}},
893 nextPageURLs: [2]string{
894 "https://gitlab.com/api/v4/issues?page=2&per_page=100",
895 "https://gitlab.com/api/v4/issues?page=3&per_page=100",
896 },
897 wantMethod: http.MethodGet,
898 wantStdout: `[{"id":1}][{"id":2}][{"id":3}]`,
899 },
900 {
901 // glab api --paginate -f a=b issues, an inferred POST: the field is
902 // in the body and never in the query, so every page has to re-send
903 // it. per_page is still appended to the path.
904 name: "field with no method flag",
905 fields: rawFields("a=b"),
906 requestMethodPassed: false,
907 outputFormat: "json",
908 wantFirstQuery: url.Values{"per_page": {"100"}},
909 nextPageURLs: [2]string{
910 "https://gitlab.com/api/v4/issues?page=2&per_page=100",
911 "https://gitlab.com/api/v4/issues?page=3&per_page=100",
912 },
913 wantMethod: http.MethodPost,
914 wantBody: `{"a":"b"}`,
915 wantContentType: "application/json; charset=utf-8",
916 wantStdout: `[{"id":1}][{"id":2}][{"id":3}]`,
917 },
918 }
920 for _, tt := range tests {
921 t.Run(tt.name, func(t *testing.T) {
922 t.Parallel()
924 ios, _, stdout, stderr := cmdtest.TestIOStreams()
926 responses := make([]*http.Response, len(tt.nextPageURLs)+1)
927 for i := range responses {
928 header := http.Header{"Content-Type": []string{`application/json`}}
929 if i < len(tt.nextPageURLs) {
930 header.Set("Link", fmt.Sprintf(`<%s>; rel="next"`, tt.nextPageURLs[i]))
931 }
932 responses[i] = &http.Response{
933 StatusCode: http.StatusOK,
934 // A distinct payload per page, so stdout pins order.
935 Body: io.NopCloser(bytes.NewBufferString(fmt.Sprintf(`[{"id":%d}]`, i+1))),
936 Header: header,
937 }
938 }
940 requestCount := 0
941 var gotMethods, gotBodies, gotContentTypes []string
942 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
943 // An unadvertised page errors, so a regression fails instead of hanging.
944 if requestCount >= len(responses) {
945 return nil, fmt.Errorf("unexpected request %d: %s", requestCount+1, req.URL)
946 }
947 // The transport is the last reader of the body, so draining it
948 // here records what the server would have received.
949 body := ""
950 if req.Body != nil {
951 b, err := io.ReadAll(req.Body)
952 if err != nil {
953 return nil, err
954 }
955 body = string(b)
956 }
957 gotMethods = append(gotMethods, req.Method)
958 gotBodies = append(gotBodies, body)
959 gotContentTypes = append(gotContentTypes, req.Header.Get("Content-Type"))
960 resp := responses[requestCount]
961 resp.Request = req
962 requestCount++
963 return resp, nil
964 }
965 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
966 options := options{
967 io: ios,
968 baseRepo: func() (glrepo.Interface, error) {
969 return nil, fmt.Errorf("not supposed to be called")
970 },
971 apiClient: func(repoHost string) (*api.Client, error) {
972 return a, nil
973 },
975 requestPath: "issues",
976 // "GET" is the --method default, held whether or not -X was passed.
977 requestMethod: http.MethodGet,
978 requestMethodPassed: tt.requestMethodPassed,
979 fields: tt.fields,
980 paginate: true,
981 outputFormat: tt.outputFormat,
982 }
984 err := options.run(t.Context())
985 require.NoError(t, err)
986 require.Equal(t, len(responses), requestCount, "number of requests")
988 firstQuery, err := url.ParseQuery(responses[0].Request.URL.RawQuery)
989 require.NoError(t, err)
990 assert.Equal(t, tt.wantFirstQuery, firstQuery, "query of the first request")
992 assert.Equal(t, tt.nextPageURLs[0], responses[1].Request.URL.String(),
993 `request 2 must be the rel="next" URL of response 1, unmodified`)
994 assert.Equal(t, tt.nextPageURLs[1], responses[2].Request.URL.String(),
995 `request 3 must be the rel="next" URL of response 2, unmodified`)
997 assert.Equal(t, tt.wantMethod, gotMethods[0], "method of request 1")
998 assert.Equal(t, tt.wantMethod, gotMethods[1], "method of request 2")
999 assert.Equal(t, tt.wantMethod, gotMethods[2], "method of request 3")
1001 assert.Equal(t, tt.wantBody, gotBodies[0], "body of request 1")
1002 assert.Equal(t, tt.wantBody, gotBodies[1], "body of request 2")
1003 assert.Equal(t, tt.wantBody, gotBodies[2], "body of request 3")
1005 assert.Equal(t, tt.wantContentType, gotContentTypes[0], "Content-Type of request 1")
1006 assert.Equal(t, tt.wantContentType, gotContentTypes[1], "Content-Type of request 2")
1007 assert.Equal(t, tt.wantContentType, gotContentTypes[2], "Content-Type of request 3")
1009 assert.Equal(t, tt.wantStdout, stdout.String(), "stdout")
1010 assert.Empty(t, stderr.String(), "stderr")
1011 })
1015func Test_apiRun_paginationGraphQL(t *testing.T) {
1016 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1018 requestCount := 0
1019 responses := []*http.Response{
1021 StatusCode: http.StatusOK,
1022 Header: http.Header{"Content-Type": []string{`application/json`}},
1023 Body: io.NopCloser(bytes.NewBufferString(`{
1024 "data": {
1025 "nodes": ["page one"],
1026 "pageInfo": {
1027 "endCursor": "PAGE1_END",
1028 "hasNextPage": true
1031 }`)),
1032 },
1034 StatusCode: http.StatusOK,
1035 Header: http.Header{"Content-Type": []string{`application/json`}},
1036 Body: io.NopCloser(bytes.NewBufferString(`{
1037 "data": {
1038 "nodes": ["page two"],
1039 "pageInfo": {
1040 "endCursor": "PAGE2_END",
1041 "hasNextPage": false
1044 }`)),
1045 },
1048 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1049 resp := responses[requestCount]
1050 resp.Request = req
1051 requestCount++
1052 return resp, nil
1054 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1055 options := options{
1056 io: ios,
1057 baseRepo: func() (glrepo.Interface, error) {
1058 return nil, fmt.Errorf("not supposed to be called")
1059 },
1060 apiClient: func(repoHost string) (*api.Client, error) {
1061 return a, nil
1062 },
1064 requestMethod: http.MethodPost,
1065 requestPath: "graphql",
1066 paginate: true,
1067 outputFormat: "json",
1070 err := options.run(t.Context())
1071 require.NoError(t, err)
1073 assert.Contains(t, stdout.String(), `"page one"`)
1074 assert.Contains(t, stdout.String(), `"page two"`)
1075 assert.Empty(t, stderr.String(), "stderr")
1077 var requestData struct {
1078 Variables map[string]any
1081 bb, err := io.ReadAll(responses[0].Request.Body)
1082 require.NoError(t, err)
1083 err = json.Unmarshal(bb, &requestData)
1084 require.NoError(t, err)
1085 _, hasCursor := requestData.Variables["endCursor"].(string)
1086 assert.False(t, hasCursor)
1088 bb, err = io.ReadAll(responses[1].Request.Body)
1089 require.NoError(t, err)
1090 err = json.Unmarshal(bb, &requestData)
1091 require.NoError(t, err)
1092 endCursor, hasCursor := requestData.Variables["endCursor"].(string)
1093 assert.True(t, hasCursor)
1094 assert.Equal(t, "PAGE1_END", endCursor)
1097// Test_apiRun_paginationGraphQL_bodyNotConsumedTwice verifies that the GraphQL
1098// pagination flow doesn't consume the response body twice, ensuring cursor
1099// extraction and output both work correctly.
1100func Test_apiRun_paginationGraphQL_bodyNotConsumedTwice(t *testing.T) {
1101 t.Parallel()
1103 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1105 requestCount := 0
1106 responses := []*http.Response{
1108 StatusCode: http.StatusOK,
1109 Header: http.Header{"Content-Type": []string{`application/json`}},
1110 Body: io.NopCloser(bytes.NewBufferString(`{
1111 "data": {
1112 "project": {
1113 "issues": {
1114 "nodes": [
1115 {"id": "1", "title": "First issue"}
1116 ],
1117 "pageInfo": {
1118 "endCursor": "CURSOR_PAGE_1",
1119 "hasNextPage": true
1124 }`)),
1125 },
1127 StatusCode: http.StatusOK,
1128 Header: http.Header{"Content-Type": []string{`application/json`}},
1129 Body: io.NopCloser(bytes.NewBufferString(`{
1130 "data": {
1131 "project": {
1132 "issues": {
1133 "nodes": [
1134 {"id": "2", "title": "Second issue"}
1135 ],
1136 "pageInfo": {
1137 "endCursor": "CURSOR_PAGE_2",
1138 "hasNextPage": false
1143 }`)),
1144 },
1147 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1148 resp := responses[requestCount]
1149 resp.Request = req
1150 requestCount++
1151 return resp, nil
1153 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1154 options := options{
1155 io: ios,
1156 baseRepo: func() (glrepo.Interface, error) {
1157 return nil, fmt.Errorf("not supposed to be called")
1158 },
1159 apiClient: func(repoHost string) (*api.Client, error) {
1160 return a, nil
1161 },
1163 requestMethod: http.MethodPost,
1164 requestPath: "graphql",
1165 paginate: true,
1166 outputFormat: "json",
1169 err := options.run(t.Context())
1170 require.NoError(t, err)
1172 // Verify both pages were output
1173 output := stdout.String()
1174 assert.Contains(t, output, "First issue")
1175 assert.Contains(t, output, "Second issue")
1176 assert.Empty(t, stderr.String(), "stderr should be empty")
1178 // Verify exactly 2 requests were made (one per page)
1179 assert.Equal(t, 2, requestCount, "expected 2 requests for pagination")
1181 // Verify the second request included the endCursor from the first page
1182 var requestData struct {
1183 Variables map[string]any
1185 bb, err := io.ReadAll(responses[1].Request.Body)
1186 require.NoError(t, err)
1187 err = json.Unmarshal(bb, &requestData)
1188 require.NoError(t, err)
1189 endCursor, hasCursor := requestData.Variables["endCursor"].(string)
1190 assert.True(t, hasCursor, "second request should have endCursor variable")
1191 assert.Equal(t, "CURSOR_PAGE_1", endCursor, "endCursor should match first page's endCursor")
1194// Test_apiRun_paginationGraphQL_emptyResults tests pagination with empty result sets
1195func Test_apiRun_paginationGraphQL_emptyResults(t *testing.T) {
1196 t.Parallel()
1198 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1200 responses := []*http.Response{
1202 StatusCode: http.StatusOK,
1203 Header: http.Header{"Content-Type": []string{`application/json`}},
1204 Body: io.NopCloser(bytes.NewBufferString(`{
1205 "data": {
1206 "project": {
1207 "issues": {
1208 "nodes": [],
1209 "pageInfo": {
1210 "endCursor": "",
1211 "hasNextPage": false
1216 }`)),
1217 },
1220 requestCount := 0
1221 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1222 resp := responses[requestCount]
1223 resp.Request = req
1224 requestCount++
1225 return resp, nil
1227 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1228 options := options{
1229 io: ios,
1230 baseRepo: func() (glrepo.Interface, error) {
1231 return nil, fmt.Errorf("not supposed to be called")
1232 },
1233 apiClient: func(repoHost string) (*api.Client, error) {
1234 return a, nil
1235 },
1237 requestMethod: http.MethodPost,
1238 requestPath: "graphql",
1239 paginate: true,
1240 outputFormat: "json",
1243 err := options.run(t.Context())
1244 require.NoError(t, err)
1246 // Verify empty nodes array is in output
1247 output := stdout.String()
1248 assert.Contains(t, output, "nodes")
1249 assert.Empty(t, stderr.String(), "stderr should be empty")
1251 // Should only make 1 request since hasNextPage is false
1252 assert.Equal(t, 1, requestCount, "expected only 1 request for empty results")
1255// Test_apiRun_paginationGraphQL_withNDJSON verifies that NDJSON output works with GraphQL pagination.
1256// With GraphQL, each full response object is output as one line per page (not array elements).
1257func Test_apiRun_paginationGraphQL_withNDJSON(t *testing.T) {
1258 t.Parallel()
1260 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1262 requestCount := 0
1263 responses := []*http.Response{
1265 StatusCode: http.StatusOK,
1266 Header: http.Header{"Content-Type": []string{`application/json`}},
1267 Body: io.NopCloser(bytes.NewBufferString(`{
1268 "data": {
1269 "nodes": [
1270 {"id": "1", "title": "Issue 1"}
1271 ],
1272 "pageInfo": {
1273 "endCursor": "CURSOR_1",
1274 "hasNextPage": true
1277 }`)),
1278 },
1280 StatusCode: http.StatusOK,
1281 Header: http.Header{"Content-Type": []string{`application/json`}},
1282 Body: io.NopCloser(bytes.NewBufferString(`{
1283 "data": {
1284 "nodes": [
1285 {"id": "2", "title": "Issue 2"}
1286 ],
1287 "pageInfo": {
1288 "endCursor": "CURSOR_2",
1289 "hasNextPage": false
1292 }`)),
1293 },
1296 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1297 resp := responses[requestCount]
1298 resp.Request = req
1299 requestCount++
1300 return resp, nil
1302 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1303 options := options{
1304 io: ios,
1305 baseRepo: func() (glrepo.Interface, error) {
1306 return nil, fmt.Errorf("not supposed to be called")
1307 },
1308 apiClient: func(repoHost string) (*api.Client, error) {
1309 return a, nil
1310 },
1312 requestMethod: http.MethodPost,
1313 requestPath: "graphql",
1314 paginate: true,
1315 outputFormat: "ndjson",
1318 err := options.run(t.Context())
1319 require.NoError(t, err)
1321 // NDJSON with GraphQL outputs each full response object as one line
1322 output := stdout.String()
1323 lines := strings.Split(strings.TrimSpace(output), "\n")
1325 // Each GraphQL response is output as a complete object per line
1326 assert.GreaterOrEqual(t, len(lines), 2, "should have at least 2 lines")
1328 // Verify both issues appear in the output
1329 assert.Contains(t, output, "Issue 1")
1330 assert.Contains(t, output, "Issue 2")
1331 assert.Empty(t, stderr.String(), "stderr should be empty")
1333 // Verify that both pages were fetched
1334 assert.Equal(t, 2, requestCount, "should have made 2 requests for pagination")
1337func Test_apiRun_ndjson(t *testing.T) {
1338 t.Parallel()
1340 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1342 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1343 return &http.Response{
1344 StatusCode: http.StatusOK,
1345 Header: http.Header{"Content-Type": []string{`application/json`}},
1346 Body: io.NopCloser(bytes.NewBufferString(`[{"id":1,"title":"Issue 1"},{"id":2,"title":"Issue 2"}]`)),
1347 Request: req,
1348 }, nil
1350 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1351 options := options{
1352 io: ios,
1353 baseRepo: func() (glrepo.Interface, error) {
1354 return nil, fmt.Errorf("not supposed to be called")
1355 },
1356 apiClient: func(repoHost string) (*api.Client, error) {
1357 return a, nil
1358 },
1359 requestPath: "issues",
1360 outputFormat: "ndjson",
1363 err := options.run(t.Context())
1364 require.NoError(t, err)
1366 // NDJSON should output each element on a separate line
1367 lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
1368 assert.Len(t, lines, 2, "should have 2 lines")
1370 // Verify each line is valid JSON
1371 var obj1, obj2 map[string]any
1372 require.NoError(t, json.Unmarshal([]byte(lines[0]), &obj1))
1373 require.NoError(t, json.Unmarshal([]byte(lines[1]), &obj2))
1375 assert.InDelta(t, 1, obj1["id"], 0)
1376 assert.Equal(t, "Issue 1", obj1["title"])
1377 assert.InDelta(t, 2, obj2["id"], 0)
1378 assert.Equal(t, "Issue 2", obj2["title"])
1379 assert.Empty(t, stderr.String(), "stderr")
1382func Test_apiRun_ndjson_singleObject(t *testing.T) {
1383 t.Parallel()
1385 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1387 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1388 return &http.Response{
1389 StatusCode: http.StatusOK,
1390 Header: http.Header{"Content-Type": []string{`application/json`}},
1391 Body: io.NopCloser(bytes.NewBufferString(`{"id":1,"title":"Single Issue"}`)),
1392 Request: req,
1393 }, nil
1395 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1396 options := options{
1397 io: ios,
1398 baseRepo: func() (glrepo.Interface, error) {
1399 return nil, fmt.Errorf("not supposed to be called")
1400 },
1401 apiClient: func(repoHost string) (*api.Client, error) {
1402 return a, nil
1403 },
1404 requestPath: "issues/1",
1405 outputFormat: "ndjson",
1408 err := options.run(t.Context())
1409 require.NoError(t, err)
1411 // Single object should be output as one line
1412 output := strings.TrimSpace(stdout.String())
1413 assert.Len(t, strings.Split(output, "\n"), 1, "should have 1 line")
1415 var obj map[string]any
1416 require.NoError(t, json.Unmarshal([]byte(output), &obj))
1417 assert.InDelta(t, 1, obj["id"], 0)
1418 assert.Equal(t, "Single Issue", obj["title"])
1419 assert.Empty(t, stderr.String(), "stderr")
1422func Test_streamNDJSON_preservesLargeNumber(t *testing.T) {
1423 t.Parallel()
1425 var out bytes.Buffer
1426 err := streamNDJSON(strings.NewReader("18446744073709551615"), &out)
1428 require.NoError(t, err)
1429 assert.Equal(t, "18446744073709551615\n", out.String())
1432func Test_parseErrorResponse_doesNotAppendPlus(t *testing.T) {
1433 t.Parallel()
1435 _, message, err := parseErrorResponse(strings.NewReader(`["request failed"]`), http.StatusBadRequest)
1437 require.NoError(t, err)
1438 assert.Equal(t, "[request failed]", message)
1441func Test_apiRun_ndjson_pagination(t *testing.T) {
1442 t.Parallel()
1444 ios, _, stdout, stderr := cmdtest.TestIOStreams()
1446 requestCount := 0
1447 responses := []*http.Response{
1449 StatusCode: http.StatusOK,
1450 Header: http.Header{
1451 "Content-Type": []string{`application/json`},
1452 "Link": []string{`<https://gitlab.com/api/v4/issues?page=2>; rel="next"`},
1453 },
1454 Body: io.NopCloser(bytes.NewBufferString(`[{"id":1,"title":"Issue 1"}]`)),
1455 },
1457 StatusCode: http.StatusOK,
1458 Header: http.Header{
1459 "Content-Type": []string{`application/json`},
1460 },
1461 Body: io.NopCloser(bytes.NewBufferString(`[{"id":2,"title":"Issue 2"}]`)),
1462 },
1465 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1466 resp := responses[requestCount]
1467 resp.Request = req
1468 requestCount++
1469 return resp, nil
1471 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1472 options := options{
1473 io: ios,
1474 baseRepo: func() (glrepo.Interface, error) {
1475 return nil, fmt.Errorf("not supposed to be called")
1476 },
1477 apiClient: func(repoHost string) (*api.Client, error) {
1478 return a, nil
1479 },
1480 requestPath: "issues",
1481 paginate: true,
1482 outputFormat: "ndjson",
1485 err := options.run(t.Context())
1486 require.NoError(t, err)
1488 // Should have 2 lines total (one from each page)
1489 lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
1490 assert.Len(t, lines, 2, "should have 2 lines from 2 pages")
1492 var obj1, obj2 map[string]any
1493 require.NoError(t, json.Unmarshal([]byte(lines[0]), &obj1))
1494 require.NoError(t, json.Unmarshal([]byte(lines[1]), &obj2))
1496 assert.InDelta(t, 1, obj1["id"], 0)
1497 assert.InDelta(t, 2, obj2["id"], 0)
1498 assert.Empty(t, stderr.String(), "stderr")
1501func Test_apiRun_inputFile(t *testing.T) {
1502 tests := []struct {
1503 name string
1504 inputFile string
1505 inputContents []byte
1506 requestHeaders []string
1508 contentLength int64
1509 expectedContents []byte
1510 expectedContentType string
1511 }{
1513 name: "stdin",
1514 inputFile: "-",
1515 inputContents: []byte("I WORK OUT"),
1516 contentLength: 0,
1517 expectedContentType: "application/json; charset=utf-8",
1518 },
1520 name: "from file",
1521 inputFile: "gitlab-test-file",
1522 inputContents: []byte("I WORK OUT"),
1523 contentLength: 10,
1524 expectedContentType: "application/json; charset=utf-8",
1525 },
1527 name: "explicit content type is not overridden",
1528 inputFile: "-",
1529 inputContents: []byte("I WORK OUT"),
1530 requestHeaders: []string{"Content-Type: text/plain"},
1531 contentLength: 0,
1532 expectedContentType: "text/plain",
1533 },
1535 for _, tt := range tests {
1536 t.Run(tt.name, func(t *testing.T) {
1537 ios, stdin, _, _ := cmdtest.TestIOStreams()
1538 resp := &http.Response{StatusCode: http.StatusNoContent}
1540 inputFile := tt.inputFile
1541 if tt.inputFile == "-" {
1542 _, _ = stdin.Write(tt.inputContents)
1543 } else {
1544 f, err := os.CreateTemp(t.TempDir(), tt.inputFile)
1545 if err != nil {
1546 t.Fatal(err)
1548 _, _ = f.Write(tt.inputContents)
1549 require.NoError(t, f.Close())
1550 inputFile = f.Name()
1553 var bodyBytes []byte
1554 var tr roundTripFunc = func(req *http.Request) (*http.Response, error) {
1555 var err error
1556 if bodyBytes, err = io.ReadAll(req.Body); err != nil {
1557 return nil, err
1559 resp.Request = req
1560 return resp, nil
1562 a := cmdtest.NewTestApiClient(t, &http.Client{Transport: tr}, "OTOKEN", "gitlab.com")
1563 options := options{
1564 requestPath: "hello",
1565 requestInputFile: inputFile,
1566 fields: rawFields("a=b", "c=d"),
1567 requestHeaders: tt.requestHeaders,
1569 io: ios,
1570 baseRepo: func() (glrepo.Interface, error) {
1571 return nil, fmt.Errorf("not supposed to be called")
1572 },
1573 apiClient: func(repoHost string) (*api.Client, error) {
1574 return a, nil
1575 },
1578 err := options.run(t.Context())
1579 if err != nil {
1580 t.Errorf("got error %v", err)
1583 assert.Equal(t, http.MethodPost, resp.Request.Method)
1584 assert.Equal(t, "/api/v4/hello?a=b&c=d", resp.Request.URL.RequestURI())
1585 assert.Equal(t, tt.contentLength, resp.Request.ContentLength)
1586 assert.Equal(t, tt.expectedContentType, resp.Request.Header.Get("Content-Type"))
1587 assert.Len(t, resp.Request.Header.Values("Content-Type"), 1)
1588 assert.Equal(t, tt.inputContents, bodyBytes)
1589 })
1593// Test_options_methodForRequest pins the method the request is sent with. Only
1594// a --method flag, a field, a --form field, or --input moves it off the flag's
1595// GET default, and each of those four has to move it on its own: a request with
1596// no fields must stay a GET, and --form and --input each have to reach POST
1597// without help from a field.
1598func Test_options_methodForRequest(t *testing.T) {
1599 tests := []struct {
1600 name string
1601 opts options
1602 want string
1603 }{
1604 {name: "no fields and no --input stay GET", opts: options{requestMethod: http.MethodGet}, want: http.MethodGet},
1605 {name: "a field alone becomes POST", opts: options{requestMethod: http.MethodGet, fields: rawFields("a=1")}, want: http.MethodPost},
1606 {name: "a form field alone becomes POST", opts: options{requestMethod: http.MethodGet, formFields: []string{"a=1"}}, want: http.MethodPost},
1607 {name: "--input alone becomes POST", opts: options{requestMethod: http.MethodGet, requestInputFile: "-"}, want: http.MethodPost},
1608 {name: "an explicit --method wins over both", opts: options{requestMethod: http.MethodDelete, requestMethodPassed: true, fields: rawFields("a=1"), requestInputFile: "-"}, want: http.MethodDelete},
1609 {name: "an explicit --method GET is kept", opts: options{requestMethod: http.MethodGet, requestMethodPassed: true, fields: rawFields("a=1")}, want: http.MethodGet},
1612 for _, tt := range tests {
1613 t.Run(tt.name, func(t *testing.T) {
1614 assert.Equal(t, tt.want, tt.opts.methodForRequest())
1615 })
1619// Test_parseFields_queryString pins the query strings bracketed names produce,
1620// at the two seams apiRun builds one with: parseFields for the parameters and
1621// parseQuery for the bytes. The path is the one apiRun would have reached, so a
1622// case's expectation is the whole request URI.
1624// Reading brackets on GET and DELETE would alter commands that work now, so most
1625// of these are regression guards. Every case also asserts its fields took the
1626// query path rather than a body, which is what a method spelled "get" and an
1627// --input alongside a POST are here to prove. An absent method means -X GET.
1628func Test_parseFields_queryString(t *testing.T) {
1629 // An @file value is the one non-scalar that reaches an accumulated array.
1630 dir := t.TempDir()
1631 first, second := dir+"/first.txt", dir+"/second.txt"
1632 require.NoError(t, os.WriteFile(first, []byte("alpha"), 0o600))
1633 require.NoError(t, os.WriteFile(second, []byte("beta"), 0o600))
1635 tests := []struct {
1636 method string
1637 input string
1638 fields []fieldFlag
1639 wantURI string
1640 wantErr string
1641 }{
1642 // The issue's own repro, minus the body, as a GET.
1643 {fields: rawFields("position[base_sha]=abc", "position[position_type]=text", "position[new_path]=file.kt"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc&position%5Bnew_path%5D=file.kt&position%5Bposition_type%5D=text"},
1644 {fields: rawFields("position[base_sha]=abc"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1645 {fields: rawFields("body=test", "position[base_sha]=abc"), wantURI: "/api/v4/hello?body=test&position%5Bbase_sha%5D=abc"},
1646 {fields: rawFields("a[b][c]=1"), wantURI: "/api/v4/hello?a%5Bb%5D%5Bc%5D=1"},
1647 // Also the single-value case the accumulating suffix leaves unchanged.
1648 {fields: rawFields("ids[]=1"), wantURI: "/api/v4/hello?ids%5B%5D=1"},
1649 // --field types the value, so this leaf is an int and renders as 42.
1650 {fields: magicFields("position[new_line]=42"), wantURI: "/api/v4/hello?position%5Bnew_line%5D=42"},
1651 {method: http.MethodDelete, fields: rawFields("position[base_sha]=abc", "position[new_path]=file.kt"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc&position%5Bnew_path%5D=file.kt"},
1652 // A case-sensitive method comparison would send these as a body instead.
1653 {method: "get", fields: rawFields("position[base_sha]=abc"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1654 {method: "delete", fields: rawFields("position[base_sha]=abc"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1655 // An empty array contributes no parameters, so no separator either.
1656 {fields: magicFields("a[b]=[]"), wantURI: "/api/v4/hello"},
1657 {fields: magicFields(`a[b]={"c":1}`), wantErr: `query parameter "a[b]": objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body`},
1659 // Repeated names ending in "[]" send every value, in the order typed.
1660 {fields: rawFields("ids[]=1", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1661 {fields: magicFields("ids[]=1", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1662 // Across the two flags as well: because the name collects, --field does
1663 // not override a --raw-field of the same name the way it does for other
1664 // names. This is the order that override would drop a value in.
1665 {fields: slices.Concat(magicFields("ids[]=1"), rawFields("ids[]=2")), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1666 {fields: rawFields("ids[]=3", "ids[]=1", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=3&ids%5B%5D=1&ids%5B%5D=2"},
1667 {method: http.MethodDelete, fields: rawFields("ids[]=1", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1668 {fields: rawFields("ids[]=1", "ids[]=1"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=1"},
1669 {fields: rawFields("ids[]="), wantURI: "/api/v4/hello?ids%5B%5D="},
1670 {fields: rawFields("ids[]=", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=&ids%5B%5D=2"},
1671 // null has no query representation, so it encodes as an empty value.
1672 {fields: magicFields("ids[]=true", "ids[]=null", "ids[]=false"), wantURI: "/api/v4/hello?ids%5B%5D=true&ids%5B%5D=&ids%5B%5D=false"},
1673 // A []byte is the one value the array-element renderer has no case for.
1674 {fields: magicFields("ids[]=@"+first, "ids[]=@"+second), wantURI: "/api/v4/hello?ids%5B%5D=alpha&ids%5B%5D=beta"},
1675 // --input puts the fields on the query path whatever the method is.
1676 {method: http.MethodPost, input: "-", fields: rawFields("ids[]=1", "ids[]=2"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1678 // Only a trailing "[]" accumulates; every other name holds one value.
1679 {fields: rawFields("position[base_sha]=first", "position[base_sha]=second"), wantURI: "/api/v4/hello?position%5Bbase_sha%5D=second"},
1680 {fields: rawFields("search=first", "search=second"), wantURI: "/api/v4/hello?search=second"},
1681 {fields: magicFields("search=first", "search=second"), wantURI: "/api/v4/hello?search=second"},
1682 // "a[][b]" contains "[]" without ending in it, so it holds one value like
1683 // any other name: the last raw value wins, and a --field still overrides.
1684 // Reading the suffix test as a substring would accumulate both instead.
1685 {fields: rawFields("a[][b]=1", "a[][b]=2"), wantURI: "/api/v4/hello?a%5B%5D%5Bb%5D=2"},
1686 {fields: slices.Concat(rawFields("a[][b]=1"), magicFields("a[][b]=2")), wantURI: "/api/v4/hello?a%5B%5D%5Bb%5D=2"},
1687 // A trailing "[]" after a segment does accumulate, which is the boundary
1688 // the two names above sit on the other side of.
1689 {fields: rawFields("a[b][]=1", "a[b][]=2"), wantURI: "/api/v4/hello?a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=2"},
1690 // Only the body path rejects this, where it has no shape to advise on.
1691 {fields: rawFields("[]=1"), wantURI: "/api/v4/hello?%5B%5D=1"},
1692 // No bracket character, so the body error misses it too. Not decoded,
1693 // deliberately: decoding a key typed literally would be a guess. With no
1694 // trailing bracket there is no accumulation either, so last wins.
1695 {fields: rawFields("ids%5B%5D=1", "ids%5B%5D=2"), wantURI: "/api/v4/hello?ids%255B%255D=2"},
1697 // One flag, a name carrying its own "[]", and a JSON array value: the
1698 // name already spells the repetition the wire format has, so the values
1699 // go under it as given. A single occurrence never becomes a queryList,
1700 // so this reaches parseQuery as a bare array and is the one shape where
1701 // both sites could append a second pair and send ids[][]=.
1702 {fields: magicFields("ids[]=[1,2]"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1703 // The same request as the bare name with the same array, which is the
1704 // point: the two spellings are one wire key, so they also collide below.
1705 {fields: magicFields("ids=[1,2]"), wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2"},
1706 {fields: magicFields("a[b][]=[1,2]"), wantURI: "/api/v4/hello?a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=2"},
1708 // A query string cannot express an array of arrays.
1709 {fields: magicFields("ids[]=[1]", "ids[]=[2]"), wantErr: `query parameter "ids[]": nested arrays and objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body`},
1710 {fields: magicFields(`ids[]={"a":1}`, "ids[]=2"), wantErr: `query parameter "ids[]": nested arrays and objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body`},
1711 // A second occurrence is what makes each occurrence one element, so the
1712 // array the single-flag row above sends as a whole list becomes a nested
1713 // element here and has no query representation. Adding a value to that
1714 // row's request is --input's job, not a flattening this cannot tell from
1715 // the array-of-arrays the two rows above reject.
1716 {fields: slices.Concat(magicFields("ids[]=[1,2]"), rawFields("ids[]=3")), wantErr: `query parameter "ids[]": nested arrays and objects are not supported as query parameters; use --input or a POST, PUT, or PATCH request body`},
1717 // Both emit ids[]=, and map order would interleave them unpredictably.
1718 // The message is the same whichever flag came first.
1719 {fields: slices.Concat(rawFields("ids[]=1"), magicFields("ids=[2,3]")), wantErr: `field names "ids" and "ids[]" both address query parameter "ids[]"; spell the array one way, or use --input`},
1720 {fields: slices.Concat(magicFields("ids=[2,3]"), rawFields("ids[]=1")), wantErr: `field names "ids" and "ids[]" both address query parameter "ids[]"; spell the array one way, or use --input`},
1721 // The same clash with the array one spelled with its own brackets, which
1722 // only shows up once the wire key stops picking up a second pair. Before
1723 // that it addressed ids[][] and this sent three values under two keys.
1724 {fields: slices.Concat(magicFields("ids[]=[1,2]"), magicFields("ids=[3]")), wantErr: `field names "ids" and "ids[]" both address query parameter "ids[]"; spell the array one way, or use --input`},
1725 {fields: slices.Concat(magicFields("ids[]=[1,2]"), rawFields("ids=3")), wantURI: "/api/v4/hello?ids=3&ids%5B%5D=1&ids%5B%5D=2"},
1728 for _, tt := range tests {
1729 method := cmp.Or(tt.method, http.MethodGet)
1730 name := method
1731 if tt.input != "" {
1732 name += " --input " + tt.input
1734 t.Run(name+" "+flagLine(tt.fields), func(t *testing.T) {
1735 params, jsonBody, err := parseFieldsAt(t, method, tt.input, tt.fields)
1736 assert.False(t, jsonBody, "these fields must take the query path")
1737 got := ""
1738 if err == nil {
1739 got, err = parseQuery("/api/v4/hello", params)
1741 if tt.wantErr != "" {
1742 require.Error(t, err)
1743 assert.Equal(t, tt.wantErr, err.Error())
1744 return
1746 require.NoError(t, err)
1747 assert.Equal(t, tt.wantURI, got)
1748 })
1752// Test_parseFields_bracketedFieldNameError pins the advice each shape of name
1753// earns where the fields become a JSON body, replacing a silent no-op. A name
1754// whose brackets describe a shape is advised with its own segments, so the fix
1755// is mechanical rather than an example to translate; a name that describes none
1756// is advised with the forms that work and no invented key.
1758// A case is the name and the advice; the rest of the message is invariant, and
1759// Test_apiRun_bracketNameWiring spells one whole message out. An absent method
1760// means no -X, which the fields alone turn into a POST; an absent field list
1761// means the name with a value of 1.
1762func Test_parseFields_bracketedFieldNameError(t *testing.T) {
1763 tests := []struct {
1764 key string
1765 method string
1766 fields []fieldFlag
1767 advice string
1768 }{
1769 {key: "position[base_sha]", advice: `, for example -F 'position={"base_sha":"..."}'`},
1770 {key: "ids[]", advice: `, for example -F 'ids=[...]'`},
1771 // PUT and PATCH take a JSON body on the same terms POST does, and an
1772 // explicit POST is no different from the one the fields default to.
1773 {key: "position[base_sha]", method: http.MethodPut, advice: `, for example -F 'position={"base_sha":"..."}'`},
1774 {key: "position[base_sha]", method: http.MethodPatch, advice: `, for example -F 'position={"base_sha":"..."}'`},
1775 {key: "position[base_sha]", method: http.MethodPost, advice: `, for example -F 'position={"base_sha":"..."}'`},
1776 // --field is rejected on the same terms --raw-field is.
1777 {key: "position[new_line]", fields: magicFields("position[new_line]=42"), advice: `, for example -F 'position={"new_line":"..."}'`},
1778 // The boundary the emit-every-value fix does not reach: with no -X these
1779 // are a body, so they error rather than accumulating.
1780 {key: "ids[]", fields: rawFields("ids[]=1", "ids[]=2"), advice: `, for example -F 'ids=[...]'`},
1782 // Segments nest outermost first, and an empty one is an array.
1783 {key: "a[b][c]", advice: `, for example -F 'a={"b":{"c":"..."}}'`},
1784 {key: "a[b][]", advice: `, for example -F 'a={"b":[...]}'`},
1785 {key: "a[][b]", advice: `, for example -F 'a=[{"b":"..."}]'`},
1786 // A quote in a segment must not break the JSON the advice shows.
1787 {key: `a[b"c]`, advice: `, for example -F 'a={"b\"c":"..."}'`},
1789 // Not bracketed names, just names holding a bracket: no shape to read, so
1790 // the advice names the forms and invents neither a root nor an inner key.
1791 {key: "a[b", advice: " with -F"},
1792 {key: "a]", advice: " with -F"},
1793 {key: "[]", advice: " with -F"},
1794 {key: "]x", advice: " with -F"},
1795 {key: "[b]", advice: " with -F"},
1796 {key: "a[b]c", advice: " with -F"},
1797 {key: "a[[b]]", advice: " with -F"},
1799 // The first bracketed name is the one reported.
1800 {key: "first[x]", fields: rawFields("body=test", "first[x]=1", "second[y]=2"), advice: `, for example -F 'first={"x":"..."}'`},
1801 // The GraphQL spec spells a variable name /[_A-Za-z][_0-9A-Za-z]*/, so no
1802 // valid query can name one with a bracket: this removes no working use.
1803 // GraphQL adds no branch of its own ahead of the check.
1804 {key: "input[title]", fields: rawFields("query=query { currentUser { username } }", "input[title]=Hello"), advice: `, for example -F 'input={"title":"..."}'`},
1807 for _, tt := range tests {
1808 t.Run(cmp.Or(tt.method, "default POST")+" "+tt.key, func(t *testing.T) {
1809 fields := tt.fields
1810 if fields == nil {
1811 fields = rawFields(tt.key + "=1")
1813 _, jsonBody, err := parseFieldsAt(t, tt.method, "", fields)
1814 assert.True(t, jsonBody, "these fields must become a JSON body")
1815 require.Error(t, err)
1816 want := fmt.Sprintf("field name %q: %s%s, or use --input", tt.key, bracketedNameProblem, tt.advice)
1817 assert.Equal(t, want, err.Error())
1818 })
1822// Test_parseFields_percentEncodedBracketsStayLiteral pins the silent no-op that
1823// survives: -f 'ids%5B%5D=1' holds no bracket character, so the body error
1824// misses it and the key reaches the body literally. Test_httpRequest pins the
1825// bytes it becomes, and Test_parseFields_queryString the query half.
1826func Test_parseFields_percentEncodedBracketsStayLiteral(t *testing.T) {
1827 params, jsonBody, err := parseFieldsAt(t, http.MethodPost, "", rawFields("ids%5B%5D=1"))
1829 assert.True(t, jsonBody)
1830 require.NoError(t, err)
1831 assert.Equal(t, map[string]any{"ids%5B%5D": "1"}, params)
1834// Test_apiRun_bracketNameWiring covers what only a whole run reaches: cobra's
1835// flag parsing, which is the only thing that decides the order two field flags
1836// interleave in and which of them overrides the other; run()'s choice of method,
1837// which decides whether a bracketed name is an error at all; the method the
1838// request leaves with; and the two paths that never reach the field parser,
1839// --form and GraphQL. The bytes and the advice are pinned at the parseFields and
1840// parseQuery seams, and the first case here is the one deliberate place the
1841// whole error message is spelled out, so the shared clause cannot drift unseen.
1842func Test_apiRun_bracketNameWiring(t *testing.T) {
1843 tests := []struct {
1844 cli string
1845 wantMethod string
1846 wantURI string
1847 wantBody string
1848 wantBodyContains []string
1849 wantErr string
1850 }{
1852 // No -X, so the field makes this a POST, where the name is an error.
1853 cli: `hello --silent -f 'position[base_sha]=abc'`,
1854 wantErr: `field name "position[base_sha]": a field name containing a bracket is not supported in a JSON request body; pass the value as JSON, for example -F 'position={"base_sha":"..."}', or use --input`,
1855 },
1856 // An explicit GET or DELETE, in either case, sends the name as a parameter
1857 // and reaches the wire with the method as typed.
1858 {cli: `hello --silent -X GET -f 'position[base_sha]=abc'`, wantMethod: http.MethodGet, wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1859 {cli: `hello --silent -X get -f 'position[base_sha]=abc'`, wantMethod: "get", wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1860 {cli: `hello --silent -X DELETE -f 'position[base_sha]=abc' -f 'position[new_path]=file.kt'`, wantMethod: http.MethodDelete, wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc&position%5Bnew_path%5D=file.kt"},
1861 {cli: `hello --silent -X delete -f 'position[base_sha]=abc'`, wantMethod: "delete", wantURI: "/api/v4/hello?position%5Bbase_sha%5D=abc"},
1863 // The file supplies the body, so the fields are query parameters even
1864 // though the method still defaults to POST. As query parameters, "a" and
1865 // "a[b]" are two unrelated names.
1866 cli: `hello --silent --input - -f 'position[base_sha]=abc' -f a=1 -f 'a[b]=2'`,
1867 wantMethod: http.MethodPost,
1868 wantURI: "/api/v4/hello?a=1&a%5Bb%5D=2&position%5Bbase_sha%5D=abc",
1869 wantBody: "RAW BODY",
1870 },
1871 // Read from a slice per flag, the interleave would come out 1, 3, 2.
1872 {cli: `hello --silent -X GET -f 'ids[]=1' -F 'ids[]=2' -f 'ids[]=3'`, wantMethod: http.MethodGet, wantURI: "/api/v4/hello?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3"},
1873 // --field overrides --raw-field of the same name, given in either order.
1874 {cli: `hello --silent -X GET -f search=raw -F search=magic`, wantMethod: http.MethodGet, wantURI: "/api/v4/hello?search=magic"},
1875 {cli: `hello --silent -X GET -F search=magic -f search=raw`, wantMethod: http.MethodGet, wantURI: "/api/v4/hello?search=magic"},
1877 // --form never reaches the field parser, so a bracketed part name is sent
1878 // as typed. This is the pin behind the help text's "--form is unaffected".
1879 cli: `hello --silent --form 'position[base_sha]=abc' --form branch=main`,
1880 wantMethod: http.MethodPost,
1881 wantURI: "/api/v4/hello",
1882 wantBodyContains: []string{`name="position[base_sha]"`, "abc", `name="branch"`},
1883 },
1885 // GraphQL is the one path that rewrites the URL and wraps the fields:
1886 // everything but query and operationName is nested under "variables".
1887 cli: `graphql --silent -f 'query=query { currentUser { username } }' -f fullPath=gitlab-org/cli`,
1888 wantMethod: http.MethodPost,
1889 wantURI: "/api/graphql",
1890 wantBodyContains: []string{`"query":"query { currentUser { username } }"`, `"variables":{"fullPath":"gitlab-org/cli"}`},
1891 },
1893 // A bracketed variable name errors before groupGraphQLVariables would
1894 // nest it under "variables", so the guard proves the check runs ahead of
1895 // the GraphQL wrapping rather than after it. The GraphQL spec spells a
1896 // variable name /[_A-Za-z][_0-9A-Za-z]*/, so no valid query names one
1897 // with a bracket: rejecting it removes no working use.
1898 cli: `graphql --silent -f 'query=query { currentUser { username } }' -f 'input[title]=Hello'`,
1899 wantErr: `field name "input[title]": ` + bracketedNameProblem + `, for example -F 'input={"title":"..."}', or use --input`,
1900 },
1903 for _, tt := range tests {
1904 t.Run(tt.cli, func(t *testing.T) {
1905 if tt.wantErr != "" {
1906 err := runAPIArgvGuarded(t, tt.cli)
1907 require.Error(t, err)
1908 assert.Equal(t, tt.wantErr, err.Error())
1909 return
1911 got, err := runAPIArgvRecording(t, tt.cli)
1912 require.NoError(t, err)
1913 assert.Equal(t, tt.wantMethod, got.req.Method)
1914 assert.Equal(t, tt.wantURI, got.req.URL.RequestURI())
1915 if tt.wantBody != "" {
1916 assert.Equal(t, tt.wantBody, string(got.body))
1918 for _, want := range tt.wantBodyContains {
1919 assert.Contains(t, string(got.body), want)
1921 })
1925func Test_parseFields(t *testing.T) {
1926 ios, stdin, _, _ := cmdtest.TestIOStreams()
1927 fmt.Fprint(stdin, "pasted contents")
1929 opts := options{
1930 io: ios,
1931 fields: slices.Concat(
1932 rawFields(
1933 "robot=Hubot",
1934 "destroyer=false",
1935 "helper=true",
1936 "location=@work",
1937 ),
1938 magicFields(
1939 "input=@-",
1940 "enabled=true",
1941 "victories=123",
1942 ),
1943 ),
1946 params, _, err := parseFields(&opts, true)
1947 if err != nil {
1948 t.Fatalf("parseFields error: %v", err)
1951 expect := map[string]any{
1952 "robot": "Hubot",
1953 "destroyer": "false",
1954 "helper": "true",
1955 "location": "@work",
1956 "input": []byte("pasted contents"),
1957 "enabled": true,
1958 "victories": 123,
1960 assert.Equal(t, expect, params)
1963// Test_parseFields_bracketedValuesByFlag pins the boundary between the two
1964// flags: --field parses JSON, --raw-field sends its value literally.
1966// Before this change the array regex ran over the merged parameter map in
1967// httpRequest, so bracketed --raw-field values were converted into arrays too.
1968// That was never the documented behaviour of --raw-field, and array handling now
1969// belongs to --field alone.
1970func Test_parseFields_bracketedValuesByFlag(t *testing.T) {
1971 ios, _, _, _ := cmdtest.TestIOStreams()
1973 opts := options{
1974 io: ios,
1975 fields: slices.Concat(
1976 rawFields(
1977 "rawArray=[api,read_api]",
1978 "rawEmpty=[]",
1979 ),
1980 magicFields(
1981 `parsedArray=["api","read_api"]`,
1982 "parsedEmpty=[]",
1983 ),
1984 ),
1987 params, _, err := parseFields(&opts, true)
1988 require.NoError(t, err)
1990 assert.Equal(t, map[string]any{
1991 "rawArray": "[api,read_api]",
1992 "rawEmpty": "[]",
1993 "parsedArray": []any{"api", "read_api"},
1994 "parsedEmpty": []any{},
1995 }, params)
1998func Test_warnOnLegacyRawArrays(t *testing.T) {
1999 tests := []struct {
2000 name string
2001 method string
2002 fields []fieldFlag
2003 wantHint bool
2004 // wantOrder, when set, pins the keys warned about and their order.
2005 // Warnings follow flag order: reading them off the parameter map would
2006 // leave the order to Go's map iteration, which is the same defect this
2007 // change rejects in expandPlaceholdersIn.
2008 wantOrder []string
2009 // wantHintText, when set, is the whole line the hint prints.
2010 wantHintText string
2011 // wantErr, when set, is the error parseFields returns instead.
2012 wantErr string
2013 }{
2015 name: "warns on a write method for the old shorthand shape",
2016 method: http.MethodPost,
2017 fields: rawFields("scopes=[api,read_api]"),
2018 wantHint: true,
2019 },
2021 name: "silent on GET, where values were never converted",
2022 method: http.MethodGet,
2023 fields: rawFields("scopes=[api,read_api]"),
2024 wantHint: false,
2025 },
2027 name: "silent for a value the old regex never matched",
2028 method: http.MethodPost,
2029 fields: rawFields("topics=[My-Topic]"),
2030 wantHint: false,
2031 },
2033 name: "silent for an ordinary string",
2034 method: http.MethodPost,
2035 fields: rawFields("robot=Hubot"),
2036 wantHint: false,
2037 },
2039 name: "warns in flag order, not map order",
2040 method: http.MethodPost,
2041 fields: rawFields("ccc=[three]", "aaa=[one]", "bbb=[two]"),
2042 wantHint: true,
2043 wantOrder: []string{"ccc", "aaa", "bbb"},
2044 },
2046 // Warning here would describe a request body that does not exist.
2047 name: "silent when a --field of the same name overrides the raw value",
2048 method: http.MethodPost,
2049 fields: slices.Concat(rawFields("scopes=[api,read_api]"), magicFields(`scopes=["api","read_api"]`)),
2050 wantHint: false,
2051 },
2053 // The hint's own -F 's[scopes]=[...]' is a bracketed name too.
2054 name: "errors before warning for a nested name on a write method",
2055 method: http.MethodPost,
2056 fields: rawFields("s[scopes]=[api,read_api]"),
2057 wantErr: `field name "s[scopes]": ` + bracketedNameProblem + `, for example -F 's={"scopes":"..."}', or use --input`,
2058 },
2060 // The case the hint used to advise on directly.
2061 name: "errors before warning for an array element on a write method",
2062 method: http.MethodPost,
2063 fields: rawFields("s[]=[api,read_api]"),
2064 wantErr: `field name "s[]": ` + bracketedNameProblem + `, for example -F 's=[...]', or use --input`,
2065 },
2067 name: "silent for a nested name on GET, where names stay literal",
2068 method: http.MethodGet,
2069 fields: rawFields("s[scopes]=[api,read_api]"),
2070 wantHint: false,
2071 },
2074 for _, tt := range tests {
2075 t.Run(tt.name, func(t *testing.T) {
2076 ios, _, _, stderr := cmdtest.TestIOStreams()
2077 opts := options{io: ios, fields: tt.fields}
2079 params, rawKeys, err := parseFields(&opts, opts.fieldsBecomeJSONBody(tt.method))
2080 if tt.wantErr != "" {
2081 require.Error(t, err)
2082 assert.Equal(t, tt.wantErr, err.Error())
2083 assert.Empty(t, stderr.String())
2084 return
2086 require.NoError(t, err)
2088 opts.warnOnLegacyRawArrays(tt.method, params, rawKeys)
2090 if tt.wantOrder != nil {
2091 var warned []string
2092 for line := range strings.SplitSeq(strings.TrimSpace(stderr.String()), "\n") {
2093 warned = append(warned, strings.SplitN(line, `"`, 3)[1])
2095 assert.Equal(t, tt.wantOrder, warned)
2098 if tt.wantHintText != "" {
2099 assert.Equal(t, tt.wantHintText, stderr.String())
2102 if tt.wantHint {
2103 assert.Contains(t, stderr.String(), "sent as the literal string")
2104 } else {
2105 assert.Empty(t, stderr.String())
2107 })
2111func Test_magicFieldValue(t *testing.T) {
2112 f, err := os.CreateTemp(t.TempDir(), "gitlab-test")
2113 require.NoError(t, err)
2114 _, err = f.WriteString("file contents")
2115 require.NoError(t, err)
2116 require.NoError(t, f.Close())
2118 ios, _, _, _ := cmdtest.TestIOStreams()
2120 type args struct {
2121 v string
2122 opts *options
2124 tests := []struct {
2125 name string
2126 args args
2127 want any
2128 wantErr bool
2129 // wantErrContains pins the user-visible wording where it carries
2130 // diagnostic value, so a future refactor cannot quietly flatten a
2131 // decoder message back into a generic one.
2132 wantErrContains string
2133 }{
2135 name: "string",
2136 args: args{v: "hello"},
2137 want: "hello",
2138 wantErr: false,
2139 },
2141 name: "bool true",
2142 args: args{v: "true"},
2143 want: true,
2144 wantErr: false,
2145 },
2147 name: "bool false",
2148 args: args{v: "false"},
2149 want: false,
2150 wantErr: false,
2151 },
2153 name: "null",
2154 args: args{v: "null"},
2155 want: nil,
2156 wantErr: false,
2157 },
2159 name: "placeholder",
2160 args: args{
2161 v: ":namespace",
2162 opts: &options{
2163 io: ios,
2164 baseRepo: func() (glrepo.Interface, error) {
2165 return glrepo.New("gitlab-com", "www-gitlab-com", glinstance.DefaultHostname), nil
2166 },
2167 },
2168 },
2169 want: "gitlab-com",
2170 wantErr: false,
2171 },
2173 name: "branch placeholder is not URL-encoded in field values",
2174 args: args{
2175 v: ":branch",
2176 opts: &options{
2177 io: ios,
2178 baseRepo: func() (glrepo.Interface, error) {
2179 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2180 },
2181 branch: func() (string, error) {
2182 return "feature/foo", nil
2183 },
2184 },
2185 },
2186 want: "feature/foo",
2187 wantErr: false,
2188 },
2190 name: "file",
2191 args: args{
2192 v: "@" + f.Name(),
2193 opts: &options{io: ios},
2194 },
2195 want: []byte("file contents"),
2196 wantErr: false,
2197 },
2199 name: "file error",
2200 args: args{
2201 v: "@",
2202 opts: &options{io: ios},
2203 },
2204 want: nil,
2205 wantErr: true,
2206 },
2208 name: "JSON array",
2209 args: args{v: `["my-topic","GitLab"]`},
2210 want: []any{"my-topic", "GitLab"},
2211 wantErr: false,
2212 },
2214 name: "JSON object preserves number as json.Number",
2215 args: args{v: `{"key":"value","count":42}`},
2216 want: map[string]any{"key": "value", "count": json.Number("42")},
2217 wantErr: false,
2218 },
2220 name: "nested JSON array",
2221 args: args{v: `["a",["b","c"]]`},
2222 want: []any{"a", []any{"b", "c"}},
2223 wantErr: false,
2224 },
2226 name: "unparseable value with bracket prefix",
2227 args: args{v: `[api,read_api]`},
2228 want: nil,
2229 wantErr: true,
2230 wantErrContains: "invalid character 'a' looking for beginning of value",
2231 },
2233 name: "unparseable value with brace prefix",
2234 args: args{v: `{bad}`},
2235 want: nil,
2236 wantErr: true,
2237 wantErrContains: "invalid character 'b' looking for beginning of object key string",
2238 },
2240 name: "large integer keeps full precision",
2241 args: args{v: `[9007199254740993]`},
2242 want: []any{json.Number("9007199254740993")},
2243 wantErr: false,
2244 },
2246 name: "trailing data after JSON is rejected",
2247 args: args{v: `[1,2]oops`},
2248 want: nil,
2249 wantErr: true,
2250 wantErrContains: "unexpected trailing data: invalid character 'o' looking for beginning of value",
2251 },
2253 // dec.Token() returns a legal token and a nil error here, so the
2254 // trailing-data branch has to supply its own reason rather than
2255 // wrapping nil.
2256 name: "a second JSON value is rejected",
2257 args: args{v: `[1,2][3]`},
2258 want: nil,
2259 wantErr: true,
2260 wantErrContains: "unexpected trailing data: more than one JSON value",
2261 },
2262 // Empty-array handling carried over from !3596, which fixed it in
2263 // parseStringArrayField before this change replaced that function with
2264 // json.Unmarshal. Both inputs must still yield an empty slice, not a
2265 // slice holding one empty string.
2267 name: "empty array",
2268 args: args{v: `[]`},
2269 want: []any{},
2270 wantErr: false,
2271 },
2273 name: "empty array with interior whitespace",
2274 args: args{v: `[ ]`},
2275 want: []any{},
2276 wantErr: false,
2277 },
2278 // Boundary cases from !3536 by @ihopenre-eng, which pinned the same
2279 // string-versus-array question from the regex side. Unquoted integers are
2280 // not a JSON array of strings, so they parse as numbers; quoting is how a
2281 // caller asks for string elements.
2283 name: "digit elements parse as numbers, not strings",
2284 args: args{v: `[1, 2]`},
2285 want: []any{json.Number("1"), json.Number("2")},
2286 wantErr: false,
2287 },
2289 name: "quoted digit elements stay strings",
2290 args: args{v: `["1", "2"]`},
2291 want: []any{"1", "2"},
2292 wantErr: false,
2293 },
2295 name: "quoted element containing a comma is one element",
2296 args: args{v: `["one, two", "three"]`},
2297 want: []any{"one, two", "three"},
2298 wantErr: false,
2299 },
2301 name: "mixed quoted and unquoted is not valid JSON",
2302 args: args{v: `["one, two", three]`},
2303 want: nil,
2304 wantErr: true,
2305 },
2307 name: "placeholder inside a JSON object is expanded",
2308 args: args{
2309 v: `{"projectPath":":fullpath"}`,
2310 opts: &options{
2311 io: ios,
2312 baseRepo: func() (glrepo.Interface, error) {
2313 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2314 },
2315 },
2316 },
2317 want: map[string]any{"projectPath": "glab-cli/test"},
2318 wantErr: false,
2319 },
2321 // Go randomizes map iteration order, so resolving this by last
2322 // writer wins would drop one of the two fields non-deterministically
2323 // between runs of the same command. Reject it instead.
2324 name: "two keys expanding to the same name are rejected",
2325 args: args{
2326 v: `{":repo":"from-placeholder","test":"from-literal"}`,
2327 opts: &options{
2328 io: ios,
2329 baseRepo: func() (glrepo.Interface, error) {
2330 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2331 },
2332 },
2333 },
2334 want: nil,
2335 wantErr: true,
2336 wantErrContains: `keys ":repo" and "test" both expand to "test"`,
2337 },
2339 name: "a key expanding onto an unrelated name is left alone",
2340 args: args{
2341 v: `{":repo":"a","other":"b"}`,
2342 opts: &options{
2343 io: ios,
2344 baseRepo: func() (glrepo.Interface, error) {
2345 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2346 },
2347 },
2348 },
2349 want: map[string]any{"test": "a", "other": "b"},
2350 wantErr: false,
2351 },
2353 name: "placeholder inside a JSON array is expanded",
2354 args: args{
2355 v: `[":branch"]`,
2356 opts: &options{
2357 io: ios,
2358 baseRepo: func() (glrepo.Interface, error) {
2359 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2360 },
2361 branch: func() (string, error) {
2362 return "feature/foo", nil
2363 },
2364 },
2365 },
2366 want: []any{"feature/foo"},
2367 wantErr: false,
2368 },
2370 // git accepts a double quote in a refname, so expanding into the raw
2371 // JSON text before decoding would let a branch name close the string
2372 // and inject structure. Expansion happens after decoding, so the
2373 // quote stays data: one key, one string value, no injected field.
2374 name: "quote in an expanded branch name cannot inject JSON structure",
2375 args: args{
2376 v: `{"ref":":branch"}`,
2377 opts: &options{
2378 io: ios,
2379 baseRepo: func() (glrepo.Interface, error) {
2380 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2381 },
2382 branch: func() (string, error) {
2383 return `x","admin":"true`, nil
2384 },
2385 },
2386 },
2387 want: map[string]any{"ref": `x","admin":"true`},
2388 wantErr: false,
2389 },
2391 for _, tt := range tests {
2392 t.Run(tt.name, func(t *testing.T) {
2393 if tt.args.opts == nil {
2394 tt.args.opts = &options{}
2396 got, err := magicFieldValue(tt.args.v, tt.args.opts)
2397 if (err != nil) != tt.wantErr {
2398 t.Errorf("magicFieldValue() error = %v, wantErr %v", err, tt.wantErr)
2399 return
2401 if tt.wantErr {
2402 if tt.wantErrContains != "" {
2403 require.ErrorContains(t, err, tt.wantErrContains)
2404 assert.NotContains(t, err.Error(), "invalid JSON",
2405 "the decoder message and the field-name prefix already say the value did not parse")
2407 return
2409 assert.Equal(t, tt.want, got)
2410 })
2414func Test_openUserFile(t *testing.T) {
2415 f, err := os.CreateTemp(t.TempDir(), "gitlab-test")
2416 require.NoError(t, err)
2417 _, err = f.WriteString("file contents")
2418 require.NoError(t, err)
2419 require.NoError(t, f.Close())
2421 file, length, err := openUserFile(f.Name(), nil)
2422 if err != nil {
2423 t.Fatal(err)
2425 defer file.Close()
2427 fb, err := io.ReadAll(file)
2428 if err != nil {
2429 t.Fatal(err)
2432 assert.Equal(t, int64(13), length)
2433 assert.Equal(t, "file contents", string(fb))
2436func Test_fillPlaceholders(t *testing.T) {
2437 type args struct {
2438 value string
2439 opts *options
2441 tests := []struct {
2442 name string
2443 args args
2444 want string
2445 wantErr bool
2446 }{
2448 name: "no changes",
2449 args: args{
2450 value: "projects/namespace%2Frepo/releases",
2451 opts: &options{
2452 baseRepo: nil,
2453 },
2454 },
2455 want: "projects/namespace%2Frepo/releases",
2456 wantErr: false,
2457 },
2459 name: "has substitutes",
2460 args: args{
2461 value: "projects/:namespace%2F:repo/releases",
2462 opts: &options{
2463 baseRepo: func() (glrepo.Interface, error) {
2464 return glrepo.New("gitlab-com", "www-gitlab-com", glinstance.DefaultHostname), nil
2465 },
2466 },
2467 },
2468 want: "projects/gitlab-com%2Fwww-gitlab-com/releases",
2469 wantErr: false,
2470 },
2472 name: "has branch placeholder",
2473 args: args{
2474 value: "projects/glab-cli%2Ftest/branches/:branch/.../.../",
2475 opts: &options{
2476 baseRepo: func() (glrepo.Interface, error) {
2477 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2478 },
2479 branch: func() (string, error) {
2480 return "master", nil
2481 },
2482 },
2483 },
2484 want: "projects/glab-cli%2Ftest/branches/master/.../.../",
2485 wantErr: false,
2486 },
2488 name: "branch placeholder URL-encodes slashes",
2489 args: args{
2490 value: "projects/:fullpath/protected_branches/:branch",
2491 opts: &options{
2492 baseRepo: func() (glrepo.Interface, error) {
2493 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2494 },
2495 branch: func() (string, error) {
2496 return "feature/foo", nil
2497 },
2498 },
2499 },
2500 want: "projects/glab-cli%2Ftest/protected_branches/feature%2Ffoo",
2501 wantErr: false,
2502 },
2504 name: "has branch placeholder and git is in detached head",
2505 args: args{
2506 value: "projects/:fullpath/branches/:branch",
2507 opts: &options{
2508 baseRepo: func() (glrepo.Interface, error) {
2509 return glrepo.New("glab-cli", "test", glinstance.DefaultHostname), nil
2510 },
2511 branch: func() (string, error) {
2512 return "", git.ErrNotOnAnyBranch
2513 },
2514 },
2515 },
2516 want: "projects/:fullpath/branches/:branch",
2517 wantErr: true,
2518 },
2520 name: "no greedy substitutes",
2521 args: args{
2522 value: ":namespaces/:repository",
2523 opts: &options{
2524 baseRepo: nil,
2525 },
2526 },
2527 want: ":namespaces/:repository",
2528 wantErr: false,
2529 },
2531 for _, tt := range tests {
2532 t.Run(tt.name, func(t *testing.T) {
2533 if tt.args.opts == nil {
2534 tt.args.opts = &options{}
2536 got, err := fillPlaceholders(tt.args.value, tt.args.opts, true)
2537 if (err != nil) != tt.wantErr {
2538 t.Errorf("fillPlaceholders() error = %v, wantErr %v", err, tt.wantErr)
2539 return
2541 if got != tt.want {
2542 t.Errorf("fillPlaceholders() got = %v, want %v", got, tt.want)
2544 })
2548func Test_apiRun_customPlaceholders(t *testing.T) {
2549 tests := []struct {
2550 name string
2551 cli string
2552 wantURI string
2553 wantBody string
2554 }{
2556 name: "percent-encoded in the endpoint",
2557 cli: `projects/123/repository/branches/:target --silent --placeholder target=feat/x`,
2558 wantURI: "/api/v4/projects/123/repository/branches/feat%2Fx",
2559 },
2561 name: "sent unchanged in a --field value",
2562 cli: `projects/123/repository/branches --silent --placeholder target=feat/x -F branch=:target -f ref=main`,
2563 wantURI: "/api/v4/projects/123/repository/branches",
2564 wantBody: `{"branch":"feat/x","ref":"main"}`,
2565 },
2567 name: "left alone in a --raw-field value",
2568 cli: `projects/123/repository/branches --silent --placeholder target=feat/x -f branch=:target`,
2569 wantURI: "/api/v4/projects/123/repository/branches",
2570 wantBody: `{"branch":":target"}`,
2571 },
2573 name: "built-in and custom placeholders together",
2574 cli: `projects/:fullpath/repository/branches/:target --silent --placeholder target=feat/x`,
2575 wantURI: "/api/v4/projects/OWNER%2FREPO/repository/branches/feat%2Fx",
2576 },
2578 name: "several placeholders",
2579 cli: `projects/:proj/repository/branches/:target --silent --placeholder proj=group/app --placeholder target=main`,
2580 wantURI: "/api/v4/projects/group%2Fapp/repository/branches/main",
2581 },
2583 name: "a name extending a built-in one wins over it",
2584 cli: `hello/:repo-name --silent --placeholder repo-name=x`,
2585 wantURI: "/api/v4/hello/x",
2586 },
2588 name: "a value is not expanded again",
2589 cli: `hello/:wrapped --silent --placeholder wrapped=:repo`,
2590 wantURI: "/api/v4/hello/:repo",
2591 },
2593 name: "an undefined name is sent verbatim",
2594 cli: `hello/:other --silent --placeholder target=x`,
2595 wantURI: "/api/v4/hello/:other",
2596 },
2598 name: "an empty value is allowed",
2599 cli: `hello/:empty/world --silent --placeholder empty=`,
2600 wantURI: "/api/v4/hello//world",
2601 },
2604 for _, tt := range tests {
2605 t.Run(tt.name, func(t *testing.T) {
2606 got, err := runAPIArgvRecording(t, tt.cli)
2607 require.NoError(t, err)
2608 assert.Equal(t, tt.wantURI, got.req.URL.RequestURI())
2609 if tt.wantBody != "" {
2610 assert.JSONEq(t, tt.wantBody, string(got.body))
2612 })
2616func Test_apiRun_customPlaceholderErrors(t *testing.T) {
2617 const badName = `must start with a letter, contain only letters, digits, '_', or '-', and not end with '-'`
2618 tests := []struct {
2619 cli string
2620 wantErr string
2621 }{
2622 {cli: `hello --placeholder target`, wantErr: `invalid --placeholder "target": expected <name>=<value>`},
2623 {cli: `hello --placeholder =value`, wantErr: `invalid --placeholder name "": ` + badName},
2624 {cli: `hello --placeholder 1st=value`, wantErr: `invalid --placeholder name "1st": ` + badName},
2625 {cli: `hello --placeholder trailing-=value`, wantErr: `invalid --placeholder name "trailing-": ` + badName},
2626 {cli: `hello --placeholder a/b=value`, wantErr: `invalid --placeholder name "a/b": ` + badName},
2627 {cli: `hello --placeholder branch=main`, wantErr: `invalid --placeholder name "branch": conflicts with a built-in placeholder`},
2628 {cli: `hello --placeholder target=a --placeholder target=b`, wantErr: `invalid --placeholder name "target": defined more than once`},
2631 for _, tt := range tests {
2632 t.Run(tt.cli, func(t *testing.T) {
2633 err := runAPIArgvGuarded(t, tt.cli)
2634 var flagErr *cmdutils.FlagError
2635 require.ErrorAs(t, err, &flagErr)
2636 assert.Equal(t, tt.wantErr, err.Error())
2637 })
2641func TestFillPlaceholdersReturnsAPIClientError(t *testing.T) {
2642 t.Parallel()
2644 apiClientErr := errors.New("failed to create API client")
2645 tests := []struct {
2646 name string
2647 value string
2648 baseRepo func() (glrepo.Interface, error)
2649 }{
2651 name: "project ID",
2652 value: "projects/:id",
2653 baseRepo: func() (glrepo.Interface, error) {
2654 return glrepo.New("gitlab-com", "www-gitlab-com", glinstance.DefaultHostname), nil
2655 },
2656 },
2658 name: "current user",
2659 value: "users/:user",
2660 },
2663 for _, tt := range tests {
2664 t.Run(tt.name, func(t *testing.T) {
2665 t.Parallel()
2667 opts := &options{
2668 baseRepo: tt.baseRepo,
2669 apiClient: func(string) (*api.Client, error) {
2670 return nil, apiClientErr
2671 },
2674 got, err := fillPlaceholders(tt.value, opts, true)
2676 require.ErrorIs(t, err, apiClientErr)
2677 assert.Equal(t, tt.value, got)
2678 })
2682func Test_processResponse_bodyConsumption(t *testing.T) {
2683 t.Parallel()
2685 tests := []struct {
2686 name string
2687 options options
2688 httpResponse *http.Response
2689 expectedCursor string
2690 expectError bool
2691 checkStdout func(t *testing.T, stdout string)
2692 checkStderr func(t *testing.T, stderr string)
2693 }{
2695 name: "GraphQL pagination - body read once",
2696 options: options{
2697 requestPath: "graphql",
2698 paginate: true,
2699 },
2700 httpResponse: &http.Response{
2701 StatusCode: http.StatusOK,
2702 Header: http.Header{"Content-Type": []string{"application/json"}},
2703 Body: io.NopCloser(bytes.NewBufferString(`{
2704 "data": {
2705 "issues": ["issue1", "issue2"],
2706 "pageInfo": {
2707 "endCursor": "CURSOR_123",
2708 "hasNextPage": true
2711 }`)),
2712 },
2713 expectedCursor: "CURSOR_123",
2714 expectError: false,
2715 checkStdout: func(t *testing.T, stdout string) {
2716 t.Helper()
2717 assert.Contains(t, stdout, "issue")
2718 },
2719 checkStderr: func(t *testing.T, stderr string) {
2720 t.Helper()
2721 assert.Empty(t, stderr)
2722 },
2723 },
2725 name: "GraphQL pagination - no next page",
2726 options: options{
2727 requestPath: "graphql",
2728 paginate: true,
2729 },
2730 httpResponse: &http.Response{
2731 StatusCode: http.StatusOK,
2732 Header: http.Header{"Content-Type": []string{"application/json"}},
2733 Body: io.NopCloser(bytes.NewBufferString(`{
2734 "data": {
2735 "issues": ["issue3"],
2736 "pageInfo": {
2737 "endCursor": "CURSOR_END",
2738 "hasNextPage": false
2741 }`)),
2742 },
2743 expectedCursor: "",
2744 expectError: false,
2745 checkStdout: func(t *testing.T, stdout string) {
2746 t.Helper()
2747 assert.Contains(t, stdout, "issue")
2748 },
2749 },
2751 name: "GraphQL pagination - empty data array",
2752 options: options{
2753 requestPath: "graphql",
2754 paginate: true,
2755 },
2756 httpResponse: &http.Response{
2757 StatusCode: http.StatusOK,
2758 Header: http.Header{"Content-Type": []string{"application/json"}},
2759 Body: io.NopCloser(bytes.NewBufferString(`{
2760 "data": {
2761 "issues": [],
2762 "pageInfo": {
2763 "endCursor": "",
2764 "hasNextPage": false
2767 }`)),
2768 },
2769 expectedCursor: "",
2770 expectError: false,
2771 checkStdout: func(t *testing.T, stdout string) {
2772 t.Helper()
2773 assert.Contains(t, stdout, "issues")
2774 },
2775 },
2777 name: "GraphQL error response with empty errors array",
2778 options: options{
2779 requestPath: "graphql",
2780 paginate: true,
2781 },
2782 httpResponse: &http.Response{
2783 StatusCode: http.StatusOK,
2784 Header: http.Header{"Content-Type": []string{"application/json"}},
2785 Body: io.NopCloser(bytes.NewBufferString(`{
2786 "data": {
2787 "issues": ["issue1"],
2788 "pageInfo": {
2789 "endCursor": "CURSOR_XYZ",
2790 "hasNextPage": true
2792 },
2793 "errors": []
2794 }`)),
2795 },
2796 expectedCursor: "CURSOR_XYZ",
2797 expectError: false,
2798 checkStdout: func(t *testing.T, stdout string) {
2799 t.Helper()
2800 assert.Contains(t, stdout, "issue")
2801 },
2802 },
2804 name: "GraphQL error response - body not consumed twice",
2805 options: options{
2806 requestPath: "graphql",
2807 paginate: true,
2808 },
2809 httpResponse: &http.Response{
2810 StatusCode: http.StatusOK,
2811 Header: http.Header{"Content-Type": []string{"application/json"}},
2812 Body: io.NopCloser(bytes.NewBufferString(`{
2813 "data": {
2814 "issues": [],
2815 "pageInfo": {
2816 "endCursor": "",
2817 "hasNextPage": false
2819 },
2820 "errors": [{"message": "Invalid query"}]
2821 }`)),
2822 },
2823 expectedCursor: "",
2824 expectError: true,
2825 checkStdout: func(t *testing.T, stdout string) {
2826 t.Helper()
2827 assert.Contains(t, stdout, "errors")
2828 },
2829 checkStderr: func(t *testing.T, stderr string) {
2830 t.Helper()
2831 assert.Contains(t, stderr, "Invalid query")
2832 },
2833 },
2835 name: "GraphQL error response with data and cursor",
2836 options: options{
2837 requestPath: "graphql",
2838 paginate: true,
2839 },
2840 httpResponse: &http.Response{
2841 StatusCode: http.StatusOK,
2842 Header: http.Header{"Content-Type": []string{"application/json"}},
2843 Body: io.NopCloser(bytes.NewBufferString(`{
2844 "data": {
2845 "issues": [{"id": 1}],
2846 "pageInfo": {
2847 "endCursor": "CURSOR_AFTER_ERROR",
2848 "hasNextPage": true
2850 },
2851 "errors": [
2852 {"message": "Field 'nonexistent' doesn't exist on type 'Issue'"},
2853 {"message": "Some other error"}
2855 }`)),
2856 },
2857 expectedCursor: "",
2858 expectError: true,
2859 checkStdout: func(t *testing.T, stdout string) {
2860 t.Helper()
2861 assert.Contains(t, stdout, "issues")
2862 },
2863 checkStderr: func(t *testing.T, stderr string) {
2864 t.Helper()
2865 assert.Contains(t, stderr, "nonexistent")
2866 assert.Contains(t, stderr, "Some other error")
2867 },
2868 },
2870 name: "GraphQL with color output enabled",
2871 options: options{
2872 requestPath: "graphql",
2873 paginate: true,
2874 },
2875 httpResponse: &http.Response{
2876 StatusCode: http.StatusOK,
2877 Header: http.Header{"Content-Type": []string{"application/json"}},
2878 Body: io.NopCloser(bytes.NewBufferString(`{
2879 "data": {
2880 "issues": [{"id": 1, "title": "Test Issue"}],
2881 "pageInfo": {
2882 "endCursor": "NEXT_CURSOR",
2883 "hasNextPage": true
2886 }`)),
2887 },
2888 expectedCursor: "NEXT_CURSOR",
2889 expectError: false,
2890 checkStdout: func(t *testing.T, stdout string) {
2891 t.Helper()
2892 assert.Contains(t, stdout, "Test Issue")
2893 },
2894 },
2896 name: "REST pagination - body not consumed twice",
2897 options: options{
2898 requestPath: "issues",
2899 paginate: true,
2900 },
2901 httpResponse: &http.Response{
2902 StatusCode: http.StatusOK,
2903 Header: http.Header{"Content-Type": []string{"application/json"}},
2904 Body: io.NopCloser(bytes.NewBufferString(`[{"id": 1}]`)),
2905 },
2906 expectedCursor: "",
2907 expectError: false,
2908 checkStdout: func(t *testing.T, stdout string) {
2909 t.Helper()
2910 assert.Contains(t, stdout, "id")
2911 },
2912 },
2915 for _, tt := range tests {
2916 t.Run(tt.name, func(t *testing.T) {
2917 t.Parallel()
2919 ios, _, stdout, stderr := cmdtest.TestIOStreams()
2920 tt.options.io = ios
2921 tt.options.baseRepo = func() (glrepo.Interface, error) {
2922 return nil, fmt.Errorf("not supposed to be called")
2924 tt.options.apiClient = func(repoHost string) (*api.Client, error) {
2925 return nil, fmt.Errorf("not supposed to be called")
2928 endCursor, err := processResponse(tt.httpResponse, &tt.options, io.Discard)
2930 if tt.expectError {
2931 require.Error(t, err, "expected error but got none")
2932 } else {
2933 require.NoError(t, err, "unexpected error: %v", err)
2936 // Verify cursor extraction
2937 assert.Equal(t, tt.expectedCursor, endCursor, "cursor mismatch")
2939 // Run custom stdout/stderr checks if provided
2940 if tt.checkStdout != nil {
2941 tt.checkStdout(t, stdout.String())
2943 if tt.checkStderr != nil {
2944 tt.checkStderr(t, stderr.String())
2946 })
2950// Test_findEndCursor_multiplePages verifies cursor extraction works across multiple pages
2951func Test_findEndCursor_multiplePages(t *testing.T) {
2952 t.Parallel()
2954 tests := []struct {
2955 name string
2956 jsonResponse string
2957 expectedCursor string
2958 }{
2960 name: "simple pagination",
2961 jsonResponse: `{
2962 "data": {
2963 "issues": [{"id": 1}],
2964 "pageInfo": {
2965 "endCursor": "ABC123",
2966 "hasNextPage": true
2969 }`,
2970 expectedCursor: "ABC123",
2971 },
2973 name: "nested pagination",
2974 jsonResponse: `{
2975 "data": {
2976 "project": {
2977 "issues": {
2978 "nodes": [{"id": 1}],
2979 "pageInfo": {
2980 "endCursor": "XYZ789",
2981 "hasNextPage": true
2986 }`,
2987 expectedCursor: "XYZ789",
2988 },
2990 name: "no next page",
2991 jsonResponse: `{
2992 "data": {
2993 "issues": [{"id": 1}],
2994 "pageInfo": {
2995 "endCursor": "LAST",
2996 "hasNextPage": false
2999 }`,
3000 expectedCursor: "",
3001 },
3003 name: "missing pageInfo",
3004 jsonResponse: `{
3005 "data": {
3006 "issues": [{"id": 1}]
3008 }`,
3009 expectedCursor: "",
3010 },
3013 for _, tt := range tests {
3014 t.Run(tt.name, func(t *testing.T) {
3015 t.Parallel()
3017 reader := bytes.NewBufferString(tt.jsonResponse)
3018 cursor := findEndCursor(reader)
3019 if cursor != tt.expectedCursor {
3020 t.Errorf("expected cursor %q, got %q", tt.expectedCursor, cursor)
3022 })
Diagnostics7 git calls · 36 ms
CallStatusSizeTime036 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 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
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=44
Open pack map
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=3
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:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
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=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200285 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=285
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=48bbcb7832066dead74ecdecb6257183f3d03bf0
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=285
git.pkt.payload.max=258
git.sideband.band1.bytes=258
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=258
git.pack.trailer=2570819fa1caef7b0fd3f7ba2d1b1a658b78702e
Open pack map
fetch20019 KiB · hit24 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=24
git.response.bytes=19526
git.response.cache=HIT
git.response.duration_ms=24
git.fetch.wants=cfdad800fc480685826c4e86484559b247a451cf
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=19526
git.pkt.payload.max=19499
git.sideband.band1.bytes=19499
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
a5e1af854842f54b
Request
4caeb4ab…
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