Skip to content

Commit

Permalink
Error out on not setting binarytype to arraybuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 24, 2024
1 parent 282803b commit e908745
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/dop251/goja v0.0.0-20230919151941-fc55792775de
github.com/gorilla/websocket v1.5.0
github.com/mstoykov/k6-taskqueue-lib v0.1.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
go.k6.io/k6 v0.47.1-0.20231012091148-de19a6a1bc53
go.uber.org/goleak v1.2.1
Expand Down Expand Up @@ -38,6 +37,7 @@ require (
github.com/onsi/gomega v1.20.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand Down
7 changes: 3 additions & 4 deletions websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ func (w *webSocket) loop() {
}
}

const binarytypeWarning = `You have not set a Websocket binaryType to "arraybuffer", but you got a binary response. ` +
`This has been done automatically now, but in the future this will not work.`
const binarytypeError = `You have not set a Websocket binaryType to "arraybuffer", but you got a binary response. ` +
`"blob" is still not supported we are erroring out.`

func (w *webSocket) queueMessage(msg *message) {
w.tq.Queue(func() error {
Expand All @@ -414,8 +414,7 @@ func (w *webSocket) queueMessage(msg *message) {

if msg.mtype == websocket.BinaryMessage {
if w.binaryType == "" {
w.binaryType = "arraybuffer"
w.vu.State().Logger.Warn(binarytypeWarning)
return errors.New(binarytypeError)

Check failure on line 417 in websockets/websockets.go

View workflow job for this annotation

GitHub Actions / checks / lint

ST1005: error strings should not end with punctuation or newlines (stylecheck)
}
// TODO this technically could be BLOB , but we don't support that
ab := rt.NewArrayBuffer(msg.data)
Expand Down
9 changes: 1 addition & 8 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (
"testing"

"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"

httpModule "go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -266,8 +264,6 @@ func TestReadyState(t *testing.T) {
func TestBinaryState(t *testing.T) {
t.Parallel()
ts := newTestState(t)
logger, hook := testutils.NewLoggerWithHook(t, logrus.WarnLevel)
ts.runtime.VU.StateField.Logger = logger
_, err := ts.runtime.RunOnEventLoop(ts.tb.Replacer.Replace(`
var ws = new WebSocket("WSBIN_URL/ws-echo-invalid")
ws.addEventListener("open", () => {
Expand All @@ -293,10 +289,7 @@ func TestBinaryState(t *testing.T) {
throw new Error("Expects ws.binaryType to not be writable")
}
`))
require.NoError(t, err)
logs := hook.Drain()
require.Len(t, logs, 1)
require.Contains(t, logs[0].Message, binarytypeWarning)
require.ErrorContains(t, err, binarytypeError)
}

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

0 comments on commit e908745

Please sign in to comment.