From d08400d4d4a552c9521b7e5379d79b18a409694e Mon Sep 17 00:00:00 2001 From: PABLO JOSE GONZALEZ Date: Mon, 13 Feb 2017 15:03:39 +0100 Subject: [PATCH 1/2] chore(errorHandler):new abortWithStatus method with Json body --- context.go | 7 +++++++ context_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/context.go b/context.go index d2b98b65..d834b356 100644 --- a/context.go +++ b/context.go @@ -116,6 +116,13 @@ func (c *Context) AbortWithStatus(code int) { c.Abort() } +// AbortWithStatusJSON calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body +// It also sets the Content-Type as "application/json". +func (c *Context) AbortWithStatusJSON(code int, jsonObj interface{}) { + c.Abort() + c.JSON(code, jsonObj) +} + // AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and // pushes the specified error to `c.Errors`. // See Context.Error() for more details. diff --git a/context_test.go b/context_test.go index 81e18841..6eaa06f8 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" @@ -627,6 +628,36 @@ func TestContextAbortWithStatus(t *testing.T) { assert.True(t, c.IsAborted()) } +func TestContextAbortWithStatusJSON(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + + c.index = 4 + type inboundJsonType struct { + Foo string `json:"foo"` + Bar string `json:"bar"` + } + + in := new(inboundJsonType) + in.Bar = "barValue" + in.Foo = "fooValue" + + c.AbortWithStatusJSON(415, in) + + assert.Equal(t, c.index, abortIndex) + assert.Equal(t, c.Writer.Status(), 415) + assert.Equal(t, w.Code, 415) + assert.True(t, c.IsAborted()) + + contentType := w.Header().Get("Content-Type") + assert.Equal(t, contentType, "application/json; charset=utf-8") + + buf := new(bytes.Buffer) + buf.ReadFrom(w.Body) + jsonStringBody := buf.String() + assert.Equal(t, fmt.Sprintln(`{"foo":"fooValue","bar":"barValue"}`), jsonStringBody) +} + func TestContextError(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) assert.Empty(t, c.Errors) From 5a446cb2b0cc689b204de520386ecc57f40c96ea Mon Sep 17 00:00:00 2001 From: PABLO JOSE GONZALEZ Date: Mon, 13 Feb 2017 15:03:39 +0100 Subject: [PATCH 2/2] chore(errorHandler):new abortWithStatus method with Json body --- context.go | 7 +++++++ context_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/context.go b/context.go index d2b98b65..d834b356 100644 --- a/context.go +++ b/context.go @@ -116,6 +116,13 @@ func (c *Context) AbortWithStatus(code int) { c.Abort() } +// AbortWithStatusJSON calls `Abort()` and then `JSON` internally. This method stops the chain, writes the status code and return a JSON body +// It also sets the Content-Type as "application/json". +func (c *Context) AbortWithStatusJSON(code int, jsonObj interface{}) { + c.Abort() + c.JSON(code, jsonObj) +} + // AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and // pushes the specified error to `c.Errors`. // See Context.Error() for more details. diff --git a/context_test.go b/context_test.go index 81e18841..e44fddab 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" @@ -627,6 +628,36 @@ func TestContextAbortWithStatus(t *testing.T) { assert.True(t, c.IsAborted()) } +func TestContextAbortWithStatusJSON(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + + c.index = 4 + type inboundJsonType struct { + Foo string `json:"foo"` + Bar string `json:"bar"` + } + + in := new(inboundJsonType) + in.Bar = "barValue" + in.Foo = "fooValue" + + c.AbortWithStatusJSON(415, in) + + assert.Equal(t, c.index, abortIndex) + assert.Equal(t, c.Writer.Status(), 415) + assert.Equal(t, w.Code, 415) + assert.True(t, c.IsAborted()) + + contentType := w.Header().Get("Content-Type") + assert.Equal(t, contentType, "application/json; charset=utf-8") + + buf := new(bytes.Buffer) + buf.ReadFrom(w.Body) + jsonStringBody := buf.String() + assert.Equal(t, fmt.Sprintln(`{"foo":"fooValue","bar":"barValue"}`), jsonStringBody) +} + func TestContextError(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) assert.Empty(t, c.Errors)