Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic on binaryResponses where the body wasn't set #2041

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2212,3 +2212,15 @@ func TestDigestAuthWithBody(t *testing.T) {
assertRequestMetricsEmitted(t, sampleContainers[0:1], "POST", urlRaw, urlRaw, 401, "")
assertRequestMetricsEmitted(t, sampleContainers[1:2], "POST", urlRaw, urlRaw, 200, "")
}

func TestBinaryResponseWithStatus0(t *testing.T) {
t.Parallel()
_, state, _, rt, _ := newRuntime(t) //nolint:dogsled
state.Options.Throw = null.BoolFrom(false)
_, err := rt.RunString(`
var res = http.get("https://asdajkdahdqiuwhejkasdnakjdnadasdlkas.com", { responseType: "binary" });
if (res.status !== 0) { throw new Error("wrong status: " + res.status); }
if (res.body !== null) { throw new Error("wrong body: " + JSON.stringify(res.body)); }
`)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion js/modules/k6/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (j jsonError) Error() string {
// respType. This is done here instead of in httpext.readResponseBody to avoid
// a reverse dependency on js/common or goja.
func processResponse(ctx context.Context, resp *httpext.Response, respType httpext.ResponseType) {
if respType == httpext.ResponseTypeBinary {
if respType == httpext.ResponseTypeBinary && resp.Body != nil {
rt := common.GetRuntime(ctx)
resp.Body = rt.NewArrayBuffer(resp.Body.([]byte))
}
Expand Down