2544
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
2507
|
3 |
"{ Package: 'stx:goodies/regression' }"
|
|
4 |
|
|
5 |
"{ NameSpace: RegressionTests }"
|
|
6 |
|
|
7 |
TestCase subclass:#WebSocketTest
|
2536
|
8 |
instanceVariableNames:''
|
|
9 |
classVariableNames:'ServerWebSocket ClientWebSocket'
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'tests-Regression-Web'
|
2507
|
12 |
!
|
|
13 |
|
|
14 |
HTTPService subclass:#DemoWebSocketService
|
2536
|
15 |
instanceVariableNames:'webSocketCreationCallback'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
privateIn:WebSocketTest
|
2507
|
19 |
!
|
|
20 |
|
|
21 |
!WebSocketTest class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
documentation
|
|
24 |
"
|
|
25 |
documentation to be added.
|
|
26 |
|
|
27 |
class:
|
|
28 |
<a short class summary here, describing what instances represent>
|
|
29 |
|
|
30 |
responsibilities:
|
|
31 |
<describing what my main role is>
|
|
32 |
|
|
33 |
collaborators:
|
|
34 |
<describing with whom and how I talk to>
|
|
35 |
|
|
36 |
API:
|
|
37 |
<public api and main messages>
|
|
38 |
|
|
39 |
example:
|
|
40 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
41 |
|
|
42 |
implementation:
|
|
43 |
<implementation points>
|
|
44 |
|
|
45 |
[author:]
|
|
46 |
Stefan Reise
|
|
47 |
|
|
48 |
[instance variables:]
|
|
49 |
|
|
50 |
[class variables:]
|
|
51 |
|
|
52 |
[see also:]
|
|
53 |
|
|
54 |
"
|
|
55 |
! !
|
|
56 |
|
2551
|
57 |
!WebSocketTest class methodsFor:'initialization'!
|
|
58 |
|
|
59 |
initialize
|
|
60 |
|needsToReloadDemoWebSocketService|
|
|
61 |
|
|
62 |
super initialize.
|
|
63 |
|
|
64 |
needsToReloadDemoWebSocketService := (Smalltalk at:#HTTPService) isNil.
|
|
65 |
|
|
66 |
Smalltalk loadPackage:'stx:goodies/communication'.
|
|
67 |
Smalltalk loadPackage:'stx:goodies/webServer'.
|
|
68 |
|
|
69 |
needsToReloadDemoWebSocketService ifFalse:[
|
|
70 |
^ self
|
|
71 |
].
|
|
72 |
|
|
73 |
"file in again to get a working DemoWebSocketService"
|
|
74 |
(self packageDirectory / 'RegressionTests__WebSocketTest.st') fileIn.
|
|
75 |
|
|
76 |
"Created: / 05-02-2020 / 13:24:53 / Stefan Reise"
|
|
77 |
! !
|
|
78 |
|
2507
|
79 |
!WebSocketTest class methodsFor:'actions'!
|
|
80 |
|
|
81 |
resetServerAndClientWebsocket
|
|
82 |
"
|
|
83 |
self resetServerAndClientWebsocket
|
|
84 |
"
|
|
85 |
|
|
86 |
ServerWebSocket := nil.
|
|
87 |
ClientWebSocket := nil.
|
|
88 |
|
|
89 |
"Created: / 17-01-2020 / 13:37:18 / Stefan Reise"
|
|
90 |
! !
|
|
91 |
|
|
92 |
!WebSocketTest methodsFor:'data'!
|
|
93 |
|
|
94 |
allData
|
2516
|
95 |
"
|
|
96 |
self new allData
|
|
97 |
"
|
|
98 |
|
2507
|
99 |
|allData|
|
|
100 |
|
|
101 |
allData := OrderedCollection new.
|
|
102 |
self allDataWithFrameHash pairWiseDo:[:first :second |
|
|
103 |
allData add:first.
|
|
104 |
].
|
|
105 |
|
|
106 |
^ allData
|
|
107 |
|
|
108 |
"Created: / 20-01-2020 / 13:56:05 / Stefan Reise"
|
2516
|
109 |
"Modified (comment): / 21-01-2020 / 10:38:54 / Stefan Reise"
|
2507
|
110 |
!
|
|
111 |
|
|
112 |
allDataWithFrameHash
|
2516
|
113 |
"
|
|
114 |
self new allDataWithFrameHash
|
|
115 |
"
|
|
116 |
|
2507
|
117 |
^ self dataWithLength0AndFrameHash "/ zero data
|
|
118 |
, self dataWithLength11AndFrameHash "/ just hello world
|
|
119 |
, self dataWithLength125AndFrameHash "/ last fitting length for the single byte
|
|
120 |
, self dataWithLength126AndFrameHash "/ first length to put into for 2 bytes according to the protocol
|
|
121 |
, self dataWithLength127AndFrameHash "/ first length + 1 to put into 2 bytes
|
|
122 |
, self dataWithLength65535AndFrameHash "/ last fitting length for 2 bytes
|
|
123 |
, self dataWithLength65536AndFrameHash "/ first length to put into 8 bytes according to the protocol
|
|
124 |
, self dataWithLength70536AndFrameHash "/ random length to put into 8 bytes
|
|
125 |
, self dataWithLengthMaxPerFrameAndFrameHash "/ last fitting length for max frame length
|
|
126 |
, self dataWithLengthMaxPerFramePlus1AndFrameHash "/ first length to put into multiple frames
|
|
127 |
, self dataWithLengthMaxTestAndFrameHash "/ max length for testing
|
|
128 |
|
|
129 |
"Created: / 20-01-2020 / 13:54:14 / Stefan Reise"
|
2516
|
130 |
"Modified (comment): / 21-01-2020 / 10:38:30 / Stefan Reise"
|
2507
|
131 |
!
|
|
132 |
|
2524
|
133 |
byteArray100
|
|
134 |
^ ByteArray new:100 * 1024 * 1024
|
|
135 |
|
|
136 |
"Created: / 22-01-2020 / 15:42:28 / Stefan Reise"
|
|
137 |
!
|
|
138 |
|
|
139 |
data100
|
|
140 |
^ String new:100 * 1024 * 1024
|
|
141 |
|
|
142 |
"Created: / 22-01-2020 / 15:40:35 / Stefan Reise"
|
|
143 |
!
|
|
144 |
|
2507
|
145 |
dataWithLength0AndFrameHash
|
|
146 |
^ #(
|
|
147 |
''
|
|
148 |
33024
|
|
149 |
)
|
|
150 |
|
|
151 |
"Created: / 20-01-2020 / 13:24:23 / Stefan Reise"
|
|
152 |
!
|
|
153 |
|
|
154 |
dataWithLength11AndFrameHash
|
|
155 |
^ #(
|
|
156 |
'Hello World'
|
|
157 |
457351956
|
|
158 |
)
|
|
159 |
|
|
160 |
"Created: / 20-01-2020 / 13:24:42 / Stefan Reise"
|
|
161 |
!
|
|
162 |
|
|
163 |
dataWithLength125AndFrameHash
|
|
164 |
^ #(
|
|
165 |
'12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345'
|
|
166 |
854990094
|
|
167 |
)
|
|
168 |
|
|
169 |
"Created: / 20-01-2020 / 13:21:12 / Stefan Reise"
|
|
170 |
!
|
|
171 |
|
|
172 |
dataWithLength126AndFrameHash
|
|
173 |
^ #(
|
|
174 |
'123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456'
|
|
175 |
386953194
|
|
176 |
)
|
|
177 |
|
|
178 |
"Created: / 20-01-2020 / 13:21:15 / Stefan Reise"
|
|
179 |
!
|
|
180 |
|
|
181 |
dataWithLength127AndFrameHash
|
|
182 |
^ #(
|
|
183 |
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567'
|
|
184 |
1015153950
|
|
185 |
)
|
|
186 |
|
|
187 |
"Created: / 20-01-2020 / 13:21:18 / Stefan Reise"
|
|
188 |
!
|
|
189 |
|
|
190 |
dataWithLength65535AndFrameHash
|
|
191 |
|string|
|
|
192 |
|
|
193 |
string := String new:65535.
|
|
194 |
|
|
195 |
^ Array
|
|
196 |
with:string
|
|
197 |
with:526675651
|
|
198 |
|
|
199 |
"Created: / 20-01-2020 / 13:21:20 / Stefan Reise"
|
|
200 |
!
|
|
201 |
|
|
202 |
dataWithLength65536AndFrameHash
|
|
203 |
|string|
|
|
204 |
|
|
205 |
string := String new:65536.
|
|
206 |
|
|
207 |
^ Array
|
|
208 |
with:string
|
|
209 |
with:894588570
|
|
210 |
|
|
211 |
"Created: / 20-01-2020 / 13:21:23 / Stefan Reise"
|
|
212 |
!
|
|
213 |
|
|
214 |
dataWithLength70536AndFrameHash
|
|
215 |
|string|
|
|
216 |
|
|
217 |
string := String new:70536.
|
|
218 |
|
|
219 |
^ Array
|
|
220 |
with:string
|
|
221 |
with:201475330
|
|
222 |
|
|
223 |
"Created: / 20-01-2020 / 13:21:25 / Stefan Reise"
|
|
224 |
!
|
|
225 |
|
|
226 |
dataWithLengthMaxPerFrameAndFrameHash
|
|
227 |
|string|
|
|
228 |
|
2530
|
229 |
string := String new:WebSocketStream maxBytesPerFrame.
|
2507
|
230 |
|
|
231 |
^ Array
|
|
232 |
with:string
|
|
233 |
with:656112522
|
|
234 |
|
|
235 |
"Created: / 20-01-2020 / 13:59:13 / Stefan Reise"
|
|
236 |
!
|
|
237 |
|
|
238 |
dataWithLengthMaxPerFramePlus1AndFrameHash
|
|
239 |
|string|
|
|
240 |
|
2530
|
241 |
string := String new:WebSocketStream maxBytesPerFrame + 1.
|
2507
|
242 |
|
|
243 |
^ Array
|
|
244 |
with:string
|
|
245 |
with:95386507
|
|
246 |
|
|
247 |
"Created: / 20-01-2020 / 14:03:29 / Stefan Reise"
|
|
248 |
!
|
|
249 |
|
|
250 |
dataWithLengthMaxTestAndFrameHash
|
|
251 |
|string|
|
|
252 |
|
|
253 |
string := String new:5 * 1024 * 1024. "/ 5mb
|
|
254 |
|
|
255 |
^ Array
|
|
256 |
with:string
|
|
257 |
with:888620042
|
|
258 |
|
|
259 |
"Created: / 20-01-2020 / 14:04:40 / Stefan Reise"
|
|
260 |
! !
|
|
261 |
|
|
262 |
!WebSocketTest methodsFor:'helper'!
|
|
263 |
|
2537
|
264 |
assertDataOrFilenameA:dataOrFilenameA
|
|
265 |
equalsDataOrFilenameB:dataOrFilenameB
|
|
266 |
|
|
267 |
|convertToDataBlock
|
|
268 |
dataA dataB|
|
|
269 |
|
|
270 |
"compare the whole content"
|
|
271 |
"/ compareSizeLimit := 20 * 1024 * 1024. "/ 20mb
|
|
272 |
|
|
273 |
convertToDataBlock :=
|
|
274 |
[:eachDataOrFilename |
|
|
275 |
eachDataOrFilename isFilename ifTrue:[
|
|
276 |
|fileContentForCompare|
|
|
277 |
|
|
278 |
eachDataOrFilename readingFileDo:[:rs |
|
|
279 |
fileContentForCompare := rs upToEnd.
|
|
280 |
"/ fileContentForCompare := rs nextAvailable:compareSizeLimit.
|
|
281 |
].
|
|
282 |
|
|
283 |
fileContentForCompare
|
|
284 |
] ifFalse:[
|
|
285 |
eachDataOrFilename
|
|
286 |
"/ eachDataOrFilename to:(compareSizeLimit min:dataOrFilenameA size).
|
|
287 |
]
|
|
288 |
].
|
|
289 |
|
|
290 |
dataA := convertToDataBlock value:dataOrFilenameA.
|
|
291 |
dataB := convertToDataBlock value:dataOrFilenameB.
|
|
292 |
|
|
293 |
self assert:dataA asByteArray = dataB asByteArray.
|
|
294 |
|
|
295 |
"Created: / 27-01-2020 / 16:19:43 / Stefan Reise"
|
|
296 |
!
|
|
297 |
|
2545
|
298 |
assertOnEchoServer:data
|
|
299 |
|reply webSocket|
|
|
300 |
|
|
301 |
reply := HTTPInterface get:'ws://echo.websocket.org'.
|
|
302 |
self assert:reply isErrorResponse not.
|
|
303 |
|
|
304 |
webSocket := reply webSocket.
|
|
305 |
webSocket nextPut:data.
|
|
306 |
|
|
307 |
Delay waitForSeconds:5.
|
|
308 |
|
|
309 |
self assert:webSocket next = data.
|
|
310 |
|
|
311 |
"Created: / 04-02-2020 / 12:05:27 / Stefan Reise"
|
|
312 |
!
|
|
313 |
|
2524
|
314 |
communicationWithData:dataArrayOrData
|
|
315 |
doAssert:doAssert
|
|
316 |
|
2528
|
317 |
|nextData|
|
|
318 |
|
2524
|
319 |
^ self
|
|
320 |
withData:dataArrayOrData
|
2537
|
321 |
do:[:eachDataOrFilename |
|
|
322 |
self clientWebSocket nextPut:eachDataOrFilename.
|
2528
|
323 |
nextData := self serverWebSocket next.
|
2524
|
324 |
doAssert ifTrue:[
|
2537
|
325 |
self
|
|
326 |
assertDataOrFilenameA:nextData
|
|
327 |
equalsDataOrFilenameB:eachDataOrFilename.
|
2524
|
328 |
].
|
2507
|
329 |
|
2537
|
330 |
self serverWebSocket nextPut:eachDataOrFilename.
|
2539
|
331 |
nextData := self clientWebSocket next.
|
2524
|
332 |
doAssert ifTrue:[
|
2537
|
333 |
self
|
|
334 |
assertDataOrFilenameA:nextData
|
|
335 |
equalsDataOrFilenameB:eachDataOrFilename.
|
2524
|
336 |
].
|
|
337 |
]
|
|
338 |
|
|
339 |
"Created: / 22-01-2020 / 16:19:34 / Stefan Reise"
|
2539
|
340 |
"Modified: / 27-01-2020 / 17:28:33 / Stefan Reise"
|
2524
|
341 |
!
|
|
342 |
|
|
343 |
maskAndUnmaskData:dataArrayOrData
|
|
344 |
doAssert:doAssert
|
2507
|
345 |
|
2524
|
346 |
|webSocket mask eachDataAsByteArray eachMasked eachUnmasked|
|
|
347 |
|
|
348 |
webSocket := WebSocketStream new.
|
|
349 |
mask := #[12 214 166 43].
|
|
350 |
|
|
351 |
^ self
|
|
352 |
withData:dataArrayOrData
|
|
353 |
do:[:eachByteArrayOrString |
|
|
354 |
eachDataAsByteArray := eachByteArrayOrString asByteArray.
|
2507
|
355 |
|
2524
|
356 |
eachMasked := webSocket
|
|
357 |
maskOrUnmaskPayload:eachDataAsByteArray
|
|
358 |
withMask:mask.
|
|
359 |
|
|
360 |
eachUnmasked := webSocket
|
|
361 |
maskOrUnmaskPayload:eachMasked
|
|
362 |
withMask:mask.
|
|
363 |
|
|
364 |
doAssert ifTrue:[
|
|
365 |
self assert:eachUnmasked = eachDataAsByteArray.
|
|
366 |
].
|
|
367 |
]
|
2507
|
368 |
|
2524
|
369 |
"Created: / 22-01-2020 / 16:22:41 / Stefan Reise"
|
|
370 |
!
|
|
371 |
|
|
372 |
withData:dataArrayOrData
|
|
373 |
do:aBlock
|
|
374 |
|
|
375 |
|startTimestamp dataArray timeDurationUsed|
|
|
376 |
|
|
377 |
startTimestamp := Timestamp now.
|
|
378 |
|
|
379 |
dataArray := dataArrayOrData.
|
2537
|
380 |
|
|
381 |
(dataArrayOrData
|
2541
|
382 |
perform:#'firstIfEmpty:'
|
2537
|
383 |
with:nil
|
|
384 |
ifNotUnderstood:nil) isCollection ifFalse:[
|
|
385 |
dataArray := Array with:dataArrayOrData.
|
|
386 |
].
|
2524
|
387 |
|
|
388 |
dataArray do:aBlock.
|
|
389 |
|
|
390 |
timeDurationUsed := Timestamp now - startTimestamp.
|
|
391 |
Transcript showCR:timeDurationUsed printString.
|
|
392 |
|
|
393 |
^ timeDurationUsed
|
2507
|
394 |
|
2524
|
395 |
"Created: / 22-01-2020 / 16:21:30 / Stefan Reise"
|
2541
|
396 |
"Modified: / 27-01-2020 / 17:49:37 / Stefan Reise"
|
2524
|
397 |
! !
|
|
398 |
|
|
399 |
!WebSocketTest methodsFor:'queries'!
|
|
400 |
|
|
401 |
clientWebSocket
|
|
402 |
ClientWebSocket isNil ifTrue:[
|
|
403 |
self setupServerAndClientWebsocket.
|
|
404 |
].
|
|
405 |
|
|
406 |
^ ClientWebSocket
|
2507
|
407 |
|
2524
|
408 |
"Created: / 17-01-2020 / 13:23:11 / Stefan Reise"
|
2507
|
409 |
!
|
|
410 |
|
2524
|
411 |
serverWebSocket
|
|
412 |
ServerWebSocket isNil ifTrue:[
|
|
413 |
self setupServerAndClientWebsocket.
|
|
414 |
].
|
|
415 |
|
|
416 |
^ ServerWebSocket
|
|
417 |
|
|
418 |
"Created: / 17-01-2020 / 13:23:15 / Stefan Reise"
|
|
419 |
! !
|
|
420 |
|
2552
|
421 |
!WebSocketTest methodsFor:'release'!
|
|
422 |
|
|
423 |
setUp
|
|
424 |
|response|
|
|
425 |
|
|
426 |
DemoWebSocketService start
|
|
427 |
webSocketCreationCallback:[:newWebSocket |
|
|
428 |
ServerWebSocket := newWebSocket.
|
|
429 |
].
|
|
430 |
|
|
431 |
OperatingSystem isUNIXlike ifTrue:[
|
|
432 |
"the first request,
|
|
433 |
gets error response,
|
|
434 |
hmmm linux need some time..."
|
|
435 |
Delay waitForSeconds:3.
|
|
436 |
].
|
|
437 |
|
|
438 |
response := HTTPInterface
|
|
439 |
get:('ws://localhost:%1%2'
|
|
440 |
bindWith:DemoWebSocketService defaultPort
|
|
441 |
with:DemoWebSocketService linkName).
|
|
442 |
|
|
443 |
response isErrorResponse ifTrue:[
|
|
444 |
self error:'failed to connect'.
|
|
445 |
].
|
|
446 |
|
|
447 |
ClientWebSocket := response webSocket.
|
|
448 |
|
|
449 |
"Created: / 05-02-2020 / 13:54:35 / Stefan Reise"
|
|
450 |
!
|
|
451 |
|
|
452 |
tearDown
|
|
453 |
|runningServer|
|
|
454 |
|
|
455 |
super tearDown.
|
|
456 |
|
|
457 |
ServerWebSocket notNil ifTrue:[
|
|
458 |
ServerWebSocket close.
|
|
459 |
].
|
|
460 |
|
|
461 |
ClientWebSocket notNil ifTrue:[
|
|
462 |
ClientWebSocket close.
|
|
463 |
].
|
|
464 |
|
|
465 |
runningServer := HTTPServer runningServerOnPort:DemoWebSocketService defaultPort.
|
|
466 |
runningServer isNil ifTrue:[
|
|
467 |
^ self
|
|
468 |
].
|
|
469 |
|
|
470 |
runningServer terminateServer.
|
|
471 |
|
|
472 |
"Created: / 05-02-2020 / 13:34:55 / Stefan Reise"
|
|
473 |
! !
|
|
474 |
|
2524
|
475 |
!WebSocketTest methodsFor:'setup'!
|
|
476 |
|
2507
|
477 |
setupServerAndClientWebsocket
|
|
478 |
"
|
|
479 |
self resetServerAndClientWebsocket
|
|
480 |
"
|
|
481 |
|
|
482 |
|response|
|
|
483 |
|
|
484 |
DemoWebSocketService start
|
|
485 |
webSocketCreationCallback:[:newWebSocket |
|
|
486 |
ServerWebSocket := newWebSocket.
|
|
487 |
].
|
|
488 |
|
2533
|
489 |
OperatingSystem isUNIXlike ifTrue:[
|
|
490 |
"the first request,
|
|
491 |
gets error response,
|
|
492 |
hmmm linux need some time..."
|
|
493 |
Delay waitForSeconds:3.
|
|
494 |
].
|
|
495 |
|
2507
|
496 |
response := HTTPInterface
|
|
497 |
get:('ws://localhost:%1%2'
|
|
498 |
bindWith:DemoWebSocketService defaultPort
|
|
499 |
with:DemoWebSocketService linkName).
|
|
500 |
|
|
501 |
response isErrorResponse ifTrue:[
|
|
502 |
self error:'failed to connect'.
|
|
503 |
].
|
|
504 |
|
|
505 |
ClientWebSocket := response webSocket.
|
|
506 |
|
|
507 |
"Created: / 17-01-2020 / 13:21:28 / Stefan Reise"
|
2515
|
508 |
"Modified: / 20-01-2020 / 18:08:26 / Stefan Reise"
|
2507
|
509 |
! !
|
|
510 |
|
|
511 |
!WebSocketTest methodsFor:'tests'!
|
|
512 |
|
|
513 |
testCommunication
|
|
514 |
"
|
2524
|
515 |
!! call the following method in case the sockets did get corrupted !!
|
|
516 |
self resetServerAndClientWebsocket
|
|
517 |
|
|
518 |
WebSocketStream verbose:true.
|
|
519 |
WebSocketStream verboseProtocol:true.
|
|
520 |
|
|
521 |
WebSocketStream verbose:false.
|
|
522 |
WebSocketStream verboseProtocol:false.
|
|
523 |
|
2530
|
524 |
WebSocketStream maxBytesPerFrame:1024.
|
|
525 |
WebSocketStream maxBytesPerFrame:nil.
|
2524
|
526 |
"
|
|
527 |
|
|
528 |
self
|
|
529 |
communicationWithData:self allData
|
|
530 |
doAssert:true.
|
|
531 |
|
|
532 |
"Created: / 17-01-2020 / 13:24:24 / Stefan Reise"
|
|
533 |
"Modified: / 22-01-2020 / 16:25:01 / Stefan Reise"
|
2530
|
534 |
"Modified (comment): / 23-01-2020 / 11:59:45 / Stefan Reise"
|
2524
|
535 |
!
|
|
536 |
|
|
537 |
testCommunication100
|
|
538 |
"
|
|
539 |
!! call the following method in case the sockets did get corrupted !!
|
2515
|
540 |
self resetServerAndClientWebsocket
|
|
541 |
|
|
542 |
WebSocketStream verbose:true.
|
|
543 |
WebSocketStream verboseProtocol:true.
|
|
544 |
|
|
545 |
WebSocketStream verbose:false.
|
|
546 |
WebSocketStream verboseProtocol:false.
|
|
547 |
|
2530
|
548 |
WebSocketStream maxBytesPerFrame:1024.
|
|
549 |
WebSocketStream maxBytesPerFrame:nil.
|
2507
|
550 |
"
|
|
551 |
|
2524
|
552 |
self
|
|
553 |
communicationWithData:self data100
|
|
554 |
doAssert:true.
|
|
555 |
|
|
556 |
"Created: / 22-01-2020 / 16:39:15 / Stefan Reise"
|
2530
|
557 |
"Modified (comment): / 23-01-2020 / 11:59:47 / Stefan Reise"
|
2524
|
558 |
!
|
|
559 |
|
2546
|
560 |
testCommunication100Speed
|
2543
|
561 |
"
|
|
562 |
!! call the following method in case the sockets did get corrupted !!
|
|
563 |
self resetServerAndClientWebsocket
|
|
564 |
|
|
565 |
WebSocketStream verbose:true.
|
|
566 |
WebSocketStream verboseProtocol:true.
|
|
567 |
|
|
568 |
WebSocketStream verbose:false.
|
|
569 |
WebSocketStream verboseProtocol:false.
|
|
570 |
|
|
571 |
WebSocketStream maxBytesPerFrame:1024.
|
|
572 |
WebSocketStream maxBytesPerFrame:nil.
|
|
573 |
"
|
|
574 |
|
|
575 |
self
|
2546
|
576 |
communicationWithData:self data100
|
|
577 |
doAssert:false.
|
2524
|
578 |
|
2546
|
579 |
"Created: / 23-01-2020 / 11:32:34 / Stefan Reise"
|
2524
|
580 |
!
|
|
581 |
|
2535
|
582 |
testCommunicationByteArray100
|
|
583 |
"
|
|
584 |
!! call the following method in case the sockets did get corrupted !!
|
|
585 |
self resetServerAndClientWebsocket
|
|
586 |
|
|
587 |
WebSocketStream verbose:true.
|
|
588 |
WebSocketStream verboseProtocol:true.
|
|
589 |
|
|
590 |
WebSocketStream verbose:false.
|
|
591 |
WebSocketStream verboseProtocol:false.
|
|
592 |
|
|
593 |
WebSocketStream maxBytesPerFrame:1024.
|
|
594 |
WebSocketStream maxBytesPerFrame:nil.
|
|
595 |
"
|
|
596 |
|
|
597 |
self
|
|
598 |
communicationWithData:self byteArray100
|
|
599 |
doAssert:true.
|
|
600 |
|
|
601 |
"Created: / 22-01-2020 / 17:45:53 / Stefan Reise"
|
|
602 |
"Modified (comment): / 23-01-2020 / 11:59:55 / Stefan Reise"
|
|
603 |
!
|
|
604 |
|
2537
|
605 |
testCommunicationFilename
|
|
606 |
"
|
|
607 |
!! call the following method in case the sockets did get corrupted !!
|
|
608 |
self resetServerAndClientWebsocket
|
|
609 |
|
|
610 |
WebSocketStream verbose:true.
|
|
611 |
WebSocketStream verboseProtocol:true.
|
|
612 |
|
|
613 |
WebSocketStream verbose:false.
|
|
614 |
WebSocketStream verboseProtocol:false.
|
|
615 |
|
|
616 |
WebSocketStream maxBytesPerFrame:1024.
|
|
617 |
WebSocketStream maxBytesPerFrame:nil.
|
|
618 |
"
|
|
619 |
|
2546
|
620 |
|tempFile|
|
2544
|
621 |
|
2546
|
622 |
tempFile := Filename newTemporary.
|
|
623 |
tempFile contents:self data100.
|
2537
|
624 |
|
2538
|
625 |
self
|
2546
|
626 |
communicationWithData:tempFile
|
2540
|
627 |
doAssert:true.
|
2537
|
628 |
|
|
629 |
"Created: / 27-01-2020 / 15:07:37 / Stefan Reise"
|
2546
|
630 |
"Modified (format): / 04-02-2020 / 12:22:23 / Stefan Reise"
|
2537
|
631 |
!
|
|
632 |
|
2524
|
633 |
testCommunicationSmallFrameSize
|
|
634 |
"
|
|
635 |
!! call the following method in case the sockets did get corrupted !!
|
|
636 |
self resetServerAndClientWebsocket
|
|
637 |
|
|
638 |
WebSocketStream verbose:true.
|
|
639 |
WebSocketStream verboseProtocol:true.
|
|
640 |
|
|
641 |
WebSocketStream verbose:false.
|
|
642 |
WebSocketStream verboseProtocol:false.
|
2507
|
643 |
|
2530
|
644 |
WebSocketStream maxBytesPerFrame:1024.
|
|
645 |
WebSocketStream maxBytesPerFrame:nil.
|
2524
|
646 |
"
|
|
647 |
|
|
648 |
|maxSizeInByterPerFrameBefore|
|
|
649 |
|
2530
|
650 |
maxSizeInByterPerFrameBefore := WebSocketStream maxBytesPerFrame.
|
|
651 |
WebSocketStream maxBytesPerFrame:1024.
|
2524
|
652 |
|
|
653 |
self
|
|
654 |
communicationWithData:self allData
|
|
655 |
doAssert:true.
|
|
656 |
|
2530
|
657 |
WebSocketStream maxBytesPerFrame:maxSizeInByterPerFrameBefore.
|
2524
|
658 |
|
|
659 |
"Created: / 22-01-2020 / 16:17:28 / Stefan Reise"
|
2530
|
660 |
"Modified (comment): / 23-01-2020 / 11:59:57 / Stefan Reise"
|
2516
|
661 |
!
|
|
662 |
|
|
663 |
testCommunicationWithDelay
|
|
664 |
"
|
2524
|
665 |
!! call the following method in case the sockets did get corrupted !!
|
2516
|
666 |
self resetServerAndClientWebsocket
|
|
667 |
|
|
668 |
WebSocketStream verbose:true.
|
|
669 |
WebSocketStream verboseProtocol:true.
|
|
670 |
|
|
671 |
WebSocketStream verbose:false.
|
|
672 |
WebSocketStream verboseProtocol:false.
|
|
673 |
|
2530
|
674 |
WebSocketStream maxBytesPerFrame:1024.
|
|
675 |
WebSocketStream maxBytesPerFrame:nil.
|
2516
|
676 |
"
|
|
677 |
|
|
678 |
self allData do:[:data |
|
|
679 |
self clientWebSocket nextPut:data.
|
|
680 |
].
|
|
681 |
|
|
682 |
Delay waitForSeconds:2.
|
|
683 |
|
|
684 |
self allData do:[:data |
|
|
685 |
self assert:self serverWebSocket next = data.
|
|
686 |
self serverWebSocket nextPut:data.
|
|
687 |
].
|
|
688 |
|
|
689 |
Delay waitForSeconds:2.
|
|
690 |
|
|
691 |
self allData do:[:data |
|
|
692 |
self assert:self clientWebSocket next = data.
|
|
693 |
].
|
|
694 |
|
|
695 |
"Created: / 21-01-2020 / 11:26:23 / Stefan Reise"
|
2530
|
696 |
"Modified (comment): / 23-01-2020 / 12:00:00 / Stefan Reise"
|
2507
|
697 |
!
|
|
698 |
|
|
699 |
testEncoding
|
|
700 |
"
|
2524
|
701 |
!! call the following method in case the sockets did get corrupted !!
|
2515
|
702 |
self resetServerAndClientWebsocket
|
|
703 |
|
|
704 |
WebSocketStream verbose:true.
|
|
705 |
WebSocketStream verboseProtocol:true.
|
|
706 |
|
|
707 |
WebSocketStream verbose:false.
|
|
708 |
WebSocketStream verboseProtocol:false.
|
2507
|
709 |
"
|
|
710 |
|
2524
|
711 |
|serverWebSocket clientWebSocket
|
|
712 |
decodeBlock
|
2530
|
713 |
maxBytesPerFrameBefore|
|
2524
|
714 |
|
|
715 |
serverWebSocket := self serverWebSocket.
|
|
716 |
clientWebSocket := self clientWebSocket.
|
|
717 |
|
|
718 |
decodeBlock :=
|
|
719 |
[:eachWebSocket :eachFrame |
|
|
720 |
((eachWebSocket decodeFrameHybi17:eachFrame) payload ? '') asString
|
|
721 |
].
|
|
722 |
|
|
723 |
[
|
|
724 |
"inside #allDataWithFrameHash is #dataWithLengthMaxPerFrameAndFrameHash,
|
2530
|
725 |
which hash do depent on the #maxBytesPerFrame,
|
2536
|
726 |
so set the value, which was used when the hash has been recorded"
|
2530
|
727 |
maxBytesPerFrameBefore := WebSocketStream maxBytesPerFrame.
|
|
728 |
WebSocketStream maxBytesPerFrame:512 * 1024.
|
2524
|
729 |
|
|
730 |
self allDataWithFrameHash
|
|
731 |
pairWiseDo:[:eachData :eachExpectedHashForServer |
|
|
732 |
|eachFrame|
|
|
733 |
|
|
734 |
"server -> client"
|
|
735 |
eachFrame := serverWebSocket encodeFrameHybi17Text:eachData.
|
|
736 |
self assert:eachFrame hash = eachExpectedHashForServer.
|
|
737 |
self assert:eachData = (decodeBlock value:clientWebSocket value:eachFrame).
|
|
738 |
|
|
739 |
"client -> server"
|
|
740 |
eachFrame := clientWebSocket encodeFrameHybi17Text:eachData.
|
|
741 |
"/ not able to check against hash from client requests, because they are masked randomly
|
|
742 |
self assert:eachData = (decodeBlock value:serverWebSocket value:eachFrame).
|
|
743 |
].
|
|
744 |
] ensure:[
|
2530
|
745 |
maxBytesPerFrameBefore notNil ifTrue:[
|
|
746 |
WebSocketStream maxBytesPerFrame:maxBytesPerFrameBefore.
|
2524
|
747 |
].
|
|
748 |
].
|
2507
|
749 |
|
|
750 |
"Created: / 20-01-2020 / 13:13:50 / Stefan Reise"
|
2536
|
751 |
"Modified (format): / 24-01-2020 / 16:03:42 / Stefan Reise"
|
2507
|
752 |
!
|
|
753 |
|
|
754 |
testMask
|
2524
|
755 |
self
|
|
756 |
maskAndUnmaskData:self allData
|
|
757 |
doAssert:true.
|
2507
|
758 |
|
|
759 |
"Created: / 20-01-2020 / 15:08:27 / Stefan Reise"
|
2524
|
760 |
"Modified: / 22-01-2020 / 16:22:48 / Stefan Reise"
|
|
761 |
!
|
|
762 |
|
|
763 |
testMask100
|
|
764 |
self
|
|
765 |
maskAndUnmaskData:self byteArray100
|
|
766 |
doAssert:true.
|
|
767 |
|
|
768 |
"Created: / 22-01-2020 / 15:41:29 / Stefan Reise"
|
|
769 |
!
|
|
770 |
|
2546
|
771 |
testMask100Speed
|
2524
|
772 |
self
|
2546
|
773 |
assert:(self
|
|
774 |
maskAndUnmaskData:self byteArray100
|
|
775 |
doAssert:false)
|
|
776 |
< 10 seconds.
|
2524
|
777 |
|
2546
|
778 |
"Created: / 22-01-2020 / 15:49:01 / Stefan Reise"
|
2507
|
779 |
!
|
|
780 |
|
2545
|
781 |
testOnEchoServer
|
|
782 |
"/ empty (1) and big data (>5) is not supported by echo server
|
|
783 |
(self allData from:2 to:5) do:[:eachData |
|
|
784 |
self assertOnEchoServer:eachData.
|
|
785 |
].
|
|
786 |
|
|
787 |
"Created: / 15-01-2020 / 15:55:27 / Stefan Reise"
|
|
788 |
"Modified: / 04-02-2020 / 12:09:52 / Stefan Reise"
|
|
789 |
!
|
|
790 |
|
2529
|
791 |
testParallelSocketWrite
|
|
792 |
"
|
|
793 |
!! call the following method in case the sockets did get corrupted !!
|
|
794 |
self resetServerAndClientWebsocket
|
|
795 |
|
|
796 |
WebSocketStream verbose:true.
|
|
797 |
WebSocketStream verboseProtocol:true.
|
|
798 |
|
|
799 |
WebSocketStream verbose:false.
|
|
800 |
WebSocketStream verboseProtocol:false.
|
2531
|
801 |
|
2534
|
802 |
WebSocketStream maxBytesPerFrame:8178.
|
2531
|
803 |
WebSocketStream maxBytesPerFrame:nil.
|
2529
|
804 |
"
|
|
805 |
|
|
806 |
^ self
|
|
807 |
withData:self data100
|
|
808 |
do:[:eachData |
|
|
809 |
self clientWebSocket nextPut:eachData.
|
|
810 |
self serverWebSocket nextPut:eachData.
|
2546
|
811 |
|
|
812 |
Delay waitForSeconds:20.
|
|
813 |
|
|
814 |
self clientWebSocket next.
|
|
815 |
self serverWebSocket next.
|
|
816 |
].
|
2529
|
817 |
|
|
818 |
"Created: / 23-01-2020 / 11:47:47 / Stefan Reise"
|
2546
|
819 |
"Modified: / 04-02-2020 / 12:26:59 / Stefan Reise"
|
2529
|
820 |
!
|
|
821 |
|
2507
|
822 |
testPing
|
|
823 |
"
|
2524
|
824 |
!! call the following method in case the sockets did get corrupted !!
|
|
825 |
self resetServerAndClientWebsocket
|
2515
|
826 |
|
|
827 |
WebSocketStream verbose:true.
|
|
828 |
WebSocketStream verboseProtocol:true.
|
|
829 |
|
|
830 |
WebSocketStream verbose:false.
|
|
831 |
WebSocketStream verboseProtocol:false.
|
2507
|
832 |
"
|
|
833 |
|
|
834 |
|pingTimeDuration|
|
|
835 |
|
|
836 |
pingTimeDuration := self clientWebSocket ping.
|
|
837 |
Transcript showCR:pingTimeDuration printString.
|
|
838 |
self assert:pingTimeDuration notNil.
|
|
839 |
|
|
840 |
pingTimeDuration := self serverWebSocket ping.
|
|
841 |
Transcript showCR:pingTimeDuration printString.
|
|
842 |
self assert:pingTimeDuration notNil.
|
|
843 |
|
|
844 |
"Created: / 17-01-2020 / 13:43:58 / Stefan Reise"
|
2524
|
845 |
"Modified (comment): / 22-01-2020 / 15:25:11 / Stefan Reise"
|
|
846 |
! !
|
|
847 |
|
2547
|
848 |
!WebSocketTest methodsFor:'tests manual'!
|
|
849 |
|
|
850 |
manualTestWithFirefox
|
|
851 |
"
|
|
852 |
self new manualTestWithFirefox
|
|
853 |
|
|
854 |
opens firefox with a javascript websocket testpage.
|
|
855 |
the testpage logs everything with javascript console.log().
|
|
856 |
open the console window in firefox to see whats going on.
|
|
857 |
|
|
858 |
you need a running websocket service, may start as followed:
|
|
859 |
ATTENTION the Callback / Inspector opens after the connect from the test page (not directly after starting the service)
|
|
860 |
use: self nextPut:'Hello World'
|
|
861 |
inside the inspector of the webSocket to send something to the javascript websocket client
|
|
862 |
|
|
863 |
DemoWebSocketService start
|
|
864 |
webSocketCreationCallback:[:webSocket |
|
|
865 |
webSocket
|
|
866 |
closedByPeerCallback:[
|
|
867 |
Transcript showCR:'socket close by peer'.
|
|
868 |
self halt.
|
|
869 |
];
|
|
870 |
closedByErrorCallback:[
|
|
871 |
Transcript showCR:'socket close by error'.
|
|
872 |
self halt.
|
|
873 |
];
|
|
874 |
inspect.
|
|
875 |
].
|
|
876 |
|
|
877 |
|
|
878 |
enter the url in the connect box of the test page in firefox: (doit)
|
|
879 |
|
|
880 |
'ws://localhost:%1%2'
|
|
881 |
bindWith:DemoWebSocketService defaultPort
|
|
882 |
with:DemoWebSocketService linkName
|
|
883 |
|
|
884 |
ws://localhost:9096/websocket
|
|
885 |
"
|
|
886 |
|
|
887 |
|firefox regEntry testHtml|
|
|
888 |
|
|
889 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
890 |
regEntry := OperatingSystem registryEntry key:'HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe'.
|
|
891 |
regEntry isNil ifTrue:[
|
|
892 |
self error:'no firefox (at least in registry)'.
|
|
893 |
].
|
|
894 |
|
|
895 |
firefox := '"', regEntry defaultValue, '"'.
|
|
896 |
] ifFalse:[
|
2548
|
897 |
firefox := 'firefox'.
|
2547
|
898 |
].
|
|
899 |
|
|
900 |
testHtml := Filename tempDirectory / (UUID genRandomUUID printString, '.html').
|
|
901 |
testHtml contents:
|
|
902 |
'<html>
|
|
903 |
<head>
|
|
904 |
<script language="javascript" type="text/javascript">
|
|
905 |
var websocket;
|
|
906 |
|
|
907 |
function onOpen(evt) {
|
|
908 |
console.log("callback onopen");
|
|
909 |
}
|
|
910 |
|
|
911 |
function onClose(evt) {
|
|
912 |
console.log("callback onClose");
|
|
913 |
}
|
|
914 |
|
|
915 |
function onMessage(evt) {
|
|
916 |
console.log("callback onMessage: " + evt.data);
|
|
917 |
}
|
|
918 |
|
|
919 |
function onError(evt) {
|
|
920 |
console.log("callback onError: " + evt.data);
|
|
921 |
}
|
|
922 |
|
|
923 |
function startWebSocket(wsUrl) {
|
|
924 |
websocket = new WebSocket(wsUrl);
|
|
925 |
websocket.onopen = function(evt) { onOpen(evt) };
|
|
926 |
websocket.onclose = function(evt) { onClose(evt) };
|
|
927 |
websocket.onmessage = function(evt) { onMessage(evt) };
|
|
928 |
websocket.onerror = function(evt) { onError(evt) };
|
|
929 |
}
|
|
930 |
|
|
931 |
function closeWebSocket() {
|
|
932 |
if (websocket) {
|
|
933 |
console.log("close");
|
|
934 |
websocket.close();
|
|
935 |
} else {
|
|
936 |
console.log("var websocket not set");
|
|
937 |
}
|
|
938 |
}
|
|
939 |
|
|
940 |
function doSend(message) {
|
|
941 |
if (websocket) {
|
|
942 |
console.log("send: " + message);
|
|
943 |
websocket.send(message);
|
|
944 |
} else {
|
|
945 |
console.log("var websocket not set");
|
|
946 |
}
|
|
947 |
}
|
|
948 |
</script>
|
|
949 |
</head>
|
|
950 |
<body>
|
|
951 |
<table><tr><td align="right">
|
|
952 |
Websocket URL:
|
|
953 |
</td><td>
|
|
954 |
<input type="edit" id="wsUrl">
|
|
955 |
</td><td>
|
|
956 |
<input type="button" value="Connect" onClick="startWebSocket(document.getElementById("wsUrl").value);">
|
|
957 |
</td></tr><tr><td align="right">
|
|
958 |
Message:
|
|
959 |
</td><td>
|
|
960 |
<input type="edit" id="message">
|
|
961 |
</td><td>
|
|
962 |
<input type="button" value="Send" onClick="doSend(document.getElementById("message").value);">
|
|
963 |
</td></tr><tr><td align="right"></td><td align="right"></td><td>
|
|
964 |
<input type="button" value="Disconnect" onClick="closeWebSocket();">
|
|
965 |
</td></tr></table>
|
|
966 |
</body>
|
|
967 |
</html>'.
|
|
968 |
|
|
969 |
OperatingSystem
|
|
970 |
executeCommand:('%1 "file://%2"'
|
|
971 |
bindWith:firefox
|
|
972 |
with:testHtml).
|
|
973 |
|
|
974 |
"Created: / 04-02-2020 / 14:37:54 / Stefan Reise"
|
|
975 |
! !
|
|
976 |
|
2507
|
977 |
!WebSocketTest::DemoWebSocketService class methodsFor:'constants'!
|
|
978 |
|
|
979 |
defaultPort
|
2532
|
980 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
2552
|
981 |
^ 9158
|
2532
|
982 |
].
|
|
983 |
|
2507
|
984 |
^ 9095
|
|
985 |
|
|
986 |
"Created: / 18-11-2019 / 13:19:29 / Stefan Reise"
|
2552
|
987 |
"Modified: / 05-02-2020 / 13:48:39 / Stefan Reise"
|
2507
|
988 |
!
|
|
989 |
|
|
990 |
linkName
|
|
991 |
^ '/websocket'
|
|
992 |
|
|
993 |
"Created: / 13-11-2019 / 13:30:11 / Stefan Reise"
|
|
994 |
! !
|
|
995 |
|
|
996 |
!WebSocketTest::DemoWebSocketService class methodsFor:'documentation'!
|
|
997 |
|
|
998 |
documentation
|
|
999 |
"
|
|
1000 |
[start Server with:]
|
|
1001 |
HTTPServer startServerOnPort:8080
|
|
1002 |
|
|
1003 |
[start with:]
|
|
1004 |
(self new)
|
|
1005 |
registerServiceOn:(HTTPServer runningServerOnPort:8080)
|
|
1006 |
|
|
1007 |
class:
|
|
1008 |
<a short class summary here, describing what instances represent>
|
|
1009 |
|
|
1010 |
responsibilities:
|
|
1011 |
<describing what my main role is>
|
|
1012 |
|
|
1013 |
collaborators:
|
|
1014 |
<describing with whom and how I talk to>
|
|
1015 |
|
|
1016 |
API:
|
|
1017 |
<public api and main messages>
|
|
1018 |
|
|
1019 |
example:
|
|
1020 |
<a one-line examples on how to use - can also be in a separate example method>
|
|
1021 |
|
|
1022 |
implementation:
|
|
1023 |
<implementation points>
|
|
1024 |
|
|
1025 |
[author:]
|
|
1026 |
Stefan Reise
|
|
1027 |
|
|
1028 |
[instance variables:]
|
|
1029 |
|
|
1030 |
[class variables:]
|
|
1031 |
|
|
1032 |
[see also:]
|
|
1033 |
|
|
1034 |
"
|
|
1035 |
! !
|
|
1036 |
|
|
1037 |
!WebSocketTest::DemoWebSocketService class methodsFor:'examples'!
|
|
1038 |
|
|
1039 |
start
|
|
1040 |
"
|
|
1041 |
self start
|
|
1042 |
"
|
|
1043 |
|
|
1044 |
|httpServer instance|
|
|
1045 |
|
|
1046 |
httpServer := HTTPServer serverOnPort:self defaultPort.
|
|
1047 |
instance := self registerServiceOn:httpServer.
|
|
1048 |
httpServer start.
|
|
1049 |
|
|
1050 |
^ instance
|
|
1051 |
|
|
1052 |
"Created: / 17-01-2020 / 13:19:08 / Stefan Reise"
|
|
1053 |
! !
|
|
1054 |
|
|
1055 |
!WebSocketTest::DemoWebSocketService methodsFor:'accessing'!
|
|
1056 |
|
|
1057 |
webSocketCreationCallback:something
|
|
1058 |
webSocketCreationCallback := something.
|
|
1059 |
! !
|
|
1060 |
|
|
1061 |
!WebSocketTest::DemoWebSocketService methodsFor:'processing'!
|
|
1062 |
|
|
1063 |
acceptsWebSocket:webSocket
|
|
1064 |
^ true
|
|
1065 |
|
|
1066 |
"Created: / 13-11-2019 / 13:57:03 / Stefan Reise"
|
|
1067 |
!
|
|
1068 |
|
|
1069 |
startServingWebSocket:webSocket
|
|
1070 |
"to be redefined in subclasses (services) which do handle webSockets;
|
|
1071 |
close here, in case it was not redefined"
|
|
1072 |
|
2535
|
1073 |
|callback|
|
|
1074 |
|
|
1075 |
callback := webSocketCreationCallback.
|
|
1076 |
callback notNil ifTrue:[
|
|
1077 |
callback value:webSocket.
|
|
1078 |
].
|
2507
|
1079 |
|
|
1080 |
"Created: / 13-11-2019 / 14:42:43 / Stefan Reise"
|
|
1081 |
"Modified: / 17-01-2020 / 13:19:52 / Stefan Reise"
|
|
1082 |
! !
|
|
1083 |
|
|
1084 |
!WebSocketTest::DemoWebSocketService methodsFor:'queries'!
|
|
1085 |
|
|
1086 |
allowWebSockets
|
|
1087 |
^ true
|
|
1088 |
|
|
1089 |
"Created: / 13-11-2019 / 13:29:48 / Stefan Reise"
|
|
1090 |
! !
|
|
1091 |
|
|
1092 |
!WebSocketTest class methodsFor:'documentation'!
|
|
1093 |
|
|
1094 |
version_CVS
|
|
1095 |
^ '$Header$'
|
|
1096 |
! !
|
|
1097 |
|
2551
|
1098 |
|
|
1099 |
WebSocketTest initialize!
|