Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
feat: add content type validate
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 2, 2019
1 parent 92070fb commit 373a321
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
26 changes: 23 additions & 3 deletions body_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ type (
// Limit the limit size of body
Limit int
// Decoders decode list
Decoders []*Decoder
Skipper elton.Skipper
Decoders []*Decoder
Skipper elton.Skipper
ContentTypeValidate Validate
}
)

Expand Down Expand Up @@ -159,6 +160,21 @@ func NewFormURLEncodedDecoder() *Decoder {
}
}

// DefaultJSONContentTypeValidate default json content type validate
func DefaultJSONContentTypeValidate(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
return strings.HasPrefix(ct, jsonContentType)
}

// DefaultJSONAndFormContentTypeValidate default json and form content type validate
func DefaultJSONAndFormContentTypeValidate(c *elton.Context) bool {
ct := c.GetRequestHeader(elton.HeaderContentType)
if strings.HasPrefix(ct, jsonContentType) {
return true
}
return strings.HasPrefix(ct, formURLEncodedContentType)
}

// NewDefault create a default body parser, default limit and only json parser
func NewDefault() elton.Handler {
conf := Config{}
Expand Down Expand Up @@ -187,8 +203,12 @@ func New(config Config) elton.Handler {
if skipper == nil {
skipper = elton.DefaultSkipper
}
contentTypeValidate := config.ContentTypeValidate
if contentTypeValidate == nil {
contentTypeValidate = DefaultJSONContentTypeValidate
}
return func(c *elton.Context) (err error) {
if skipper(c) || c.RequestBody != nil {
if skipper(c) || c.RequestBody != nil || !contentTypeValidate(c) {
return c.Next()
}
method := c.Request.Method
Expand Down
27 changes: 26 additions & 1 deletion body_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func TestJSONDecoder(t *testing.T) {
assert.Nil(err)
assert.Nil(data)

_, err = jsonDecoder.Decode(c, []byte("{"))
assert.Equal(errInvalidJSON, err)

_, err = jsonDecoder.Decode(c, []byte("abcd"))
assert.Equal(errInvalidJSON, err)

Expand Down Expand Up @@ -128,6 +131,26 @@ func TestBodyParser(t *testing.T) {
assert.Equal(len(c.RequestBody), 0)
})

t.Run("request body content type is not json", func(t *testing.T) {
assert := assert.New(t)
bodyParser := NewDefault()

body := `<xml>xxx</xml>`
req := httptest.NewRequest("POST", "https://aslant.site/", strings.NewReader(body))
req.Header.Set(elton.HeaderContentType, "application/xml")
c := elton.NewContext(nil, req)
done := false
c.Next = func() error {
done = true
return nil
}
err := bodyParser(c)

assert.Nil(err)
assert.True(done)
assert.Nil(c.RequestBody)
})

t.Run("request body is not nil", func(t *testing.T) {
assert := assert.New(t)
bodyParser := NewDefault()
Expand Down Expand Up @@ -288,7 +311,9 @@ func TestBodyParser(t *testing.T) {

t.Run("parse form url encoded success", func(t *testing.T) {
assert := assert.New(t)
conf := Config{}
conf := Config{
ContentTypeValidate: DefaultJSONAndFormContentTypeValidate,
}
conf.AddDecoder(NewFormURLEncodedDecoder())
bodyParser := New(conf)
body := `name=tree.xie&type=1&type=2`
Expand Down

0 comments on commit 373a321

Please sign in to comment.