Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func BindBody(c *Context, target any) (err error) {

// mediatype is found like `mime.ParseMediaType()` does it
base, _, _ := strings.Cut(req.Header.Get(HeaderContentType), ";")
mediatype := strings.TrimSpace(base)
mediatype := strings.ToLower(strings.TrimSpace(base))

switch mediatype {
case MIMEApplicationJSON:
Expand Down
60 changes: 60 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,66 @@ func TestDefaultBinder_BindBody(t *testing.T) {
}
}

func TestDefaultBinder_BindBody_MediaTypeIsCaseInsensitive(t *testing.T) {
// media type and subtype are case-insensitive, see https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3.1
type Node struct {
ID int `json:"id" xml:"id" form:"id"`
}

var testCases = []struct {
name string
givenContentType string
givenContent string
}{
{
name: "JSON",
givenContentType: "Application/JSON",
givenContent: `{"id": 1}`,
},
{
name: "JSON with charset",
givenContentType: "APPLICATION/JSON; charset=UTF-8",
givenContent: `{"id": 1}`,
},
{
name: "application XML",
givenContentType: "Application/XML",
givenContent: `<node><id>1</id></node>`,
},
{
name: "text XML",
givenContentType: "Text/XML; charset=utf-8",
givenContent: `<node><id>1</id></node>`,
},
{
name: "form",
givenContentType: "Application/X-WWW-Form-Urlencoded",
givenContent: `id=1`,
},
{
name: "multipart form",
givenContentType: "Multipart/Form-Data; boundary=xxx",
givenContent: "--xxx\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n1\r\n--xxx--\r\n",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tc.givenContent))
req.Header.Set(HeaderContentType, tc.givenContentType)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

target := &Node{}
err := BindBody(c, target)

assert.NoError(t, err)
assert.Equal(t, &Node{ID: 1}, target)
})
}
}

func testBindURL(queryString string, target any) error {
e := New()
req := httptest.NewRequest(http.MethodGet, queryString, nil)
Expand Down