| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\HeaderParser; |
| 4 |
|
| 5 |
use MediaWiki\Rest\HeaderParser\HeaderParserError; |
| 6 |
use MediaWiki\Rest\HeaderParser\Origin; |
| 7 |
use MediaWikiUnitTestCase; |
| 8 |
use Wikimedia\Assert\PreconditionException; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class OriginTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
public function testNullHeader() { |
| 16 |
$header = Origin::parseHeaderList( [ 'null' ] ); |
| 17 |
$this->assertTrue( $header->isNullOrigin() ); |
| 18 |
$this->assertFalse( $header->isMultiOrigin() ); |
| 19 |
$this->assertArrayEquals( [], $header->getOriginList() ); |
| 20 |
} |
| 21 |
|
| 22 |
public function testMultipleHeaders() { |
| 23 |
$this->expectException( HeaderParserError::class ); |
| 24 |
Origin::parseHeaderList( [ 'null', 'null' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
public function testGetSingleOrigin() { |
| 28 |
$this->assertSame( 'https://en.wikipedia.org', |
| 29 |
Origin::parseHeaderList( [ 'https://en.wikipedia.org' ] )->getSingleOrigin() ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testGetSingleOriginThrows() { |
| 33 |
$this->expectException( PreconditionException::class ); |
| 34 |
Origin::parseHeaderList( [ 'https://en.wikipedia.org https://ru.wikipedia.org' ] ) |
| 35 |
->getSingleOrigin(); |
| 36 |
} |
| 37 |
|
| 38 |
public static function provideOriginParse() { |
| 39 |
yield 'Single origin' => |
| 40 |
[ 'https://en.wikipedia.org', [ 'https://en.wikipedia.org' ], false ]; |
| 41 |
yield 'Multiple origins' => |
| 42 |
[ 'https://en.wikipedia.org https://foo.bar', [ 'https://en.wikipedia.org', 'https://foo.bar' ], true ]; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testOriginParse( string $header, array $originList, bool $isMulti ) { |
| 49 |
$header = Origin::parseHeaderList( [ $header ] ); |
| 50 |
$this->assertArrayEquals( $originList, $header->getOriginList() ); |
| 51 |
$this->assertSame( $isMulti, $header->isMultiOrigin() ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function provideMatch() { |
| 55 |
yield 'null origin' => [ 'null', [ 'null' ], [], false ]; |
| 56 |
yield 'empty allow list' => [ 'https://en.wikipedia.org', [], [], false ]; |
| 57 |
yield 'empty exclude list' => [ 'https://en.wikipedia.org', [ 'en.wikipedia.org' ], [], true ]; |
| 58 |
yield 'single origin, match' => [ 'https://en.wikipedia.org', [ 'en.wikipedia.org' ], [ 'example.com' ], true ]; |
| 59 |
yield 'single origin, exclude no match' => |
| 60 |
[ |
| 61 |
'https://en.wikipedia.org', |
| 62 |
[ 'en.wikipedia.org' ], |
| 63 |
[ 'en.wikipedia.org' ], |
| 64 |
false, |
| 65 |
]; |
| 66 |
yield 'single origin, * match' => |
| 67 |
[ |
| 68 |
'https://en.wikipedia.org', |
| 69 |
[ '*.wikipedia.org' ], |
| 70 |
[ '*.example.com' ], |
| 71 |
true, |
| 72 |
]; |
| 73 |
yield 'single origin, exclude * no match' => |
| 74 |
[ |
| 75 |
'https://en.wikipedia.org', |
| 76 |
[ '*.wikipedia.org' ], |
| 77 |
[ '*.wikipedia.org' ], |
| 78 |
false, |
| 79 |
]; |
| 80 |
yield 'single origin, ? match' => |
| 81 |
[ |
| 82 |
'https://en.wikipedia.org', |
| 83 |
[ '??.wikipedia.org' ], |
| 84 |
[ '??.example.com' ], |
| 85 |
true, |
| 86 |
]; |
| 87 |
yield 'single origin, exclude ? no match' => |
| 88 |
[ |
| 89 |
'https://en.wikipedia.org', |
| 90 |
[ '??.wikipedia.org' ], |
| 91 |
[ '??.wikipedia.org' ], |
| 92 |
false, |
| 93 |
]; |
| 94 |
yield 'single origin, no match' => |
| 95 |
[ |
| 96 |
'https://en.wiktionary.org', |
| 97 |
[ 'en.wikipedia.org' ], |
| 98 |
[ 'example.com' ], |
| 99 |
false, |
| 100 |
]; |
| 101 |
yield 'single origin, * no match' => |
| 102 |
[ |
| 103 |
'https://en.wiktionary.org', |
| 104 |
[ '*.wikipedia.org' ], |
| 105 |
[ '*.example.com' ], |
| 106 |
false, |
| 107 |
]; |
| 108 |
yield 'single origin, ? no match' => |
| 109 |
[ |
| 110 |
'https://en.wiktionary.org', |
| 111 |
[ '??.wikipedia.org' ], |
| 112 |
[ '??.example.com' ], |
| 113 |
false, |
| 114 |
]; |
| 115 |
yield 'single origin, multi-list, match' => |
| 116 |
[ |
| 117 |
'https://en.wiktionary.org', |
| 118 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 119 |
[ 'example.com', 'www.mediawiki.org' ], |
| 120 |
true |
| 121 |
]; |
| 122 |
yield 'single origin, exclude multi-list, no match' => |
| 123 |
[ |
| 124 |
'https://en.wiktionary.org', |
| 125 |
[ 'en.wiktionary.org' ], |
| 126 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 127 |
false |
| 128 |
]; |
| 129 |
yield 'single origin, multi-list, * match' => |
| 130 |
[ |
| 131 |
'https://en.wiktionary.org', |
| 132 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 133 |
[ '*.example.com', '*.mediawiki.org' ], |
| 134 |
true, |
| 135 |
]; |
| 136 |
yield 'single origin, exclude multi-list, * no match' => |
| 137 |
[ |
| 138 |
'https://en.wiktionary.org', |
| 139 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 140 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 141 |
false, |
| 142 |
]; |
| 143 |
yield 'single origin, multi-list, ? match' => |
| 144 |
[ |
| 145 |
'https://en.wiktionary.org', |
| 146 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 147 |
[ '??.example.com', '??.mediawiki.org' ], |
| 148 |
true, |
| 149 |
]; |
| 150 |
yield 'single origin, exclude multi-list, ? no match' => |
| 151 |
[ |
| 152 |
'https://en.wiktionary.org', |
| 153 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 154 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 155 |
false, |
| 156 |
]; |
| 157 |
yield 'single origin, multi-list, no match' => |
| 158 |
[ 'https://en.wikibooks.org', [ 'en.wikipedia.org', 'en.wiktionary.org' ], [], false ]; |
| 159 |
yield 'single origin, multi-list, * no match' => |
| 160 |
[ 'https://en.wikibooks.org', [ '*.wikipedia.org', '*.wiktionary.org' ], [], false ]; |
| 161 |
yield 'single origin, multi-list, ? no match' => |
| 162 |
[ 'https://en.wikibooks.org', [ '??.wikipedia.org', '??.wiktionary.org' ], [], false ]; |
| 163 |
yield 'multi origin, multi-list, match' => |
| 164 |
[ |
| 165 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 166 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 167 |
[ 'example.com', 'www.mediawiki.org' ], |
| 168 |
true, |
| 169 |
]; |
| 170 |
yield 'multi origin, exclude multi-list, no match' => |
| 171 |
[ |
| 172 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 173 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 174 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 175 |
false, |
| 176 |
]; |
| 177 |
yield 'multi origin, multi-list, * match' => |
| 178 |
[ |
| 179 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 180 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 181 |
[ '*.example.com', '*.mediawiki.org' ], |
| 182 |
true |
| 183 |
]; |
| 184 |
yield 'multi origin, exclude multi-list, * no match' => |
| 185 |
[ |
| 186 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 187 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 188 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 189 |
false |
| 190 |
]; |
| 191 |
yield 'multi origin, multi-list, ? match' => |
| 192 |
[ |
| 193 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 194 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 195 |
[ '??.example.com', '??.mediawiki.org' ], |
| 196 |
true, |
| 197 |
]; |
| 198 |
yield 'multi origin, exclude multi-list, ? no match' => |
| 199 |
[ |
| 200 |
'https://en.wikipedia.org https://en.wiktionary.org', |
| 201 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 202 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 203 |
false, |
| 204 |
]; |
| 205 |
yield 'multi origin, multi-list, no match' => |
| 206 |
[ |
| 207 |
'https://en.wikipedia.org https://en.wikibooks.org', |
| 208 |
[ 'en.wikipedia.org', 'en.wiktionary.org' ], |
| 209 |
[ 'example.com', 'www.mediawiki.org' ], |
| 210 |
false, |
| 211 |
]; |
| 212 |
yield 'multi origin, multi-list, * no match' => |
| 213 |
[ |
| 214 |
'https://en.wikipedia.org https://en.wikibooks.org', |
| 215 |
[ '*.wikipedia.org', '*.wiktionary.org' ], |
| 216 |
[ '*.example.com', '*.mediawiki.org' ], |
| 217 |
false, |
| 218 |
]; |
| 219 |
yield 'multi origin, multi-list, ? no match' => |
| 220 |
[ |
| 221 |
'https://en.wikipedia.org https://en.wikibooks.org', |
| 222 |
[ '??.wikipedia.org', '??.wiktionary.org' ], |
| 223 |
[ '??.example.com', '??.mediawiki.org' ], |
| 224 |
false, |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public function testMatch( string $header, array $allowList, array $excludeList, bool $expect ) { |
| 232 |
$header = Origin::parseHeaderList( [ $header ] ); |
| 233 |
$this->assertSame( $expect, $header->match( $allowList, $excludeList ) ); |
| 234 |
} |
| 235 |
} |