Skip to content

Commit

Permalink
Merge pull request grafana#2060 from k6io/fix/1713-params-array-encoding
Browse files Browse the repository at this point in the history
Fix array encoding in form data requests
  • Loading branch information
Ivan Mirić authored Jun 15, 2021
2 parents 7daba1f + c7bb596 commit 91bfc1c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 6 additions & 0 deletions js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ func (h *HTTP) parseRequest(
if !requestContainsFile(data) {
bodyQuery := make(url.Values, len(data))
for k, v := range data {
if arr, ok := v.([]interface{}); ok {
for _, el := range arr {
bodyQuery.Add(k, formatFormVal(el))
}
continue
}
bodyQuery.Set(k, formatFormVal(v))
}
result.Body = bytes.NewBufferString(bodyQuery.Encode())
Expand Down
6 changes: 5 additions & 1 deletion js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,14 @@ func TestRequestAndBatch(t *testing.T) {

t.Run("object", func(t *testing.T) {
_, err := rt.RunString(fmt.Sprintf(sr(`
var res = http.%s("HTTPBIN_URL/%s", {a: "a", b: 2});
var equalArray = function(a, b) {
return a.length === b.length && a.every(function(v, i) { return v === b[i]});
}
var res = http.%s("HTTPBIN_URL/%s", {a: "a", b: 2, c: ["one", "two"]});
if (res.status != 200) { throw new Error("wrong status: " + res.status); }
if (res.json().form.a != "a") { throw new Error("wrong a=: " + res.json().form.a); }
if (res.json().form.b != "2") { throw new Error("wrong b=: " + res.json().form.b); }
if (!equalArray(res.json().form.c, ["one", "two"])) { throw new Error("wrong c: " + res.json().form.c); }
if (res.json().headers["Content-Type"] != "application/x-www-form-urlencoded") { throw new Error("wrong content type: " + res.json().headers["Content-Type"]); }
`), fn, strings.ToLower(method)))
assert.NoError(t, err)
Expand Down

0 comments on commit 91bfc1c

Please sign in to comment.