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

Commit

Permalink
fix: empty body panic error
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 1, 2019
1 parent f98a940 commit 92070fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions body_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func NewJSONDecoder() *Decoder {
},
Decode: func(c *elton.Context, originalData []byte) (data []byte, err error) {
originalData = bytes.TrimSpace(originalData)
if len(originalData) == 0 {
return nil, nil
}
firstByte := originalData[0]
lastByte := originalData[len(originalData)-1]

Expand Down
6 changes: 6 additions & 0 deletions body_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func TestJSONDecoder(t *testing.T) {
data, err := jsonDecoder.Decode(c, buf)
assert.Nil(err)
assert.Equal(buf, data)

buf = []byte(``)
data, err = jsonDecoder.Decode(c, buf)
assert.Nil(err)
assert.Nil(data)

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

Expand Down

0 comments on commit 92070fb

Please sign in to comment.