123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?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->assertEquals('close', $client->getLastOpcode());
- $client->close();
- $this->assertFalse($client->isConnected());
- $this->assertEquals(1000, $client->getCloseStatus());
- $this->assertEquals('close', $client->getLastOpcode());
- $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->assertEquals('', $message);
- $this->assertFalse($client->isConnected());
- $this->assertEquals(17260, $client->getCloseStatus());
- $this->assertEquals('close', $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->assertEquals('close', $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');
- $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 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 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();
- }
- }
|