| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Utils; |
| 4 |
|
| 5 |
use Wikimedia\Parsoid\Utils\UrlUtils; |
| 6 |
|
| 7 |
/** |
| 8 |
* @coversDefaultClass \Wikimedia\Parsoid\Utils\UrlUtils |
| 9 |
*/ |
| 10 |
class UrlUtilsTest extends \PHPUnit\Framework\TestCase { |
| 11 |
|
| 12 |
/** |
| 13 |
* @covers ::parseUrl |
| 14 |
* @dataProvider provideParsedUrl |
| 15 |
*/ |
| 16 |
public function testParseUrl( string $url, array $parsed ) { |
| 17 |
$parsed += [ |
| 18 |
'scheme' => null, |
| 19 |
'authority' => null, |
| 20 |
'query' => null, |
| 21 |
'fragment' => null, |
| 22 |
]; |
| 23 |
$this->assertEquals( $parsed, UrlUtils::parseUrl( $url ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @covers ::assembleUrl |
| 28 |
* @dataProvider provideParsedUrl |
| 29 |
*/ |
| 30 |
public function testAssembleUrl( string $url, array $parsed ) { |
| 31 |
$this->assertEquals( $url, UrlUtils::assembleUrl( $parsed ) ); |
| 32 |
} |
| 33 |
|
| 34 |
public function provideParsedUrl(): array { |
| 35 |
return [ |
| 36 |
'Full URL' => [ |
| 37 |
'http://[email protected]/some/path?que/ry#fra/gme?nt', |
| 38 |
[ |
| 39 |
'scheme' => 'http', |
| 40 |
'authority' => '[email protected]', |
| 41 |
'path' => '/some/path', |
| 42 |
'query' => 'que/ry', |
| 43 |
'fragment' => 'fra/gme?nt', |
| 44 |
], |
| 45 |
], |
| 46 |
'Full URL, no fragment' => [ |
| 47 |
'http://[email protected]/some/path?que/ry', |
| 48 |
[ |
| 49 |
'scheme' => 'http', |
| 50 |
'authority' => '[email protected]', |
| 51 |
'path' => '/some/path', |
| 52 |
'query' => 'que/ry', |
| 53 |
], |
| 54 |
], |
| 55 |
'Full URL, no query' => [ |
| 56 |
'http://[email protected]/some/path#fra/gme?nt', |
| 57 |
[ |
| 58 |
'scheme' => 'http', |
| 59 |
'authority' => '[email protected]', |
| 60 |
'path' => '/some/path', |
| 61 |
'fragment' => 'fra/gme?nt', |
| 62 |
], |
| 63 |
], |
| 64 |
'Full URL, no path' => [ |
| 65 |
'http://[email protected]?que/ry#fra/gme?nt', |
| 66 |
[ |
| 67 |
'scheme' => 'http', |
| 68 |
'authority' => '[email protected]', |
| 69 |
'path' => '', |
| 70 |
'query' => 'que/ry', |
| 71 |
'fragment' => 'fra/gme?nt', |
| 72 |
], |
| 73 |
], |
| 74 |
'Full URL, no authority' => [ |
| 75 |
'mailto:[email protected]?que/ry#fra/gme?nt', |
| 76 |
[ |
| 77 |
'scheme' => 'mailto', |
| 78 |
'path' => '[email protected]', |
| 79 |
'query' => 'que/ry', |
| 80 |
'fragment' => 'fra/gme?nt', |
| 81 |
], |
| 82 |
], |
| 83 |
'Full URL, empty authority' => [ |
| 84 |
'file:///some/path?que/ry#fra/gme?nt', |
| 85 |
[ |
| 86 |
'scheme' => 'file', |
| 87 |
'authority' => '', |
| 88 |
'path' => '/some/path', |
| 89 |
'query' => 'que/ry', |
| 90 |
'fragment' => 'fra/gme?nt', |
| 91 |
], |
| 92 |
], |
| 93 |
'Protocol-relative URL' => [ |
| 94 |
'//[email protected]/some/path?que/ry#fra/gme?nt', |
| 95 |
[ |
| 96 |
'authority' => '[email protected]', |
| 97 |
'path' => '/some/path', |
| 98 |
'query' => 'que/ry', |
| 99 |
'fragment' => 'fra/gme?nt', |
| 100 |
], |
| 101 |
], |
| 102 |
'Path-absolute relative URL' => [ |
| 103 |
'/some/path?que/ry#fra/gme?nt', |
| 104 |
[ |
| 105 |
'path' => '/some/path', |
| 106 |
'query' => 'que/ry', |
| 107 |
'fragment' => 'fra/gme?nt', |
| 108 |
], |
| 109 |
], |
| 110 |
'Path-relative URL' => [ |
| 111 |
'some/path?que/ry#fra/gme?nt', |
| 112 |
[ |
| 113 |
'path' => 'some/path', |
| 114 |
'query' => 'que/ry', |
| 115 |
'fragment' => 'fra/gme?nt', |
| 116 |
], |
| 117 |
], |
| 118 |
'Minimal URL' => [ |
| 119 |
'//example.com', |
| 120 |
[ |
| 121 |
'authority' => 'example.com', |
| 122 |
'path' => '', |
| 123 |
], |
| 124 |
], |
| 125 |
'Fragment only' => [ |
| 126 |
'#fra/gme?nt', |
| 127 |
[ |
| 128 |
'path' => '', |
| 129 |
'fragment' => 'fra/gme?nt', |
| 130 |
], |
| 131 |
], |
| 132 |
'Empty path' => [ |
| 133 |
'?que/ry#fra/gme?nt', |
| 134 |
[ |
| 135 |
'path' => '', |
| 136 |
'query' => 'que/ry', |
| 137 |
'fragment' => 'fra/gme?nt', |
| 138 |
], |
| 139 |
], |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @covers ::removeDotSegments |
| 145 |
* @dataProvider provideRemoveDotSegments |
| 146 |
*/ |
| 147 |
public function testRemoveDotSegments( $path, $expect ) { |
| 148 |
$this->assertEquals( $expect, UrlUtils::removeDotSegments( $path ) ); |
| 149 |
} |
| 150 |
|
| 151 |
public function provideRemoveDotSegments() { |
| 152 |
return [ |
| 153 |
[ '/a/b/c/./../../g', '/a/g' ], |
| 154 |
[ 'mid/content=5/../6', 'mid/6' ], |
| 155 |
[ '/a//../b', '/a/b' ], |
| 156 |
[ '/.../a', '/.../a' ], |
| 157 |
[ '.../a', '.../a' ], |
| 158 |
[ '', '' ], |
| 159 |
[ '/', '/' ], |
| 160 |
[ '//', '//' ], |
| 161 |
[ '.', '' ], |
| 162 |
[ '..', '' ], |
| 163 |
[ '...', '...' ], |
| 164 |
[ '/.', '/' ], |
| 165 |
[ '/..', '/' ], |
| 166 |
[ './', '' ], |
| 167 |
[ '../', '' ], |
| 168 |
[ './a', 'a' ], |
| 169 |
[ '../a', 'a' ], |
| 170 |
[ '../../a', 'a' ], |
| 171 |
[ '.././a', 'a' ], |
| 172 |
[ './../a', 'a' ], |
| 173 |
[ '././a', 'a' ], |
| 174 |
[ '../../', '' ], |
| 175 |
[ '.././', '' ], |
| 176 |
[ './../', '' ], |
| 177 |
[ '././', '' ], |
| 178 |
[ '../..', '' ], |
| 179 |
[ '../.', '' ], |
| 180 |
[ './..', '' ], |
| 181 |
[ './.', '' ], |
| 182 |
[ '/../../a', '/a' ], |
| 183 |
[ '/.././a', '/a' ], |
| 184 |
[ '/./../a', '/a' ], |
| 185 |
[ '/././a', '/a' ], |
| 186 |
[ '/../../', '/' ], |
| 187 |
[ '/.././', '/' ], |
| 188 |
[ '/./../', '/' ], |
| 189 |
[ '/././', '/' ], |
| 190 |
[ '/../..', '/' ], |
| 191 |
[ '/../.', '/' ], |
| 192 |
[ '/./..', '/' ], |
| 193 |
[ '/./.', '/' ], |
| 194 |
[ 'b/../../a', '/a' ], |
| 195 |
[ 'b/.././a', '/a' ], |
| 196 |
[ 'b/./../a', '/a' ], |
| 197 |
[ 'b/././a', 'b/a' ], |
| 198 |
[ 'b/../../', '/' ], |
| 199 |
[ 'b/.././', '/' ], |
| 200 |
[ 'b/./../', '/' ], |
| 201 |
[ 'b/././', 'b/' ], |
| 202 |
[ 'b/../..', '/' ], |
| 203 |
[ 'b/../.', '/' ], |
| 204 |
[ 'b/./..', '/' ], |
| 205 |
[ 'b/./.', 'b/' ], |
| 206 |
[ '/b/../../a', '/a' ], |
| 207 |
[ '/b/.././a', '/a' ], |
| 208 |
[ '/b/./../a', '/a' ], |
| 209 |
[ '/b/././a', '/b/a' ], |
| 210 |
[ '/b/../../', '/' ], |
| 211 |
[ '/b/.././', '/' ], |
| 212 |
[ '/b/./../', '/' ], |
| 213 |
[ '/b/././', '/b/' ], |
| 214 |
[ '/b/../..', '/' ], |
| 215 |
[ '/b/../.', '/' ], |
| 216 |
[ '/b/./..', '/' ], |
| 217 |
[ '/b/./.', '/b/' ], |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* @covers ::expandUrl |
| 223 |
* @dataProvider provideExpandUrl |
| 224 |
*/ |
| 225 |
public function testExpandUrl( $url, $base, $expect ) { |
| 226 |
$this->assertSame( $expect, UrlUtils::expandUrl( $url, $base ) ); |
| 227 |
} |
| 228 |
|
| 229 |
public function provideExpandUrl() { |
| 230 |
return [ |
| 231 |
[ 'g:h', 'http://a/b/c/d;p?q', 'g:h' ], |
| 232 |
[ 'g', 'http://a/b/c/d;p?q', 'http://a/b/c/g' ], |
| 233 |
[ './g', 'http://a/b/c/d;p?q', 'http://a/b/c/g' ], |
| 234 |
[ 'g/', 'http://a/b/c/d;p?q', 'http://a/b/c/g/' ], |
| 235 |
[ '/g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 236 |
[ '//g', 'http://a/b/c/d;p?q', 'http://g' ], |
| 237 |
[ '?y', 'http://a/b/c/d;p?q', 'http://a/b/c/d;p?y' ], |
| 238 |
[ 'g?y', 'http://a/b/c/d;p?q', 'http://a/b/c/g?y' ], |
| 239 |
[ '#s', 'http://a/b/c/d;p?q', 'http://a/b/c/d;p?q#s' ], |
| 240 |
[ 'g#s', 'http://a/b/c/d;p?q', 'http://a/b/c/g#s' ], |
| 241 |
[ 'g?y#s', 'http://a/b/c/d;p?q', 'http://a/b/c/g?y#s' ], |
| 242 |
[ ';x', 'http://a/b/c/d;p?q', 'http://a/b/c/;x' ], |
| 243 |
[ 'g;x', 'http://a/b/c/d;p?q', 'http://a/b/c/g;x' ], |
| 244 |
[ 'g;x?y#s', 'http://a/b/c/d;p?q', 'http://a/b/c/g;x?y#s' ], |
| 245 |
[ '', 'http://a/b/c/d;p?q', 'http://a/b/c/d;p?q' ], |
| 246 |
[ '.', 'http://a/b/c/d;p?q', 'http://a/b/c/' ], |
| 247 |
[ './', 'http://a/b/c/d;p?q', 'http://a/b/c/' ], |
| 248 |
[ '..', 'http://a/b/c/d;p?q', 'http://a/b/' ], |
| 249 |
[ '../', 'http://a/b/c/d;p?q', 'http://a/b/' ], |
| 250 |
[ '../g', 'http://a/b/c/d;p?q', 'http://a/b/g' ], |
| 251 |
[ '../..', 'http://a/b/c/d;p?q', 'http://a/' ], |
| 252 |
[ '../../', 'http://a/b/c/d;p?q', 'http://a/' ], |
| 253 |
[ '../../g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 254 |
[ '../../../g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 255 |
[ '../../../../g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 256 |
[ '/./g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 257 |
[ '/../g', 'http://a/b/c/d;p?q', 'http://a/g' ], |
| 258 |
[ 'g.', 'http://a/b/c/d;p?q', 'http://a/b/c/g.' ], |
| 259 |
[ '.g', 'http://a/b/c/d;p?q', 'http://a/b/c/.g' ], |
| 260 |
[ 'g..', 'http://a/b/c/d;p?q', 'http://a/b/c/g..' ], |
| 261 |
[ '..g', 'http://a/b/c/d;p?q', 'http://a/b/c/..g' ], |
| 262 |
[ './../g', 'http://a/b/c/d;p?q', 'http://a/b/g' ], |
| 263 |
[ './g/.', 'http://a/b/c/d;p?q', 'http://a/b/c/g/' ], |
| 264 |
[ 'g/./h', 'http://a/b/c/d;p?q', 'http://a/b/c/g/h' ], |
| 265 |
[ 'g/../h', 'http://a/b/c/d;p?q', 'http://a/b/c/h' ], |
| 266 |
[ 'g;x=1/./y', 'http://a/b/c/d;p?q', 'http://a/b/c/g;x=1/y' ], |
| 267 |
[ 'g;x=1/../y', 'http://a/b/c/d;p?q', 'http://a/b/c/y' ], |
| 268 |
[ 'g?y/./x', 'http://a/b/c/d;p?q', 'http://a/b/c/g?y/./x' ], |
| 269 |
[ 'g?y/../x', 'http://a/b/c/d;p?q', 'http://a/b/c/g?y/../x' ], |
| 270 |
[ 'g#s/./x', 'http://a/b/c/d;p?q', 'http://a/b/c/g#s/./x' ], |
| 271 |
[ 'g#s/../x', 'http://a/b/c/d;p?q', 'http://a/b/c/g#s/../x' ], |
| 272 |
[ 'http:g', 'http://a/b/c/d;p?q', 'http:g' ], |
| 273 |
[ 'foo/bar', 'http://example.com', 'http://example.com/foo/bar' ], |
| 274 |
[ 'foo/bar', 'a:xyz', 'a:foo/bar' ], |
| 275 |
]; |
| 276 |
} |
| 277 |
|
| 278 |
} |