diff options
author | Wolfy-J <[email protected]> | 2019-03-21 18:34:12 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-03-21 18:34:12 +0300 |
commit | 70671d83a718f9d9ca51ad5604fa88a197d91bed (patch) | |
tree | 750db414bf8139884af4fd499a84e97450b3a7bb | |
parent | c5f8f9d12de48d93db2c9ef63acc5bfab4f1d08a (diff) |
last element of POST data
-rw-r--r-- | service/http/handler_test.go | 52 | ||||
-rw-r--r-- | service/http/parse.go | 4 | ||||
-rw-r--r-- | service/http/parse_test.go | 2 |
3 files changed, 54 insertions, 4 deletions
diff --git a/service/http/handler_test.go b/service/http/handler_test.go index aaf9760b..d876ef8e 100644 --- a/service/http/handler_test.go +++ b/service/http/handler_test.go @@ -510,6 +510,58 @@ func TestHandler_FormData_POST(t *testing.T) { assert.Equal(t, `{"arr":{"c":{"p":"l","z":""},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`, string(b)) } +func TestHandler_FormData_POST_Overwrite(t *testing.T) { + h := &Handler{ + cfg: &Config{ + MaxRequest: 1024, + Uploads: &UploadsConfig{ + Dir: os.TempDir(), + Forbid: []string{}, + }, + }, + rr: roadrunner.NewServer(&roadrunner.ServerConfig{ + Command: "php ../../tests/http/client.php data pipes", + Relay: "pipes", + Pool: &roadrunner.Config{ + NumWorkers: 1, + AllocateTimeout: 10000000, + DestroyTimeout: 10000000, + }, + }), + } + + assert.NoError(t, h.rr.Start()) + defer h.rr.Stop() + + hs := &http.Server{Addr: ":8083", Handler: h} + defer hs.Shutdown(context.Background()) + + go func() { hs.ListenAndServe() }() + time.Sleep(time.Millisecond * 10) + + form := url.Values{} + + form.Add("key", "value1") + form.Add("key", "value2") + + req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, strings.NewReader(form.Encode())) + assert.NoError(t, err) + + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + defer r.Body.Close() + + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) + + assert.Equal(t, `{"key":"value2","arr":{"x":{"y":null}}}`, string(b)) +} + func TestHandler_FormData_POST_Form_UrlEncoded_Charset(t *testing.T) { h := &Handler{ cfg: &Config{ diff --git a/service/http/parse.go b/service/http/parse.go index 1f90930a..9b58d328 100644 --- a/service/http/parse.go +++ b/service/http/parse.go @@ -39,8 +39,8 @@ func (d dataTree) push(k string, v []string) { // mount mounts data tree recursively. func (d dataTree) mount(i []string, v []string) { if len(i) == 1 { - // single value context - d[i[0]] = v[0] + // single value context (last element) + d[i[0]] = v[len(v)-1] return } diff --git a/service/http/parse_test.go b/service/http/parse_test.go index 34c0dc0d..f95a3f9d 100644 --- a/service/http/parse_test.go +++ b/service/http/parse_test.go @@ -50,5 +50,3 @@ func same(in, out []string) bool { return true } - -// bench |