Skip to content

Commit

Permalink
feat: support method not allowed hanlder
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 1, 2020
1 parent d4a76be commit b774464
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go:
- "1.12"
- "1.13"
- "1.14"
- "1.15"
- master

script:
Expand Down
28 changes: 24 additions & 4 deletions elton.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type (
ErrorHandler ErrorHandler
// NotFoundHandler set the function for not found handler
NotFoundHandler http.HandlerFunc
// MethodNotAllowedHandler set the function for method not allowed handler
MethodNotAllowedHandler http.HandlerFunc
// GenerateID generate id function, will use it for create id for context
GenerateID GenerateID
// EnableTrace enable trace
Expand Down Expand Up @@ -238,9 +240,13 @@ func (e *Elton) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
method := methodMap[req.Method]
rn := e.tree.findRoute(method, req.URL.Path, c.Params)
if rn == nil {
// 404处理
e.NotFound(resp, req)
// 404的所有context都可复用
if c.Params.methodNotAllowed {
e.MethodNotAllowed(resp, req)
} else {
// 404处理
e.NotFound(resp, req)
}
// not found 与method not allowed所有context都可复用
e.ctxPool.Put(c)
return
}
Expand Down Expand Up @@ -448,7 +454,21 @@ func (e *Elton) NotFound(resp http.ResponseWriter, req *http.Request) *Elton {
return e
}
resp.WriteHeader(http.StatusNotFound)
_, err := resp.Write([]byte("Not found"))
_, err := resp.Write([]byte("Not Found"))
if err != nil {
e.emitError(resp, req, err)
}
return e
}

// MethodNotAllowed method not allowed handle
func (e *Elton) MethodNotAllowed(resp http.ResponseWriter, req *http.Request) *Elton {
if e.MethodNotAllowedHandler != nil {
e.MethodNotAllowedHandler(resp, req)
return e
}
resp.WriteHeader(http.StatusMethodNotAllowed)
_, err := resp.Write([]byte("Method Not Allowed"))
if err != nil {
e.emitError(resp, req, err)
}
Expand Down
12 changes: 10 additions & 2 deletions elton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ func TestPreHandle(t *testing.T) {
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(404, resp.Code)
assert.Equal("Not found", resp.Body.String())
assert.Equal("Not Found", resp.Body.String())
})
t.Run("method not allow", func(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("POST", "/ping", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(405, resp.Code)
assert.Equal("Method Not Allowed", resp.Body.String())
})

t.Run("pong", func(t *testing.T) {
Expand Down Expand Up @@ -275,7 +283,7 @@ func TestHandle(t *testing.T) {
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(http.StatusNotFound, resp.Code)
assert.Equal("Not found", resp.Body.String())
assert.Equal("Not Found", resp.Body.String())
})

t.Run("error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/stretchr/testify v1.6.1
github.com/tidwall/gjson v1.6.0
github.com/tidwall/gjson v1.6.1
github.com/vicanso/fresh v1.0.0
github.com/vicanso/hes v0.2.2
github.com/vicanso/intranet-ip v0.0.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/gjson v1.6.1 h1:LRbvNuNuvAiISWg6gxLEFuCe72UKy5hDqhxW/8183ws=
github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/vicanso/fresh v1.0.0 h1:u3ykbW6SYW5CbI6rx1lZCVfWKVIptvL6/2KOnrTKsTY=
github.com/vicanso/fresh v1.0.0/go.mod h1:gr1RKSFxQ1OnQHzUMBHCigifni7KrXveJjWCTlPjICA=
github.com/vicanso/hes v0.2.2 h1:asekH5sz7ekWvTHajxO0ly/Z8jrvYyx+OxzkBRw75es=
Expand Down
2 changes: 1 addition & 1 deletion middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

var (
DefaultCompressRegexp = regexp.MustCompile("text|javascript|json")
DefaultCompressRegexp = regexp.MustCompile("text|javascript|json|wasm")
)

const (
Expand Down

0 comments on commit b774464

Please sign in to comment.