FunctionsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. class FunctionsTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @dataProvider getHeaderValuesDataOnValues2
  8. */
  9. public function testGetHeaderValuesOnValues2($result, $values1, $values2)
  10. {
  11. $this->assertEquals($result, getHeaderValues($values1, $values2));
  12. }
  13. public function getHeaderValuesDataOnValues2()
  14. {
  15. return [
  16. [
  17. ['a', 'b'],
  18. ['a'],
  19. ['b'],
  20. ],
  21. [
  22. ['a', 'b', 'c', 'd', 'e'],
  23. ['a', 'b', 'c'],
  24. ['d', 'e'],
  25. ],
  26. ];
  27. }
  28. /**
  29. * @dataProvider getHeaderValuesData
  30. */
  31. public function testGetHeaderValues($input, $output)
  32. {
  33. $this->assertEquals(
  34. $output,
  35. getHeaderValues($input)
  36. );
  37. }
  38. public function getHeaderValuesData()
  39. {
  40. return [
  41. [
  42. 'a',
  43. ['a'],
  44. ],
  45. [
  46. 'a,b',
  47. ['a', 'b'],
  48. ],
  49. [
  50. 'a, b',
  51. ['a', 'b'],
  52. ],
  53. [
  54. ['a, b'],
  55. ['a', 'b'],
  56. ],
  57. [
  58. ['a, b', 'c', 'd,e'],
  59. ['a', 'b', 'c', 'd', 'e'],
  60. ],
  61. ];
  62. }
  63. /**
  64. * @dataProvider preferData
  65. */
  66. public function testPrefer($input, $output)
  67. {
  68. $this->assertEquals(
  69. $output,
  70. parsePrefer($input)
  71. );
  72. }
  73. public function preferData()
  74. {
  75. return [
  76. [
  77. 'foo; bar',
  78. ['foo' => true],
  79. ],
  80. [
  81. 'foo; bar=""',
  82. ['foo' => true],
  83. ],
  84. [
  85. 'foo=""; bar',
  86. ['foo' => true],
  87. ],
  88. [
  89. 'FOO',
  90. ['foo' => true],
  91. ],
  92. [
  93. 'respond-async',
  94. ['respond-async' => true],
  95. ],
  96. [
  97. ['respond-async, wait=100', 'handling=lenient'],
  98. ['respond-async' => true, 'wait' => 100, 'handling' => 'lenient'],
  99. ],
  100. [
  101. ['respond-async, wait=100, handling=lenient'],
  102. ['respond-async' => true, 'wait' => 100, 'handling' => 'lenient'],
  103. ],
  104. // Old values
  105. [
  106. 'return-asynch, return-representation',
  107. ['respond-async' => true, 'return' => 'representation'],
  108. ],
  109. [
  110. 'return-minimal',
  111. ['return' => 'minimal'],
  112. ],
  113. [
  114. 'strict',
  115. ['handling' => 'strict'],
  116. ],
  117. [
  118. 'lenient',
  119. ['handling' => 'lenient'],
  120. ],
  121. // Invalid token
  122. [
  123. ['foo=%bar%'],
  124. [],
  125. ],
  126. ];
  127. }
  128. public function testParseHTTPDate()
  129. {
  130. $times = [
  131. 'Wed, 13 Oct 2010 10:26:00 GMT',
  132. 'Wednesday, 13-Oct-10 10:26:00 GMT',
  133. 'Wed Oct 13 10:26:00 2010',
  134. ];
  135. $expected = 1286965560;
  136. foreach ($times as $time) {
  137. $result = parseDate($time);
  138. $this->assertEquals($expected, $result->format('U'));
  139. }
  140. $result = parseDate('Wed Oct 6 10:26:00 2010');
  141. $this->assertEquals(1286360760, $result->format('U'));
  142. }
  143. public function testParseHTTPDateFail()
  144. {
  145. $times = [
  146. // random string
  147. 'NOW',
  148. // not-GMT timezone
  149. 'Wednesday, 13-Oct-10 10:26:00 UTC',
  150. // No space before the 6
  151. 'Wed Oct 6 10:26:00 2010',
  152. // Invalid day
  153. 'Wed Oct 0 10:26:00 2010',
  154. 'Wed Oct 32 10:26:00 2010',
  155. 'Wed, 0 Oct 2010 10:26:00 GMT',
  156. 'Wed, 32 Oct 2010 10:26:00 GMT',
  157. 'Wednesday, 32-Oct-10 10:26:00 GMT',
  158. // Invalid hour
  159. 'Wed, 13 Oct 2010 24:26:00 GMT',
  160. 'Wednesday, 13-Oct-10 24:26:00 GMT',
  161. 'Wed Oct 13 24:26:00 2010',
  162. ];
  163. foreach ($times as $time) {
  164. $this->assertFalse(parseDate($time), 'We used the string: '.$time);
  165. }
  166. }
  167. public function testTimezones()
  168. {
  169. $default = date_default_timezone_get();
  170. date_default_timezone_set('Europe/Amsterdam');
  171. $this->testParseHTTPDate();
  172. date_default_timezone_set($default);
  173. }
  174. public function testToHTTPDate()
  175. {
  176. $dt = new \DateTime('2011-12-10 12:00:00 +0200');
  177. $this->assertEquals(
  178. 'Sat, 10 Dec 2011 10:00:00 GMT',
  179. toDate($dt)
  180. );
  181. }
  182. public function testParseMimeTypeOnInvalidMimeType()
  183. {
  184. if (false === \getenv('EXECUTE_INVALID_MIME_TYPE_TEST')) {
  185. $this->markTestSkipped('Test skipped because parseMimeType with an invalid mime type will exit in 5.x');
  186. }
  187. $this->expectException(\InvalidArgumentException::class);
  188. $this->expectExceptionMessage('Not a valid mime-type: invalid_mime_type');
  189. parseMimeType('invalid_mime_type');
  190. }
  191. }