@@ -20,6 +20,7 @@ import (
20
20
"bufio"
21
21
"bytes"
22
22
"context"
23
+ "errors"
23
24
"io"
24
25
"net"
25
26
"net/http/httptest"
@@ -28,6 +29,8 @@ import (
28
29
"strings"
29
30
"testing"
30
31
"time"
32
+
33
+ "github.com/gorilla/websocket"
31
34
)
32
35
33
36
func TestServerRegisterName (t * testing.T ) {
@@ -217,14 +220,14 @@ func TestServerWebsocketReadLimit(t *testing.T) {
217
220
}{
218
221
{
219
222
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
222
225
shouldFail : false ,
223
226
},
224
227
{
225
228
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
228
231
shouldFail : true ,
229
232
},
230
233
}
@@ -261,9 +264,18 @@ func TestServerWebsocketReadLimit(t *testing.T) {
261
264
if err == nil {
262
265
t .Fatalf ("expected error for request size %d with limit %d, but got none" , tc .testSize , tc .readLimit )
263
266
}
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 )
267
279
}
268
280
} else {
269
281
// Expecting success
0 commit comments