1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
<?php
/**
* Test case for Server.
* Note that this test is performed by mocking socket/stream calls.
*/
declare(strict_types=1);
namespace WebSocket;
use PHPUnit\Framework\TestCase;
class ServerTest extends TestCase
{
public function setUp(): void
{
error_reporting(-1);
}
public function testServerMasked(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertEquals(8000, $server->getPort());
$this->assertEquals('/my/mock/path', $server->getPath());
$this->assertTrue($server->isConnected());
$this->assertEquals(4096, $server->getFragmentSize());
$this->assertNull($server->getCloseStatus());
$this->assertEquals([
'GET /my/mock/path HTTP/1.1',
'host: localhost:8000',
'user-agent: websocket-client-php',
'connection: Upgrade',
'upgrade: websocket',
'sec-websocket-key: cktLWXhUdDQ2OXF0ZCFqOQ==',
'sec-websocket-version: 13',
'',
'',
], $server->getRequest());
$this->assertEquals('websocket-client-php', $server->getHeader('USER-AGENT'));
$this->assertNull($server->getHeader('no such header'));
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('send-receive', $this);
$server->send('Sending a message');
$message = $server->receive();
$this->assertEquals('Receiving a message', $message);
$this->assertTrue(MockSocket::isEmpty());
$this->assertNull($server->getCloseStatus());
$this->assertEquals('text', $server->getLastOpcode());
MockSocket::initialize('server.close', $this);
$server->close();
$this->assertFalse($server->isConnected());
$this->assertEquals(1000, $server->getCloseStatus());
$this->assertTrue(MockSocket::isEmpty());
$server->close(); // Already closed
}
public function testDestruct(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept-destruct', $this);
$server->accept();
$message = $server->receive();
}
public function testServerWithTimeout(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server(['timeout' => 300]);
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept-timeout', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue(MockSocket::isEmpty());
}
public function testPayload128(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
$payload = file_get_contents(__DIR__ . '/mock/payload.128.txt');
MockSocket::initialize('send-receive-128', $this);
$server->send($payload, 'text', false);
$message = $server->receive();
$this->assertEquals($payload, $message);
$this->assertTrue(MockSocket::isEmpty());
}
public function testPayload65536(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
$payload = file_get_contents(__DIR__ . '/mock/payload.65536.txt');
$server->setFragmentSize(65540);
MockSocket::initialize('send-receive-65536', $this);
$server->send($payload, 'text', false);
$message = $server->receive();
$this->assertEquals($payload, $message);
$this->assertTrue(MockSocket::isEmpty());
}
public function testMultiFragment(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('send-receive-multi-fragment', $this);
$server->setFragmentSize(8);
$server->send('Multi fragment test');
$message = $server->receive();
$this->assertEquals('Multi fragment test', $message);
$this->assertTrue(MockSocket::isEmpty());
}
public function testPingPong(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('ping-pong', $this);
$server->send('Server ping', 'ping');
$server->send('', 'ping');
$message = $server->receive();
$this->assertEquals('Receiving a message', $message);
$this->assertEquals('text', $server->getLastOpcode());
$this->assertTrue(MockSocket::isEmpty());
}
public function testRemoteClose(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('close-remote', $this);
$message = $server->receive();
$this->assertEquals('', $message);
$this->assertTrue(MockSocket::isEmpty());
$this->assertFalse($server->isConnected());
$this->assertEquals(17260, $server->getCloseStatus());
$this->assertNull($server->getLastOpcode());
}
public function testSetTimeout(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('config-timeout', $this);
$server->setTimeout(300);
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
}
public function testFailedSocketServer(): void
{
MockSocket::initialize('server.construct-failed-socket-server', $this);
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Could not open listening socket:');
$server = new Server(['port' => 9999]);
}
public function testFailedSocketServerWithError(): void
{
MockSocket::initialize('server.construct-error-socket-server', $this);
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Could not open listening socket:');
$server = new Server(['port' => 9999]);
}
public function testFailedConnect(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept-failed-connect', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Server failed to connect');
$server->send('Connect');
}
public function testFailedConnectWithError(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept-error-connect', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Server failed to connect');
$server->send('Connect');
}
public function testFailedConnectTimeout(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server(['timeout' => 300]);
MockSocket::initialize('server.accept-failed-connect', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Server failed to connect');
$server->send('Connect');
}
public function testFailedHttp(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept-failed-http', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('No GET in request');
$server->send('Connect');
}
public function testFailedWsKey(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept-failed-ws-key', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Client had no Key in upgrade request');
$server->send('Connect');
}
public function testSendBadOpcode(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->expectException('WebSocket\BadOpcodeException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Bad opcode \'bad\'. Try \'text\' or \'binary\'.');
$server->send('Bad Opcode', 'bad');
}
public function testRecieveBadOpcode(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('receive-bad-opcode', $this);
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(1026);
$this->expectExceptionMessage('Bad opcode in websocket frame: 12');
$message = $server->receive();
}
public function testBrokenWrite(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('send-broken-write', $this);
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(1025);
$this->expectExceptionMessage('Could only write 18 out of 22 bytes.');
$server->send('Failing to write');
}
public function testFailedWrite(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('send-failed-write', $this);
$this->expectException('WebSocket\TimeoutException');
$this->expectExceptionCode(1024);
$this->expectExceptionMessage('Failed to write 22 bytes.');
$server->send('Failing to write');
}
public function testBrokenRead(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('receive-broken-read', $this);
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(1025);
$this->expectExceptionMessage('Broken frame, read 0 of stated 2 bytes.');
$server->receive();
}
public function testEmptyRead(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('receive-empty-read', $this);
$this->expectException('WebSocket\TimeoutException');
$this->expectExceptionCode(1024);
$this->expectExceptionMessage('Empty read; connection dead?');
$server->receive();
}
public function testFrameFragmentation(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server(['filter' => ['text', 'binary', 'pong', 'close']]);
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('receive-fragmentation', $this);
$message = $server->receive();
$this->assertEquals('Server ping', $message);
$this->assertEquals('pong', $server->getLastOpcode());
$message = $server->receive();
$this->assertEquals('Multi fragment test', $message);
$this->assertEquals('text', $server->getLastOpcode());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('close-remote', $this);
$message = $server->receive();
$this->assertEquals('Closing', $message);
$this->assertTrue(MockSocket::isEmpty());
$this->assertFalse($server->isConnected());
$this->assertEquals(17260, $server->getCloseStatus());
$this->assertEquals('close', $server->getLastOpcode());
}
public function testMessageFragmentation(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server(['filter' => ['text', 'binary', 'pong', 'close'], 'return_obj' => true]);
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
MockSocket::initialize('receive-fragmentation', $this);
$message = $server->receive();
$this->assertInstanceOf('WebSocket\Message\Message', $message);
$this->assertInstanceOf('WebSocket\Message\Pong', $message);
$this->assertEquals('Server ping', $message->getContent());
$this->assertEquals('pong', $message->getOpcode());
$message = $server->receive();
$this->assertInstanceOf('WebSocket\Message\Message', $message);
$this->assertInstanceOf('WebSocket\Message\Text', $message);
$this->assertEquals('Multi fragment test', $message->getContent());
$this->assertEquals('text', $message->getOpcode());
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('close-remote', $this);
$message = $server->receive();
$this->assertInstanceOf('WebSocket\Message\Message', $message);
$this->assertInstanceOf('WebSocket\Message\Close', $message);
$this->assertEquals('Closing', $message->getContent());
$this->assertEquals('close', $message->getOpcode());
}
public function testConvenicanceMethods(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertNull($server->getName());
$this->assertNull($server->getPier());
$this->assertEquals('WebSocket\Server(closed)', "{$server}");
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->text('Connect');
MockSocket::initialize('send-convenicance', $this);
$server->binary(base64_encode('Binary content'));
$server->ping();
$server->pong();
$this->assertEquals('127.0.0.1:12345', $server->getName());
$this->assertEquals('127.0.0.1:8000', $server->getPier());
$this->assertEquals('WebSocket\Server(127.0.0.1:12345)', "{$server}");
$this->assertTrue(MockSocket::isEmpty());
}
}
|