9 protected function setUp() {
12 $this->oldServer = $_SERVER;
27 $this->
setMwGlobals(
'wgAssumeProxiesUseDefaultProtocolPorts',
true );
30 $result = WebRequest::detectServer();
31 $this->assertEquals( $expected, $result, $description );
49 'Host header with secure'
57 'Default SERVER_PORT',
71 'HTTP_X_FORWARDED_PROTO' =>
'https',
80 'SERVER_PORT' =>
'81',
81 'HTTP_X_FORWARDED_PROTO' =>
'https',
98 'Host server name precedence'
103 'HTTP_HOST' =>
'[::1]',
104 'SERVER_NAME' =>
'::1',
105 'SERVER_PORT' =>
'81',
112 'SERVER_NAME' =>
'[2001'
114 'Kind of like lighttpd per commit message in MW r83847',
117 'http://[2a01:e35:2eb4:1::2]:777',
119 'SERVER_NAME' =>
'[2a01:e35:2eb4:1::2]:777'
121 'Possible lighttpd environment per bug 14977 comment 13',
129 $reflection =
new ReflectionClass( WebRequest::class );
130 $req = $reflection->newInstanceWithoutConstructor();
132 $prop = $reflection->getProperty(
'data' );
133 $prop->setAccessible(
true );
134 $prop->setValue(
$req, $data );
136 $prop = $reflection->getProperty(
'requestTime' );
137 $prop->setAccessible(
true );
138 $prop->setValue(
$req, microtime(
true ) );
148 $this->assertGreaterThanOrEqual( 0.0,
$req->getElapsedTime() );
149 $this->assertEquals( 0.0,
$req->getElapsedTime(),
'', 0.2 );
160 $normal =
"a \xef\xbf\xbd null";
162 $this->assertSame( $normal,
$req->getVal(
'x' ) );
163 $this->assertNotSame(
$input,
$req->getVal(
'x' ) );
164 $this->assertSame( [ $normal, $normal ],
$req->getArray(
'y' ) );
172 $req = $this->
mockWebRequest( [
'x' =>
'Value',
'y' => [
'a' ],
'crlf' =>
"A\r\nb" ] );
173 $this->assertSame(
'Value',
$req->getVal(
'x' ),
'Simple value' );
174 $this->assertSame(
null,
$req->getVal(
'z' ),
'Not found' );
175 $this->assertSame(
null,
$req->getVal(
'y' ),
'Array is ignored' );
176 $this->assertSame(
"A\r\nb",
$req->getVal(
'crlf' ),
'CRLF' );
188 $this->assertSame(
'Value',
$req->getRawVal(
'x' ) );
189 $this->assertSame(
null,
$req->getRawVal(
'z' ),
'Not found' );
190 $this->assertSame(
null,
$req->getRawVal(
'y' ),
'Array is ignored' );
191 $this->assertSame(
"A\r\nb",
$req->getRawVal(
'crlf' ),
'CRLF' );
199 $this->assertSame( [
'Value' ],
$req->getArray(
'x' ),
'Value becomes array' );
200 $this->assertSame(
null,
$req->getArray(
'z' ),
'Not found' );
201 $this->assertSame( [
'a',
'b' ],
$req->getArray(
'y' ) );
209 $this->assertSame( [ 0 ],
$req->getIntArray(
'x' ),
'Text becomes 0' );
210 $this->assertSame(
null,
$req->getIntArray(
'z' ),
'Not found' );
211 $this->assertSame( [ 0, 4, -2 ],
$req->getIntArray(
'y' ) );
225 $this->assertSame( 0,
$req->getInt(
'x' ),
'Text' );
226 $this->assertSame( 0,
$req->getInt(
'y' ),
'Array' );
227 $this->assertSame( 0,
$req->getInt(
'z' ),
'Not found' );
228 $this->assertSame( 0,
$req->getInt(
'zero' ) );
229 $this->assertSame( 4,
$req->getInt(
'answer' ) );
230 $this->assertSame( -2,
$req->getInt(
'neg' ) );
244 $this->assertSame(
null,
$req->getIntOrNull(
'x' ),
'Text' );
245 $this->assertSame(
null,
$req->getIntOrNull(
'y' ),
'Array' );
246 $this->assertSame(
null,
$req->getIntOrNull(
'z' ),
'Not found' );
247 $this->assertSame( 0,
$req->getIntOrNull(
'zero' ) );
248 $this->assertSame( 4,
$req->getIntOrNull(
'answer' ) );
249 $this->assertSame( -2,
$req->getIntOrNull(
'neg' ) );
263 $this->assertSame( 0.0,
$req->getFloat(
'x' ),
'Text' );
264 $this->assertSame( 0.0,
$req->getFloat(
'y' ),
'Array' );
265 $this->assertSame( 0.0,
$req->getFloat(
'z' ),
'Not found' );
266 $this->assertSame( 0.0,
$req->getFloat(
'zero' ) );
267 $this->assertSame( 4.2,
$req->getFloat(
'answer' ) );
268 $this->assertSame( -2.0,
$req->getFloat(
'neg' ) );
282 $this->assertSame(
true,
$req->getBool(
'x' ),
'Text' );
283 $this->assertSame(
false,
$req->getBool(
'y' ),
'Array' );
284 $this->assertSame(
false,
$req->getBool(
'z' ),
'Not found' );
285 $this->assertSame(
false,
$req->getBool(
'zero' ) );
286 $this->assertSame(
true,
$req->getBool(
'f' ) );
287 $this->assertSame(
true,
$req->getBool(
't' ) );
293 [
'',
false,
'(empty string)' ],
311 $this->assertSame( $expected,
$req->getFuzzyBool(
'x' ), $message ?:
"Value: '$value'" );
319 $this->assertSame(
false,
$req->getFuzzyBool(
'z' ),
'Not found' );
327 $this->assertSame(
false,
$req->getCheck(
'z' ),
'Not found' );
328 $this->assertSame(
true,
$req->getCheck(
'x' ),
'Text' );
329 $this->assertSame(
true,
$req->getCheck(
'zero' ) );
338 $this->assertSame(
"Va\nlue",
$req->getText(
'crlf' ),
'CR stripped' );
345 $values = [
'x' =>
'Value',
'y' =>
'' ];
348 $this->assertSame( $values,
$req->getValues() );
349 $this->assertSame( [
'x' =>
'Value' ],
$req->getValues(
'x' ),
'Specific keys' );
357 $this->assertSame( [
'x',
'y' ],
$req->getValueNames() );
358 $this->assertSame( [
'x' ],
$req->getValueNames( [
'y' ] ),
'Exclude keys' );
365 public function testGetIP( $expected,
$input, $squid, $xffList, $private, $description ) {
368 'wgUsePrivateIPs' => $private,
370 'IsTrustedProxy' => [
371 function ( &$ip, &$trusted ) use ( $xffList ) {
372 $trusted = $trusted || in_array( $ip, $xffList );
383 $this->assertEquals( $expected, $result, $description );
391 'REMOTE_ADDR' =>
'127.0.0.1'
401 'REMOTE_ADDR' =>
'::1'
411 'REMOTE_ADDR' =>
'abcd:0001:002:03:4:555:6666:7777',
412 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
414 [
'ABCD:1:2:3:4:555:6666:7777' ],
422 'REMOTE_ADDR' =>
'12.0.0.1',
423 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
425 [
'12.0.0.1',
'12.0.0.2' ],
428 'With X-Forwaded-For'
433 'REMOTE_ADDR' =>
'12.0.0.1',
434 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
439 'With X-Forwaded-For and disallowed server'
444 'REMOTE_ADDR' =>
'12.0.0.1',
445 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
450 'With multiple X-Forwaded-For and only one allowed server'
455 'REMOTE_ADDR' =>
'12.0.0.2',
456 'HTTP_X_FORWARDED_FOR' =>
'10.0.0.4, 10.0.0.3, 12.0.0.2'
458 [
'12.0.0.1',
'12.0.0.2' ],
461 'With X-Forwaded-For and private IP (from cache proxy)'
466 'REMOTE_ADDR' =>
'12.0.0.2',
467 'HTTP_X_FORWARDED_FOR' =>
'10.0.0.4, 10.0.0.3, 12.0.0.2'
469 [
'12.0.0.1',
'12.0.0.2',
'10.0.0.3' ],
472 'With X-Forwaded-For and private IP (allowed)'
477 'REMOTE_ADDR' =>
'12.0.0.2',
478 'HTTP_X_FORWARDED_FOR' =>
'10.0.0.4, 10.0.0.3, 12.0.0.2'
480 [
'12.0.0.1',
'12.0.0.2' ],
483 'With X-Forwaded-For and private IP (allowed)'
488 'REMOTE_ADDR' =>
'12.0.0.2',
489 'HTTP_X_FORWARDED_FOR' =>
'10.0.0.4, 10.0.0.3, 12.0.0.2'
491 [
'12.0.0.1',
'12.0.0.2' ],
494 'With X-Forwaded-For and private IP (disallowed)'
499 'REMOTE_ADDR' =>
'12.0.0.1',
500 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
503 [
'12.0.0.1',
'12.0.0.2' ],
505 'With X-Forwaded-For'
510 'REMOTE_ADDR' =>
'12.0.0.1',
511 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
516 'With multiple X-Forwaded-For and only one allowed server'
521 'REMOTE_ADDR' =>
'12.0.0.2',
522 'HTTP_X_FORWARDED_FOR' =>
'10.0.0.3, 12.0.0.2'
527 'With X-Forwaded-For and private IP and hook (disallowed)'
532 'REMOTE_ADDR' =>
'abcd:0001:002:03:4:555:6666:7777',
533 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.1, abcd:0001:002:03:4:555:6666:7777',
535 [
'ABCD:1:2:3::/64' ],
543 'REMOTE_ADDR' =>
'12.0.0.1',
544 'HTTP_X_FORWARDED_FOR' =>
'12.0.0.3, 12.0.0.2'
561 'wgSquidServersNoPurge' => [],
562 'wgSquidServers' => [],
563 'wgUsePrivateIPs' =>
false,
569 # Next call throw an exception about lacking an IP
575 [
'', [],
'Empty Accept-Language header' ],
576 [
'en', [
'en' => 1 ],
'One language' ],
577 [
'en, ar', [
'en' => 1,
'ar' => 1 ],
'Two languages listed in appearance order.' ],
580 [
'zh-cn' => 1,
'zh-tw' => 1 ],
581 'Two equally prefered languages, listed in appearance order per rfc3282. Checks c9119'
585 [
'es' => 1,
'en' =>
'0.5' ],
586 'Spanish as first language and English and second'
588 [
'en; q=0.5, es', [
'es' => 1,
'en' =>
'0.5' ],
'Less prefered language first' ],
589 [
'fr, en; q=0.5, es', [
'fr' => 1,
'es' => 1,
'en' =>
'0.5' ],
'Three languages' ],
590 [
'en; q=0.5, es', [
'es' => 1,
'en' =>
'0.5' ],
'Two languages' ],
591 [
'en, zh;q=0', [
'en' => 1 ],
"It's Chinese to me" ],
593 'es; q=1, pt;q=0.7, it; q=0.6, de; q=0.1, ru;q=0',
594 [
'es' =>
'1',
'pt' =>
'0.7',
'it' =>
'0.6',
'de' =>
'0.1' ],
595 'Preference for Romance languages'
599 [
'en-gb' => 1,
'en-us' =>
'1' ],
600 'Two equally prefered English variants'
602 [
'_', [],
'Invalid input' ],
610 public function testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description ) {
611 $this->
setServerVars( [
'HTTP_ACCEPT_LANGUAGE' => $acceptLanguageHeader ] );
613 $this->assertSame(
$request->getAcceptLang(), $expectedLanguages, $description );
618 if ( !isset(
$vars[
'REQUEST_TIME_FLOAT'] ) ) {
619 $vars[
'REQUEST_TIME_FLOAT'] = $_SERVER[
'REQUEST_TIME_FLOAT'];
621 if ( !isset(
$vars[
'REQUEST_TIME'] ) ) {
622 $vars[
'REQUEST_TIME'] = $_SERVER[
'REQUEST_TIME'];
testGetText()
WebRequest::getText.
testGetIpLackOfRemoteAddrThrowAnException()
MWException WebRequest::getIP.
mockWebRequest( $data=[])
testDetectServer( $expected, $input, $description)
provideDetectServer WebRequest::detectServer WebRequest::detectProtocol
static provideLanguageData()
testGetValues()
WebRequest::getValues.
testGetRawVal()
WebRequest::getRawVal.
testGetIntOrNull()
WebRequest::getIntOrNull.
testGetBool()
WebRequest::getBool.
testGetCheck()
WebRequest::getCheck.
static provideFuzzyBool()
testGetValueNames()
WebRequest::getValueNames.
testGetFuzzyBoolDefault()
WebRequest::getFuzzyBool.
testGetFuzzyBool( $value, $expected, $message=null)
provideFuzzyBool WebRequest::getFuzzyBool
testGetElapsedTime()
WebRequest::getElapsedTime.
testGetValNormal()
WebRequest::getVal WebRequest::getGPCVal WebRequest::normalizeUnicode.
static provideDetectServer()
testGetArray()
WebRequest::getArray.
testGetFloat()
WebRequest::getFloat.
testGetIP( $expected, $input, $squid, $xffList, $private, $description)
provideGetIP WebRequest::getIP
testGetInt()
WebRequest::getInt.
testGetIntArray()
WebRequest::getIntArray.
testAcceptLang( $acceptLanguageHeader, $expectedLanguages, $description)
provideLanguageData WebRequest::getAcceptLang
testGetVal()
WebRequest::getVal WebRequest::getGPCVal.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
this hook is for auditing only $req
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
processing should stop and the error should be shown to the user * false
if(is_array($mode)) switch( $mode) $input