Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (c *context) json(code int, i interface{}, indent string) error {
enc.SetIndent("", indent)
}
c.writeContentType(MIMEApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
c.response.Status = code
return enc.Encode(i)
}

Expand Down
29 changes: 29 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"math"
"mime/multipart"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -374,6 +375,34 @@ func TestContext(t *testing.T) {
assert.Equal(0, len(c.QueryParams()))
}

func TestContext_JSON_CommitsCustomResponseCode(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context)
err := c.JSON(http.StatusCreated, user{1, "Jon Snow"})

assert := testify.New(t)
if assert.NoError(err) {
assert.Equal(http.StatusCreated, rec.Code)
assert.Equal(MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(userJSON+"\n", rec.Body.String())
}
}

func TestContext_JSON_DoesntCommitResponseCodePrematurely(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec).(*context)
err := c.JSON(http.StatusCreated, map[string]float64{"a": math.NaN()})

assert := testify.New(t)
if assert.Error(err) {
assert.False(c.response.Committed)
}
}

func TestContextCookie(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
Expand Down
5 changes: 4 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ func (r *Response) WriteHeader(code int) {
// Write writes the data to the connection as part of an HTTP reply.
func (r *Response) Write(b []byte) (n int, err error) {
if !r.Committed {
r.WriteHeader(http.StatusOK)
if r.Status == 0 {
r.Status = http.StatusOK
}
r.WriteHeader(r.Status)
}
n, err = r.Writer.Write(b)
r.Size += int64(n)
Expand Down
19 changes: 19 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ func TestResponse(t *testing.T) {
res.Write([]byte("test"))
assert.Equal(t, "echo", rec.Header().Get(HeaderServer))
}

func TestResponse_Write_FallsBackToDefaultStatus(t *testing.T) {
e := New()
rec := httptest.NewRecorder()
res := &Response{echo: e, Writer: rec}

res.Write([]byte("test"))
assert.Equal(t, http.StatusOK, rec.Code)
}

func TestResponse_Write_UsesSetResponseCode(t *testing.T) {
e := New()
rec := httptest.NewRecorder()
res := &Response{echo: e, Writer: rec}

res.Status = http.StatusBadRequest
res.Write([]byte("test"))
assert.Equal(t, http.StatusBadRequest, rec.Code)
}