@@ -19,8 +19,10 @@ package rpc
19
19
import (
20
20
"bufio"
21
21
"bytes"
22
+ "context"
22
23
"io"
23
24
"net"
25
+ "net/http/httptest"
24
26
"os"
25
27
"path/filepath"
26
28
"strings"
@@ -202,3 +204,115 @@ func TestServerBatchResponseSizeLimit(t *testing.T) {
202
204
}
203
205
}
204
206
}
207
+
208
+ func TestServerSetReadLimits (t * testing.T ) {
209
+ t .Parallel ()
210
+
211
+ // Test different read limits
212
+ testCases := []struct {
213
+ name string
214
+ readLimit int64
215
+ testSize int
216
+ shouldFail bool
217
+ }{
218
+ {
219
+ name : "small limit with small request - should succeed" ,
220
+ readLimit : 2048 ,
221
+ testSize : 500 , // Small request data
222
+ shouldFail : false ,
223
+ },
224
+ {
225
+ name : "small limit with large request - should fail" ,
226
+ readLimit : 2048 ,
227
+ testSize : 5000 , // Large request data that should exceed limit
228
+ shouldFail : true ,
229
+ },
230
+ {
231
+ name : "medium limit with medium request - should succeed" ,
232
+ readLimit : 10240 ,
233
+ testSize : 5000 , // Medium request data
234
+ shouldFail : false ,
235
+ },
236
+ {
237
+ name : "medium limit with large request - should fail" ,
238
+ readLimit : 10240 ,
239
+ testSize : 20000 , // Large request data
240
+ shouldFail : true ,
241
+ },
242
+ {
243
+ name : "large limit with large request - should succeed" ,
244
+ readLimit : 50000 ,
245
+ testSize : 20000 , // Large request data that should fit
246
+ shouldFail : false ,
247
+ },
248
+ }
249
+
250
+ for _ , tc := range testCases {
251
+ t .Run (tc .name , func (t * testing.T ) {
252
+ // Create server and set read limits
253
+ srv := newTestServer ()
254
+ srv .SetReadLimits (tc .readLimit )
255
+ defer srv .Stop ()
256
+
257
+ // Start HTTP server with WebSocket handler
258
+ httpsrv := httptest .NewServer (srv .WebsocketHandler ([]string {"*" }))
259
+ defer httpsrv .Close ()
260
+
261
+ wsURL := "ws:" + strings .TrimPrefix (httpsrv .URL , "http:" )
262
+
263
+ // Connect WebSocket client
264
+ client , err := DialOptions (context .Background (), wsURL )
265
+ if err != nil {
266
+ t .Fatalf ("can't dial: %v" , err )
267
+ }
268
+ defer client .Close ()
269
+
270
+ // Create large request data - this is what will be limited
271
+ largeString := strings .Repeat ("A" , tc .testSize )
272
+
273
+ // Send the large string as a parameter in the request
274
+ var result echoResult
275
+ err = client .Call (& result , "test_echo" , largeString , 42 , & echoArgs {S : "test" })
276
+
277
+ if tc .shouldFail {
278
+ // Expecting an error due to read limit exceeded
279
+ if err == nil {
280
+ t .Fatalf ("expected error for request size %d with limit %d, but got none" , tc .testSize , tc .readLimit )
281
+ }
282
+ // Check if it's the expected message size limit error
283
+ if ! strings .Contains (err .Error (), "message too big" ) {
284
+ t .Fatalf ("expected 'message too big' error, got: %v" , err )
285
+ }
286
+ } else {
287
+ // Expecting success
288
+ if err != nil {
289
+ t .Fatalf ("unexpected error for request size %d with limit %d: %v" , tc .testSize , tc .readLimit , err )
290
+ }
291
+ // Verify the response is correct - the echo should return our string
292
+ if result .String != largeString {
293
+ t .Fatalf ("expected echo result to match input" )
294
+ }
295
+ }
296
+ })
297
+ }
298
+ }
299
+
300
+ // Test that SetReadLimits properly updates the server's readerLimit field
301
+ func TestServerSetReadLimitsField (t * testing.T ) {
302
+ server := NewServer ()
303
+
304
+ // Test initial default value
305
+ if server .readLimit != wsDefaultReadLimit {
306
+ t .Errorf ("expected initial readerLimit to be %d, got %d" , wsDefaultReadLimit , server .readLimit )
307
+ }
308
+
309
+ // Test setting different values
310
+ testValues := []int64 {1024 , 10240 , 102400 , 1048576 }
311
+
312
+ for _ , expectedLimit := range testValues {
313
+ server .SetReadLimits (expectedLimit )
314
+ if server .readLimit != expectedLimit {
315
+ t .Errorf ("expected readerLimit to be %d after SetReadLimits, got %d" , expectedLimit , server .readLimit )
316
+ }
317
+ }
318
+ }
0 commit comments