Skip to content

Commit 539ff2a

Browse files
committed
Fix unit test
1 parent 9cd4ede commit 539ff2a

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

rpc/server_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"errors"
2324
"io"
2425
"net"
2526
"net/http/httptest"
@@ -28,6 +29,8 @@ import (
2829
"strings"
2930
"testing"
3031
"time"
32+
33+
"github.com/gorilla/websocket"
3134
)
3235

3336
func TestServerRegisterName(t *testing.T) {
@@ -217,14 +220,14 @@ func TestServerWebsocketReadLimit(t *testing.T) {
217220
}{
218221
{
219222
name: "limit with small request - should succeed",
220-
readLimit: 2048,
221-
testSize: 500, // Small request data
223+
readLimit: 4096, // generous limit to comfortably allow JSON overhead
224+
testSize: 256, // reasonably small payload
222225
shouldFail: false,
223226
},
224227
{
225228
name: "limit with large request - should fail",
226-
readLimit: 2048,
227-
testSize: 5000, // Large request data that should exceed limit
229+
readLimit: 256, // tight limit to trigger server-side read limit
230+
testSize: 1024, // payload that will exceed the limit including JSON overhead
228231
shouldFail: true,
229232
},
230233
}
@@ -261,9 +264,18 @@ func TestServerWebsocketReadLimit(t *testing.T) {
261264
if err == nil {
262265
t.Fatalf("expected error for request size %d with limit %d, but got none", tc.testSize, tc.readLimit)
263266
}
264-
// Check if it's the expected message size limit error
265-
if !strings.Contains(err.Error(), "message too big") {
266-
t.Fatalf("expected 'message too big' error, got: %v", err)
267+
// Be tolerant about the exact error surfaced by gorilla/websocket.
268+
// Prefer a CloseError with code 1009, but accept ErrReadLimit or an error string containing 1009/message too big.
269+
var cerr *websocket.CloseError
270+
if errors.As(err, &cerr) {
271+
if cerr.Code != websocket.CloseMessageTooBig {
272+
t.Fatalf("unexpected websocket close code: have %d want %d (err=%v)", cerr.Code, websocket.CloseMessageTooBig, err)
273+
}
274+
} else if !errors.Is(err, websocket.ErrReadLimit) &&
275+
!strings.Contains(strings.ToLower(err.Error()), "1009") &&
276+
!strings.Contains(strings.ToLower(err.Error()), "message too big") {
277+
// Not the error we expect from exceeding the message size limit.
278+
t.Fatalf("unexpected error for read limit violation: %v", err)
267279
}
268280
} else {
269281
// Expecting success

0 commit comments

Comments
 (0)