HTTPTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Tests KO7_HTTP Class
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.http
  8. *
  9. * @package KO7\Tests
  10. *
  11. * @copyright (c) 2007-2016 Kohana Team
  12. * @copyright (c) since 2016 Koseven Team
  13. * @license https://koseven.dev/LICENSE
  14. *
  15. */
  16. class KO7_HTTPTest extends Unittest_TestCase {
  17. /**
  18. * Sets up the environment
  19. *
  20. * @throws KO7_Exception
  21. * @throws ReflectionException
  22. * @throws Request_Exception
  23. */
  24. public function setUp(): void
  25. {
  26. parent::setUp();
  27. Request::$initial = new Request('/');
  28. }
  29. /**
  30. * Defaults for this test
  31. *
  32. * @var array
  33. */
  34. protected $environmentDefault = [
  35. 'Request::$initial',
  36. 'url.trusted_hosts' => ['www\.example\.com'],
  37. 'KO7::$base_url' => '/ko7/',
  38. 'KO7::$index_file' => 'index.php',
  39. 'HTTP_HOST' => 'www.example.com'
  40. ];
  41. /**
  42. * Provides test data for test_attributes()
  43. *
  44. * @return array
  45. */
  46. public function provider_redirect(): array
  47. {
  48. return [
  49. [
  50. 'http://www.example.org/',
  51. 301,
  52. 'HTTP_Exception_301',
  53. 'http://www.example.org/'
  54. ],
  55. [
  56. '/page_one',
  57. 302,
  58. 'HTTP_Exception_302',
  59. 'http://www.example.com/ko7/index.php/page_one'
  60. ],
  61. [
  62. 'page_two',
  63. 303,
  64. 'HTTP_Exception_303',
  65. 'http://www.example.com/ko7/index.php/page_two'
  66. ],
  67. [
  68. NULL,
  69. 99999,
  70. 'HTTP_Exception',
  71. NULL
  72. ]
  73. ];
  74. }
  75. /**
  76. * Tests HTTP::redirect()
  77. *
  78. * @dataProvider provider_redirect
  79. *
  80. * @param array $location Location to redirect to
  81. * @param array $code HTTP Code to use for the redirect
  82. * @param string $expected_exception Expected Exception
  83. * @param string $expected_location Expected Location
  84. */
  85. public function test_redirect($location, $code, $expected_exception, $expected_location): void
  86. {
  87. try
  88. {
  89. HTTP::redirect($location, $code);
  90. }
  91. catch (HTTP_Exception $e)
  92. {
  93. $response = $e->get_response();
  94. $this->assertInstanceOf($expected_exception, $e);
  95. $this->assertEquals($expected_location, $response->headers('Location'));
  96. }
  97. }
  98. /**
  99. * Provides test data for test_request_headers
  100. *
  101. * NOTE: Please don't add more Test Data because if you use
  102. * pecl_http this test will fail (it caches the result for the current script exec)
  103. *
  104. * @see https://github.com/m6w6/ext-http/issues/90
  105. *
  106. * @return array
  107. */
  108. public function provider_request_headers(): array
  109. {
  110. return [
  111. [
  112. [
  113. 'CONTENT_TYPE' => 'text/html; charset=utf-8',
  114. 'CONTENT_LENGTH' => '3547',
  115. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  116. 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch',
  117. 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8,fr;q=0.6,hy;q=0.4'
  118. ],
  119. [
  120. 'content-type' => 'text/html; charset=utf-8',
  121. 'content-length' => '3547',
  122. 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  123. 'accept-encoding' => 'gzip, deflate, sdch',
  124. 'accept-language' => 'en-US,en;q=0.8,fr;q=0.6,hy;q=0.4'
  125. ]
  126. ]
  127. ];
  128. }
  129. /**
  130. * Tests HTTP::request_headers()
  131. *
  132. * HTTP::request_headers relies on the $_SERVER superglobal if the function
  133. * `apache_request_headers` or the PECL `http` extension are not available.
  134. *
  135. * The test feeds the $_SERVER superglobal with the test cases' datasets
  136. * and then restores the $_SERVER superglobal so that it does not affect
  137. * other tests.
  138. *
  139. * @dataProvider provider_request_headers
  140. *
  141. * @param array $server_globals Globals to feed $_SERVER
  142. * @param array $expected_headers Expected, cleaned HTTP headers
  143. */
  144. public function test_request_headers(array $server_globals, array $expected_headers): void
  145. {
  146. $this->markTestSkipped('HTTP_Header has no public properties');
  147. // save the $_SERVER super-global into temporary local var
  148. $tmp_server = $_SERVER;
  149. // We need to clear the $_SERVER array first, other ways pecl_http does not accept it
  150. // as HTTP request (since version 3.0.0 it would return "NULL" instead )
  151. unset($_SERVER);
  152. // Set our globals
  153. $_SERVER = $server_globals;
  154. // Only get the keys we want
  155. // Get Request Headers
  156. // $headers = HTTP::request_headers();
  157. // HTTP_Header has no public properties, $headers->getArrayCopy() always returns an empty array.
  158. $allowed_headers = [
  159. 'content-type' => 'text/html; charset=utf-8',
  160. 'content-length' => '3547',
  161. 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  162. 'accept-encoding' => 'gzip, deflate, sdch',
  163. 'accept-language' => 'en-US,en;q=0.8,fr;q=0.6,hy;q=0.4'
  164. ];
  165. $actual_headers = array_intersect_key($allowed_headers, $expected_headers);
  166. // Compare Headers against the expected result to make sure they got parsed correctly
  167. $this->assertSame($expected_headers, $actual_headers);
  168. // revert the $_SERVER super-global to its previous state
  169. $_SERVER = $tmp_server;
  170. }
  171. /**
  172. * Provides test data for test_check_cache
  173. *
  174. * @return array
  175. */
  176. public function provider_check_cache(): array
  177. {
  178. return [
  179. [
  180. [
  181. 'body' => 'Test',
  182. 'etag' => NULL,
  183. 'cache' => NULL
  184. ],
  185. [
  186. 'cache' => 'must-revalidate',
  187. 'match' => FALSE
  188. ]
  189. ],
  190. [
  191. [
  192. 'body' => 'Test',
  193. 'etag' => TRUE,
  194. 'cache' => 'test-cache'
  195. ],
  196. [
  197. 'cache' => 'test-cache, must-revalidate',
  198. 'match' => TRUE
  199. ]
  200. ],
  201. [
  202. [
  203. 'body' => 'Test',
  204. 'etag' => FALSE,
  205. 'cache' => 'test-cache'
  206. ],
  207. [
  208. 'cache' => 'test-cache, must-revalidate',
  209. 'match' => FALSE
  210. ]
  211. ]
  212. ];
  213. }
  214. /**
  215. * Tests HTTP::check_cache()
  216. *
  217. * @dataProvider provider_check_cache
  218. *
  219. * @param array $input Input variables
  220. * @param array $expected Expected results
  221. */
  222. public function test_check_cache(array $input, array $expected): void
  223. {
  224. // Initialize Request
  225. $request = Request::initial();
  226. // Get Current Response and set new body
  227. $response = Response::factory()->body($input['body']);
  228. // Response has custom cache-control header
  229. if ($input['cache'] !== NULL)
  230. {
  231. $response->headers('cache-control', $input['cache']);
  232. }
  233. // Get Expected E-Tag
  234. $expectedEtag = $response->generate_etag();
  235. // Request is sent with etag
  236. if ($input['etag'] === TRUE)
  237. {
  238. $request->headers('if-none-match', $expectedEtag);
  239. }
  240. elseif ($input['etag'] === FALSE)
  241. {
  242. $request->headers('if-none-match', 'wrong-etag');
  243. }
  244. // Check cache a 304 get's only thrown if e-tags match
  245. try
  246. {
  247. $response = HTTP::check_cache($request, $response);
  248. }
  249. catch (HTTP_Exception_304|Request_Exception $e)
  250. {
  251. if ($e instanceof HTTP_Exception_304)
  252. {
  253. $this->assertTrue($expected['match']);
  254. }
  255. else
  256. {
  257. $this->fail($e->getMessage());
  258. }
  259. }
  260. // Check if cache-control was set up correctly
  261. $this->assertSame($expected['cache'], $response->headers('cache-control'));
  262. // Check if E-Tag is the same as expected
  263. $this->assertSame($expectedEtag, $response->headers('etag'));
  264. }
  265. /**
  266. * Provides test data for test_www_form_urlencode()
  267. *
  268. * @return array
  269. */
  270. public function provider_www_form_urlencode(): array
  271. {
  272. return [
  273. [
  274. [
  275. 'test' => '1',
  276. 'test2' => '2'
  277. ],
  278. 'test=1&test2=2'
  279. ],
  280. [
  281. [
  282. 'test3' => 'spaces ',
  283. 'test4' => 'have to be encoded'
  284. ],
  285. 'test3=spaces%20&test4=have%20to%20be%20encoded'
  286. ],
  287. [
  288. [
  289. 'test5' => 'this is-allowed',
  290. 'test6' => 'also.allowed'
  291. ],
  292. 'test5=this%20is-allowed&test6=also.allowed'
  293. ],
  294. [
  295. [],
  296. ''
  297. ]
  298. ];
  299. }
  300. /**
  301. * Tests HTTP::www_form_urlencode()
  302. *
  303. * @dataProvider provider_www_form_urlencode
  304. *
  305. * @param array $params Parameter to encode
  306. * @param string $expected Expected url string
  307. */
  308. public function test_www_form_urlencode(array $params, string $expected): void
  309. {
  310. $encoded = HTTP::www_form_urlencode($params);
  311. $this->assertSame($expected, $encoded);
  312. }
  313. /**
  314. * Provides test data for test_parse_header_string()
  315. *
  316. * @return array
  317. */
  318. public function provider_parse_header_string(): array
  319. {
  320. return [
  321. [
  322. 'Host: example.com' . PHP_EOL .
  323. 'Accept: text/html,application/xhtml+xml' . PHP_EOL .
  324. 'Accept-Language: en-us,en;q=0.5' . PHP_EOL .
  325. 'Accept-Encoding: gzip,deflate' . PHP_EOL .
  326. 'Accept-Charset: ISO-8859-1,utf-8' . PHP_EOL .
  327. 'Array: number1' . PHP_EOL .
  328. 'Array: number2' . PHP_EOL .
  329. 'Array: number3' . PHP_EOL .
  330. 'Cache-Control: no-cache',
  331. [
  332. 'Host' => 'example.com',
  333. 'Accept' => 'text/html,application/xhtml+xml',
  334. 'Accept-Language' => 'en-us,en;q=0.5',
  335. 'Accept-Encoding' => 'gzip,deflate',
  336. 'Accept-Charset' => 'ISO-8859-1,utf-8',
  337. 'Array' =>
  338. [
  339. 0 => 'number1',
  340. 1 => 'number2',
  341. 2 => 'number3'
  342. ],
  343. 'Cache-Control' => 'no-cache'
  344. ]
  345. ]
  346. ];
  347. }
  348. /**
  349. * Tests HTTP::parse_header_stringorm_urlencode()
  350. *
  351. * @dataProvider provider_parse_header_string
  352. *
  353. * @param string $header Header String
  354. * @param array $expected Expected Result
  355. */
  356. public function test_parse_header_string(string $header, array $expected): void
  357. {
  358. // Parse Header string
  359. $parsed = HTTP::parse_header_string($header);
  360. // Compare Expected HTTP_HEADER against parsed one
  361. $expected = new HTTP_Header($expected);
  362. $this->assertEquals($expected, $parsed);
  363. }
  364. }