Skip to content

Commit

Permalink
Add support for ArrayBuffer request bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić authored and salem84 committed Feb 3, 2021
1 parent ff2e5b3 commit 043f299
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func (h *HTTP) parseRequest(
if err := handleObjectBody(newData); err != nil {
return nil, err
}
case goja.ArrayBuffer:
result.Body = bytes.NewBuffer(data.Bytes())
case map[string]interface{}:
if err := handleObjectBody(data); err != nil {
return nil, err
Expand Down
39 changes: 39 additions & 0 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,45 @@ func TestRequestAndBatch(t *testing.T) {
})
}

func TestRequestArrayBufferBody(t *testing.T) {
t.Parallel()
tb, _, _, rt, _ := newRuntime(t) //nolint: dogsled
defer tb.Cleanup()
sr := tb.Replacer.Replace

tb.Mux.HandleFunc("/post-arraybuffer", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "POST", r.Method)
var in bytes.Buffer
_, err := io.Copy(&in, r.Body)
require.NoError(t, err)
_, err = w.Write(in.Bytes())
require.NoError(t, err)
}))

testCases := []struct {
arr, expected string
}{
{"Uint8Array", "104,101,108,108,111"},
{"Uint16Array", "104,0,101,0,108,0,108,0,111,0"},
{"Uint32Array", "104,0,0,0,101,0,0,0,108,0,0,0,108,0,0,0,111,0,0,0"},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.arr, func(t *testing.T) {
_, err := common.RunString(rt, sr(fmt.Sprintf(`
var arr = new %[1]s([104, 101, 108, 108, 111]); // "hello"
var res = http.post("HTTPBIN_URL/post-arraybuffer", arr.buffer, { responseType: 'binary' });
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
if (res.body != "%[2]s") { throw new Error(
"incorrect data: expected '%[2]s', received '" + res.body + "'") }
`, tc.arr, tc.expected)))
assert.NoError(t, err)
})
}
}

func TestRequestCompression(t *testing.T) {
t.Parallel()
tb, state, _, rt, _ := newRuntime(t)
Expand Down

0 comments on commit 043f299

Please sign in to comment.