Skip to content
Myr

Merge pull request #587 from roylou/develop

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

Write header immediately in AbortWithStatus(), close #585

authored by Javier Provecho Fernandez · committed by Javier Provecho Fernandez · parents 007bd51, 4c4444b · Browse files · 5 files+6 added−10 removed
This commit

State

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

Diff stats

5 files+6 added−10 removed

  • other:+6 added−10 removed

5 files+6 added−10 removed

modified:context_test.go-2 removed
554 unmodified lines
555func TestContextAbortWithStatus(t *testing.T) {556 c, w, _ := CreateTestContext()557 c.index = 4558 c.AbortWithStatus(401)559 c.Writer.WriteHeaderNow()559560 assert.Equal(t, c.index, abortIndex)561 assert.Equal(t, c.Writer.Status(), 401)562 assert.Equal(t, w.Code, 401)
42 unmodified lines
605606func TestContextAbortWithError(t *testing.T) {607 c, w, _ := CreateTestContext()608 c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")610 c.Writer.WriteHeaderNow()609610 assert.Equal(t, w.Code, 401)611 assert.Equal(t, c.index, abortIndex)612 assert.True(t, c.IsAborted())
94 unmodified lines
modified:context.go+1 added
113 unmodified lines
114// AbortWithStatus calls `Abort()` and writes the headers with the specified status code.115// For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401).116func (c *Context) AbortWithStatus(code int) {117 c.Status(code)118 c.Writer.WriteHeaderNow()119 c.Abort()120}121122// AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and
446 unmodified lines
modified:logger_test.go+1 added-1 removed
114 unmodified lines
115 assert.Equal(t, w.Body.String(), "{\"error\":\"no authorized\"}\n")116117 w = performRequest(router, "GET", "/print")118 assert.Equal(t, w.Code, 500)119 assert.Equal(t, w.Body.String(), "hola!")119 assert.Equal(t, w.Body.String(), "hola!{\"error\":\"this is an error\"}\n")120}121122func TestSkippingPaths(t *testing.T) {123 buffer := new(bytes.Buffer)
11 unmodified lines
modified:logger.go+3 added-6 removed
26 unmodified lines
2728func ErrorLoggerT(typ ErrorType) HandlerFunc {29 return func(c *Context) {30 c.Next()31 // avoid writting if we already wrote into the response body32 if !c.Writer.Written() {33 errors := c.Errors.ByType(typ)34 if len(errors) > 0 {35 c.JSON(-1, errors)31 errors := c.Errors.ByType(typ)32 if len(errors) > 0 {33 c.JSON(-1, errors)36 }34 }35 }36}37
86 unmodified lines
modified:recovery_test.go+1 added-1 removed
37 unmodified lines
38 })39 // RUN40 w := performRequest(router, "GET", "/recovery")41 // TEST42 assert.Equal(t, w.Code, 500) // NOT SURE42 assert.Equal(t, w.Code, 400)43}