From a6d1ba3b23bdcc4689bfc79fac22bfea673659c8 Mon Sep 17 00:00:00 2001 From: Marty Spiewak Date: Mon, 23 Sep 2019 12:33:40 -0400 Subject: [PATCH] Return response headers when both -f and -i flags are passed [#168151753] --- commands/curl.go | 8 ++++---- commands/curl_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/commands/curl.go b/commands/curl.go index 1943179b..77ebac22 100644 --- a/commands/curl.go +++ b/commands/curl.go @@ -49,10 +49,6 @@ func (c *CurlCommand) Execute([]string) error { return err } - if c.Fail && response.StatusCode >= 400 && response.StatusCode < 600 { - os.Exit(22) - } - if c.IncludeHeader { headers := new(bytes.Buffer) err = response.Header.Write(headers) @@ -67,6 +63,10 @@ func (c *CurlCommand) Execute([]string) error { fmt.Println(headers.String()) } + if c.Fail && response.StatusCode >= 400 && response.StatusCode < 600 { + os.Exit(22) + } + body, err := ioutil.ReadAll(response.Body) if err != nil { return err diff --git a/commands/curl_test.go b/commands/curl_test.go index a0196d78..92a82bd4 100644 --- a/commands/curl_test.go +++ b/commands/curl_test.go @@ -326,6 +326,28 @@ var _ = Describe("Curl", func() { Eventually(session).Should(Exit(22)) Eventually(session.Out.Contents()).Should(BeEmpty()) }) + Context("when request includes -i flag", func() { + It("displays response headers to the user and exits with proper code", func() { + //language=json + responseJson := ` + { + "error": "The request could not be completed because the credential does not exist or you do not have sufficient authorization." + } + ` + server.RouteToHandler("GET", "/api/v1/data", + CombineHandlers( + VerifyRequest("GET", "/api/v1/data"), + VerifyForm(url.Values{"name": []string{"/doesNotExist"}}), + RespondWith(http.StatusNotFound, responseJson), + ), + ) + + session := runCommand("curl", "-f", "-p", "api/v1/data?name=/doesNotExist", "-i") + + Eventually(session).Should(Exit(22)) + Eventually(session.Out).Should(Say("HTTP/1.1 404")) + }) + }) }) It("the request continues to succeed", func() {