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