Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions http/param/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package param

import (
"encoding"
"errors"
"fmt"
"net/http"
"reflect"
Expand All @@ -11,6 +12,7 @@ import (

const (
defaultTagName = "param"
defaultMaxMemory = 32 << 20 // 32 MB
queryTagValuePrefix = "query"
pathTagValuePrefix = "path"
formTagValuePrefix = "form"
Expand Down Expand Up @@ -248,8 +250,14 @@ func (p Parser) parseFormParam(r *http.Request, paramName string, v reflect.Valu
if r.Method != http.MethodPost && r.Method != http.MethodPut && r.Method != http.MethodPatch {
return fmt.Errorf("struct's field was tagged for parsing the form parameter (%s) but request method is not POST, PUT or PATCH", paramName)
}
if err := r.ParseForm(); err != nil {
return fmt.Errorf("parsing form data: %w", err)
if err := r.ParseMultipartForm(defaultMaxMemory); err != nil {
if !errors.Is(err, http.ErrNotMultipart) {
return fmt.Errorf("parsing multipart form: %w", err)
}
// Try to parse regular form if not multipart form.
if err := r.ParseForm(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called from the ParseMultipartForm, so it is unnecesary to have it also here.

return fmt.Errorf("parsing form: %w", err)
}
}
paramValue := p.FormParamFunc(r, paramName)
if paramValue != "" {
Expand Down
33 changes: 31 additions & 2 deletions http/param/param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func TestParser_Parse_FormParam(t *testing.T) {
Nothing: "",
}
var parseError error
r.Post("/hello/objects", func(w http.ResponseWriter, r *http.Request) {
r.Post("/hello/objects", func(_ http.ResponseWriter, r *http.Request) {
parseError = p.Parse(r, &result)
})

Expand All @@ -484,7 +484,36 @@ func TestParser_Parse_FormParam(t *testing.T) {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r.ServeHTTP(httptest.NewRecorder(), req)

assert.NoError(t, parseError)
require.NoError(t, parseError)
assert.Equal(t, expected, result)
}

func TestParser_Parse_FormParam_NoParams(t *testing.T) {
p := DefaultParser()
result := structWithFormParams{
Subject: "should be replaced",
Amount: ptr(123), // should be zeroed out
Nothing: "should be replaced",
}
expected := structWithFormParams{
Subject: "",
Amount: nil,
Object: nil,
Nothing: "",
}
var parseError error

r := chi.NewRouter()
r.Post("/hello/objects", func(_ http.ResponseWriter, r *http.Request) {
parseError = p.Parse(r, &result)
})

// Empty form body
req := httptest.NewRequest(http.MethodPost, "https://test.com/hello/objects", strings.NewReader(""))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r.ServeHTTP(httptest.NewRecorder(), req)

require.NoError(t, parseError)
assert.Equal(t, expected, result)
}

Expand Down