1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-03 00:56:59 +02:00

JSON Pretty Print

This commit is contained in:
Marcin Tojek
2015-11-01 19:48:55 +01:00
parent f5bf0eb0dd
commit 1bfd7762ca
2 changed files with 34 additions and 1 deletions

View File

@ -28,6 +28,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error {
func TestContext(t *testing.T) {
userJSON := `{"id":"1","name":"Joe"}`
userJSONPrettyPrint := "{\n \"id\": \"1\",\n \"name\": \"Joe\"\n }"
userXML := `<user><id>1</id><name>Joe</name></user>`
req, _ := http.NewRequest(POST, "/", strings.NewReader(userJSON))
@ -97,6 +98,26 @@ func TestContext(t *testing.T) {
assert.Equal(t, userJSON, rec.Body.String())
}
// JSONPrettyPrint (pretty)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())
err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, true)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSONPrettyPrint, rec.Body.String())
}
// JSONPrettyPrint (not pretty)
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())
err = c.JSONPrettyPrint(http.StatusOK, user{"1", "Joe"}, false)
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
assert.Equal(t, userJSON, rec.Body.String())
}
// JSONP
rec = httptest.NewRecorder()
c = NewContext(req, NewResponse(rec), New())