Skip to content

Commit

Permalink
Handle ArrayBuffer conversion from http.batch()
Browse files Browse the repository at this point in the history
This is a breaking change part of #1020.
  • Loading branch information
Ivan Mirić committed Apr 7, 2021
1 parent 372ff86 commit c84375e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ func (h *HTTP) Batch(ctx context.Context, reqsV goja.Value) (goja.Value, error)
errs := httpext.MakeBatchRequests(
ctx, batchReqs, reqCount,
int(state.Options.Batch.Int64), int(state.Options.BatchPerHost.Int64),
processResponse,
)

for i := 0; i < reqCount; i++ {
Expand Down
21 changes: 21 additions & 0 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,27 @@ func TestResponseTypes(t *testing.T) {
}
}
http.post("HTTPBIN_URL/compare-bin", respBin.body);
// Check ArrayBuffer response
var respBin = http.get("HTTPBIN_URL/get-bin", { responseType: "binary" }).body;
if (respBin.byteLength !== expBinLength) {
throw new Error("response body length should be '" + expBinLength + "' but was '" + respBin.byteLength + "'");
}
// Check ArrayBuffer responses with http.batch()
var responses = http.batch([
["GET", "HTTPBIN_URL/get-bin", null, { responseType: "binary" }],
["GET", "HTTPBIN_URL/get-bin", null, { responseType: "binary" }],
]);
if (responses.length != 2) {
throw new Error("expected 2 responses, received " + responses.length);
}
for (var i = 0; i < responses.length; i++) {
if (responses[i].body.byteLength !== expBinLength) {
throw new Error("response body length should be '"
+ expBinLength + "' but was '" + responses[i].body.byteLength + "'");
}
}
`))
assert.NoError(t, err)

Expand Down
4 changes: 4 additions & 0 deletions lib/netext/httpext/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ type BatchParsedHTTPRequest struct {
// pre-initialized. In addition, each processed request would emit either a nil
// value, or an error, via the returned errors channel. The goroutines exit when
// the requests channel is closed.
// The processResponse callback can be used to modify the response, e.g.
// to replace the body.
func MakeBatchRequests(
ctx context.Context,
requests []BatchParsedHTTPRequest,
reqCount, globalLimit, perHostLimit int,
processResponse func(context.Context, *Response, ResponseType),
) <-chan error {
workers := globalLimit
if reqCount < workers {
Expand All @@ -62,6 +65,7 @@ func MakeBatchRequests(

resp, err := MakeRequest(ctx, req.ParsedHTTPRequest)
if resp != nil {
processResponse(ctx, resp, req.ParsedHTTPRequest.ResponseType)
*req.Response = *resp
}
result <- err
Expand Down

0 comments on commit c84375e

Please sign in to comment.