Server.php 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Websocket\Test\Unit;
  36. use Hoa\Http;
  37. use Hoa\Test;
  38. use Hoa\Websocket;
  39. use Hoa\Websocket\Server as SUT;
  40. use Mock\Hoa\Socket;
  41. /**
  42. * Class \Hoa\Websocket\Test\Unit\Server.
  43. *
  44. * Test suite for the WebSocket server class.
  45. *
  46. * @copyright Copyright © 2007-2017 Hoa community
  47. * @license New BSD License
  48. */
  49. class Server extends Test\Unit\Suite
  50. {
  51. public function case_is_a_connection()
  52. {
  53. $this
  54. ->given($this->mockGenerator->orphanize('__construct'))
  55. ->when($result = new \Mock\Hoa\Websocket\Server())
  56. ->then
  57. ->object($result)
  58. ->isInstanceOf(Websocket\Connection::class);
  59. }
  60. public function case_constructor()
  61. {
  62. $this
  63. ->given(
  64. $socket = new Socket\Server('tcp://*:1234'),
  65. $request = new Http\Request()
  66. )
  67. ->when($result = new SUT($socket, $request))
  68. ->then
  69. ->object($result->getConnection())
  70. ->isIdenticalTo($socket)
  71. ->object($result->getRequest())
  72. ->isIdenticalTo($request);
  73. }
  74. public function case_constructor_with_an_undefined_request()
  75. {
  76. $this
  77. ->given($socket = new Socket\Server('tcp://*:1234'))
  78. ->when($result = new SUT($socket))
  79. ->then
  80. ->object($result->getConnection())
  81. ->isIdenticalTo($socket)
  82. ->object($result->getRequest())
  83. ->isInstanceOf(Http\Request::class);
  84. }
  85. public function case_do_handshake_rfc6455()
  86. {
  87. $self = $this;
  88. $this
  89. ->given(
  90. $socket = new Socket\Server('tcp://*:1234'),
  91. $request = new Http\Request(),
  92. $this->mockGenerator->orphanize('__construct'),
  93. $node = new \Mock\Hoa\Websocket\Node(),
  94. $server = new SUT($socket, $request),
  95. $secWebSocketKey = 'Y2FzZV9kb19oYW5kc2hhaA==',
  96. $this->calling($socket)->read[1] =
  97. 'GET /foobar HTTP/1.1' . CRLF .
  98. 'Sec-WebSocket-Key: ' . $secWebSocketKey . CRLF . CRLF,
  99. $this->calling($socket)->writeAll = function ($_data) use (&$called, $self, $secWebSocketKey) {
  100. $called = true;
  101. $self
  102. ->let($challenge = base64_encode(sha1($secWebSocketKey . Websocket\Protocol\Rfc6455::GUID, true)))
  103. ->string($_data)
  104. ->isEqualTo(
  105. 'HTTP/1.1 101 Switching Protocols' . CRLF .
  106. 'Upgrade: websocket' . CRLF .
  107. 'Connection: Upgrade' . CRLF .
  108. 'Sec-WebSocket-Accept: ' . $challenge . CRLF .
  109. 'Sec-WebSocket-Version: 13' . CRLF . CRLF
  110. );
  111. return strlen($_data);
  112. },
  113. $this->calling($socket)->getCurrentNode = $node
  114. )
  115. ->when($result = $this->invoke($server)->doHandshake())
  116. ->then
  117. ->variable($result)
  118. ->isNull()
  119. ->boolean($called)
  120. ->isTrue()
  121. ->object($node->getProtocolImplementation())
  122. ->isInstanceOf(Websocket\Protocol\Rfc6455::class)
  123. ->string($request->getMethod())
  124. ->isEqualTo($request::METHOD_GET)
  125. ->string($request->getUrl())
  126. ->isEqualTo('/foobar')
  127. ->array($request->getHeaders())
  128. ->isEqualTo([
  129. 'sec-websocket-key' => $secWebSocketKey
  130. ]);
  131. }
  132. public function case_do_handshake_hybi00()
  133. {
  134. $self = $this;
  135. $this
  136. ->given(
  137. $socket = new Socket\Server('tcp://*:1234'),
  138. $request = new Http\Request(),
  139. $this->mockGenerator->orphanize('__construct'),
  140. $node = new \Mock\Hoa\Websocket\Node(),
  141. $server = new SUT($socket, $request),
  142. $this->calling($socket)->read[1] =
  143. 'GET /foobar HTTP/1.1' . CRLF .
  144. 'Host: example.org' . CRLF .
  145. 'Origin: elpmaxe.org' . CRLF .
  146. 'Sec-WebSocket-Key1: 1a23b c45d e f' . CRLF .
  147. 'Sec-WebSocket-Key2: 6g78h i90j kl' . CRLF .
  148. 'Host: example.org' . CRLF . CRLF .
  149. 'hello' . CRLF,
  150. $this->calling($socket)->writeAll = function ($_data) use (&$called, $self) {
  151. $called = true;
  152. $self
  153. ->let($challenge = md5(pack('N', (int) (12345 / 3)) . pack('N', (int) (67890 / 2)) . 'hello', true))
  154. ->string($_data)
  155. ->isEqualTo(
  156. 'HTTP/1.1 101 WebSocket Protocol Handshake' . CRLF .
  157. 'Upgrade: WebSocket' . CRLF .
  158. 'Connection: Upgrade' . CRLF .
  159. 'Sec-WebSocket-Origin: elpmaxe.org' . CRLF .
  160. 'Sec-WebSocket-Location: ws://example.org/foobar' . CRLF . CRLF .
  161. $challenge . CRLF
  162. );
  163. return strlen($_data);
  164. },
  165. $this->calling($socket)->getCurrentNode = $node
  166. )
  167. ->when($result = $this->invoke($server)->doHandshake())
  168. ->then
  169. ->variable($result)
  170. ->isNull()
  171. ->boolean($called)
  172. ->isTrue()
  173. ->object($node->getProtocolImplementation())
  174. ->isInstanceOf(Websocket\Protocol\Hybi00::class)
  175. ->string($request->getMethod())
  176. ->isEqualTo($request::METHOD_GET)
  177. ->string($request->getUrl())
  178. ->isEqualTo('/foobar')
  179. ->array($request->getHeaders())
  180. ->isEqualTo([
  181. 'host' => 'example.org',
  182. 'origin' => 'elpmaxe.org',
  183. 'sec-websocket-key1' => '1a23b c45d e f',
  184. 'sec-websocket-key2' => '6g78h i90j kl'
  185. ]);
  186. }
  187. public function case_do_handshake_undefined_protocol()
  188. {
  189. $this
  190. ->given(
  191. $socket = new Socket\Server('tcp://*:1234'),
  192. $request = new Http\Request(),
  193. $this->mockGenerator->orphanize('__construct'),
  194. $node = new \Mock\Hoa\Websocket\Node(),
  195. $server = new SUT($socket, $request),
  196. $this->calling($socket)->read[1] =
  197. 'GET /foobar HTTP/1.1' . CRLF . CRLF,
  198. $this->calling($socket)->writeAll = function ($_data) use (&$called) {
  199. $called = true;
  200. return 0;
  201. },
  202. $this->calling($socket)->getCurrentNode = $node
  203. )
  204. ->exception(function () use ($server) {
  205. $this->invoke($server)->doHandshake();
  206. })
  207. ->isInstanceOf(Websocket\Exception\BadProtocol::class);
  208. }
  209. public function case_set_request()
  210. {
  211. $this
  212. ->given(
  213. $socket = new Socket\Server('tcp://*:1234'),
  214. $requestA = new Http\Request(),
  215. $requestB = new Http\Request(),
  216. $server = new SUT($socket, $requestA)
  217. )
  218. ->when($result = $server->setRequest($requestB))
  219. ->then
  220. ->object($result)
  221. ->isIdenticalTo($requestA);
  222. }
  223. public function case_get_request()
  224. {
  225. $this
  226. ->given(
  227. $socket = new Socket\Server('tcp://*:1234'),
  228. $requestA = new Http\Request(),
  229. $requestB = new Http\Request(),
  230. $server = new SUT($socket, $requestA),
  231. $server->setRequest($requestB)
  232. )
  233. ->when($result = $server->getRequest())
  234. ->then
  235. ->object($result)
  236. ->isIdenticalTo($requestB)
  237. ->boolean($socket->isDisconnected())
  238. ->isTrue();
  239. }
  240. }