HTTPTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Tests HTTP
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.http
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @copyright (c) Kohana Team
  13. * @license https://koseven.ga/LICENSE.md
  14. */
  15. class Kohana_HTTPTest extends Unittest_TestCase {
  16. protected $_inital_request;
  17. /**
  18. * Sets up the environment
  19. */
  20. // @codingStandardsIgnoreStart
  21. public function setUp()
  22. // @codingStandardsIgnoreEnd
  23. {
  24. parent::setUp();
  25. Kohana::$config->load('url')->set('trusted_hosts', ['www\.example\.com']);
  26. $this->_initial_request = Request::$initial;
  27. Request::$initial = new Request('/');
  28. }
  29. /**
  30. * Tears down whatever is setUp
  31. */
  32. // @codingStandardsIgnoreStart
  33. public function tearDown()
  34. // @codingStandardsIgnoreEnd
  35. {
  36. Request::$initial = $this->_initial_request;
  37. parent::tearDown();
  38. }
  39. // @codingStandardsIgnoreStart
  40. /**
  41. * Defaults for this test
  42. * @var array
  43. */
  44. // @codingStandardsIgnoreStart
  45. protected $environmentDefault = [
  46. 'Kohana::$base_url' => '/kohana/',
  47. 'Kohana::$index_file' => 'index.php',
  48. 'HTTP_HOST' => 'www.example.com',
  49. ];
  50. // @codingStandardsIgnoreEnd
  51. /**
  52. * Provides test data for test_attributes()
  53. *
  54. * @return array
  55. */
  56. public function provider_redirect()
  57. {
  58. return [
  59. [
  60. 'http://www.example.org/',
  61. 301,
  62. 'HTTP_Exception_301',
  63. 'http://www.example.org/'
  64. ],
  65. [
  66. '/page_one',
  67. 302,
  68. 'HTTP_Exception_302',
  69. 'http://www.example.com/kohana/index.php/page_one'
  70. ],
  71. [
  72. 'page_two',
  73. 303,
  74. 'HTTP_Exception_303',
  75. 'http://www.example.com/kohana/index.php/page_two'
  76. ],
  77. ];
  78. }
  79. /**
  80. * Tests HTTP::redirect()
  81. *
  82. * @test
  83. * @dataProvider provider_redirect
  84. * @param array $location Location to redirect to
  85. * @param array $code HTTP Code to use for the redirect
  86. * @param string $expected_exception Expected exception
  87. * @param string $expected_location Expected exception
  88. */
  89. public function test_redirect($location, $code, $expected_exception, $expected_location)
  90. {
  91. try
  92. {
  93. HTTP::redirect($location, $code);
  94. }
  95. catch (HTTP_Exception_Redirect $e)
  96. {
  97. $response = $e->get_response();
  98. $this->assertInstanceOf($expected_exception, $e);
  99. $this->assertEquals($expected_location, $response->headers('Location'));
  100. return;
  101. }
  102. $this->fail('HTTP_Exception_Redirect not thrown');
  103. }
  104. /**
  105. * Provides test data for test_request_headers
  106. *
  107. * @return array
  108. */
  109. public function provider_request_headers()
  110. {
  111. return [
  112. [
  113. [
  114. 'CONTENT_TYPE' => 'text/html; charset=utf-8',
  115. 'CONTENT_LENGTH' => '3547',
  116. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  117. 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch',
  118. 'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8,fr;q=0.6,hy;q=0.4',
  119. ],
  120. [
  121. 'content-type' => 'text/html; charset=utf-8',
  122. 'content-length' => '3547',
  123. 'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  124. 'accept-encoding'=>'gzip, deflate, sdch',
  125. 'accept-language'=>'en-US,en;q=0.8,fr;q=0.6,hy;q=0.4',
  126. ]
  127. ],
  128. [
  129. [
  130. 'HTTP_WEIRD_HTTP_HEADER' => 'A weird value for a weird header',
  131. ],
  132. [
  133. 'weird-http-header' => 'A weird value for a weird header',
  134. ]
  135. ],
  136. ];
  137. }
  138. /**
  139. * Tests HTTP::request_headers()
  140. *
  141. * HTTP::request_headers relies on the $_SERVER superglobal if the function
  142. * `apache_request_headers` or the PECL `http` extension are not available.
  143. *
  144. * The test feeds the $_SERVER superglobal with the test cases' datasets
  145. * and then restores the $_SERVER superglobal so that it does not affect
  146. * other tests.
  147. *
  148. * @test
  149. * @dataProvider provider_request_headers
  150. * @param array $server_globals globals to feed $_SERVER
  151. * @param array $expected_headers Expected, cleaned HTTP headers
  152. */
  153. public function test_request_headers(array $server_globals, array $expected_headers)
  154. {
  155. // save the $_SERVER super-global into temporary local var
  156. $tmp_server = $_SERVER;
  157. $_SERVER = array_replace_recursive($_SERVER, $server_globals);
  158. $headers = HTTP::request_headers();
  159. $actual_headers = array_intersect_key($headers->getArrayCopy(), $expected_headers);
  160. $this->assertSame($expected_headers, $actual_headers);
  161. // revert the super-global to its previous state
  162. $_SERVER = $tmp_server;
  163. }
  164. }