ServerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * Test case for Server.
  4. * Note that this test is performed by mocking socket/stream calls.
  5. */
  6. declare(strict_types=1);
  7. namespace WebSocket;
  8. use PHPUnit\Framework\TestCase;
  9. class ServerTest extends TestCase
  10. {
  11. public function setUp(): void
  12. {
  13. error_reporting(-1);
  14. }
  15. public function testServerMasked(): void
  16. {
  17. MockSocket::initialize('server.construct', $this);
  18. $server = new Server();
  19. $this->assertTrue(MockSocket::isEmpty());
  20. MockSocket::initialize('server.accept', $this);
  21. $server->accept();
  22. $server->send('Connect');
  23. $this->assertEquals(8000, $server->getPort());
  24. $this->assertEquals('/my/mock/path', $server->getPath());
  25. $this->assertTrue($server->isConnected());
  26. $this->assertEquals(4096, $server->getFragmentSize());
  27. $this->assertNull($server->getCloseStatus());
  28. $this->assertEquals([
  29. 'GET /my/mock/path HTTP/1.1',
  30. 'host: localhost:8000',
  31. 'user-agent: websocket-client-php',
  32. 'connection: Upgrade',
  33. 'upgrade: websocket',
  34. 'sec-websocket-key: cktLWXhUdDQ2OXF0ZCFqOQ==',
  35. 'sec-websocket-version: 13',
  36. '',
  37. '',
  38. ], $server->getRequest());
  39. $this->assertEquals('websocket-client-php', $server->getHeader('USER-AGENT'));
  40. $this->assertNull($server->getHeader('no such header'));
  41. $this->assertTrue(MockSocket::isEmpty());
  42. MockSocket::initialize('send-receive', $this);
  43. $server->send('Sending a message');
  44. $message = $server->receive();
  45. $this->assertEquals('Receiving a message', $message);
  46. $this->assertTrue(MockSocket::isEmpty());
  47. $this->assertNull($server->getCloseStatus());
  48. $this->assertEquals('text', $server->getLastOpcode());
  49. MockSocket::initialize('server.close', $this);
  50. $server->close();
  51. $this->assertFalse($server->isConnected());
  52. $this->assertEquals(1000, $server->getCloseStatus());
  53. $this->assertEquals('close', $server->getLastOpcode());
  54. $this->assertTrue(MockSocket::isEmpty());
  55. $server->close(); // Already closed
  56. }
  57. public function testDestruct(): void
  58. {
  59. MockSocket::initialize('server.construct', $this);
  60. $server = new Server();
  61. MockSocket::initialize('server.accept-destruct', $this);
  62. $server->accept();
  63. $message = $server->receive();
  64. }
  65. public function testServerWithTimeout(): void
  66. {
  67. MockSocket::initialize('server.construct', $this);
  68. $server = new Server(['timeout' => 300]);
  69. $this->assertTrue(MockSocket::isEmpty());
  70. MockSocket::initialize('server.accept-timeout', $this);
  71. $server->accept();
  72. $server->send('Connect');
  73. $this->assertTrue(MockSocket::isEmpty());
  74. }
  75. public function testPayload128(): void
  76. {
  77. MockSocket::initialize('server.construct', $this);
  78. $server = new Server();
  79. $this->assertTrue(MockSocket::isEmpty());
  80. MockSocket::initialize('server.accept', $this);
  81. $server->accept();
  82. $server->send('Connect');
  83. $this->assertTrue($server->isConnected());
  84. $this->assertTrue(MockSocket::isEmpty());
  85. $payload = file_get_contents(__DIR__ . '/mock/payload.128.txt');
  86. MockSocket::initialize('send-receive-128', $this);
  87. $server->send($payload, 'text', false);
  88. $message = $server->receive();
  89. $this->assertEquals($payload, $message);
  90. $this->assertTrue(MockSocket::isEmpty());
  91. }
  92. public function testPayload65536(): void
  93. {
  94. MockSocket::initialize('server.construct', $this);
  95. $server = new Server();
  96. $this->assertTrue(MockSocket::isEmpty());
  97. MockSocket::initialize('server.accept', $this);
  98. $server->accept();
  99. $server->send('Connect');
  100. $this->assertTrue($server->isConnected());
  101. $this->assertTrue(MockSocket::isEmpty());
  102. $payload = file_get_contents(__DIR__ . '/mock/payload.65536.txt');
  103. $server->setFragmentSize(65540);
  104. MockSocket::initialize('send-receive-65536', $this);
  105. $server->send($payload, 'text', false);
  106. $message = $server->receive();
  107. $this->assertEquals($payload, $message);
  108. $this->assertTrue(MockSocket::isEmpty());
  109. }
  110. public function testMultiFragment(): void
  111. {
  112. MockSocket::initialize('server.construct', $this);
  113. $server = new Server();
  114. $this->assertTrue(MockSocket::isEmpty());
  115. MockSocket::initialize('server.accept', $this);
  116. $server->accept();
  117. $server->send('Connect');
  118. $this->assertTrue($server->isConnected());
  119. $this->assertTrue(MockSocket::isEmpty());
  120. MockSocket::initialize('send-receive-multi-fragment', $this);
  121. $server->setFragmentSize(8);
  122. $server->send('Multi fragment test');
  123. $message = $server->receive();
  124. $this->assertEquals('Multi fragment test', $message);
  125. $this->assertTrue(MockSocket::isEmpty());
  126. }
  127. public function testPingPong(): void
  128. {
  129. MockSocket::initialize('server.construct', $this);
  130. $server = new Server();
  131. $this->assertTrue(MockSocket::isEmpty());
  132. MockSocket::initialize('server.accept', $this);
  133. $server->accept();
  134. $server->send('Connect');
  135. $this->assertTrue($server->isConnected());
  136. $this->assertTrue(MockSocket::isEmpty());
  137. MockSocket::initialize('ping-pong', $this);
  138. $server->send('Server ping', 'ping');
  139. $server->send('', 'ping');
  140. $message = $server->receive();
  141. $this->assertEquals('Receiving a message', $message);
  142. $this->assertEquals('text', $server->getLastOpcode());
  143. $this->assertTrue(MockSocket::isEmpty());
  144. }
  145. public function testRemoteClose(): void
  146. {
  147. MockSocket::initialize('server.construct', $this);
  148. $server = new Server();
  149. $this->assertTrue(MockSocket::isEmpty());
  150. MockSocket::initialize('server.accept', $this);
  151. $server->accept();
  152. $server->send('Connect');
  153. $this->assertTrue($server->isConnected());
  154. $this->assertTrue(MockSocket::isEmpty());
  155. MockSocket::initialize('close-remote', $this);
  156. $message = $server->receive();
  157. $this->assertEquals('', $message);
  158. $this->assertTrue(MockSocket::isEmpty());
  159. $this->assertFalse($server->isConnected());
  160. $this->assertEquals(17260, $server->getCloseStatus());
  161. $this->assertEquals('close', $server->getLastOpcode());
  162. }
  163. public function testSetTimeout(): void
  164. {
  165. MockSocket::initialize('server.construct', $this);
  166. $server = new Server();
  167. $this->assertTrue(MockSocket::isEmpty());
  168. MockSocket::initialize('server.accept', $this);
  169. $server->accept();
  170. $server->send('Connect');
  171. $this->assertTrue($server->isConnected());
  172. $this->assertTrue(MockSocket::isEmpty());
  173. MockSocket::initialize('config-timeout', $this);
  174. $server->setTimeout(300);
  175. $this->assertTrue($server->isConnected());
  176. $this->assertTrue(MockSocket::isEmpty());
  177. }
  178. public function testFailedSocketServer(): void
  179. {
  180. MockSocket::initialize('server.construct-failed-socket-server', $this);
  181. $this->expectException('WebSocket\ConnectionException');
  182. $this->expectExceptionCode(0);
  183. $this->expectExceptionMessage('Could not open listening socket:');
  184. $server = new Server(['port' => 9999]);
  185. }
  186. public function testFailedConnect(): void
  187. {
  188. MockSocket::initialize('server.construct', $this);
  189. $server = new Server();
  190. MockSocket::initialize('server.accept-failed-connect', $this);
  191. $server->accept();
  192. $this->expectException('WebSocket\ConnectionException');
  193. $this->expectExceptionCode(0);
  194. $this->expectExceptionMessage('Server failed to connect');
  195. $server->send('Connect');
  196. }
  197. public function testFailedConnectTimeout(): void
  198. {
  199. MockSocket::initialize('server.construct', $this);
  200. $server = new Server(['timeout' => 300]);
  201. MockSocket::initialize('server.accept-failed-connect', $this);
  202. $server->accept();
  203. $this->expectException('WebSocket\ConnectionException');
  204. $this->expectExceptionCode(0);
  205. $this->expectExceptionMessage('Server failed to connect');
  206. $server->send('Connect');
  207. }
  208. public function testFailedHttp(): void
  209. {
  210. MockSocket::initialize('server.construct', $this);
  211. $server = new Server();
  212. MockSocket::initialize('server.accept-failed-http', $this);
  213. $server->accept();
  214. $this->expectException('WebSocket\ConnectionException');
  215. $this->expectExceptionCode(0);
  216. $this->expectExceptionMessage('No GET in request');
  217. $server->send('Connect');
  218. }
  219. public function testFailedWsKey(): void
  220. {
  221. MockSocket::initialize('server.construct', $this);
  222. $server = new Server();
  223. MockSocket::initialize('server.accept-failed-ws-key', $this);
  224. $server->accept();
  225. $this->expectException('WebSocket\ConnectionException');
  226. $this->expectExceptionCode(0);
  227. $this->expectExceptionMessage('Client had no Key in upgrade request');
  228. $server->send('Connect');
  229. }
  230. public function testSendBadOpcode(): void
  231. {
  232. MockSocket::initialize('server.construct', $this);
  233. $server = new Server();
  234. MockSocket::initialize('server.accept', $this);
  235. $server->accept();
  236. $server->send('Connect');
  237. $this->expectException('WebSocket\BadOpcodeException');
  238. $this->expectExceptionCode(0);
  239. $this->expectExceptionMessage('Bad opcode \'bad\'. Try \'text\' or \'binary\'.');
  240. $server->send('Bad Opcode', 'bad');
  241. }
  242. public function testRecieveBadOpcode(): void
  243. {
  244. MockSocket::initialize('server.construct', $this);
  245. $server = new Server();
  246. MockSocket::initialize('server.accept', $this);
  247. $server->accept();
  248. $server->send('Connect');
  249. MockSocket::initialize('receive-bad-opcode', $this);
  250. $this->expectException('WebSocket\ConnectionException');
  251. $this->expectExceptionCode(1026);
  252. $this->expectExceptionMessage('Bad opcode in websocket frame: 12');
  253. $message = $server->receive();
  254. }
  255. public function testBrokenWrite(): void
  256. {
  257. MockSocket::initialize('server.construct', $this);
  258. $server = new Server();
  259. MockSocket::initialize('server.accept', $this);
  260. $server->accept();
  261. $server->send('Connect');
  262. MockSocket::initialize('send-broken-write', $this);
  263. $this->expectException('WebSocket\ConnectionException');
  264. $this->expectExceptionCode(1025);
  265. $this->expectExceptionMessage('Could only write 18 out of 22 bytes.');
  266. $server->send('Failing to write');
  267. }
  268. public function testFailedWrite(): void
  269. {
  270. MockSocket::initialize('server.construct', $this);
  271. $server = new Server();
  272. MockSocket::initialize('server.accept', $this);
  273. $server->accept();
  274. $server->send('Connect');
  275. MockSocket::initialize('send-failed-write', $this);
  276. $this->expectException('WebSocket\TimeoutException');
  277. $this->expectExceptionCode(1024);
  278. $this->expectExceptionMessage('Failed to write 22 bytes.');
  279. $server->send('Failing to write');
  280. }
  281. public function testBrokenRead(): void
  282. {
  283. MockSocket::initialize('server.construct', $this);
  284. $server = new Server();
  285. MockSocket::initialize('server.accept', $this);
  286. $server->accept();
  287. $server->send('Connect');
  288. MockSocket::initialize('receive-broken-read', $this);
  289. $this->expectException('WebSocket\ConnectionException');
  290. $this->expectExceptionCode(1025);
  291. $this->expectExceptionMessage('Broken frame, read 0 of stated 2 bytes.');
  292. $server->receive();
  293. }
  294. public function testEmptyRead(): void
  295. {
  296. MockSocket::initialize('server.construct', $this);
  297. $server = new Server();
  298. MockSocket::initialize('server.accept', $this);
  299. $server->accept();
  300. $server->send('Connect');
  301. MockSocket::initialize('receive-empty-read', $this);
  302. $this->expectException('WebSocket\TimeoutException');
  303. $this->expectExceptionCode(1024);
  304. $this->expectExceptionMessage('Empty read; connection dead?');
  305. $server->receive();
  306. }
  307. }