UriTest.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Uri;
  5. use Grav\Common\Utils;
  6. /**
  7. * Class UriTest
  8. */
  9. class UriTest extends \Codeception\TestCase\Test
  10. {
  11. /** @var Grav $grav */
  12. protected $grav;
  13. /** @var Uri $uri */
  14. protected $uri;
  15. protected $tests = [
  16. '/path' => [
  17. 'scheme' => '',
  18. 'user' => null,
  19. 'password' => null,
  20. 'host' => null,
  21. 'port' => null,
  22. 'path' => '/path',
  23. 'query' => '',
  24. 'fragment' => null,
  25. 'route' => '/path',
  26. 'paths' => ['path'],
  27. 'params' => null,
  28. 'url' => '/path',
  29. 'environment' => 'unknown',
  30. 'basename' => 'path',
  31. 'base' => '',
  32. 'currentPage' => 1,
  33. 'rootUrl' => '',
  34. 'extension' => null,
  35. 'addNonce' => '/path/nonce:{{nonce}}',
  36. ],
  37. '//localhost/' => [
  38. 'scheme' => '//',
  39. 'user' => null,
  40. 'password' => null,
  41. 'host' => 'localhost',
  42. 'port' => null,
  43. 'path' => '/',
  44. 'query' => '',
  45. 'fragment' => null,
  46. 'route' => '/',
  47. 'paths' => [],
  48. 'params' => null,
  49. 'url' => '/',
  50. 'environment' => 'localhost',
  51. 'basename' => '',
  52. 'base' => '//localhost',
  53. 'currentPage' => 1,
  54. 'rootUrl' => '//localhost',
  55. 'extension' => null,
  56. 'addNonce' => '//localhost/nonce:{{nonce}}',
  57. ],
  58. 'http://localhost/' => [
  59. 'scheme' => 'http://',
  60. 'user' => null,
  61. 'password' => null,
  62. 'host' => 'localhost',
  63. 'port' => 80,
  64. 'path' => '/',
  65. 'query' => '',
  66. 'fragment' => null,
  67. 'route' => '/',
  68. 'paths' => [],
  69. 'params' => null,
  70. 'url' => '/',
  71. 'environment' => 'localhost',
  72. 'basename' => '',
  73. 'base' => 'http://localhost',
  74. 'currentPage' => 1,
  75. 'rootUrl' => 'http://localhost',
  76. 'extension' => null,
  77. 'addNonce' => 'http://localhost/nonce:{{nonce}}',
  78. ],
  79. 'http://127.0.0.1/' => [
  80. 'scheme' => 'http://',
  81. 'user' => null,
  82. 'password' => null,
  83. 'host' => '127.0.0.1',
  84. 'port' => 80,
  85. 'path' => '/',
  86. 'query' => '',
  87. 'fragment' => null,
  88. 'route' => '/',
  89. 'paths' => [],
  90. 'params' => null,
  91. 'url' => '/',
  92. 'environment' => 'localhost',
  93. 'basename' => '',
  94. 'base' => 'http://127.0.0.1',
  95. 'currentPage' => 1,
  96. 'rootUrl' => 'http://127.0.0.1',
  97. 'extension' => null,
  98. 'addNonce' => 'http://127.0.0.1/nonce:{{nonce}}',
  99. ],
  100. 'https://localhost/' => [
  101. 'scheme' => 'https://',
  102. 'user' => null,
  103. 'password' => null,
  104. 'host' => 'localhost',
  105. 'port' => 443,
  106. 'path' => '/',
  107. 'query' => '',
  108. 'fragment' => null,
  109. 'route' => '/',
  110. 'paths' => [],
  111. 'params' => null,
  112. 'url' => '/',
  113. 'environment' => 'localhost',
  114. 'basename' => '',
  115. 'base' => 'https://localhost',
  116. 'currentPage' => 1,
  117. 'rootUrl' => 'https://localhost',
  118. 'extension' => null,
  119. 'addNonce' => 'https://localhost/nonce:{{nonce}}',
  120. ],
  121. 'http://localhost:8080/grav/it/ueper' => [
  122. 'scheme' => 'http://',
  123. 'user' => null,
  124. 'password' => null,
  125. 'host' => 'localhost',
  126. 'port' => 8080,
  127. 'path' => '/grav/it/ueper',
  128. 'query' => '',
  129. 'fragment' => null,
  130. 'route' => '/grav/it/ueper',
  131. 'paths' => ['grav', 'it', 'ueper'],
  132. 'params' => null,
  133. 'url' => '/grav/it/ueper',
  134. 'environment' => 'localhost',
  135. 'basename' => 'ueper',
  136. 'base' => 'http://localhost:8080',
  137. 'currentPage' => 1,
  138. 'rootUrl' => 'http://localhost:8080',
  139. 'extension' => null,
  140. 'addNonce' => 'http://localhost:8080/grav/it/ueper/nonce:{{nonce}}',
  141. ],
  142. 'http://localhost:8080/grav/it/ueper:xxx' => [
  143. 'scheme' => 'http://',
  144. 'user' => null,
  145. 'password' => null,
  146. 'host' => 'localhost',
  147. 'port' => 8080,
  148. 'path' => '/grav/it',
  149. 'query' => '',
  150. 'fragment' => null,
  151. 'route' => '/grav/it',
  152. 'paths' => ['grav', 'it'],
  153. 'params' => '/ueper:xxx',
  154. 'url' => '/grav/it',
  155. 'environment' => 'localhost',
  156. 'basename' => 'it',
  157. 'base' => 'http://localhost:8080',
  158. 'currentPage' => 1,
  159. 'rootUrl' => 'http://localhost:8080',
  160. 'extension' => null,
  161. 'addNonce' => 'http://localhost:8080/grav/it/ueper:xxx/nonce:{{nonce}}',
  162. ],
  163. 'http://localhost:8080/grav/it/ueper:xxx/page:/test:yyy' => [
  164. 'scheme' => 'http://',
  165. 'user' => null,
  166. 'password' => null,
  167. 'host' => 'localhost',
  168. 'port' => 8080,
  169. 'path' => '/grav/it',
  170. 'query' => '',
  171. 'fragment' => null,
  172. 'route' => '/grav/it',
  173. 'paths' => ['grav', 'it'],
  174. 'params' => '/ueper:xxx/page:/test:yyy',
  175. 'url' => '/grav/it',
  176. 'environment' => 'localhost',
  177. 'basename' => 'it',
  178. 'base' => 'http://localhost:8080',
  179. 'currentPage' => '',
  180. 'rootUrl' => 'http://localhost:8080',
  181. 'extension' => null,
  182. 'addNonce' => 'http://localhost:8080/grav/it/ueper:xxx/page:/test:yyy/nonce:{{nonce}}',
  183. ],
  184. 'http://localhost:8080/grav/it/ueper?test=x' => [
  185. 'scheme' => 'http://',
  186. 'user' => null,
  187. 'password' => null,
  188. 'host' => 'localhost',
  189. 'port' => 8080,
  190. 'path' => '/grav/it/ueper',
  191. 'query' => 'test=x',
  192. 'fragment' => null,
  193. 'route' => '/grav/it/ueper',
  194. 'paths' => ['grav', 'it', 'ueper'],
  195. 'params' => null,
  196. 'url' => '/grav/it/ueper',
  197. 'environment' => 'localhost',
  198. 'basename' => 'ueper',
  199. 'base' => 'http://localhost:8080',
  200. 'currentPage' => 1,
  201. 'rootUrl' => 'http://localhost:8080',
  202. 'extension' => null,
  203. 'addNonce' => 'http://localhost:8080/grav/it/ueper/nonce:{{nonce}}?test=x',
  204. ],
  205. 'http://localhost:80/grav/it/ueper?test=x' => [
  206. 'scheme' => 'http://',
  207. 'user' => null,
  208. 'password' => null,
  209. 'host' => 'localhost',
  210. 'port' => 80,
  211. 'path' => '/grav/it/ueper',
  212. 'query' => 'test=x',
  213. 'fragment' => null,
  214. 'route' => '/grav/it/ueper',
  215. 'paths' => ['grav', 'it', 'ueper'],
  216. 'params' => null,
  217. 'url' => '/grav/it/ueper',
  218. 'environment' => 'localhost',
  219. 'basename' => 'ueper',
  220. 'base' => 'http://localhost:80',
  221. 'currentPage' => 1,
  222. 'rootUrl' => 'http://localhost:80',
  223. 'extension' => null,
  224. 'addNonce' => 'http://localhost:80/grav/it/ueper/nonce:{{nonce}}?test=x',
  225. ],
  226. 'http://localhost/grav/it/ueper?test=x' => [
  227. 'scheme' => 'http://',
  228. 'user' => null,
  229. 'password' => null,
  230. 'host' => 'localhost',
  231. 'port' => 80,
  232. 'path' => '/grav/it/ueper',
  233. 'query' => 'test=x',
  234. 'fragment' => null,
  235. 'route' => '/grav/it/ueper',
  236. 'paths' => ['grav', 'it', 'ueper'],
  237. 'params' => null,
  238. 'url' => '/grav/it/ueper',
  239. 'environment' => 'localhost',
  240. 'basename' => 'ueper',
  241. 'base' => 'http://localhost',
  242. 'currentPage' => 1,
  243. 'rootUrl' => 'http://localhost',
  244. 'extension' => null,
  245. 'addNonce' => 'http://localhost/grav/it/ueper/nonce:{{nonce}}?test=x',
  246. ],
  247. 'http://grav/grav/it/ueper' => [
  248. 'scheme' => 'http://',
  249. 'user' => null,
  250. 'password' => null,
  251. 'host' => 'grav',
  252. 'port' => 80,
  253. 'path' => '/grav/it/ueper',
  254. 'query' => '',
  255. 'fragment' => null,
  256. 'route' => '/grav/it/ueper',
  257. 'paths' => ['grav', 'it', 'ueper'],
  258. 'params' => null,
  259. 'url' => '/grav/it/ueper',
  260. 'environment' => 'grav',
  261. 'basename' => 'ueper',
  262. 'base' => 'http://grav',
  263. 'currentPage' => 1,
  264. 'rootUrl' => 'http://grav',
  265. 'extension' => null,
  266. 'addNonce' => 'http://grav/grav/it/ueper/nonce:{{nonce}}',
  267. ],
  268. 'https://username:password@api.getgrav.com:4040/v1/post/128/page:x/?all=1' => [
  269. 'scheme' => 'https://',
  270. 'user' => 'username',
  271. 'password' => 'password',
  272. 'host' => 'api.getgrav.com',
  273. 'port' => 4040,
  274. 'path' => '/v1/post/128/', // FIXME <-
  275. 'query' => 'all=1',
  276. 'fragment' => null,
  277. 'route' => '/v1/post/128',
  278. 'paths' => ['v1', 'post', '128'],
  279. 'params' => '/page:x',
  280. 'url' => '/v1/post/128',
  281. 'environment' => 'api.getgrav.com',
  282. 'basename' => '128',
  283. 'base' => 'https://api.getgrav.com:4040',
  284. 'currentPage' => 'x',
  285. 'rootUrl' => 'https://api.getgrav.com:4040',
  286. 'extension' => null,
  287. 'addNonce' => 'https://username:password@api.getgrav.com:4040/v1/post/128/page:x/nonce:{{nonce}}?all=1',
  288. 'toOriginalString' => 'https://username:password@api.getgrav.com:4040/v1/post/128/page:x?all=1'
  289. ],
  290. 'https://google.com:443/' => [
  291. 'scheme' => 'https://',
  292. 'user' => null,
  293. 'password' => null,
  294. 'host' => 'google.com',
  295. 'port' => 443,
  296. 'path' => '/',
  297. 'query' => '',
  298. 'fragment' => null,
  299. 'route' => '/',
  300. 'paths' => [],
  301. 'params' => null,
  302. 'url' => '/',
  303. 'environment' => 'google.com',
  304. 'basename' => '',
  305. 'base' => 'https://google.com:443',
  306. 'currentPage' => 1,
  307. 'rootUrl' => 'https://google.com:443',
  308. 'extension' => null,
  309. 'addNonce' => 'https://google.com:443/nonce:{{nonce}}',
  310. ],
  311. // Path tests.
  312. 'http://localhost:8080/a/b/c/d' => [
  313. 'scheme' => 'http://',
  314. 'user' => null,
  315. 'password' => null,
  316. 'host' => 'localhost',
  317. 'port' => 8080,
  318. 'path' => '/a/b/c/d',
  319. 'query' => '',
  320. 'fragment' => null,
  321. 'route' => '/a/b/c/d',
  322. 'paths' => ['a', 'b', 'c', 'd'],
  323. 'params' => null,
  324. 'url' => '/a/b/c/d',
  325. 'environment' => 'localhost',
  326. 'basename' => 'd',
  327. 'base' => 'http://localhost:8080',
  328. 'currentPage' => 1,
  329. 'rootUrl' => 'http://localhost:8080',
  330. 'extension' => null,
  331. 'addNonce' => 'http://localhost:8080/a/b/c/d/nonce:{{nonce}}',
  332. ],
  333. 'http://localhost:8080/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f' => [
  334. 'scheme' => 'http://',
  335. 'user' => null,
  336. 'password' => null,
  337. 'host' => 'localhost',
  338. 'port' => 8080,
  339. 'path' => '/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f',
  340. 'query' => '',
  341. 'fragment' => null,
  342. 'route' => '/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f',
  343. 'paths' => ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f'],
  344. 'params' => null,
  345. 'url' => '/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f',
  346. 'environment' => 'localhost',
  347. 'basename' => 'f',
  348. 'base' => 'http://localhost:8080',
  349. 'currentPage' => 1,
  350. 'rootUrl' => 'http://localhost:8080',
  351. 'extension' => null,
  352. 'addNonce' => 'http://localhost:8080/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f/nonce:{{nonce}}',
  353. ],
  354. 'http://localhost/this is the path/my page' => [
  355. 'scheme' => 'http://',
  356. 'user' => null,
  357. 'password' => null,
  358. 'host' => 'localhost',
  359. 'port' => 80,
  360. 'path' => '/this%20is%20the%20path/my%20page',
  361. 'query' => '',
  362. 'fragment' => null,
  363. 'route' => '/this%20is%20the%20path/my%20page',
  364. 'paths' => ['this%20is%20the%20path', 'my%20page'],
  365. 'params' => null,
  366. 'url' => '/this%20is%20the%20path/my%20page',
  367. 'environment' => 'localhost',
  368. 'basename' => 'my%20page',
  369. 'base' => 'http://localhost',
  370. 'currentPage' => 1,
  371. 'rootUrl' => 'http://localhost',
  372. 'extension' => null,
  373. 'addNonce' => 'http://localhost/this%20is%20the%20path/my%20page/nonce:{{nonce}}',
  374. 'toOriginalString' => 'http://localhost/this%20is%20the%20path/my%20page'
  375. ],
  376. 'http://localhost/pölöpölö/päläpälä' => [
  377. 'scheme' => 'http://',
  378. 'user' => null,
  379. 'password' => null,
  380. 'host' => 'localhost',
  381. 'port' => 80,
  382. 'path' => '/p%C3%B6l%C3%B6p%C3%B6l%C3%B6/p%C3%A4l%C3%A4p%C3%A4l%C3%A4',
  383. 'query' => '',
  384. 'fragment' => null,
  385. 'route' => '/p%C3%B6l%C3%B6p%C3%B6l%C3%B6/p%C3%A4l%C3%A4p%C3%A4l%C3%A4',
  386. 'paths' => ['p%C3%B6l%C3%B6p%C3%B6l%C3%B6', 'p%C3%A4l%C3%A4p%C3%A4l%C3%A4'],
  387. 'params' => null,
  388. 'url' => '/p%C3%B6l%C3%B6p%C3%B6l%C3%B6/p%C3%A4l%C3%A4p%C3%A4l%C3%A4',
  389. 'environment' => 'localhost',
  390. 'basename' => 'p%C3%A4l%C3%A4p%C3%A4l%C3%A4',
  391. 'base' => 'http://localhost',
  392. 'currentPage' => 1,
  393. 'rootUrl' => 'http://localhost',
  394. 'extension' => null,
  395. 'addNonce' => 'http://localhost/p%C3%B6l%C3%B6p%C3%B6l%C3%B6/p%C3%A4l%C3%A4p%C3%A4l%C3%A4/nonce:{{nonce}}',
  396. 'toOriginalString' => 'http://localhost/p%C3%B6l%C3%B6p%C3%B6l%C3%B6/p%C3%A4l%C3%A4p%C3%A4l%C3%A4'
  397. ],
  398. // Query params tests.
  399. 'http://localhost:8080/grav/it/ueper?test=x&test2=y' => [
  400. 'scheme' => 'http://',
  401. 'user' => null,
  402. 'password' => null,
  403. 'host' => 'localhost',
  404. 'port' => 8080,
  405. 'path' => '/grav/it/ueper',
  406. 'query' => 'test=x&test2=y',
  407. 'fragment' => null,
  408. 'route' => '/grav/it/ueper',
  409. 'paths' => ['grav', 'it', 'ueper'],
  410. 'params' => null,
  411. 'url' => '/grav/it/ueper',
  412. 'environment' => 'localhost',
  413. 'basename' => 'ueper',
  414. 'base' => 'http://localhost:8080',
  415. 'currentPage' => 1,
  416. 'rootUrl' => 'http://localhost:8080',
  417. 'extension' => null,
  418. 'addNonce' => 'http://localhost:8080/grav/it/ueper/nonce:{{nonce}}?test=x&test2=y',
  419. ],
  420. 'http://localhost:8080/grav/it/ueper?test=x&test2=y&test3=x&test4=y' => [
  421. 'scheme' => 'http://',
  422. 'user' => null,
  423. 'password' => null,
  424. 'host' => 'localhost',
  425. 'port' => 8080,
  426. 'path' => '/grav/it/ueper',
  427. 'query' => 'test=x&test2=y&test3=x&test4=y',
  428. 'fragment' => null,
  429. 'route' => '/grav/it/ueper',
  430. 'paths' => ['grav', 'it', 'ueper'],
  431. 'params' => null,
  432. 'url' => '/grav/it/ueper',
  433. 'environment' => 'localhost',
  434. 'basename' => 'ueper',
  435. 'base' => 'http://localhost:8080',
  436. 'currentPage' => 1,
  437. 'rootUrl' => 'http://localhost:8080',
  438. 'extension' => null,
  439. 'addNonce' => 'http://localhost:8080/grav/it/ueper/nonce:{{nonce}}?test=x&test2=y&test3=x&test4=y',
  440. ],
  441. 'http://localhost:8080/grav/it/ueper?test=x&test2=y&test3=x&test4=y/test' => [
  442. 'scheme' => 'http://',
  443. 'user' => null,
  444. 'password' => null,
  445. 'host' => 'localhost',
  446. 'port' => 8080,
  447. 'path' => '/grav/it/ueper',
  448. 'query' => 'test=x&test2=y&test3=x&test4=y%2Ftest',
  449. 'fragment' => null,
  450. 'route' => '/grav/it/ueper',
  451. 'paths' => ['grav', 'it', 'ueper'],
  452. 'params' => null,
  453. 'url' => '/grav/it/ueper',
  454. 'environment' => 'localhost',
  455. 'basename' => 'ueper',
  456. 'base' => 'http://localhost:8080',
  457. 'currentPage' => 1,
  458. 'rootUrl' => 'http://localhost:8080',
  459. 'extension' => null,
  460. 'addNonce' => 'http://localhost:8080/grav/it/ueper/nonce:{{nonce}}?test=x&test2=y&test3=x&test4=y/test',
  461. ],
  462. // Port tests.
  463. 'http://localhost/a-page' => [
  464. 'scheme' => 'http://',
  465. 'user' => null,
  466. 'password' => null,
  467. 'host' => 'localhost',
  468. 'port' => 80,
  469. 'path' => '/a-page',
  470. 'query' => '',
  471. 'fragment' => null,
  472. 'route' => '/a-page',
  473. 'paths' => ['a-page'],
  474. 'params' => null,
  475. 'url' => '/a-page',
  476. 'environment' => 'localhost',
  477. 'basename' => 'a-page',
  478. 'base' => 'http://localhost',
  479. 'currentPage' => 1,
  480. 'rootUrl' => 'http://localhost',
  481. 'extension' => null,
  482. 'addNonce' => 'http://localhost/a-page/nonce:{{nonce}}',
  483. ],
  484. 'http://localhost:8080/a-page' => [
  485. 'scheme' => 'http://',
  486. 'user' => null,
  487. 'password' => null,
  488. 'host' => 'localhost',
  489. 'port' => 8080,
  490. 'path' => '/a-page',
  491. 'query' => '',
  492. 'fragment' => null,
  493. 'route' => '/a-page',
  494. 'paths' => ['a-page'],
  495. 'params' => null,
  496. 'url' => '/a-page',
  497. 'environment' => 'localhost',
  498. 'basename' => 'a-page',
  499. 'base' => 'http://localhost:8080',
  500. 'currentPage' => 1,
  501. 'rootUrl' => 'http://localhost:8080',
  502. 'extension' => null,
  503. 'addNonce' => 'http://localhost:8080/a-page/nonce:{{nonce}}',
  504. ],
  505. 'http://localhost:443/a-page' => [
  506. 'scheme' => 'http://',
  507. 'user' => null,
  508. 'password' => null,
  509. 'host' => 'localhost',
  510. 'port' => 443,
  511. 'path' => '/a-page',
  512. 'query' => '',
  513. 'fragment' => null,
  514. 'route' => '/a-page',
  515. 'paths' => ['a-page'],
  516. 'params' => null,
  517. 'url' => '/a-page',
  518. 'environment' => 'localhost',
  519. 'basename' => 'a-page',
  520. 'base' => 'http://localhost:443',
  521. 'currentPage' => 1,
  522. 'rootUrl' => 'http://localhost:443',
  523. 'extension' => null,
  524. 'addNonce' => 'http://localhost:443/a-page/nonce:{{nonce}}',
  525. ],
  526. // Extension tests.
  527. 'http://localhost/a-page.html' => [
  528. 'scheme' => 'http://',
  529. 'user' => null,
  530. 'password' => null,
  531. 'host' => 'localhost',
  532. 'port' => 80,
  533. 'path' => '/a-page',
  534. 'query' => '',
  535. 'fragment' => null,
  536. 'route' => '/a-page',
  537. 'paths' => ['a-page'],
  538. 'params' => null,
  539. 'url' => '/a-page',
  540. 'environment' => 'localhost',
  541. 'basename' => 'a-page.html',
  542. 'base' => 'http://localhost',
  543. 'currentPage' => 1,
  544. 'rootUrl' => 'http://localhost',
  545. 'extension' => 'html',
  546. 'addNonce' => 'http://localhost/a-page.html/nonce:{{nonce}}',
  547. 'toOriginalString' => 'http://localhost/a-page.html',
  548. ],
  549. 'http://localhost/a-page.json' => [
  550. 'scheme' => 'http://',
  551. 'user' => null,
  552. 'password' => null,
  553. 'host' => 'localhost',
  554. 'port' => 80,
  555. 'path' => '/a-page',
  556. 'query' => '',
  557. 'fragment' => null,
  558. 'route' => '/a-page',
  559. 'paths' => ['a-page'],
  560. 'params' => null,
  561. 'url' => '/a-page',
  562. 'environment' => 'localhost',
  563. 'basename' => 'a-page.json',
  564. 'base' => 'http://localhost',
  565. 'currentPage' => 1,
  566. 'rootUrl' => 'http://localhost',
  567. 'extension' => 'json',
  568. 'addNonce' => 'http://localhost/a-page.json/nonce:{{nonce}}',
  569. 'toOriginalString' => 'http://localhost/a-page.json',
  570. ],
  571. 'http://localhost/admin/ajax.json/task:getnewsfeed' => [
  572. 'scheme' => 'http://',
  573. 'user' => null,
  574. 'password' => null,
  575. 'host' => 'localhost',
  576. 'port' => 80,
  577. 'path' => '/admin/ajax',
  578. 'query' => '',
  579. 'fragment' => null,
  580. 'route' => '/admin/ajax',
  581. 'paths' => ['admin', 'ajax'],
  582. 'params' => '/task:getnewsfeed',
  583. 'url' => '/admin/ajax',
  584. 'environment' => 'localhost',
  585. 'basename' => 'ajax.json',
  586. 'base' => 'http://localhost',
  587. 'currentPage' => 1,
  588. 'rootUrl' => 'http://localhost',
  589. 'extension' => 'json',
  590. 'addNonce' => 'http://localhost/admin/ajax.json/task:getnewsfeed/nonce:{{nonce}}',
  591. 'toOriginalString' => 'http://localhost/admin/ajax.json/task:getnewsfeed',
  592. ],
  593. 'http://localhost/grav/admin/media.json/route:L1VzZXJzL3JodWsvd29ya3NwYWNlL2dyYXYtZGVtby1zYW1wbGVyL3VzZXIvYXNzZXRzL3FRMXB4Vk1ERTNJZzh5Ni5qcGc=/task:removeFileFromBlueprint/proute:/blueprint:Y29uZmlnL2RldGFpbHM=/type:config/field:deep.nested.custom_file/path:dXNlci9hc3NldHMvcVExcHhWTURFM0lnOHk2LmpwZw==' => [
  594. 'scheme' => 'http://',
  595. 'user' => null,
  596. 'password' => null,
  597. 'host' => 'localhost',
  598. 'port' => 80,
  599. 'path' => '/grav/admin/media',
  600. 'query' => '',
  601. 'fragment' => null,
  602. 'route' => '/grav/admin/media',
  603. 'paths' => ['grav','admin','media'],
  604. 'params' => '/route:L1VzZXJzL3JodWsvd29ya3NwYWNlL2dyYXYtZGVtby1zYW1wbGVyL3VzZXIvYXNzZXRzL3FRMXB4Vk1ERTNJZzh5Ni5qcGc=/task:removeFileFromBlueprint/proute:/blueprint:Y29uZmlnL2RldGFpbHM=/type:config/field:deep.nested.custom_file/path:dXNlci9hc3NldHMvcVExcHhWTURFM0lnOHk2LmpwZw==',
  605. 'url' => '/grav/admin/media',
  606. 'environment' => 'localhost',
  607. 'basename' => 'media.json',
  608. 'base' => 'http://localhost',
  609. 'currentPage' => 1,
  610. 'rootUrl' => 'http://localhost',
  611. 'extension' => 'json',
  612. 'addNonce' => 'http://localhost/grav/admin/media.json/route:L1VzZXJzL3JodWsvd29ya3NwYWNlL2dyYXYtZGVtby1zYW1wbGVyL3VzZXIvYXNzZXRzL3FRMXB4Vk1ERTNJZzh5Ni5qcGc=/task:removeFileFromBlueprint/proute:/blueprint:Y29uZmlnL2RldGFpbHM=/type:config/field:deep.nested.custom_file/path:dXNlci9hc3NldHMvcVExcHhWTURFM0lnOHk2LmpwZw==/nonce:{{nonce}}',
  613. 'toOriginalString' => 'http://localhost/grav/admin/media.json/route:L1VzZXJzL3JodWsvd29ya3NwYWNlL2dyYXYtZGVtby1zYW1wbGVyL3VzZXIvYXNzZXRzL3FRMXB4Vk1ERTNJZzh5Ni5qcGc=/task:removeFileFromBlueprint/proute:/blueprint:Y29uZmlnL2RldGFpbHM=/type:config/field:deep.nested.custom_file/path:dXNlci9hc3NldHMvcVExcHhWTURFM0lnOHk2LmpwZw==',
  614. ],
  615. 'http://localhost/a-page.foo' => [
  616. 'scheme' => 'http://',
  617. 'user' => null,
  618. 'password' => null,
  619. 'host' => 'localhost',
  620. 'port' => 80,
  621. 'path' => '/a-page.foo',
  622. 'query' => '',
  623. 'fragment' => null,
  624. 'route' => '/a-page.foo',
  625. 'paths' => ['a-page.foo'],
  626. 'params' => null,
  627. 'url' => '/a-page.foo',
  628. 'environment' => 'localhost',
  629. 'basename' => 'a-page.foo',
  630. 'base' => 'http://localhost',
  631. 'currentPage' => 1,
  632. 'rootUrl' => 'http://localhost',
  633. 'extension' => 'foo',
  634. 'addNonce' => 'http://localhost/a-page.foo/nonce:{{nonce}}',
  635. 'toOriginalString' => 'http://localhost/a-page.foo'
  636. ],
  637. // Fragment tests.
  638. 'http://localhost:8080/a/b/c#my-fragment' => [
  639. 'scheme' => 'http://',
  640. 'user' => null,
  641. 'password' => null,
  642. 'host' => 'localhost',
  643. 'port' => 8080,
  644. 'path' => '/a/b/c',
  645. 'query' => '',
  646. 'fragment' => 'my-fragment',
  647. 'route' => '/a/b/c',
  648. 'paths' => ['a', 'b', 'c'],
  649. 'params' => null,
  650. 'url' => '/a/b/c',
  651. 'environment' => 'localhost',
  652. 'basename' => 'c',
  653. 'base' => 'http://localhost:8080',
  654. 'currentPage' => 1,
  655. 'rootUrl' => 'http://localhost:8080',
  656. 'extension' => null,
  657. 'addNonce' => 'http://localhost:8080/a/b/c/nonce:{{nonce}}#my-fragment',
  658. ],
  659. // Attacks.
  660. '"><script>alert</script>://localhost' => [
  661. 'scheme' => '',
  662. 'user' => null,
  663. 'password' => null,
  664. 'host' => null,
  665. 'port' => null,
  666. 'path' => '%22%3E%3Cscript%3Ealert%3C/localhost',
  667. 'query' => '',
  668. 'fragment' => null,
  669. 'route' => '/%22%3E%3Cscript%3Ealert%3C/localhost',
  670. 'paths' => ['%22%3E%3Cscript%3Ealert%3C', 'localhost'],
  671. 'params' => '/script%3E:',
  672. 'url' => '%22%3E%3Cscript%3Ealert%3C//localhost',
  673. 'environment' => 'unknown',
  674. 'basename' => 'localhost',
  675. 'base' => '',
  676. 'currentPage' => 1,
  677. 'rootUrl' => '',
  678. 'extension' => null,
  679. //'addNonce' => '%22%3E%3Cscript%3Ealert%3C/localhost/script%3E:/nonce:{{nonce}}', // FIXME <-
  680. 'toOriginalString' => '%22%3E%3Cscript%3Ealert%3C/localhost/script%3E:' // FIXME <-
  681. ],
  682. 'http://"><script>alert</script>' => [
  683. 'scheme' => 'http://',
  684. 'user' => null,
  685. 'password' => null,
  686. 'host' => 'unknown',
  687. 'port' => 80,
  688. 'path' => '/script%3E',
  689. 'query' => '',
  690. 'fragment' => null,
  691. 'route' => '/script%3E',
  692. 'paths' => ['script%3E'],
  693. 'params' => null,
  694. 'url' => '/script%3E',
  695. 'environment' => 'unknown',
  696. 'basename' => 'script%3E',
  697. 'base' => 'http://unknown',
  698. 'currentPage' => 1,
  699. 'rootUrl' => 'http://unknown',
  700. 'extension' => null,
  701. 'addNonce' => 'http://unknown/script%3E/nonce:{{nonce}}',
  702. 'toOriginalString' => 'http://unknown/script%3E'
  703. ],
  704. 'http://localhost/"><script>alert</script>' => [
  705. 'scheme' => 'http://',
  706. 'user' => null,
  707. 'password' => null,
  708. 'host' => 'localhost',
  709. 'port' => 80,
  710. 'path' => '/%22%3E%3Cscript%3Ealert%3C/script%3E',
  711. 'query' => '',
  712. 'fragment' => null,
  713. 'route' => '/%22%3E%3Cscript%3Ealert%3C/script%3E',
  714. 'paths' => ['%22%3E%3Cscript%3Ealert%3C', 'script%3E'],
  715. 'params' => null,
  716. 'url' => '/%22%3E%3Cscript%3Ealert%3C/script%3E',
  717. 'environment' => 'localhost',
  718. 'basename' => 'script%3E',
  719. 'base' => 'http://localhost',
  720. 'currentPage' => 1,
  721. 'rootUrl' => 'http://localhost',
  722. 'extension' => null,
  723. 'addNonce' => 'http://localhost/%22%3E%3Cscript%3Ealert%3C/script%3E/nonce:{{nonce}}',
  724. 'toOriginalString' => 'http://localhost/%22%3E%3Cscript%3Ealert%3C/script%3E'
  725. ],
  726. 'http://localhost/something/p1:foo/p2:"><script>alert</script>' => [
  727. 'scheme' => 'http://',
  728. 'user' => null,
  729. 'password' => null,
  730. 'host' => 'localhost',
  731. 'port' => 80,
  732. 'path' => '/something/script%3E',
  733. 'query' => '',
  734. 'fragment' => null,
  735. 'route' => '/something/script%3E',
  736. 'paths' => ['something', 'script%3E'],
  737. 'params' => '/p1:foo/p2:%22%3E%3Cscript%3Ealert%3C',
  738. 'url' => '/something/script%3E',
  739. 'environment' => 'localhost',
  740. 'basename' => 'script%3E',
  741. 'base' => 'http://localhost',
  742. 'currentPage' => 1,
  743. 'rootUrl' => 'http://localhost',
  744. 'extension' => null,
  745. //'addNonce' => 'http://localhost/something/script%3E/p1:foo/p2:%22%3E%3Cscript%3Ealert%3C/nonce:{{nonce}}', // FIXME <-
  746. 'toOriginalString' => 'http://localhost/something/script%3E/p1:foo/p2:%22%3E%3Cscript%3Ealert%3C'
  747. ],
  748. 'http://localhost/something?p="><script>alert</script>' => [
  749. 'scheme' => 'http://',
  750. 'user' => null,
  751. 'password' => null,
  752. 'host' => 'localhost',
  753. 'port' => 80,
  754. 'path' => '/something',
  755. 'query' => 'p=%22%3E%3Cscript%3Ealert%3C%2Fscript%3E',
  756. 'fragment' => null,
  757. 'route' => '/something',
  758. 'paths' => ['something'],
  759. 'params' => null,
  760. 'url' => '/something',
  761. 'environment' => 'localhost',
  762. 'basename' => 'something',
  763. 'base' => 'http://localhost',
  764. 'currentPage' => 1,
  765. 'rootUrl' => 'http://localhost',
  766. 'extension' => null,
  767. 'addNonce' => 'http://localhost/something/nonce:{{nonce}}?p=%22%3E%3Cscript%3Ealert%3C/script%3E',
  768. 'toOriginalString' => 'http://localhost/something?p=%22%3E%3Cscript%3Ealert%3C/script%3E'
  769. ],
  770. 'http://localhost/something#"><script>alert</script>' => [
  771. 'scheme' => 'http://',
  772. 'user' => null,
  773. 'password' => null,
  774. 'host' => 'localhost',
  775. 'port' => 80,
  776. 'path' => '/something',
  777. 'query' => '',
  778. 'fragment' => '%22%3E%3Cscript%3Ealert%3C/script%3E',
  779. 'route' => '/something',
  780. 'paths' => ['something'],
  781. 'params' => null,
  782. 'url' => '/something',
  783. 'environment' => 'localhost',
  784. 'basename' => 'something',
  785. 'base' => 'http://localhost',
  786. 'currentPage' => 1,
  787. 'rootUrl' => 'http://localhost',
  788. 'extension' => null,
  789. 'addNonce' => 'http://localhost/something/nonce:{{nonce}}#%22%3E%3Cscript%3Ealert%3C/script%3E',
  790. 'toOriginalString' => 'http://localhost/something#%22%3E%3Cscript%3Ealert%3C/script%3E'
  791. ],
  792. 'https://www.getgrav.org/something/"><script>eval(atob("aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ=="))</script><' => [
  793. 'scheme' => 'https://',
  794. 'user' => null,
  795. 'password' => null,
  796. 'host' => 'www.getgrav.org',
  797. 'port' => 443,
  798. 'path' => '/something/%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C/script%3E%3C',
  799. 'query' => '',
  800. 'fragment' => null,
  801. 'route' => '/something/%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C/script%3E%3C',
  802. 'paths' => ['something', '%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C', 'script%3E%3C'],
  803. 'params' => null,
  804. 'url' => '/something/%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C/script%3E%3C',
  805. 'environment' => 'www.getgrav.org',
  806. 'basename' => 'script%3E%3C',
  807. 'base' => 'https://www.getgrav.org',
  808. 'currentPage' => 1,
  809. 'rootUrl' => 'https://www.getgrav.org',
  810. 'extension' => null,
  811. 'addNonce' => 'https://www.getgrav.org/something/%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C/script%3E%3C/nonce:{{nonce}}',
  812. 'toOriginalString' => 'https://www.getgrav.org/something/%22%3E%3Cscript%3Eeval%28atob%28%22aGlzdG9yeS5wdXNoU3RhdGUoJycsJycsJy8nKTskKCdoZWFkLGJvZHknKS5odG1sKCcnKS5sb2FkKCcvJyk7JC5wb3N0KCcvYWRtaW4nLGZ1bmN0aW9uKGRhdGEpeyQucG9zdCgkKGRhdGEpLmZpbmQoJ1tpZD1hZG1pbi11c2VyLWRldGFpbHNdIGEnKS5hdHRyKCdocmVmJykseydhZG1pbi1ub25jZSc6JChkYXRhKS5maW5kKCdbZGF0YS1jbGVhci1jYWNoZV0nKS5hdHRyKCdkYXRhLWNsZWFyLWNhY2hlJykuc3BsaXQoJzonKS5wb3AoKS50cmltKCksJ2RhdGFbcGFzc3dvcmRdJzonSW0zdjFsaDR4eDByJywndGFzayc6J3NhdmUnfSl9KQ==%22%29%29%3C/script%3E%3C'
  813. ],
  814. ];
  815. protected function _before()
  816. {
  817. $grav = Fixtures::get('grav');
  818. $this->grav = $grav();
  819. $this->uri = $this->grav['uri'];
  820. }
  821. protected function _after()
  822. {
  823. }
  824. protected function runTestSet(array $tests, $method, $params = [])
  825. {
  826. foreach ($tests as $url => $candidates) {
  827. if (!array_key_exists($method, $candidates) && $method !== 'toOriginalString') {
  828. continue;
  829. }
  830. if ($method === 'addNonce') {
  831. $nonce = Utils::getNonce('test-action');
  832. $expected = str_replace('{{nonce}}', $nonce, $candidates[$method]);
  833. $this->assertSame($expected, Uri::addNonce($url, 'test-action'));
  834. continue;
  835. }
  836. $this->uri->initializeWithURL($url)->init();
  837. if ($method === 'toOriginalString' && !isset($candidates[$method])) {
  838. $expected = $url;
  839. } else {
  840. $expected = $candidates[$method];
  841. }
  842. if ($params) {
  843. $result = call_user_func_array([$this->uri, $method], $params);
  844. } else {
  845. $result = $this->uri->{$method}();
  846. }
  847. $this->assertSame($expected, $result, "Test \$url->{$method}() for {$url}");
  848. // Deal with $url->query($key)
  849. if ($method === 'query') {
  850. parse_str($expected, $queryParams);
  851. foreach ($queryParams as $key => $value) {
  852. $this->assertSame($value, $this->uri->{$method}($key), "Test \$url->{$method}('{$key}') for {$url}");
  853. }
  854. $this->assertNull($this->uri->{$method}('non-existing'), "Test \$url->{$method}('non-existing') for {$url}");
  855. }
  856. }
  857. }
  858. public function testValidatingHostname()
  859. {
  860. $this->assertTrue($this->uri->validateHostname('localhost'));
  861. $this->assertTrue($this->uri->validateHostname('google.com'));
  862. $this->assertTrue($this->uri->validateHostname('google.it'));
  863. $this->assertTrue($this->uri->validateHostname('goog.le'));
  864. $this->assertTrue($this->uri->validateHostname('goog.wine'));
  865. $this->assertTrue($this->uri->validateHostname('goog.localhost'));
  866. $this->assertFalse($this->uri->validateHostname('localhost:80') );
  867. $this->assertFalse($this->uri->validateHostname('http://localhost'));
  868. $this->assertFalse($this->uri->validateHostname('localhost!'));
  869. }
  870. public function testToString()
  871. {
  872. $this->runTestSet($this->tests, 'toOriginalString');
  873. }
  874. public function testScheme()
  875. {
  876. $this->runTestSet($this->tests, 'scheme');
  877. }
  878. public function testUser()
  879. {
  880. $this->runTestSet($this->tests, 'user');
  881. }
  882. public function testPassword()
  883. {
  884. $this->runTestSet($this->tests, 'password');
  885. }
  886. public function testHost()
  887. {
  888. $this->runTestSet($this->tests, 'host');
  889. }
  890. public function testPort()
  891. {
  892. $this->runTestSet($this->tests, 'port');
  893. }
  894. public function testPath()
  895. {
  896. $this->runTestSet($this->tests, 'path');
  897. }
  898. public function testQuery()
  899. {
  900. $this->runTestSet($this->tests, 'query');
  901. }
  902. public function testFragment()
  903. {
  904. $this->runTestSet($this->tests, 'fragment');
  905. $this->uri->fragment('something-new');
  906. $this->assertSame('something-new', $this->uri->fragment());
  907. }
  908. public function testPaths()
  909. {
  910. $this->runTestSet($this->tests, 'paths');
  911. }
  912. public function testRoute()
  913. {
  914. $this->runTestSet($this->tests, 'route');
  915. }
  916. public function testParams()
  917. {
  918. $this->runTestSet($this->tests, 'params');
  919. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx')->init();
  920. $this->assertSame('/ueper:xxx', $this->uri->params('ueper'));
  921. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx/test:yyy')->init();
  922. $this->assertSame('/ueper:xxx', $this->uri->params('ueper'));
  923. $this->assertSame('/test:yyy', $this->uri->params('test'));
  924. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy')->init();
  925. $this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  926. $this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  927. $this->assertSame('/test:yyy', $this->uri->params('test'));
  928. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy#something')->init();
  929. $this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  930. $this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  931. $this->assertSame('/test:yyy', $this->uri->params('test'));
  932. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy?foo=bar')->init();
  933. $this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  934. $this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  935. $this->assertSame('/test:yyy', $this->uri->params('test'));
  936. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x')->init();
  937. $this->assertNull($this->uri->params());
  938. $this->assertNull($this->uri->params('ueper'));
  939. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x&test2=y')->init();
  940. $this->assertNull($this->uri->params());
  941. $this->assertNull($this->uri->params('ueper'));
  942. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x&test2=y&test3=x&test4=y')->init();
  943. $this->assertNull($this->uri->params());
  944. $this->assertNull($this->uri->params('ueper'));
  945. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x&test2=y&test3=x&test4=y/test')->init();
  946. $this->assertNull($this->uri->params());
  947. $this->assertNull($this->uri->params('ueper'));
  948. $this->uri->initializeWithURL('http://localhost:8080/a/b/c/d')->init();
  949. $this->assertNull($this->uri->params());
  950. $this->assertNull($this->uri->params('ueper'));
  951. $this->uri->initializeWithURL('http://localhost:8080/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f')->init();
  952. $this->assertNull($this->uri->params());
  953. $this->assertNull($this->uri->params('ueper'));
  954. }
  955. public function testParam()
  956. {
  957. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx')->init();
  958. $this->assertSame('xxx', $this->uri->param('ueper'));
  959. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx/test:yyy')->init();
  960. $this->assertSame('xxx', $this->uri->param('ueper'));
  961. $this->assertSame('yyy', $this->uri->param('test'));
  962. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yy%20y/foo:bar_baz-bank')->init();
  963. $this->assertSame('xxx++', $this->uri->param('ueper'));
  964. $this->assertSame('yy y', $this->uri->param('test'));
  965. $this->assertSame('bar_baz-bank', $this->uri->param('foo'));
  966. }
  967. public function testUrl()
  968. {
  969. $this->runTestSet($this->tests, 'url');
  970. }
  971. public function testExtension()
  972. {
  973. $this->runTestSet($this->tests, 'extension');
  974. $this->uri->initializeWithURL('http://localhost/a-page')->init();
  975. $this->assertSame('x', $this->uri->extension('x'));
  976. }
  977. public function testEnvironment()
  978. {
  979. $this->runTestSet($this->tests, 'environment');
  980. }
  981. public function testBasename()
  982. {
  983. $this->runTestSet($this->tests, 'basename');
  984. }
  985. public function testBase()
  986. {
  987. $this->runTestSet($this->tests, 'base');
  988. }
  989. public function testRootUrl()
  990. {
  991. $this->runTestSet($this->tests, 'rootUrl', [true]);
  992. $this->uri->initializeWithUrlAndRootPath('https://localhost/grav/page-foo', '/grav')->init();
  993. $this->assertSame('/grav', $this->uri->rootUrl());
  994. $this->assertSame('https://localhost/grav', $this->uri->rootUrl(true));
  995. }
  996. public function testCurrentPage()
  997. {
  998. $this->runTestSet($this->tests, 'currentPage');
  999. $this->uri->initializeWithURL('http://localhost:8080/a-page/page:2')->init();
  1000. $this->assertSame('2', $this->uri->currentPage());
  1001. }
  1002. public function testReferrer()
  1003. {
  1004. $this->uri->initializeWithURL('http://localhost/foo/page:test')->init();
  1005. $this->assertSame('/foo', $this->uri->referrer());
  1006. $this->uri->initializeWithURL('http://localhost/foo/bar/page:test')->init();
  1007. $this->assertSame('/foo/bar', $this->uri->referrer());
  1008. }
  1009. public function testIp()
  1010. {
  1011. $this->uri->initializeWithURL('http://localhost/foo/page:test')->init();
  1012. $this->assertSame('UNKNOWN', Uri::ip());
  1013. }
  1014. public function testIsExternal()
  1015. {
  1016. $this->uri->initializeWithURL('http://localhost/')->init();
  1017. $this->assertFalse(Uri::isExternal('/test'));
  1018. $this->assertFalse(Uri::isExternal('/foo/bar'));
  1019. $this->assertTrue(Uri::isExternal('http://localhost/test'));
  1020. $this->assertTrue(Uri::isExternal('http://google.it/test'));
  1021. }
  1022. public function testBuildUrl()
  1023. {
  1024. $parsed_url = [
  1025. 'scheme' => 'http',
  1026. 'host' => 'localhost',
  1027. 'port' => 8080,
  1028. ];
  1029. $this->assertSame('http://localhost:8080', Uri::buildUrl($parsed_url));
  1030. $parsed_url = [
  1031. 'scheme' => 'http',
  1032. 'host' => 'localhost',
  1033. 'port' => 8080,
  1034. 'user' => 'foo',
  1035. 'pass' => 'bar',
  1036. 'path' => '/test',
  1037. 'query' => 'x=2',
  1038. 'fragment' => 'xxx',
  1039. ];
  1040. $this->assertSame('http://foo:bar@localhost:8080/test?x=2#xxx', Uri::buildUrl($parsed_url));
  1041. /** @var Uri $uri */
  1042. $uri = Grav::instance()['uri'];
  1043. $uri->initializeWithUrlAndRootPath('https://testing.dev/subdir/path1/path2/file.html', '/subdir')->init();
  1044. $this->assertSame('https://testing.dev/subdir/path1/path2/file.html', Uri::buildUrl($uri->toArray(true)));
  1045. $uri->initializeWithUrlAndRootPath('https://testing.dev/subdir/path1/path2/file.foo', '/subdir')->init();
  1046. $this->assertSame('https://testing.dev/subdir/path1/path2/file.foo', Uri::buildUrl($uri->toArray(true)));
  1047. $uri->initializeWithUrlAndRootPath('https://testing.dev/subdir/path1/path2/file.html', '/subdir/path1')->init();
  1048. $this->assertSame('https://testing.dev/subdir/path1/path2/file.html', Uri::buildUrl($uri->toArray(true)));
  1049. $uri->initializeWithUrlAndRootPath('https://testing.dev/subdir/path1/path2/file.html/foo:blah/bang:boom', '/subdir')->init();
  1050. $this->assertSame('https://testing.dev/subdir/path1/path2/file.html/foo:blah/bang:boom', Uri::buildUrl($uri->toArray(true)));
  1051. $uri->initializeWithUrlAndRootPath('https://testing.dev/subdir/path1/path2/file.html/foo:blah/bang:boom?fig=something', '/subdir')->init();
  1052. $this->assertSame('https://testing.dev/subdir/path1/path2/file.html/foo:blah/bang:boom?fig=something', Uri::buildUrl($uri->toArray(true)));
  1053. }
  1054. public function testConvertUrl()
  1055. {
  1056. }
  1057. public function testAddNonce()
  1058. {
  1059. $this->runTestSet($this->tests, 'addNonce');
  1060. }
  1061. }