UriTest.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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' => 1,
  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' => 1,
  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(): void
  816. {
  817. $grav = Fixtures::get('grav');
  818. $this->grav = $grav();
  819. $this->uri = $this->grav['uri'];
  820. }
  821. protected function _after(): void
  822. {
  823. }
  824. protected function runTestSet(array $tests, $method, $params = []): void
  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. self::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. self::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. self::assertSame($value, $this->uri->{$method}($key), "Test \$url->{$method}('{$key}') for {$url}");
  853. }
  854. self::assertNull($this->uri->{$method}('non-existing'), "Test \$url->{$method}('non-existing') for {$url}");
  855. }
  856. }
  857. }
  858. public function testValidatingHostname(): void
  859. {
  860. self::assertTrue($this->uri->validateHostname('localhost'));
  861. self::assertTrue($this->uri->validateHostname('google.com'));
  862. self::assertTrue($this->uri->validateHostname('google.it'));
  863. self::assertTrue($this->uri->validateHostname('goog.le'));
  864. self::assertTrue($this->uri->validateHostname('goog.wine'));
  865. self::assertTrue($this->uri->validateHostname('goog.localhost'));
  866. self::assertFalse($this->uri->validateHostname('localhost:80'));
  867. self::assertFalse($this->uri->validateHostname('http://localhost'));
  868. self::assertFalse($this->uri->validateHostname('localhost!'));
  869. }
  870. public function testToString(): void
  871. {
  872. $this->runTestSet($this->tests, 'toOriginalString');
  873. }
  874. public function testScheme(): void
  875. {
  876. $this->runTestSet($this->tests, 'scheme');
  877. }
  878. public function testUser(): void
  879. {
  880. $this->runTestSet($this->tests, 'user');
  881. }
  882. public function testPassword(): void
  883. {
  884. $this->runTestSet($this->tests, 'password');
  885. }
  886. public function testHost(): void
  887. {
  888. $this->runTestSet($this->tests, 'host');
  889. }
  890. public function testPort(): void
  891. {
  892. $this->runTestSet($this->tests, 'port');
  893. }
  894. public function testPath(): void
  895. {
  896. $this->runTestSet($this->tests, 'path');
  897. }
  898. public function testQuery(): void
  899. {
  900. $this->runTestSet($this->tests, 'query');
  901. }
  902. public function testFragment(): void
  903. {
  904. $this->runTestSet($this->tests, 'fragment');
  905. $this->uri->fragment('something-new');
  906. self::assertSame('something-new', $this->uri->fragment());
  907. }
  908. public function testPaths(): void
  909. {
  910. $this->runTestSet($this->tests, 'paths');
  911. }
  912. public function testRoute(): void
  913. {
  914. $this->runTestSet($this->tests, 'route');
  915. }
  916. public function testParams(): void
  917. {
  918. $this->runTestSet($this->tests, 'params');
  919. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx')->init();
  920. self::assertSame('/ueper:xxx', $this->uri->params('ueper'));
  921. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx/test:yyy')->init();
  922. self::assertSame('/ueper:xxx', $this->uri->params('ueper'));
  923. self::assertSame('/test:yyy', $this->uri->params('test'));
  924. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy')->init();
  925. self::assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  926. self::assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  927. self::assertSame('/test:yyy', $this->uri->params('test'));
  928. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy#something')->init();
  929. self::assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  930. self::assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  931. self::assertSame('/test:yyy', $this->uri->params('test'));
  932. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy?foo=bar')->init();
  933. self::assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
  934. self::assertSame('/ueper:xxx++', $this->uri->params('ueper'));
  935. self::assertSame('/test:yyy', $this->uri->params('test'));
  936. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x')->init();
  937. self::assertNull($this->uri->params());
  938. self::assertNull($this->uri->params('ueper'));
  939. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x&test2=y')->init();
  940. self::assertNull($this->uri->params());
  941. self::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. self::assertNull($this->uri->params());
  944. self::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. self::assertNull($this->uri->params());
  947. self::assertNull($this->uri->params('ueper'));
  948. $this->uri->initializeWithURL('http://localhost:8080/a/b/c/d')->init();
  949. self::assertNull($this->uri->params());
  950. self::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. self::assertNull($this->uri->params());
  953. self::assertNull($this->uri->params('ueper'));
  954. }
  955. public function testParam(): void
  956. {
  957. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx')->init();
  958. self::assertSame('xxx', $this->uri->param('ueper'));
  959. $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx/test:yyy')->init();
  960. self::assertSame('xxx', $this->uri->param('ueper'));
  961. self::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. self::assertSame('xxx++', $this->uri->param('ueper'));
  964. self::assertSame('yy y', $this->uri->param('test'));
  965. self::assertSame('bar_baz-bank', $this->uri->param('foo'));
  966. }
  967. public function testUrl(): void
  968. {
  969. $this->runTestSet($this->tests, 'url');
  970. }
  971. public function testExtension(): void
  972. {
  973. $this->runTestSet($this->tests, 'extension');
  974. $this->uri->initializeWithURL('http://localhost/a-page')->init();
  975. self::assertSame('x', $this->uri->extension('x'));
  976. }
  977. public function testEnvironment(): void
  978. {
  979. $this->runTestSet($this->tests, 'environment');
  980. }
  981. public function testBasename(): void
  982. {
  983. $this->runTestSet($this->tests, 'basename');
  984. }
  985. public function testBase(): void
  986. {
  987. $this->runTestSet($this->tests, 'base');
  988. }
  989. public function testRootUrl(): void
  990. {
  991. $this->runTestSet($this->tests, 'rootUrl', [true]);
  992. $this->uri->initializeWithUrlAndRootPath('https://localhost/grav/page-foo', '/grav')->init();
  993. self::assertSame('/grav', $this->uri->rootUrl());
  994. self::assertSame('https://localhost/grav', $this->uri->rootUrl(true));
  995. }
  996. public function testCurrentPage(): void
  997. {
  998. $this->runTestSet($this->tests, 'currentPage');
  999. $this->uri->initializeWithURL('http://localhost:8080/a-page/page:2')->init();
  1000. self::assertSame(2, $this->uri->currentPage());
  1001. }
  1002. public function testReferrer(): void
  1003. {
  1004. $this->uri->initializeWithURL('http://localhost/foo/page:test')->init();
  1005. self::assertSame('/foo', $this->uri->referrer());
  1006. $this->uri->initializeWithURL('http://localhost/foo/bar/page:test')->init();
  1007. self::assertSame('/foo/bar', $this->uri->referrer());
  1008. }
  1009. public function testIp(): void
  1010. {
  1011. $this->uri->initializeWithURL('http://localhost/foo/page:test')->init();
  1012. self::assertSame('UNKNOWN', Uri::ip());
  1013. }
  1014. public function testIsExternal(): void
  1015. {
  1016. $this->uri->initializeWithURL('http://localhost/')->init();
  1017. self::assertFalse(Uri::isExternal('/test'));
  1018. self::assertFalse(Uri::isExternal('/foo/bar'));
  1019. self::assertTrue(Uri::isExternal('http://localhost/test'));
  1020. self::assertTrue(Uri::isExternal('http://google.it/test'));
  1021. }
  1022. public function testBuildUrl(): void
  1023. {
  1024. $parsed_url = [
  1025. 'scheme' => 'http',
  1026. 'host' => 'localhost',
  1027. 'port' => 8080,
  1028. ];
  1029. self::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. self::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. self::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. self::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. self::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. self::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. self::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(): void
  1055. {
  1056. }
  1057. public function testAddNonce(): void
  1058. {
  1059. $this->runTestSet($this->tests, 'addNonce');
  1060. }
  1061. }