chore(errorHandler):new abortWithStatus method with Json body (#800)
This commit is contained in:
@ -788,6 +788,36 @@ func TestContextAbortWithStatus(t *testing.T) {
|
||||
assert.True(t, c.IsAborted())
|
||||
}
|
||||
|
||||
type testJSONAbortMsg struct {
|
||||
Foo string `json:"foo"`
|
||||
Bar string `json:"bar"`
|
||||
}
|
||||
|
||||
func TestContextAbortWithStatusJSON(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
c.index = 4
|
||||
|
||||
in := new(testJSONAbortMsg)
|
||||
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.Sprint(`{"foo":"fooValue","bar":"barValue"}`), jsonStringBody)
|
||||
}
|
||||
|
||||
func TestContextError(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
assert.Empty(t, c.Errors)
|
||||
|
Reference in New Issue
Block a user