| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Status\Status; |
| 4 |
use PHPUnit\Framework\Constraint\IsType; |
| 5 |
use PHPUnit\Framework\MockObject\MockObject; |
| 6 |
use Wikimedia\Http\MultiHttpClient; |
| 7 |
use Wikimedia\Http\TelemetryHeadersInterface; |
| 8 |
use Wikimedia\TestingAccessWrapper; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class MultiHttpClientTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private function createClient( $options = [] ) { |
| 21 |
$client = $this->getMockBuilder( MultiHttpClient::class ) |
| 22 |
->setConstructorArgs( [ $options ] ) |
| 23 |
->onlyMethods( [ 'isCurlEnabled' ] )->getMock(); |
| 24 |
$client->method( 'isCurlEnabled' )->willReturn( false ); |
| 25 |
return $client; |
| 26 |
} |
| 27 |
|
| 28 |
private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) { |
| 29 |
$options = [ |
| 30 |
'timeout' => 1, |
| 31 |
'connectTimeout' => 1 |
| 32 |
]; |
| 33 |
$httpRequest = $this->getMockBuilder( MWHttpRequest::class ) |
| 34 |
->setConstructorArgs( [ '', $options ] ) |
| 35 |
->getMock(); |
| 36 |
$httpRequest->method( 'execute' ) |
| 37 |
->willReturn( Status::wrap( $statusValue ) ); |
| 38 |
$httpRequest->method( 'getResponseHeaders' ) |
| 39 |
->willReturn( $headers ); |
| 40 |
$httpRequest->method( 'getStatus' ) |
| 41 |
->willReturn( $statusCode ); |
| 42 |
return $httpRequest; |
| 43 |
} |
| 44 |
|
| 45 |
private function mockHttpRequestFactory( $httpRequest ) { |
| 46 |
$factory = $this->createMock( MediaWiki\Http\HttpRequestFactory::class ); |
| 47 |
$factory->method( 'create' ) |
| 48 |
->willReturn( $httpRequest ); |
| 49 |
return $factory; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function testMultiHttpClientSingleSuccess() { |
| 56 |
|
| 57 |
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 ); |
| 58 |
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) ); |
| 59 |
|
| 60 |
[ $rcode, $rdesc, , $rbody, $rerr ] = $this->createClient()->run( [ |
| 61 |
'method' => 'GET', |
| 62 |
'url' => "http://example.test", |
| 63 |
] ); |
| 64 |
|
| 65 |
$this->assertSame( 200, $rcode ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testMultiHttpClientSingleFailure() { |
| 72 |
|
| 73 |
$httpRequest = $this->getHttpRequest( |
| 74 |
StatusValue::newFatal( 'http-invalid-url', 'http://www.example.test' ), 0 ); |
| 75 |
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) ); |
| 76 |
|
| 77 |
[ $rcode, $rdesc, , $rbody, $rerr ] = $this->createClient()->run( [ |
| 78 |
'method' => 'GET', |
| 79 |
'url' => "http://www.example.test", |
| 80 |
] ); |
| 81 |
|
| 82 |
$this->assertSame( 0, $rcode ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testMultiHttpClientMultipleSuccess() { |
| 89 |
|
| 90 |
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 ); |
| 91 |
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) ); |
| 92 |
|
| 93 |
$reqs = [ |
| 94 |
[ |
| 95 |
'method' => 'GET', |
| 96 |
'url' => 'http://example.test', |
| 97 |
], |
| 98 |
[ |
| 99 |
'method' => 'GET', |
| 100 |
'url' => 'https://get.test', |
| 101 |
], |
| 102 |
]; |
| 103 |
$responses = $this->createClient()->runMulti( $reqs ); |
| 104 |
foreach ( $responses as $response ) { |
| 105 |
[ $rcode, $rdesc, , $rbody, $rerr ] = $response['response']; |
| 106 |
$this->assertSame( 200, $rcode ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function testMultiHttpClientMultipleFailure() { |
| 114 |
|
| 115 |
$httpRequest = $this->getHttpRequest( |
| 116 |
StatusValue::newFatal( "http-bad-status", 404, 'Not Found' ), 404 ); |
| 117 |
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) ); |
| 118 |
|
| 119 |
$reqs = [ |
| 120 |
[ |
| 121 |
'method' => 'GET', |
| 122 |
'url' => 'http://example.test/12345', |
| 123 |
], |
| 124 |
[ |
| 125 |
'method' => 'GET', |
| 126 |
'url' => 'http://example.test/67890', |
| 127 |
] |
| 128 |
]; |
| 129 |
$responses = $this->createClient()->runMulti( $reqs ); |
| 130 |
foreach ( $responses as $response ) { |
| 131 |
[ $rcode, $rdesc, , $rbody, $rerr ] = $response['response']; |
| 132 |
$this->assertSame( 404, $rcode ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function testMultiHttpClientHeaders() { |
| 140 |
|
| 141 |
$headers = [ |
| 142 |
'content-type' => [ |
| 143 |
'text/html; charset=utf-8', |
| 144 |
], |
| 145 |
'date' => [ |
| 146 |
'Wed, 18 Jul 2018 14:52:41 GMT', |
| 147 |
], |
| 148 |
'set-cookie' => [ |
| 149 |
'COUNTRY=NAe6; expires=Wed, 25-Jul-2018 14:52:41 GMT; path=/; domain=.example.test', |
| 150 |
'LAST_NEWS=1531925562; expires=Thu, 18-Jul-2019 14:52:41 GMT; path=/; domain=.example.test', |
| 151 |
] |
| 152 |
]; |
| 153 |
|
| 154 |
|
| 155 |
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200, $headers ); |
| 156 |
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) ); |
| 157 |
|
| 158 |
[ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $this->createClient()->run( [ |
| 159 |
'method' => 'GET', |
| 160 |
'url' => 'http://example.test', |
| 161 |
] ); |
| 162 |
|
| 163 |
$this->assertSame( 200, $rcode ); |
| 164 |
$this->assertSameSize( $headers, $rhdrs ); |
| 165 |
foreach ( $headers as $name => $values ) { |
| 166 |
$value = implode( ', ', $values ); |
| 167 |
$this->assertArrayHasKey( $name, $rhdrs ); |
| 168 |
$this->assertEquals( $value, $rhdrs[$name] ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public static function provideMultiHttpTimeout() { |
| 173 |
return [ |
| 174 |
'default 10/30' => [ |
| 175 |
[], |
| 176 |
[], |
| 177 |
10, |
| 178 |
30 |
| 179 |
], |
| 180 |
'constructor override' => [ |
| 181 |
[ 'connTimeout' => 2, 'reqTimeout' => 3 ], |
| 182 |
[], |
| 183 |
2, |
| 184 |
3 |
| 185 |
], |
| 186 |
'run override' => [ |
| 187 |
[], |
| 188 |
[ 'connTimeout' => 2, 'reqTimeout' => 3 ], |
| 189 |
2, |
| 190 |
3 |
| 191 |
], |
| 192 |
'constructor max option limits default' => [ |
| 193 |
[ 'maxConnTimeout' => 2, 'maxReqTimeout' => 3 ], |
| 194 |
[], |
| 195 |
2, |
| 196 |
3 |
| 197 |
], |
| 198 |
'constructor max option limits regular constructor option' => [ |
| 199 |
[ |
| 200 |
'maxConnTimeout' => 2, |
| 201 |
'maxReqTimeout' => 3, |
| 202 |
'connTimeout' => 100, |
| 203 |
'reqTimeout' => 100 |
| 204 |
], |
| 205 |
[], |
| 206 |
2, |
| 207 |
3 |
| 208 |
], |
| 209 |
'constructor max option greater than regular constructor option' => [ |
| 210 |
[ |
| 211 |
'maxConnTimeout' => 2, |
| 212 |
'maxReqTimeout' => 3, |
| 213 |
'connTimeout' => 1, |
| 214 |
'reqTimeout' => 1 |
| 215 |
], |
| 216 |
[], |
| 217 |
1, |
| 218 |
1 |
| 219 |
], |
| 220 |
'constructor max option limits run option' => [ |
| 221 |
[ |
| 222 |
'maxConnTimeout' => 2, |
| 223 |
'maxReqTimeout' => 3, |
| 224 |
], |
| 225 |
[ |
| 226 |
'connTimeout' => 100, |
| 227 |
'reqTimeout' => 100 |
| 228 |
], |
| 229 |
2, |
| 230 |
3 |
| 231 |
], |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function testMultiHttpTimeout( $createOptions, $runOptions, |
| 240 |
$expectedConnTimeout, $expectedReqTimeout |
| 241 |
) { |
| 242 |
$url = 'http://www.example.test'; |
| 243 |
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 ); |
| 244 |
$factory = $this->createMock( MediaWiki\Http\HttpRequestFactory::class ); |
| 245 |
$factory->method( 'create' ) |
| 246 |
->with( |
| 247 |
$url, |
| 248 |
$this->callback( |
| 249 |
static function ( $options ) use ( $expectedReqTimeout, $expectedConnTimeout ) { |
| 250 |
return $options['timeout'] === $expectedReqTimeout |
| 251 |
&& $options['connectTimeout'] === $expectedConnTimeout; |
| 252 |
} |
| 253 |
) |
| 254 |
) |
| 255 |
->willReturn( $httpRequest ); |
| 256 |
$this->setService( 'HttpRequestFactory', $factory ); |
| 257 |
|
| 258 |
$client = $this->createClient( $createOptions ); |
| 259 |
|
| 260 |
$client->run( |
| 261 |
[ 'method' => 'GET', 'url' => $url ], |
| 262 |
$runOptions |
| 263 |
); |
| 264 |
|
| 265 |
$this->addToAssertionCount( 1 ); |
| 266 |
} |
| 267 |
|
| 268 |
public function testUseReverseProxy() { |
| 269 |
|
| 270 |
|
| 271 |
$class = new ReflectionClass( MultiHttpClient::class ); |
| 272 |
$func = $class->getMethod( 'useReverseProxy' ); |
| 273 |
$req = [ |
| 274 |
'url' => 'https://example.org/path?query=string', |
| 275 |
]; |
| 276 |
$func->invokeArgs( new MultiHttpClient( [] ), [ &$req, 'http://localhost:1234' ] ); |
| 277 |
$this->assertSame( 'http://localhost:1234/path?query=string', $req['url'] ); |
| 278 |
$this->assertSame( 'example.org', $req['headers']['Host'] ); |
| 279 |
} |
| 280 |
|
| 281 |
public function testNormalizeRequests() { |
| 282 |
|
| 283 |
|
| 284 |
$class = new ReflectionClass( MultiHttpClient::class ); |
| 285 |
$func = $class->getMethod( 'normalizeRequests' ); |
| 286 |
$reqs = [ |
| 287 |
[ 'GET', 'https://example.org/path?query=string' ], |
| 288 |
[ |
| 289 |
'method' => 'GET', |
| 290 |
'url' => 'https://example.com/path?query=another%20string', |
| 291 |
'headers' => [ |
| 292 |
'header2' => 'value2' |
| 293 |
] |
| 294 |
], |
| 295 |
]; |
| 296 |
$client = new MultiHttpClient( [ |
| 297 |
'localVirtualHosts' => [ 'example.org' ], |
| 298 |
'localProxy' => 'http://localhost:1234', |
| 299 |
'headers' => [ |
| 300 |
'header1' => 'value1' |
| 301 |
] |
| 302 |
] ); |
| 303 |
$func->invokeArgs( $client, [ &$reqs ] ); |
| 304 |
|
| 305 |
$this->assertSame( 'value1', $reqs[0]['headers']['header1'] ); |
| 306 |
$this->assertSame( 'value1', $reqs[1]['headers']['header1'] ); |
| 307 |
|
| 308 |
$this->assertSame( 'value2', $reqs[1]['headers']['header2'] ); |
| 309 |
$this->assertArrayNotHasKey( 'header2', $reqs[0]['headers'] ); |
| 310 |
|
| 311 |
|
| 312 |
$this->assertSame( 'http://localhost:1234/path?query=string', $reqs[0]['url'] ); |
| 313 |
$this->assertSame( 'example.org', $reqs[0]['headers']['host'] ); |
| 314 |
$this->assertFalse( $reqs[0]['proxy'] ); |
| 315 |
|
| 316 |
$this->assertSame( 'https://example.com/path?query=another%20string', $reqs[1]['url'] ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function testAssembleUrl( array $bits, string $expected ) { |
| 326 |
$class = TestingAccessWrapper::newFromClass( MultiHttpClient::class ); |
| 327 |
$this->assertSame( $expected, $class->assembleUrl( $bits ) ); |
| 328 |
} |
| 329 |
|
| 330 |
public static function provideAssembleUrl(): Generator { |
| 331 |
$schemes = [ |
| 332 |
'' => [], |
| 333 |
'http://' => [ |
| 334 |
'scheme' => 'http', |
| 335 |
], |
| 336 |
]; |
| 337 |
|
| 338 |
$hosts = [ |
| 339 |
'' => [], |
| 340 |
'example.com' => [ |
| 341 |
'host' => 'example.com', |
| 342 |
], |
| 343 |
'example.com:123' => [ |
| 344 |
'host' => 'example.com', |
| 345 |
'port' => 123, |
| 346 |
], |
| 347 |
'[email protected]' => [ |
| 348 |
'user' => 'id', |
| 349 |
'host' => 'example.com', |
| 350 |
], |
| 351 |
'[email protected]:123' => [ |
| 352 |
'user' => 'id', |
| 353 |
'host' => 'example.com', |
| 354 |
'port' => 123, |
| 355 |
], |
| 356 |
'id:[email protected]' => [ |
| 357 |
'user' => 'id', |
| 358 |
'pass' => 'key', |
| 359 |
'host' => 'example.com', |
| 360 |
], |
| 361 |
'id:[email protected]:123' => [ |
| 362 |
'user' => 'id', |
| 363 |
'pass' => 'key', |
| 364 |
'host' => 'example.com', |
| 365 |
'port' => 123, |
| 366 |
], |
| 367 |
]; |
| 368 |
|
| 369 |
foreach ( $schemes as $scheme => $schemeParts ) { |
| 370 |
foreach ( $hosts as $host => $hostParts ) { |
| 371 |
foreach ( [ '', '/', '/0', '/path' ] as $path ) { |
| 372 |
foreach ( [ '', '0', 'query' ] as $query ) { |
| 373 |
foreach ( [ '', '0', 'fragment' ] as $fragment ) { |
| 374 |
$parts = array_merge( |
| 375 |
$schemeParts, |
| 376 |
$hostParts |
| 377 |
); |
| 378 |
$url = $scheme . |
| 379 |
$host . |
| 380 |
$path; |
| 381 |
|
| 382 |
if ( $path !== '' ) { |
| 383 |
$parts['path'] = $path; |
| 384 |
} |
| 385 |
if ( $query !== '' ) { |
| 386 |
$parts['query'] = $query; |
| 387 |
$url .= '?' . $query; |
| 388 |
} |
| 389 |
if ( $fragment !== '' ) { |
| 390 |
$parts['fragment'] = $fragment; |
| 391 |
$url .= '#' . $fragment; |
| 392 |
} |
| 393 |
|
| 394 |
yield [ $parts, $url ]; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
yield [ |
| 402 |
[ |
| 403 |
'scheme' => 'http', |
| 404 |
'user' => 'id', |
| 405 |
'pass' => 'key', |
| 406 |
'host' => 'example.org', |
| 407 |
'port' => 321, |
| 408 |
'path' => '/over/there', |
| 409 |
'query' => 'name=ferret&foo=bar', |
| 410 |
'fragment' => 'nose', |
| 411 |
], |
| 412 |
'http://id:[email protected]:321/over/there?name=ferret&foo=bar#nose', |
| 413 |
]; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
yield [ |
| 418 |
[ |
| 419 |
'scheme' => 'http', |
| 420 |
'host' => 'url.with.empty.query', |
| 421 |
'path' => '/foo', |
| 422 |
'query' => '', |
| 423 |
], |
| 424 |
'http://url.with.empty.query/foo', |
| 425 |
]; |
| 426 |
} |
| 427 |
|
| 428 |
public static function provideHeader() { |
| 429 |
|
| 430 |
yield 'colon space' => [ false, [ 'Foo: X' => 'Y' ] ]; |
| 431 |
yield 'colon' => [ false, [ 'Foo:bar' => 'X' ] ]; |
| 432 |
yield 'two colon' => [ false, [ 'Foo:bar:baz' => 'X' ] ]; |
| 433 |
yield 'trailing colon' => [ false, [ 'Foo:' => 'Y' ] ]; |
| 434 |
yield 'leading colon' => [ false, [ ':Foo' => 'Y' ] ]; |
| 435 |
|
| 436 |
yield 'word' => [ true, [ 'Foo' => 'X' ] ]; |
| 437 |
yield 'dash' => [ true, [ 'Foo-baz' => 'X' ] ]; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function testNormalizeIllegalHeader( bool $valid, array $headers ) { |
| 444 |
$class = new ReflectionClass( MultiHttpClient::class ); |
| 445 |
$func = $class->getMethod( 'getCurlHandle' ); |
| 446 |
$req = [ |
| 447 |
'method' => 'GET', |
| 448 |
'url' => 'http://localhost:1234', |
| 449 |
'query' => [], |
| 450 |
'body' => '', |
| 451 |
'headers' => $headers |
| 452 |
]; |
| 453 |
|
| 454 |
if ( $valid ) { |
| 455 |
$this->expectNotToPerformAssertions(); |
| 456 |
} else { |
| 457 |
$this->expectException( Exception::class ); |
| 458 |
$this->expectExceptionMessage( 'Header name must not contain colon-space' ); |
| 459 |
} |
| 460 |
$func->invokeArgs( new MultiHttpClient( [] ), [ &$req, [ |
| 461 |
'connTimeout' => 1, |
| 462 |
'reqTimeout' => 1, |
| 463 |
] ] ); |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
public function testForwardsTelemetryHeaders() { |
| 470 |
$telemetry = $this->getMockBuilder( TelemetryHeadersInterface::class ) |
| 471 |
->getMock(); |
| 472 |
$telemetry->expects( $this->once() ) |
| 473 |
->method( 'getRequestHeaders' ) |
| 474 |
->willReturn( [ 'header1' => 'value1', 'header2' => 'value2' ] ); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
$class = new ReflectionClass( MultiHttpClient::class ); |
| 479 |
$func = $class->getMethod( 'normalizeRequests' ); |
| 480 |
$reqs = [ |
| 481 |
[ 'GET', 'https://example.org/path?query=string' ], |
| 482 |
]; |
| 483 |
$client = new MultiHttpClient( [ |
| 484 |
'localVirtualHosts' => [ 'example.org' ], |
| 485 |
'localProxy' => 'http://localhost:1234', |
| 486 |
'telemetry' => $telemetry |
| 487 |
] ); |
| 488 |
$func->invokeArgs( $client, [ &$reqs ] ); |
| 489 |
$this->assertArrayHasKey( 'header1', $reqs[0]['headers'] ); |
| 490 |
$this->assertSame( 'value1', $reqs[0]['headers']['header1'] ); |
| 491 |
$this->assertArrayHasKey( 'header2', $reqs[0]['headers'] ); |
| 492 |
$this->assertSame( 'value2', $reqs[0]['headers']['header2'] ); |
| 493 |
} |
| 494 |
|
| 495 |
public function testGetCurlMulti() { |
| 496 |
$cm = TestingAccessWrapper::newFromObject( new MultiHttpClient( [] ) ); |
| 497 |
$resource = $cm->getCurlMulti( [ 'usePipelining' => true ] ); |
| 498 |
$this->assertThat( |
| 499 |
$resource, |
| 500 |
$this->logicalOr( |
| 501 |
$this->isType( IsType::TYPE_RESOURCE ), |
| 502 |
$this->isInstanceOf( 'CurlMultiHandle' ) |
| 503 |
) |
| 504 |
); |
| 505 |
} |
| 506 |
} |