@@ -113,6 +113,66 @@ func TestWebsocketLargeCall(t *testing.T) {
113
113
}
114
114
}
115
115
116
+ // This test checks whether the wsMessageSizeLimit option is obeyed.
117
+ func TestWebsocketLargeRead (t * testing.T ) {
118
+ t .Parallel ()
119
+
120
+ var (
121
+ srv = newTestServer ()
122
+ httpsrv = httptest .NewServer (srv .WebsocketHandler ([]string {"*" }))
123
+ wsURL = "ws:" + strings .TrimPrefix (httpsrv .URL , "http:" )
124
+ )
125
+ defer srv .Stop ()
126
+ defer httpsrv .Close ()
127
+
128
+ testLimit := func (limit * int64 ) {
129
+ opts := []ClientOption {}
130
+ expLimit := int64 (wsDefaultReadLimit )
131
+ if limit != nil && * limit >= 0 {
132
+ opts = append (opts , WithWebsocketMessageSizeLimit (* limit ))
133
+ if * limit > 0 {
134
+ expLimit = * limit // 0 means infinite
135
+ }
136
+ }
137
+ client , err := DialOptions (context .Background (), wsURL , opts ... )
138
+ if err != nil {
139
+ t .Fatalf ("can't dial: %v" , err )
140
+ }
141
+ defer client .Close ()
142
+ // Remove some bytes for json encoding overhead.
143
+ underLimit := int (expLimit - 128 )
144
+ overLimit := expLimit + 1
145
+ if expLimit == wsDefaultReadLimit {
146
+ // No point trying the full 32MB in tests. Just sanity-check that
147
+ // it's not obviously limited.
148
+ underLimit = 1024
149
+ overLimit = - 1
150
+ }
151
+ var res string
152
+ // Check under limit
153
+ if err = client .Call (& res , "test_repeat" , "A" , underLimit ); err != nil {
154
+ t .Fatalf ("unexpected error with limit %d: %v" , expLimit , err )
155
+ }
156
+ if len (res ) != underLimit || strings .Count (res , "A" ) != underLimit {
157
+ t .Fatal ("incorrect data" )
158
+ }
159
+ // Check over limit
160
+ if overLimit > 0 {
161
+ err = client .Call (& res , "test_repeat" , "A" , expLimit + 1 )
162
+ if err == nil || err != websocket .ErrReadLimit {
163
+ t .Fatalf ("wrong error with limit %d: %v expecting %v" , expLimit , err , websocket .ErrReadLimit )
164
+ }
165
+ }
166
+ }
167
+ ptr := func (v int64 ) * int64 { return & v }
168
+
169
+ testLimit (ptr (- 1 )) // Should be ignored (use default)
170
+ testLimit (ptr (0 )) // Should be ignored (use default)
171
+ testLimit (nil ) // Should be ignored (use default)
172
+ testLimit (ptr (200 ))
173
+ testLimit (ptr (wsDefaultReadLimit * 2 ))
174
+ }
175
+
116
176
func TestWebsocketPeerInfo (t * testing.T ) {
117
177
var (
118
178
s = newTestServer ()
@@ -206,7 +266,7 @@ func TestClientWebsocketLargeMessage(t *testing.T) {
206
266
defer srv .Stop ()
207
267
defer httpsrv .Close ()
208
268
209
- respLength := wsMessageSizeLimit - 50
269
+ respLength := wsDefaultReadLimit - 50
210
270
srv .RegisterName ("test" , largeRespService {respLength })
211
271
212
272
c , err := DialWebsocket (context .Background (), wsURL , "" )
0 commit comments