Socket.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\Socket as HoaSocket;
  37. use Hoa\Test;
  38. use Hoa\Websocket\Socket as SUT;
  39. /**
  40. * Class \Hoa\Websocket\Test\Unit\Socket.
  41. *
  42. * Test suite for the socket class.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Socket extends Test\Unit\Suite
  48. {
  49. public function case_is_a_socket()
  50. {
  51. $this
  52. ->when($result = new SUT('tcp://hoa-project:net:8889'))
  53. ->then
  54. ->object($result)
  55. ->isInstanceOf('Hoa\Socket\Socket');
  56. }
  57. public function case_constructor()
  58. {
  59. $this
  60. ->given(
  61. $uri = 'tcp://hoa-project.net:8889',
  62. $secured = true,
  63. $endPoint = '/foobar'
  64. )
  65. ->when($result = new SUT($uri, $secured, $endPoint))
  66. ->then
  67. ->integer($result->getAddressType())
  68. ->isEqualTo(SUT::ADDRESS_DOMAIN)
  69. ->string($result->getTransport())
  70. ->isEqualTo('tcp')
  71. ->string($result->getAddress())
  72. ->isEqualTo('hoa-project.net')
  73. ->integer($result->getPort())
  74. ->isEqualTo(8889)
  75. ->boolean($result->isSecured())
  76. ->isTrue()
  77. ->string($result->getEndPoint())
  78. ->isEqualTo($endPoint);
  79. }
  80. public function case_get_endpoint()
  81. {
  82. $this
  83. ->given(
  84. $uri = 'tcp://hoa-project.net:8889',
  85. $secured = true,
  86. $endPoint = '/foobar',
  87. $socket = new SUT($uri, $secured, $endPoint)
  88. )
  89. ->when($result = $socket->getEndPoint())
  90. ->then
  91. ->string($result)
  92. ->isEqualTo($endPoint);
  93. }
  94. public function case_is_ws_transport_registered()
  95. {
  96. $this->_case_is_transport_registered('ws');
  97. }
  98. public function case_is_wss_transport_registered()
  99. {
  100. $this->_case_is_transport_registered('wss');
  101. }
  102. protected function _case_is_transport_registered($transport)
  103. {
  104. return
  105. $this
  106. ->when($result = HoaSocket\Transport::exists($transport))
  107. ->then
  108. ->boolean($result)
  109. ->isTrue();
  110. }
  111. public function case_transport_factory_invalid_URI()
  112. {
  113. $this
  114. ->exception(function () {
  115. SUT::transportFactory('foo');
  116. })
  117. ->isInstanceOf('Hoa\Websocket\Exception');
  118. }
  119. public function case_transport_unsecured_domain_with_port_with_endpoint()
  120. {
  121. $this->_case_transport_factory(
  122. 'ws://hoa-project.net:8889/foobar',
  123. [
  124. 'type' => SUT::ADDRESS_DOMAIN,
  125. 'address' => 'hoa-project.net',
  126. 'port' => 8889,
  127. 'endPoint' => '/foobar',
  128. 'secured' => false
  129. ]
  130. );
  131. }
  132. public function case_transport_unsecured_domain_with_port_without_endpoint()
  133. {
  134. $this->_case_transport_factory(
  135. 'ws://hoa-project.net:8889',
  136. [
  137. 'type' => SUT::ADDRESS_DOMAIN,
  138. 'address' => 'hoa-project.net',
  139. 'port' => 8889,
  140. 'endPoint' => '/',
  141. 'secured' => false
  142. ]
  143. );
  144. }
  145. public function case_transport_unsecured_domain_without_port_without_endpoint()
  146. {
  147. $this->_case_transport_factory(
  148. 'ws://hoa-project.net',
  149. [
  150. 'type' => SUT::ADDRESS_DOMAIN,
  151. 'address' => 'hoa-project.net',
  152. 'port' => 80,
  153. 'endPoint' => '/',
  154. 'secured' => false
  155. ]
  156. );
  157. }
  158. public function case_transport_secured_domain_with_port_with_endpoint()
  159. {
  160. $this->_case_transport_factory(
  161. 'wss://hoa-project.net:8889/foobar',
  162. [
  163. 'type' => SUT::ADDRESS_DOMAIN,
  164. 'address' => 'hoa-project.net',
  165. 'port' => 8889,
  166. 'endPoint' => '/foobar',
  167. 'secured' => true
  168. ]
  169. );
  170. }
  171. public function case_transport_secured_domain_with_port_without_endpoint()
  172. {
  173. $this->_case_transport_factory(
  174. 'wss://hoa-project.net:8889',
  175. [
  176. 'type' => SUT::ADDRESS_DOMAIN,
  177. 'address' => 'hoa-project.net',
  178. 'port' => 8889,
  179. 'endPoint' => '/',
  180. 'secured' => true
  181. ]
  182. );
  183. }
  184. public function case_transport_secured_domain_without_port_without_endpoint()
  185. {
  186. $this->_case_transport_factory(
  187. 'wss://hoa-project.net',
  188. [
  189. 'type' => SUT::ADDRESS_DOMAIN,
  190. 'address' => 'hoa-project.net',
  191. 'port' => 443,
  192. 'endPoint' => '/',
  193. 'secured' => true
  194. ]
  195. );
  196. }
  197. public function case_transport_query_strings_in_the_endpoint()
  198. {
  199. $this->_case_transport_factory(
  200. 'wss://hoa-project.net:8889/hello/world?foo=bar&baz=qux',
  201. [
  202. 'type' => SUT::ADDRESS_DOMAIN,
  203. 'address' => 'hoa-project.net',
  204. 'port' => 8889,
  205. 'endPoint' => '/hello/world?foo=bar&baz=qux',
  206. 'secured' => true
  207. ]
  208. );
  209. }
  210. protected function _case_transport_factory($uri, array $expect)
  211. {
  212. return
  213. $this
  214. ->when($result = SUT::transportFactory($uri))
  215. ->then
  216. ->object($result)
  217. ->isInstanceOf(SUT::class)
  218. ->integer($result->getAddressType())
  219. ->isEqualTo($expect['type'])
  220. ->string($result->getTransport())
  221. ->isEqualTo('tcp')
  222. ->string($result->getAddress())
  223. ->isEqualTo($expect['address'])
  224. ->integer($result->getPort())
  225. ->isEqualTo($expect['port'])
  226. ->string($result->getEndPoint())
  227. ->isEqualTo($expect['endPoint'])
  228. ->boolean($result->isSecured())
  229. ->isEqualTo($expect['secured']);
  230. }
  231. }