@@ -140,49 +140,56 @@ func TestWebsocketLargeRead(t *testing.T) {
140140 srv = newTestServer ()
141141 httpsrv = httptest .NewServer (srv .WebsocketHandler ([]string {"*" }))
142142 wsURL = "ws:" + strings .TrimPrefix (httpsrv .URL , "http:" )
143- buffer = 64
144143 )
145144 defer srv .Stop ()
146145 defer httpsrv .Close ()
147146
148- for _ , tt := range []struct {
149- size int
150- limit int
151- err bool
152- }{
153- {200 , 200 , false }, // Small, successful request and limit
154- {2048 , 1024 , true }, // Normal, failed request
155- {wsDefaultReadLimit + buffer , 0 , false }, // Large, successful request, infinite limit
156- } {
157- func () {
158- if tt .limit != 0 {
159- // Some buffer is added to the limit to account for JSON encoding. It's
160- // skipped when the limit is zero since the intention is for the limit
161- // to be infinite.
162- tt .limit += buffer
147+ testLimit := func (limit * int64 ) {
148+ opts := []ClientOption {}
149+ expLimit := int64 (wsDefaultReadLimit )
150+ if limit != nil && * limit >= 0 {
151+ opts = append (opts , WithWebsocketMessageSizeLimit (* limit ))
152+ if * limit > 0 {
153+ expLimit = * limit // 0 means infinite
163154 }
164- opts := []ClientOption {WithWebsocketMessageSizeLimit (int64 (tt .limit ))}
165- client , err := DialOptions (context .Background (), wsURL , opts ... )
166- if err != nil {
167- t .Fatalf ("failed to dial test server: %v" , err )
168- }
169- defer client .Close ()
170-
171- var res string
172- err = client .Call (& res , "test_repeat" , "A" , tt .size )
173- if tt .err && err == nil {
174- t .Fatalf ("expected error, got none" )
175- }
176- if ! tt .err {
177- if err != nil {
178- t .Fatalf ("unexpected error with limit %d: %v" , tt .limit , err )
179- }
180- if strings .Count (res , "A" ) != tt .size {
181- t .Fatal ("incorrect data" )
182- }
155+ }
156+ client , err := DialOptions (context .Background (), wsURL , opts ... )
157+ if err != nil {
158+ t .Fatalf ("can't dial: %v" , err )
159+ }
160+ defer client .Close ()
161+ // Remove some bytes for json encoding overhead.
162+ underLimit := int (expLimit - 128 )
163+ overLimit := expLimit + 1
164+ if expLimit == wsDefaultReadLimit {
165+ // No point trying the full 32MB in tests. Just sanity-check that
166+ // it's not obviously limited.
167+ underLimit = 1024
168+ overLimit = - 1
169+ }
170+ var res string
171+ // Check under limit
172+ if err = client .Call (& res , "test_repeat" , "A" , underLimit ); err != nil {
173+ t .Fatalf ("unexpected error with limit %d: %v" , expLimit , err )
174+ }
175+ if len (res ) != underLimit || strings .Count (res , "A" ) != underLimit {
176+ t .Fatal ("incorrect data" )
177+ }
178+ // Check over limit
179+ if overLimit > 0 {
180+ err = client .Call (& res , "test_repeat" , "A" , expLimit + 1 )
181+ if err == nil || err != websocket .ErrReadLimit {
182+ t .Fatalf ("wrong error with limit %d: %v expecting %v" , expLimit , err , websocket .ErrReadLimit )
183183 }
184- }()
184+ }
185185 }
186+ ptr := func (v int64 ) * int64 { return & v }
187+
188+ testLimit (ptr (- 1 )) // Should be ignored (use default)
189+ testLimit (ptr (0 )) // Should be ignored (use default)
190+ testLimit (nil ) // Should be ignored (use default)
191+ testLimit (ptr (200 ))
192+ testLimit (ptr (wsDefaultReadLimit + 1024 ))
186193}
187194
188195func TestWebsocketPeerInfo (t * testing.T ) {
0 commit comments