URLTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Tests URL
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.url
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author BRMatt <matthew@sigswitch.com>
  13. * @copyright (c) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  15. */
  16. class Kohana_URLTest extends Unittest_TestCase
  17. {
  18. /**
  19. * Sets up the environment
  20. */
  21. // @codingStandardsIgnoreStart
  22. public function setUp(): void
  23. // @codingStandardsIgnoreEnd
  24. {
  25. parent::setUp();
  26. Kohana::$config->load('url')->set(
  27. 'trusted_hosts',
  28. ['www\.example\.com', 'sub\.example\.com', 'example\.com', 'example\.org']
  29. );
  30. }
  31. /**
  32. * Default values for the environment, see setEnvironment
  33. * @var array
  34. */
  35. // @codingStandardsIgnoreStart
  36. protected $environmentDefault = [
  37. 'Kohana::$base_url' => '/kohana/',
  38. 'Kohana::$index_file'=> 'index.php',
  39. 'HTTP_HOST' => 'example.com',
  40. '_GET' => [],
  41. ];
  42. // @codingStandardsIgnoreEnd
  43. /**
  44. * Provides test data for test_base()
  45. *
  46. * @return array
  47. */
  48. public function provider_base()
  49. {
  50. return [
  51. // $protocol, $index, $subdomain, $expected, $enviroment
  52. // Test with different combinations of parameters for max code coverage
  53. [NULL, FALSE, NULL, NULL, ['Kohana::$base_url' => NULL]],
  54. [NULL, FALSE, NULL, '/', ['Kohana::$base_url' => '/']],
  55. [NULL, FALSE, NULL, '/kohana/'],
  56. ['http', FALSE, NULL, 'http://example.com/kohana/'],
  57. ['http', FALSE, 'sub', 'http://sub.example.com/kohana/'],
  58. ['http', FALSE, 'sub', 'http://sub.example.com/kohana/', ['HTTP_HOST' => 'sub.example.com']],
  59. ['http', FALSE, 'sub', 'http://sub.example.com/kohana/', ['HTTP_HOST' => 'invalid.example.com']],
  60. [NULL, TRUE, NULL, '/kohana/index.php/'],
  61. ['http', TRUE, NULL, 'http://example.com/kohana/index.php/'],
  62. ['http', TRUE, 'sub', 'http://sub.example.com/kohana/index.php/'],
  63. ['https', TRUE, NULL, 'https://example.com/kohana/index.php/'],
  64. ['https', TRUE, 'sub', 'https://sub.example.com/kohana/index.php/'],
  65. ['ftp', TRUE, NULL, 'ftp://example.com/kohana/index.php/'],
  66. ['ftp', TRUE, 'sub', 'ftp://sub.example.com/kohana/index.php/'],
  67. // Test for automatic protocol detection, protocol = TRUE
  68. [TRUE, TRUE, NULL, 'cli://example.com/kohana/index.php/', ['HTTPS' => FALSE, 'Request::$initial' => Request::factory('/')->protocol('cli')]],
  69. // Change base url'
  70. ['https', FALSE, NULL, 'https://example.com/kohana/', ['Kohana::$base_url' => 'omglol://example.com/kohana/']],
  71. // Use port in base url, issue #3307
  72. ['http', FALSE, NULL, 'http://example.com:8080/', ['Kohana::$base_url' => 'example.com:8080/']],
  73. // Use protocol from base url if none specified
  74. [NULL, FALSE, NULL, 'http://www.example.com/', ['Kohana::$base_url' => 'http://www.example.com/']],
  75. [NULL, FALSE, 'sub', 'http://sub.example.com/', ['Kohana::$base_url' => 'http://www.example.com/']],
  76. // Use HTTP_HOST before SERVER_NAME
  77. ['http', FALSE, NULL, 'http://example.com/kohana/', ['HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.org']],
  78. // Use SERVER_NAME if HTTP_HOST DNX
  79. ['http', FALSE, NULL, 'http://example.org/kohana/', ['HTTP_HOST' => NULL, 'SERVER_NAME' => 'example.org']],
  80. ];
  81. }
  82. /**
  83. * Tests URL::base()
  84. *
  85. * @test
  86. * @dataProvider provider_base
  87. * @param mixed $protocol Parameter for Url::base()
  88. * @param boolean $index Parameter for Url::base()
  89. * @param string $subdomain Parameter for Url::base()
  90. * @param string $expected Expected url
  91. * @param array $enviroment Array of enviroment vars to change @see Kohana_URLTest::setEnvironment()
  92. */
  93. public function test_base($protocol, $index, $subdomain, $expected, array $enviroment = [])
  94. {
  95. $this->setEnvironment($enviroment);
  96. $this->assertSame(
  97. $expected,
  98. URL::base($protocol, $index, $subdomain)
  99. );
  100. }
  101. /**
  102. * Provides test data for test_site()
  103. *
  104. * @return array
  105. */
  106. public function provider_site()
  107. {
  108. return [
  109. ['', NULL, '/kohana/index.php/'],
  110. ['', 'http', 'http://example.com/kohana/index.php/'],
  111. ['my/site', NULL, '/kohana/index.php/my/site'],
  112. ['my/site', 'http', 'http://example.com/kohana/index.php/my/site'],
  113. // @ticket #3110
  114. ['my/site/page:5', NULL, '/kohana/index.php/my/site/page:5'],
  115. ['my/site/page:5', 'http', 'http://example.com/kohana/index.php/my/site/page:5'],
  116. ['my/site?var=asd&kohana=awesome', NULL, '/kohana/index.php/my/site?var=asd&kohana=awesome'],
  117. ['my/site?var=asd&kohana=awesome', 'http', 'http://example.com/kohana/index.php/my/site?var=asd&kohana=awesome'],
  118. ['?kohana=awesome&life=good', NULL, '/kohana/index.php/?kohana=awesome&life=good'],
  119. ['?kohana=awesome&life=good', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good'],
  120. ['?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/?kohana=awesome&life=good#fact'],
  121. ['?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/?kohana=awesome&life=good#fact'],
  122. ['some/long/route/goes/here?kohana=awesome&life=good#fact', NULL, '/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'],
  123. ['some/long/route/goes/here?kohana=awesome&life=good#fact', 'http', 'http://example.com/kohana/index.php/some/long/route/goes/here?kohana=awesome&life=good#fact'],
  124. ['/route/goes/here?kohana=awesome&life=good#fact', 'https', 'https://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'],
  125. ['/route/goes/here?kohana=awesome&life=good#fact', 'ftp', 'ftp://example.com/kohana/index.php/route/goes/here?kohana=awesome&life=good#fact'],
  126. ];
  127. }
  128. /**
  129. * Tests URL::site()
  130. *
  131. * @test
  132. * @dataProvider provider_site
  133. * @param string $uri URI to use
  134. * @param boolean|string $protocol Protocol to use
  135. * @param string $expected Expected result
  136. * @param array $enviroment Array of enviroment vars to set
  137. */
  138. public function test_site($uri, $protocol, $expected, array $enviroment = [])
  139. {
  140. $this->setEnvironment($enviroment);
  141. $this->assertSame(
  142. $expected,
  143. URL::site($uri, $protocol)
  144. );
  145. }
  146. /**
  147. * Provides test data for test_site_url_encode_uri()
  148. * See issue #2680
  149. *
  150. * @return array
  151. */
  152. public function provider_site_url_encode_uri()
  153. {
  154. $provider = [
  155. ['test', 'encode'],
  156. ['test', 'éñçø∂ë∂'],
  157. ['†é߆', 'encode'],
  158. ['†é߆', 'éñçø∂ë∂', 'µåñ¥'],
  159. ];
  160. foreach ($provider as $i => $params)
  161. {
  162. // Every non-ASCII character except for forward slash should be encoded...
  163. $expected = implode('/', array_map('rawurlencode', $params));
  164. // ... from a URI that is not encoded
  165. $uri = implode('/', $params);
  166. $provider[$i] = ["/kohana/index.php/{$expected}", $uri];
  167. }
  168. return $provider;
  169. }
  170. /**
  171. * Tests URL::site for proper URL encoding when working with non-ASCII characters.
  172. *
  173. * @test
  174. * @dataProvider provider_site_url_encode_uri
  175. */
  176. public function test_site_url_encode_uri($expected, $uri)
  177. {
  178. $this->assertSame($expected, URL::site($uri, FALSE));
  179. }
  180. /**
  181. * Provides test data for test_title()
  182. * @return array
  183. */
  184. public function provider_title()
  185. {
  186. return [
  187. // Tests that..
  188. // Title is converted to lowercase
  189. ['we-shall-not-be-moved', 'WE SHALL NOT BE MOVED', '-'],
  190. // Excessive white space is removed and replaced with 1 char
  191. ['thissssss-is-it', 'THISSSSSS IS IT ', '-'],
  192. // separator is either - (dash) or _ (underscore) & others are converted to underscores
  193. ['some-title', 'some title', '-'],
  194. ['some_title', 'some title', '_'],
  195. ['some!title', 'some title', '!'],
  196. ['some:title', 'some title', ':'],
  197. // Numbers are preserved
  198. ['99-ways-to-beat-apple', '99 Ways to beat apple', '-'],
  199. // ... with lots of spaces & caps
  200. ['99_ways_to_beat_apple', '99 ways TO beat APPLE', '_'],
  201. ['99-ways-to-beat-apple', '99 ways TO beat APPLE', '-'],
  202. // Invalid characters are removed
  203. ['each-gbp-is-now-worth-32-usd', 'Each GBP(£) is now worth 32 USD($)', '-'],
  204. // ... inc. separator
  205. ['is-it-reusable-or-re-usable', 'Is it reusable or re-usable?', '-'],
  206. // Doing some crazy UTF8 tests
  207. ['espana-wins', 'España-wins', '-', TRUE],
  208. ];
  209. }
  210. /**
  211. * Tests URL::title()
  212. *
  213. * @test
  214. * @dataProvider provider_title
  215. * @param string $title Input to convert
  216. * @param string $separator Seperate to replace invalid characters with
  217. * @param string $expected Expected result
  218. */
  219. public function test_title($expected, $title, $separator, $ascii_only = FALSE)
  220. {
  221. $this->assertSame(
  222. $expected,
  223. URL::title($title, $separator, $ascii_only)
  224. );
  225. }
  226. /**
  227. * Provides test data for URL::query()
  228. * @return array
  229. */
  230. public function provider_query()
  231. {
  232. return [
  233. [[], '', NULL],
  234. [['_GET' => ['test' => 'data']], '?test=data', NULL],
  235. [[], '?test=data', ['test' => 'data']],
  236. [['_GET' => ['more' => 'data']], '?more=data&test=data', ['test' => 'data']],
  237. [['_GET' => ['sort' => 'down']], '?test=data', ['test' => 'data'], FALSE],
  238. // http://dev.kohanaframework.org/issues/3362
  239. [[], '', ['key' => NULL]],
  240. [[], '?key=0', ['key' => FALSE]],
  241. [[], '?key=1', ['key' => TRUE]],
  242. [['_GET' => ['sort' => 'down']], '?sort=down&key=1', ['key' => TRUE]],
  243. [['_GET' => ['sort' => 'down']], '?sort=down&key=0', ['key' => FALSE]],
  244. // @issue 4240
  245. [['_GET' => ['foo' => ['a' => 100]]], '?foo%5Ba%5D=100&foo%5Bb%5D=bar', ['foo' => ['b' => 'bar']]],
  246. [['_GET' => ['a' => 'a']], '?a=b', ['a' => 'b']],
  247. ];
  248. }
  249. /**
  250. * Tests URL::query()
  251. *
  252. * @test
  253. * @dataProvider provider_query
  254. * @param array $enviroment Set environment
  255. * @param string $expected Expected result
  256. * @param array $params Query string
  257. * @param boolean $use_get Combine with GET parameters
  258. */
  259. public function test_query($enviroment, $expected, $params, $use_get = TRUE)
  260. {
  261. $this->setEnvironment($enviroment);
  262. $this->assertSame(
  263. $expected,
  264. URL::query($params, $use_get)
  265. );
  266. }
  267. /**
  268. * Provides test data for URL::is_trusted_host()
  269. * @return array
  270. */
  271. public function provider_is_trusted_host()
  272. {
  273. return [
  274. // data set #0
  275. [
  276. 'givenhost',
  277. [
  278. 'list-of-trusted-hosts',
  279. ],
  280. FALSE
  281. ],
  282. // data set #1
  283. [
  284. 'givenhost',
  285. [
  286. 'givenhost',
  287. 'example\.com',
  288. ],
  289. TRUE
  290. ],
  291. // data set #2
  292. [
  293. 'www.kohanaframework.org',
  294. [
  295. '.*\.kohanaframework\.org',
  296. ],
  297. TRUE
  298. ],
  299. // data set #3
  300. [
  301. 'kohanaframework.org',
  302. [
  303. '.*\.kohanaframework\.org',
  304. ],
  305. FALSE // because we are requesting a subdomain
  306. ],
  307. ];
  308. }
  309. /**
  310. * Tests URL::is_trusted_hosts()
  311. *
  312. * @test
  313. * @dataProvider provider_is_trusted_host
  314. * @param string $host the given host
  315. * @param array $trusted_hosts list of trusted hosts
  316. * @param boolean $expected TRUE if host is trusted, FALSE otherwise
  317. */
  318. public function test_is_trusted_host($host, $trusted_hosts, $expected)
  319. {
  320. $this->assertSame(
  321. $expected,
  322. URL::is_trusted_host($host, $trusted_hosts)
  323. );
  324. }
  325. /**
  326. * Tests if invalid host throws "Invalid host" exception
  327. *
  328. * @test
  329. * @expectedException Kohana_Exception
  330. * @expectedExceptionMessage Invalid host <invalid>
  331. */
  332. public function test_if_invalid_host_throws_exception()
  333. {
  334. // set the global HTTP_HOST to <invalid>
  335. $_SERVER['HTTP_HOST'] = '<invalid>';
  336. // trigger exception
  337. URL::base('https');
  338. }
  339. /**
  340. * Tests if untrusted host throws "Untrusted host" exception
  341. *
  342. * @test
  343. * @expectedException Kohana_Exception
  344. * @expectedExceptionMessage Untrusted host untrusted.com
  345. */
  346. public function test_if_untrusted_host_throws_exception()
  347. {
  348. // set the global HTTP_HOST to a valid but untrusted host
  349. $_SERVER['HTTP_HOST'] = 'untrusted.com';
  350. // trigger exception
  351. URL::base('https');
  352. }
  353. }