Skip to content

Commit

Permalink
Error with JS type in WS SendBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Apr 7, 2021
1 parent 14769d3 commit 23e3683
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion js/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestToString(t *testing.T) {
{struct{}{}, "", "invalid type struct {}, expected string, []byte or ArrayBuffer"},
}

for _, tc := range testCases { //nolint: paralleltest // false positive?
for _, tc := range testCases { //nolint: paralleltest // false positive: https://github.com/kunwardeep/paralleltest/issues/8
tc := tc
t.Run(fmt.Sprintf("%T", tc.in), func(t *testing.T) {
t.Parallel()
Expand Down
15 changes: 13 additions & 2 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,25 @@ func (s *Socket) Send(message string) {

// SendBinary writes the given ArrayBuffer message to the connection.
func (s *Socket) SendBinary(message goja.Value) {
if message == nil {
common.Throw(common.GetRuntime(s.ctx), errors.New("missing argument, expected ArrayBuffer"))
}

msg := message.Export()
if ab, ok := msg.(goja.ArrayBuffer); ok {
if err := s.conn.WriteMessage(websocket.BinaryMessage, ab.Bytes()); err != nil {
s.handleEvent("error", common.GetRuntime(s.ctx).ToValue(err))
}
} else {
common.Throw(common.GetRuntime(s.ctx),
fmt.Errorf("expected ArrayBuffer as argument, received: %T", msg))
rt := common.GetRuntime(s.ctx)
var jsType string
switch {
case goja.IsNull(message), goja.IsUndefined(message):
jsType = message.String()
default:
jsType = message.ToObject(rt).ClassName()
}
common.Throw(rt, fmt.Errorf("expected ArrayBuffer as argument, received: %s", jsType))
}

stats.PushIfNotDone(s.ctx, s.samplesOutput, stats.Sample{
Expand Down
47 changes: 35 additions & 12 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func TestSocketSendBinary(t *testing.T) { //nolint: tparallel
err = rt.Set("ws", common.Bind(rt, New(), &ctx))
assert.NoError(t, err)

t.Run("ok", func(t *testing.T) { //nolint: paralleltest // can't be run in parallel
t.Run("ok", func(t *testing.T) {
_, err = rt.RunString(sr(`
var gotMsg = false;
var res = ws.connect('WSBIN_URL/ws-echo', function(socket){
Expand All @@ -407,17 +407,40 @@ func TestSocketSendBinary(t *testing.T) { //nolint: tparallel
assert.NoError(t, err)
})

t.Run("err", func(t *testing.T) { //nolint: paralleltest // can't be run in parallel
_, err = rt.RunString(sr(`
var res = ws.connect('WSBIN_URL/ws-echo', function(socket){
socket.on('open', function() {
socket.sendBinary([104, 101, 108, 108, 111]);
})
});
`))
require.Error(t, err)
assert.Contains(t, err.Error(), "expected ArrayBuffer as argument, received: []interface {}")
})
errTestCases := []struct {
in, expErrType string
}{
{"", ""},
{"undefined", "undefined"},
{"null", "null"},
{"true", "Boolean"},
{"1", "Number"},
{"3.14", "Number"},
{"'str'", "String"},
{"[1, 2, 3]", "Array"},
{"new Uint8Array([1, 2, 3])", "Object"},
{"Symbol('a')", "Symbol"},
{"function() {}", "Function"},
}

for _, tc := range errTestCases { //nolint: paralleltest
tc := tc
t.Run(fmt.Sprintf("err_%s", tc.expErrType), func(t *testing.T) {
_, err = rt.RunString(fmt.Sprintf(sr(`
var res = ws.connect('WSBIN_URL/ws-echo', function(socket){
socket.on('open', function() {
socket.sendBinary(%s);
})
});
`), tc.in))
require.Error(t, err)
if tc.in == "" {
assert.Contains(t, err.Error(), "missing argument, expected ArrayBuffer")
} else {
assert.Contains(t, err.Error(), fmt.Sprintf("expected ArrayBuffer as argument, received: %s", tc.expErrType))
}
})
}
}

func TestErrors(t *testing.T) {
Expand Down

0 comments on commit 23e3683

Please sign in to comment.