Skip to content

Commit

Permalink
fix(ws): return error before running setup fn if WS is not connectable
Browse files Browse the repository at this point in the history
Closes #1116
  • Loading branch information
Ivan Mirić committed Aug 29, 2019
1 parent 458316a commit 20d1d5a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,18 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP
}
}

// Run the user-provided set up function
if _, err := setupFn(goja.Undefined(), rt.ToValue(&socket)); err != nil {
return nil, err
}

if connErr != nil {
// Pass the error to the user script before exiting immediately
socket.handleEvent("error", rt.ToValue(connErr))

return nil, connErr
}

// Run the user-provided set up function
if _, err := setupFn(goja.Undefined(), rt.ToValue(&socket)); err != nil {
return nil, err
}

wsResponse, wsRespErr := wrapHTTPResponse(httpResponse)
if wsRespErr != nil {
return nil, wsRespErr
Expand Down
19 changes: 19 additions & 0 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ func TestErrors(t *testing.T) {
assert.Error(t, err)
})

t.Run("invalid_url_message_panic", func(t *testing.T) {
// Attempting to send a message to a non-existent socket shouldn't panic
_, err := common.RunString(rt, `
let res = ws.connect("INVALID", function(socket){
socket.send("new message");
});
`)
assert.Error(t, err)
})

t.Run("error_in_setup", func(t *testing.T) {
_, err := common.RunString(rt, `
let res = ws.connect("ws://demos.kaazing.com/echo", function(socket){
throw new Error("error in setup");
});
`)
assert.Error(t, err)
})

t.Run("send_after_close", func(t *testing.T) {
_, err := common.RunString(rt, `
let hasError = false;
Expand Down

0 comments on commit 20d1d5a

Please sign in to comment.