Skip to content
Myr

Use json marshall in context json to fix breaking new line issue. Fixes #2209 (#2228)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* ignore IntelliJ idea generated files * update JSON renderer to use Marshall() instead of Encode(). Fix #2209 * Revert "ignore IntelliJ idea generated files" This reverts commit e7bd017227df5dbd2ed2f5fe353adb5f1b08c678.

authored by Kaushik Neelichetty · committed by GitHub · parent 863ad2d · Browse files · 5 files+15 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: thinkerou <thinkerou@gmail.com>

Diff stats

5 files+15 added−12 removed

  • other:+15 added−12 removed

5 files+15 added−12 removed

modified:render/json.go+5 added-2 removed
67 unmodified lines
6869// WriteJSON marshals the given interface object and writes it with custom ContentType.70func WriteJSON(w http.ResponseWriter, obj interface{}) error {71 writeContentType(w, jsonContentType)72 encoder := json.NewEncoder(w)73 err := encoder.Encode(&obj)72 jsonBytes, err := json.Marshal(obj)73 if err != nil {74 return err75 }76 _, err = w.Write(jsonBytes)77 return err78}7980// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
116 unmodified lines
modified:render/render_test.go+1 added-1 removed
34 unmodified lines
3536 err := (JSON{data}).Render(w)3738 assert.NoError(t, err)39 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", w.Body.String())39 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())40 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))41}4243func TestRenderJSONPanics(t *testing.T) {
467 unmodified lines
modified:context_test.go+5 added-5 removed
660 unmodified lines
661662 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})663664 assert.Equal(t, http.StatusCreated, w.Code)665 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", w.Body.String())665 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())666 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))667}668669// Tests that the response is serialized as JSONP
19 unmodified lines
689690 c.JSONP(http.StatusCreated, H{"foo": "bar"})691692 assert.Equal(t, http.StatusCreated, w.Code)693 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())693 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())694 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))695}696697// Tests that no JSON is rendered if code is 204
17 unmodified lines
715 c.Header("Content-Type", "application/vnd.api+json")716 c.JSON(http.StatusCreated, H{"foo": "bar"})717718 assert.Equal(t, http.StatusCreated, w.Code)719 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())719 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())720 assert.Equal(t, "application/vnd.api+json", w.Header().Get("Content-Type"))721}722723// Tests that no Custom JSON is rendered if code is 204
394 unmodified lines
1118 Data: H{"foo": "bar"},1119 })11201121 assert.Equal(t, http.StatusOK, w.Code)1122 assert.Equal(t, "{\"foo\":\"bar\"}\n", w.Body.String())1122 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())1123 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))1124}11251126func TestContextNegotiationWithXML(t *testing.T) {
155 unmodified lines
1282 buf := new(bytes.Buffer)1283 _, err := buf.ReadFrom(w.Body)1284 assert.NoError(t, err)1285 jsonStringBody := buf.String()1286 assert.Equal(t, fmt.Sprint("{\"foo\":\"fooValue\",\"bar\":\"barValue\"}\n"), jsonStringBody)1286 assert.Equal(t, fmt.Sprint("{\"foo\":\"fooValue\",\"bar\":\"barValue\"}"), jsonStringBody)1287}12881289func TestContextError(t *testing.T) {1290 c, _ := CreateTestContext(httptest.NewRecorder())
617 unmodified lines
modified:logger_test.go+3 added-3 removed
367 unmodified lines
368 })369370 w := performRequest(router, "GET", "/error")371 assert.Equal(t, http.StatusOK, w.Code)372 assert.Equal(t, "{\"error\":\"this is an error\"}\n", w.Body.String())372 assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())373374 w = performRequest(router, "GET", "/abort")375 assert.Equal(t, http.StatusUnauthorized, w.Code)376 assert.Equal(t, "{\"error\":\"no authorized\"}\n", w.Body.String())376 assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())377378 w = performRequest(router, "GET", "/print")379 assert.Equal(t, http.StatusInternalServerError, w.Code)380 assert.Equal(t, "hola!{\"error\":\"this is an error\"}\n", w.Body.String())380 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())381}382383func TestLoggerWithWriterSkippingPaths(t *testing.T) {384 buffer := new(bytes.Buffer)
50 unmodified lines
modified:middleware_test.go+1 added-1 removed
244 unmodified lines
245246 w := performRequest(router, "GET", "/")247248 assert.Equal(t, http.StatusBadRequest, w.Code)249 assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}\n{\"foo\":\"bar\"}\nevent:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))249 assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))250}