HTTPTest.php 9.1 KB

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