Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Append URL and Query String parameter dupplicates (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max-Pol authored and markbates committed Oct 17, 2019
1 parent 6c27e55 commit c1d8292
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
12 changes: 9 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buffalo
import (
"context"
"net/http"
"net/url"
"sync"

"github.com/gobuffalo/buffalo/binding"
Expand Down Expand Up @@ -46,16 +47,21 @@ func (a *App) newContext(info RouteInfo, res http.ResponseWriter, req *http.Requ
if ws, ok := res.(*Response); ok {
res = ws
}
params := req.URL.Query()

// Parse URL Params
params := url.Values{}
vars := mux.Vars(req)
for k, v := range vars {
params.Set(k, v)
params.Add(k, v)
}

// Parse URL Query String Params
// For POST, PUT, and PATCH requests, it also parse the request body as a form.
// Request body parameters take precedence over URL query string values in params
if err := req.ParseForm(); err == nil {
for k, v := range req.Form {
for _, vv := range v {
params.Set(k, vv)
params.Add(k, vv)
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions default_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ func Test_DefaultContext_Param_form(t *testing.T) {
r.Equal("Mark", name)
}

func Test_DefaultContext_Param_Multiple(t *testing.T) {
r := require.New(t)

app := New(Options{})
var params ParamValues
var param string
app.POST("/{id}", func(c Context) error {
params = c.Params()
param = c.Param("id")
return nil
})

w := httptest.New(app)
res := w.HTML("/a?id=c&y=z&id=d").Post(map[string]string{
"id": "b",
})
paramsExpected := url.Values{
"id": []string{"a", "b", "c", "d"},
"y": []string{"z"},
}

r.Equal(200, res.Code)
r.Equal(paramsExpected, params.(url.Values))
r.Equal("a", param)
}

func Test_DefaultContext_GetSet(t *testing.T) {
r := require.New(t)
c := basicContext()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/tools v0.0.0-20191017101817-846f856e7d71
golang.org/x/tools v0.0.0-20191017163857-e4d7c6f25b8e
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
gopkg.in/yaml.v2 v2.2.4
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ golang.org/x/tools v0.0.0-20190905035308-adb45749da8e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20190906203814-12febf440ab1/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191004055002-72853e10c5a3/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191017163857-e4d7c6f25b8e h1:DpwXve8B+hJ9vwDDTRZ4nMMM6qjYPQ5bq0OfRXTKJJY=
golang.org/x/tools v0.0.0-20191017163857-e4d7c6f25b8e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191017101817-846f856e7d71 h1:gKvBHiwvrwjVESSCHvRLeyg2OLVRH6UwgwczU5CXvcg=
golang.org/x/tools v0.0.0-20191017101817-846f856e7d71/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 1 addition & 1 deletion runtime/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime

// Version is the current version of the buffalo binary
const Version = "development"
const Version = "v0.14.11"

0 comments on commit c1d8292

Please sign in to comment.