4 * Tests for MultiHttpClient
6 * The urls herein are not actually called, because we mock the return results.
8 * @covers MultiHttpClient
10 class MultiHttpClientTest
extends MediaWikiTestCase
{
13 protected function setUp() {
15 $client = $this->getMockBuilder( MultiHttpClient
::class )
16 ->setConstructorArgs( [ [] ] )
17 ->setMethods( [ 'isCurlEnabled' ] )->getMock();
18 $client->method( 'isCurlEnabled' )->willReturn( false );
19 $this->client
= $client;
22 private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) {
23 $httpRequest = $this->getMockBuilder( PhpHttpRequest
::class )
24 ->setConstructorArgs( [ '', [] ] )
26 $httpRequest->expects( $this->any() )
28 ->willReturn( Status
::wrap( $statusValue ) );
29 $httpRequest->expects( $this->any() )
30 ->method( 'getResponseHeaders' )
31 ->willReturn( $headers );
32 $httpRequest->expects( $this->any() )
33 ->method( 'getStatus' )
34 ->willReturn( $statusCode );
38 private function mockHttpRequestFactory( $httpRequest ) {
39 $factory = $this->getMockBuilder( MediaWiki\Http\HttpRequestFactory
::class )
41 $factory->expects( $this->any() )
43 ->willReturn( $httpRequest );
48 * Test call of a single url that should succeed
50 public function testMultiHttpClientSingleSuccess() {
52 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200 );
53 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
55 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client
->run( [
57 'url' => "http://example.test",
60 $this->assertEquals( 200, $rcode );
64 * Test call of a single url that should not exist, and therefore fail
66 public function testMultiHttpClientSingleFailure() {
67 // Mock an invalid tld
68 $httpRequest = $this->getHttpRequest(
69 StatusValue
::newFatal( 'http-invalid-url', 'http://www.example.test' ), 0 );
70 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
72 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client
->run( [
74 'url' => "http://www.example.test",
77 $failure = $rcode < 200 ||
$rcode >= 400;
78 $this->assertTrue( $failure );
82 * Test call of multiple urls that should all succeed
84 public function testMultiHttpClientMultipleSuccess() {
86 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200 );
87 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
92 'url' => 'http://example.test',
96 'url' => 'https://get.test',
99 $responses = $this->client
->runMulti( $reqs );
100 foreach ( $responses as $response ) {
101 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $response['response'];
102 $this->assertEquals( 200, $rcode );
107 * Test call of multiple urls that should all fail
109 public function testMultiHttpClientMultipleFailure() {
110 // Mock page not found
111 $httpRequest = $this->getHttpRequest(
112 StatusValue
::newFatal( "http-bad-status", 404, 'Not Found' ), 404 );
113 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
118 'url' => 'http://example.test/12345',
122 'url' => 'http://example.test/67890' ,
125 $responses = $this->client
->runMulti( $reqs );
126 foreach ( $responses as $response ) {
127 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $response['response'];
128 $failure = $rcode < 200 ||
$rcode >= 400;
129 $this->assertTrue( $failure );
134 * Test of response header handling
136 public function testMultiHttpClientHeaders() {
137 // Represenative headers for typical requests, per MWHttpRequest::getResponseHeaders()
140 'text/html; charset=utf-8',
143 'Wed, 18 Jul 2018 14:52:41 GMT',
146 'COUNTRY=NAe6; expires=Wed, 25-Jul-2018 14:52:41 GMT; path=/; domain=.example.test',
147 'LAST_NEWS=1531925562; expires=Thu, 18-Jul-2019 14:52:41 GMT; path=/; domain=.example.test',
151 // Mock success with specific headers
152 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200, $headers );
153 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
155 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client
->run( [
157 'url' => 'http://example.test',
160 $this->assertEquals( 200, $rcode );
161 $this->assertEquals( count( $headers ), count( $rhdrs ) );
162 foreach ( $headers as $name => $values ) {
163 $value = implode( ', ', $values );
164 $this->assertArrayHasKey( $name, $rhdrs );
165 $this->assertEquals( $value, $rhdrs[$name] );