RequestVerifierTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Ratchet\RFC6455\Test\Unit\Handshake;
  3. use Ratchet\RFC6455\Handshake\RequestVerifier;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * @covers Ratchet\RFC6455\Handshake\RequestVerifier
  7. */
  8. class RequestVerifierTest extends TestCase {
  9. /**
  10. * @var RequestVerifier
  11. */
  12. protected $_v;
  13. public function setUp() {
  14. $this->_v = new RequestVerifier();
  15. }
  16. public static function methodProvider() {
  17. return array(
  18. array(true, 'GET'),
  19. array(true, 'get'),
  20. array(true, 'Get'),
  21. array(false, 'POST'),
  22. array(false, 'DELETE'),
  23. array(false, 'PUT'),
  24. array(false, 'PATCH')
  25. );
  26. }
  27. /**
  28. * @dataProvider methodProvider
  29. */
  30. public function testMethodMustBeGet($result, $in) {
  31. $this->assertEquals($result, $this->_v->verifyMethod($in));
  32. }
  33. public static function httpVersionProvider() {
  34. return array(
  35. array(true, 1.1),
  36. array(true, '1.1'),
  37. array(true, 1.2),
  38. array(true, '1.2'),
  39. array(true, 2),
  40. array(true, '2'),
  41. array(true, '2.0'),
  42. array(false, '1.0'),
  43. array(false, 1),
  44. array(false, '0.9'),
  45. array(false, ''),
  46. array(false, 'hello')
  47. );
  48. }
  49. /**
  50. * @dataProvider httpVersionProvider
  51. */
  52. public function testHttpVersionIsAtLeast1Point1($expected, $in) {
  53. $this->assertEquals($expected, $this->_v->verifyHTTPVersion($in));
  54. }
  55. public static function uRIProvider() {
  56. return array(
  57. array(true, '/chat'),
  58. array(true, '/hello/world?key=val'),
  59. array(false, '/chat#bad'),
  60. array(false, 'nope'),
  61. array(false, '/ ಠ_ಠ '),
  62. array(false, '/✖')
  63. );
  64. }
  65. /**
  66. * @dataProvider URIProvider
  67. */
  68. public function testRequestUri($expected, $in) {
  69. $this->assertEquals($expected, $this->_v->verifyRequestURI($in));
  70. }
  71. public static function hostProvider() {
  72. return array(
  73. array(true, ['server.example.com']),
  74. array(false, [])
  75. );
  76. }
  77. /**
  78. * @dataProvider HostProvider
  79. */
  80. public function testVerifyHostIsSet($expected, $in) {
  81. $this->assertEquals($expected, $this->_v->verifyHost($in));
  82. }
  83. public static function upgradeProvider() {
  84. return array(
  85. array(true, ['websocket']),
  86. array(true, ['Websocket']),
  87. array(true, ['webSocket']),
  88. array(false, []),
  89. array(false, [''])
  90. );
  91. }
  92. /**
  93. * @dataProvider upgradeProvider
  94. */
  95. public function testVerifyUpgradeIsWebSocket($expected, $val) {
  96. $this->assertEquals($expected, $this->_v->verifyUpgradeRequest($val));
  97. }
  98. public static function connectionProvider() {
  99. return array(
  100. array(true, ['Upgrade']),
  101. array(true, ['upgrade']),
  102. array(true, ['keep-alive', 'Upgrade']),
  103. array(true, ['Upgrade', 'keep-alive']),
  104. array(true, ['keep-alive', 'Upgrade', 'something']),
  105. // as seen in Firefox 47.0.1 - see https://github.com/ratchetphp/RFC6455/issues/14
  106. array(true, ['keep-alive, Upgrade']),
  107. array(true, ['Upgrade, keep-alive']),
  108. array(true, ['keep-alive, Upgrade, something']),
  109. array(true, ['keep-alive, Upgrade', 'something']),
  110. array(false, ['']),
  111. array(false, [])
  112. );
  113. }
  114. /**
  115. * @dataProvider connectionProvider
  116. */
  117. public function testConnectionHeaderVerification($expected, $val) {
  118. $this->assertEquals($expected, $this->_v->verifyConnection($val));
  119. }
  120. public static function keyProvider() {
  121. return array(
  122. array(true, ['hkfa1L7uwN6DCo4IS3iWAw==']),
  123. array(true, ['765vVoQpKSGJwPzJIMM2GA==']),
  124. array(true, ['AQIDBAUGBwgJCgsMDQ4PEC==']),
  125. array(true, ['axa2B/Yz2CdpfQAY2Q5P7w==']),
  126. array(false, [0]),
  127. array(false, ['Hello World']),
  128. array(false, ['1234567890123456']),
  129. array(false, ['123456789012345678901234']),
  130. array(true, [base64_encode('UTF8allthngs+✓')]),
  131. array(true, ['dGhlIHNhbXBsZSBub25jZQ==']),
  132. array(false, []),
  133. array(false, ['dGhlIHNhbXBsZSBub25jZQ==', 'Some other value']),
  134. array(false, ['Some other value', 'dGhlIHNhbXBsZSBub25jZQ=='])
  135. );
  136. }
  137. /**
  138. * @dataProvider keyProvider
  139. */
  140. public function testKeyIsBase64Encoded16BitNonce($expected, $val) {
  141. $this->assertEquals($expected, $this->_v->verifyKey($val));
  142. }
  143. public static function versionProvider() {
  144. return array(
  145. array(true, [13]),
  146. array(true, ['13']),
  147. array(false, [12]),
  148. array(false, [14]),
  149. array(false, ['14']),
  150. array(false, ['hi']),
  151. array(false, ['']),
  152. array(false, [])
  153. );
  154. }
  155. /**
  156. * @dataProvider versionProvider
  157. */
  158. public function testVersionEquals13($expected, $in) {
  159. $this->assertEquals($expected, $this->_v->verifyVersion($in));
  160. }
  161. }