| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MediaWikiServices; |
| 4 |
use Wikimedia\Http\TelemetryHeadersInterface; |
| 5 |
use Wikimedia\TestingAccessWrapper; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class MWHttpRequestTest extends PHPUnit\Framework\TestCase { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public static function provideURI() { |
| 16 |
|
| 17 |
return [ |
| 18 |
[ false, '¿non sens before!! http://a', 'Allow anything before URI' ], |
| 19 |
|
| 20 |
|
| 21 |
[ true, 'http://www.example.org/' ], |
| 22 |
[ true, 'https://www.example.org/' ], |
| 23 |
[ true, 'http://www.example.org', 'URI without directory' ], |
| 24 |
[ true, 'http://a', 'Short name' ], |
| 25 |
[ true, 'http://étoile', 'Allow UTF-8 in hostname' ],r' |
| 26 |
[ false, '\\host\directory', 'CIFS share' ], |
| 27 |
[ false, 'gopher://host/dir', 'Reject gopher scheme' ], |
| 28 |
[ false, 'telnet://host', 'Reject telnet scheme' ], |
| 29 |
|
| 30 |
|
| 31 |
[ false, 'http//example.org', 'Reject missing colon in protocol' ], |
| 32 |
[ false, 'http:/example.org', 'Reject missing slash in protocol' ], |
| 33 |
[ false, 'http:example.org', 'Must have two slashes' ], |
| 34 |
|
| 35 |
[ false, 'http:///example.org', 'Must have exactly two slashes, not three' ], |
| 36 |
|
| 37 |
|
| 38 |
[ true, 'http://user@host', 'Username provided' ], |
| 39 |
[ true, 'http://user:@host', 'Username provided, no password' ], |
| 40 |
[ true, 'http://user:pass@host', 'Username and password provided' ], |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ce' ], |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
[ true, 'http://example.org:80/' ], |
| 50 |
[ true, 'https://example.org:80/' ], |
| 51 |
[ true, 'http://example.org:443/' ], |
| 52 |
[ true, 'https://example.org:443/' ], |
| 53 |
|
| 54 |
|
| 55 |
[ true, 'http://example/#' ], |
| 56 |
[ true, 'http://example/!' ], |
| 57 |
[ true, 'http://example/:' ], |
| 58 |
[ true, 'http://example/.' ], |
| 59 |
[ true, 'http://example/?' ], |
| 60 |
[ true, 'http://example/+' ], |
| 61 |
[ true, 'http://example/=' ], |
| 62 |
[ true, 'http://example/&' ], |
| 63 |
[ true, 'http://example/%' ], |
| 64 |
[ true, 'http://example/@' ], |
| 65 |
[ true, 'http://example/-' ], |
| 66 |
[ true, 'http://example//' ], |
| 67 |
[ true, 'http://example/&' ], |
| 68 |
|
| 69 |
|
| 70 |
[ true, 'http://exam#ple.org', ], |
| 71 |
[ true, 'http://example.org:80#anchor' ], |
| 72 |
[ true, 'http://example.org/?id#anchor' ], |
| 73 |
[ true, 'http://example.org/?#anchor' ], |
| 74 |
|
| 75 |
[ false, 'http://a ¿non !!sens after', 'Allow anything after URI' ], |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function testIsValidUri( $expect, $uri, $message = '' ) { |
| 85 |
$this->assertSame( $expect, MWHttpRequest::isValidURI( $uri ), $message ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testSetReverseProxy() { |
| 89 |
$req = TestingAccessWrapper::newFromObject( |
| 90 |
MediaWikiServices::getInstance()->getHttpRequestFactory()->create( 'https://example.org/path?query=string' ) |
| 91 |
); |
| 92 |
$req->setReverseProxy( 'http://localhost:1234' ); |
| 93 |
$this->assertSame( 'http://localhost:1234/path?query=string', $req->url ); |
| 94 |
$this->assertSame( 'example.org', $req->reqHeaders['Host'] ); |
| 95 |
} |
| 96 |
|
| 97 |
public function testItInjectsTelemetryHeaders() { |
| 98 |
$telemetry = $this->createMock( TelemetryHeadersInterface::class ); |
| 99 |
$telemetry->expects( $this->once() ) |
| 100 |
->method( 'getRequestHeaders' ) |
| 101 |
->willReturn( [ |
| 102 |
'X-Request-Id' => 'request_identifier', |
| 103 |
'tracestate' => 'tracestate_value', |
| 104 |
'traceparent' => 'traceparent_value', |
| 105 |
] ); |
| 106 |
|
| 107 |
$httpRequest = $this->getMockForAbstractClass( |
| 108 |
MWHttpRequest::class, |
| 109 |
[ |
| 110 |
'http://localhost/test', |
| 111 |
[ |
| 112 |
'timeout' => 30, |
| 113 |
'connectTimeout' => 30 |
| 114 |
] |
| 115 |
] |
| 116 |
); |
| 117 |
$httpRequest->addTelemetry( $telemetry ); |
| 118 |
|
| 119 |
$accessWrapper = TestingAccessWrapper::newFromObject( $httpRequest ); |
| 120 |
$requestHeaders = $accessWrapper->reqHeaders; |
| 121 |
|
| 122 |
$this->assertEquals( 'request_identifier', $requestHeaders['X-Request-Id'] ); |
| 123 |
$this->assertEquals( 'tracestate_value', $requestHeaders['tracestate'] ); |
| 124 |
$this->assertEquals( 'traceparent_value', $requestHeaders['traceparent'] ); |
| 125 |
} |
| 126 |
} |