From 2fe9b6e97a55ffbef08cdab1d4b93852e32fd54d Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Tue, 10 May 2016 15:46:20 -0700 Subject: [PATCH 1/2] Append a newline when pretty-printing the response Instead of typing `curl http://foo/v1/nodes?pretty; echo` to inject a newline into your terminal, now pretty will provide its own newline. --- command/agent/http.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/command/agent/http.go b/command/agent/http.go index c755f3a52..f6475c409 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -184,6 +184,9 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque var buf []byte if prettyPrint { buf, err = json.MarshalIndent(obj, "", " ") + if err == nil { + buf = append(buf, "\n"...) + } } else { buf, err = json.Marshal(obj) } From 36a50e0f15c89e979745feec32136d508b140f8e Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Wed, 11 May 2016 10:55:01 -0700 Subject: [PATCH 2/2] Update tests for pretty printing that includes a newline. Add a test verifying that pretty printing can be disabled, too. --- command/agent/http_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/command/agent/http_test.go b/command/agent/http_test.go index 8001d722e..4ccbf03c9 100644 --- a/command/agent/http_test.go +++ b/command/agent/http_test.go @@ -145,14 +145,18 @@ func TestContentTypeIsJSON(t *testing.T) { } func TestPrettyPrint(t *testing.T) { - testPrettyPrint("pretty=1", t) + testPrettyPrint("pretty=1", true, t) +} + +func TestPrettyPrintOff(t *testing.T) { + testPrettyPrint("pretty=0", false, t) } func TestPrettyPrintBare(t *testing.T) { - testPrettyPrint("pretty", t) + testPrettyPrint("pretty", true, t) } -func testPrettyPrint(pretty string, t *testing.T) { +func testPrettyPrint(pretty string, prettyFmt bool, t *testing.T) { s := makeHTTPServer(t, nil) defer s.Cleanup() @@ -168,6 +172,9 @@ func testPrettyPrint(pretty string, t *testing.T) { s.Server.wrap(handler)(resp, req) expected, _ := json.MarshalIndent(r, "", " ") + if prettyFmt { + expected += "\n" + } actual, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatalf("err: %s", err)