From 4a99873e23fdd84092e5594e727bab236192df58 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Wed, 4 Jan 2017 11:06:56 +0100 Subject: [PATCH] Fix unhandled merge conflict in context_test.go --- context_test.go | 30 ++++++++++++++++++++++++++++++ render/json.go | 7 ++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 96e4d04b..8071a375 100644 --- a/context_test.go +++ b/context_test.go @@ -7,6 +7,7 @@ package gin import ( "bytes" "errors" + "fmt" "html/template" "mime/multipart" "net/http" @@ -381,6 +382,35 @@ func TestContextGetCookie(t *testing.T) { assert.Equal(t, cookie, "gin") } +func TestContextBodyAllowedForStatus(t *testing.T) { + assert.Equal(t, false, bodyAllowedForStatus(102)) + assert.Equal(t, false, bodyAllowedForStatus(204)) + assert.Equal(t, false, bodyAllowedForStatus(304)) + assert.Equal(t, true, bodyAllowedForStatus(500)) +} + +type TestPanicRender struct { +} + +func (*TestPanicRender) Render(http.ResponseWriter) error { + return errors.New("TestPanicRender") +} + +func (*TestPanicRender) WriteContentType(http.ResponseWriter) {} + +func TestContextRenderPanicIfErr(t *testing.T) { + defer func() { + r := recover() + assert.Equal(t, "TestPanicRender", fmt.Sprint(r)) + }() + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + + c.Render(http.StatusOK, &TestPanicRender{}) + + assert.Fail(t, "Panic not detected") +} + // Tests that the response is serialized as JSON // and Content-Type is set to application/json func TestContextRenderJSON(t *testing.T) { diff --git a/render/json.go b/render/json.go index 0c87399d..3ee8b132 100644 --- a/render/json.go +++ b/render/json.go @@ -34,7 +34,12 @@ func (r JSON) WriteContentType(w http.ResponseWriter) { func WriteJSON(w http.ResponseWriter, obj interface{}) error { writeContentType(w, jsonContentType) - return json.NewEncoder(w).Encode(obj) + jsonBytes, err := json.Marshal(obj) + if err != nil { + return err + } + w.Write(jsonBytes) + return nil } func (r IndentedJSON) Render(w http.ResponseWriter) error {