Merge pull request #1165 from hashicorp/f-append-newline

Append a newline when pretty-printing the response
This commit is contained in:
Sean Chittenden 2016-05-11 13:10:37 -07:00
commit 92df92f48b
2 changed files with 13 additions and 3 deletions

View File

@ -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)
}

View File

@ -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)