diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 15a2b3be4..170c41b55 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -135,7 +135,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output") cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results") cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The file to use as body for the HTTP request") - cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Silence the output") + cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body") return cmd } @@ -180,6 +180,7 @@ func apiRun(opts *ApiOptions) error { return err } + headersOutputStream := opts.IO.Out if opts.Silent { opts.IO.Out = ioutil.Discard } @@ -191,7 +192,7 @@ func apiRun(opts *ApiOptions) error { return err } - endCursor, err := processResponse(resp, opts) + endCursor, err := processResponse(resp, opts, headersOutputStream) if err != nil { return err } @@ -217,11 +218,11 @@ func apiRun(opts *ApiOptions) error { return nil } -func processResponse(resp *http.Response, opts *ApiOptions) (endCursor string, err error) { - if opts.ShowResponseHeaders && !opts.Silent { - fmt.Fprintln(opts.IO.Out, resp.Proto, resp.Status) - printHeaders(opts.IO.Out, resp.Header, opts.IO.ColorEnabled()) - fmt.Fprint(opts.IO.Out, "\r\n") +func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream io.Writer) (endCursor string, err error) { + if opts.ShowResponseHeaders { + fmt.Fprintln(headersOutputStream, resp.Proto, resp.Status) + printHeaders(headersOutputStream, resp.Header, opts.IO.ColorEnabled()) + fmt.Fprint(headersOutputStream, "\r\n") } if resp.StatusCode == 204 { diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 75c8eaa4a..f590d93b4 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -288,6 +288,36 @@ func Test_apiRun(t *testing.T) { stdout: `gateway timeout`, stderr: "gh: HTTP 502\n", }, + { + name: "silent", + options: ApiOptions{ + Silent: true, + }, + httpResponse: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), + }, + err: nil, + stdout: ``, + stderr: ``, + }, + { + name: "show response headers even when silent", + options: ApiOptions{ + ShowResponseHeaders: true, + Silent: true, + }, + httpResponse: &http.Response{ + Proto: "HTTP/1.1", + Status: "200 Okey-dokey", + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), + Header: http.Header{"Content-Type": []string{"text/plain"}}, + }, + err: nil, + stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n", + stderr: ``, + }, } for _, tt := range tests { @@ -449,38 +479,6 @@ func Test_apiRun_paginationGraphQL(t *testing.T) { assert.Equal(t, "PAGE1_END", endCursor) } -func Test_apiRun_silent(t *testing.T) { - io, _, stdout, stderr := iostreams.Test() - response := &http.Response{ - StatusCode: 200, - Body: ioutil.NopCloser(bytes.NewBufferString(`body`)), - Header: http.Header{"Content-Type": []string{"text/plain"}}, - } - - options := ApiOptions{ - IO: io, - HttpClient: func() (*http.Client, error) { - var tr roundTripper = func(req *http.Request) (*http.Response, error) { - resp := response - resp.Request = req - return resp, nil - } - return &http.Client{Transport: tr}, nil - }, - RequestPath: "issues", - ShowResponseHeaders: true, - Silent: true, - } - - err := apiRun(&options) - assert.NoError(t, err) - - assert.Equal(t, "", stdout.String(), "stdout") - assert.Equal(t, "", stderr.String(), "stderr") - - assert.Equal(t, "https://api.github.com/issues", response.Request.URL.String()) -} - func Test_apiRun_inputFile(t *testing.T) { tests := []struct { name string