X Tutup
Skip to content

Commit 7a04bf1

Browse files
api --silent Changes:
Show Response Headers (if requested) even with `--silent` flag Shift silent tests to `Test_apiRun` Changed usage string of `--silent` flag
1 parent aa43c55 commit 7a04bf1

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

pkg/cmd/api/api.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ original query accepts an '$endCursor: String' variable and that it fetches the
135135
cmd.Flags().BoolVarP(&opts.ShowResponseHeaders, "include", "i", false, "Include HTTP response headers in the output")
136136
cmd.Flags().BoolVar(&opts.Paginate, "paginate", false, "Make additional HTTP requests to fetch all pages of results")
137137
cmd.Flags().StringVar(&opts.RequestInputFile, "input", "", "The file to use as body for the HTTP request")
138-
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Silence the output")
138+
cmd.Flags().BoolVar(&opts.Silent, "silent", false, "Do not print the response body")
139139
return cmd
140140
}
141141

@@ -180,6 +180,7 @@ func apiRun(opts *ApiOptions) error {
180180
return err
181181
}
182182

183+
headersOutputStream := opts.IO.Out
183184
if opts.Silent {
184185
opts.IO.Out = ioutil.Discard
185186
}
@@ -191,7 +192,7 @@ func apiRun(opts *ApiOptions) error {
191192
return err
192193
}
193194

194-
endCursor, err := processResponse(resp, opts)
195+
endCursor, err := processResponse(resp, opts, headersOutputStream)
195196
if err != nil {
196197
return err
197198
}
@@ -217,11 +218,11 @@ func apiRun(opts *ApiOptions) error {
217218
return nil
218219
}
219220

220-
func processResponse(resp *http.Response, opts *ApiOptions) (endCursor string, err error) {
221-
if opts.ShowResponseHeaders && !opts.Silent {
222-
fmt.Fprintln(opts.IO.Out, resp.Proto, resp.Status)
223-
printHeaders(opts.IO.Out, resp.Header, opts.IO.ColorEnabled())
224-
fmt.Fprint(opts.IO.Out, "\r\n")
221+
func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream io.Writer) (endCursor string, err error) {
222+
if opts.ShowResponseHeaders {
223+
fmt.Fprintln(headersOutputStream, resp.Proto, resp.Status)
224+
printHeaders(headersOutputStream, resp.Header, opts.IO.ColorEnabled())
225+
fmt.Fprint(headersOutputStream, "\r\n")
225226
}
226227

227228
if resp.StatusCode == 204 {

pkg/cmd/api/api_test.go

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,36 @@ func Test_apiRun(t *testing.T) {
288288
stdout: `gateway timeout`,
289289
stderr: "gh: HTTP 502\n",
290290
},
291+
{
292+
name: "silent",
293+
options: ApiOptions{
294+
Silent: true,
295+
},
296+
httpResponse: &http.Response{
297+
StatusCode: 200,
298+
Body: ioutil.NopCloser(bytes.NewBufferString(`body`)),
299+
},
300+
err: nil,
301+
stdout: ``,
302+
stderr: ``,
303+
},
304+
{
305+
name: "show response headers even when silent",
306+
options: ApiOptions{
307+
ShowResponseHeaders: true,
308+
Silent: true,
309+
},
310+
httpResponse: &http.Response{
311+
Proto: "HTTP/1.1",
312+
Status: "200 Okey-dokey",
313+
StatusCode: 200,
314+
Body: ioutil.NopCloser(bytes.NewBufferString(`body`)),
315+
Header: http.Header{"Content-Type": []string{"text/plain"}},
316+
},
317+
err: nil,
318+
stdout: "HTTP/1.1 200 Okey-dokey\nContent-Type: text/plain\r\n\r\n",
319+
stderr: ``,
320+
},
291321
}
292322

293323
for _, tt := range tests {
@@ -449,38 +479,6 @@ func Test_apiRun_paginationGraphQL(t *testing.T) {
449479
assert.Equal(t, "PAGE1_END", endCursor)
450480
}
451481

452-
func Test_apiRun_silent(t *testing.T) {
453-
io, _, stdout, stderr := iostreams.Test()
454-
response := &http.Response{
455-
StatusCode: 200,
456-
Body: ioutil.NopCloser(bytes.NewBufferString(`body`)),
457-
Header: http.Header{"Content-Type": []string{"text/plain"}},
458-
}
459-
460-
options := ApiOptions{
461-
IO: io,
462-
HttpClient: func() (*http.Client, error) {
463-
var tr roundTripper = func(req *http.Request) (*http.Response, error) {
464-
resp := response
465-
resp.Request = req
466-
return resp, nil
467-
}
468-
return &http.Client{Transport: tr}, nil
469-
},
470-
RequestPath: "issues",
471-
ShowResponseHeaders: true,
472-
Silent: true,
473-
}
474-
475-
err := apiRun(&options)
476-
assert.NoError(t, err)
477-
478-
assert.Equal(t, "", stdout.String(), "stdout")
479-
assert.Equal(t, "", stderr.String(), "stderr")
480-
481-
assert.Equal(t, "https://api.github.com/issues", response.Request.URL.String())
482-
}
483-
484482
func Test_apiRun_inputFile(t *testing.T) {
485483
tests := []struct {
486484
name string

0 commit comments

Comments
 (0)
X Tutup