summaryrefslogtreecommitdiffstats
path: root/vendor/textalk/websocket/tests/ClientTest.php
blob: c7bdc16872fdd3b64e1ebc3575bb46f28e4e0608 (plain)
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
449
<?php

/**
 * Test case for Client.
 * Note that this test is performed by mocking socket/stream calls.
 */

declare(strict_types=1);

namespace WebSocket;

use PHPUnit\Framework\TestCase;

class ClientTest extends TestCase
{

    public function setUp(): void
    {
        error_reporting(-1);
    }

    public function testClientMasked(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
        $this->assertEquals(4096, $client->getFragmentSize());

        MockSocket::initialize('send-receive', $this);
        $client->send('Sending a message');
        $message = $client->receive();
        $this->assertTrue(MockSocket::isEmpty());
        $this->assertEquals('text', $client->getLastOpcode());

        MockSocket::initialize('client.close', $this);
        $this->assertTrue($client->isConnected());
        $this->assertNull($client->getCloseStatus());

        $client->close();
        $this->assertFalse($client->isConnected());
        $this->assertEquals(1000, $client->getCloseStatus());

        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testDestruct(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('client.destruct', $this);
    }

    public function testClienExtendedUrl(): void
    {
        MockSocket::initialize('client.connect-extended', $this);
        $client = new Client('ws://localhost:8000/my/mock/path?my_query=yes#my_fragment');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testClientWithTimeout(): void
    {
        MockSocket::initialize('client.connect-timeout', $this);
        $client = new Client('ws://localhost:8000/my/mock/path', ['timeout' => 300]);
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testClientWithContext(): void
    {
        MockSocket::initialize('client.connect-context', $this);
        $client = new Client('ws://localhost:8000/my/mock/path', ['context' => '@mock-stream-context']);
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testClientAuthed(): void
    {
        MockSocket::initialize('client.connect-authed', $this);
        $client = new Client('wss://usename:password@localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testWithHeaders(): void
    {
        MockSocket::initialize('client.connect-headers', $this);
        $client = new Client('ws://localhost:8000/my/mock/path', [
            'origin' => 'Origin header',
            'headers' => ['Generic header' => 'Generic content'],
        ]);
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testPayload128(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        $payload = file_get_contents(__DIR__ . '/mock/payload.128.txt');

        MockSocket::initialize('send-receive-128', $this);
        $client->send($payload, 'text', false);
        $message = $client->receive();
        $this->assertEquals($payload, $message);
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testPayload65536(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        $payload = file_get_contents(__DIR__ . '/mock/payload.65536.txt');
        $client->setFragmentSize(65540);

        MockSocket::initialize('send-receive-65536', $this);
        $client->send($payload, 'text', false);
        $message = $client->receive();
        $this->assertEquals($payload, $message);
        $this->assertTrue(MockSocket::isEmpty());
        $this->assertEquals(65540, $client->getFragmentSize());
    }

    public function testMultiFragment(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('send-receive-multi-fragment', $this);
        $client->setFragmentSize(8);
        $client->send('Multi fragment test');
        $message = $client->receive();
        $this->assertEquals('Multi fragment test', $message);
        $this->assertTrue(MockSocket::isEmpty());
        $this->assertEquals(8, $client->getFragmentSize());
    }

    public function testPingPong(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('ping-pong', $this);
        $client->send('Server ping', 'ping');
        $client->send('', 'ping');
        $message = $client->receive();
        $this->assertEquals('Receiving a message', $message);
        $this->assertEquals('text', $client->getLastOpcode());
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testRemoteClose(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('close-remote', $this);

        $message = $client->receive();
        $this->assertNull($message);

        $this->assertFalse($client->isConnected());
        $this->assertEquals(17260, $client->getCloseStatus());
        $this->assertNull($client->getLastOpcode());
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testSetTimeout(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('config-timeout', $this);
        $client->setTimeout(300);
        $this->assertTrue($client->isConnected());
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testReconnect(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('client.close', $this);
        $this->assertTrue($client->isConnected());
        $this->assertNull($client->getCloseStatus());
        $client->close();
        $this->assertFalse($client->isConnected());
        $this->assertEquals(1000, $client->getCloseStatus());
        $this->assertNull($client->getLastOpcode());
        $this->assertTrue(MockSocket::isEmpty());

        MockSocket::initialize('client.reconnect', $this);
        $message = $client->receive();
        $this->assertTrue($client->isConnected());
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testPersistentConnection(): void
    {
        MockSocket::initialize('client.connect-persistent', $this);
        $client = new Client('ws://localhost:8000/my/mock/path', ['persistent' => true]);
        $client->send('Connect');
        $client->disconnect();
        $this->assertFalse($client->isConnected());
        $this->assertTrue(MockSocket::isEmpty());
    }

    public function testBadScheme(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('bad://localhost:8000/my/mock/path');
        $this->expectException('WebSocket\BadUriException');
        $this->expectExceptionMessage('Url should have scheme ws or wss');
        $client->send('Connect');
    }

    public function testBadUrl(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('this is not an url');
        $this->expectException('WebSocket\BadUriException');
        $this->expectExceptionMessage('Invalid url \'this is not an url\' provided.');
        $client->send('Connect');
    }

    public function testBadStreamContext(): void
    {
        MockSocket::initialize('client.connect-bad-context', $this);
        $client = new Client('ws://localhost:8000/my/mock/path', ['context' => 'BAD']);
        $this->expectException('InvalidArgumentException');
        $this->expectExceptionMessage('Stream context in $options[\'context\'] isn\'t a valid context');
        $client->send('Connect');
    }

    public function testFailedConnection(): void
    {
        MockSocket::initialize('client.connect-failed', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $this->expectException('WebSocket\ConnectionException');
        $this->expectExceptionCode(0);
        $this->expectExceptionMessage('Could not open socket to "localhost:8000"');
        $client->send('Connect');
    }

    public function testFailedConnectionWithError(): void
    {
        MockSocket::initialize('client.connect-error', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $this->expectException('WebSocket\ConnectionException');
        $this->expectExceptionCode(0);
        $this->expectExceptionMessage('Could not open socket to "localhost:8000"');
        $client->send('Connect');
    }

    public function testInvalidUpgrade(): void
    {
        MockSocket::initialize('client.connect-invalid-upgrade', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $this->expectException('WebSocket\ConnectionException');
        $this->expectExceptionCode(0);
        $this->expectExceptionMessage('Connection to \'ws://localhost/my/mock/path\' failed');
        $client->send('Connect');
    }

    public function testInvalidKey(): void
    {
        MockSocket::initialize('client.connect-invalid-key', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $this->expectException('WebSocket\ConnectionException');
        $this->expectExceptionCode(0);
        $this->expectExceptionMessage('Server sent bad upgrade response');
        $client->send('Connect');
    }

    public function testSendBadOpcode(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');

        MockSocket::initialize('send-bad-opcode', $this);
        $this->expectException('WebSocket\BadOpcodeException');
        $this->expectExceptionMessage('Bad opcode \'bad\'.  Try \'text\' or \'binary\'.');
        $client->send('Bad Opcode', 'bad');
    }

    public function testRecieveBadOpcode(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        MockSocket::initialize('receive-bad-opcode', $this);
        $this->expectException('WebSocket\ConnectionException');
        $this->expectExceptionCode(1026);
        $this->expectExceptionMessage('Bad opcode in websocket frame: 12');
        $message = $client->receive();
    }

    public function testBrokenWrite(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->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.');
        $client->send('Failing to write');
    }

    public function testFailedWrite(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        MockSocket::initialize('send-failed-write', $this);
        $this->expectException('WebSocket\TimeoutException');
        $this->expectExceptionCode(1024);
        $this->expectExceptionMessage('Failed to write 22 bytes.');
        $client->send('Failing to write');
    }

    public function testBrokenRead(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->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.');
        $client->receive();
    }

    public function testReadTimeout(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        MockSocket::initialize('receive-client-timeout', $this);
        $this->expectException('WebSocket\TimeoutException');
        $this->expectExceptionCode(1024);
        $this->expectExceptionMessage('Client read timeout');
        $client->receive();
    }

    public function testEmptyRead(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $client->send('Connect');
        MockSocket::initialize('receive-empty-read', $this);
        $this->expectException('WebSocket\TimeoutException');
        $this->expectExceptionCode(1024);
        $this->expectExceptionMessage('Empty read; connection dead?');
        $client->receive();
    }

    public function testFrameFragmentation(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client(
            'ws://localhost:8000/my/mock/path',
            ['filter' => ['text', 'binary', 'pong', 'close']]
        );
        $client->send('Connect');
        MockSocket::initialize('receive-fragmentation', $this);
        $message = $client->receive();
        $this->assertEquals('Server ping', $message);
        $this->assertEquals('pong', $client->getLastOpcode());
        $message = $client->receive();
        $this->assertEquals('Multi fragment test', $message);
        $this->assertEquals('text', $client->getLastOpcode());
        $this->assertTrue(MockSocket::isEmpty());
        MockSocket::initialize('close-remote', $this);
        $message = $client->receive();
        $this->assertEquals('Closing', $message);
        $this->assertTrue(MockSocket::isEmpty());
        $this->assertFalse($client->isConnected());
        $this->assertEquals(17260, $client->getCloseStatus());
        $this->assertEquals('close', $client->getLastOpcode());
    }

    public function testMessageFragmentation(): void
    {
        MockSocket::initialize('client.connect', $this);
        $client = new Client(
            'ws://localhost:8000/my/mock/path',
            ['filter' => ['text', 'binary', 'pong', 'close'], 'return_obj' => true]
        );
        $client->send('Connect');
        MockSocket::initialize('receive-fragmentation', $this);
        $message = $client->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 = $client->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 = $client->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('client.connect', $this);
        $client = new Client('ws://localhost:8000/my/mock/path');
        $this->assertNull($client->getName());
        $this->assertNull($client->getPier());
        $this->assertEquals('WebSocket\Client(closed)', "{$client}");
        $client->text('Connect');
        MockSocket::initialize('send-convenicance', $this);
        $client->binary(base64_encode('Binary content'));
        $client->ping();
        $client->pong();
        $this->assertEquals('127.0.0.1:12345', $client->getName());
        $this->assertEquals('127.0.0.1:8000', $client->getPier());
        $this->assertEquals('WebSocket\Client(127.0.0.1:12345)', "{$client}");
    }
}