4 * The urls herein are not actually called, because we mock the return results.
6 * @covers MultiHttpClient
8 class MultiHttpClientTest
extends MediaWikiTestCase
{
11 protected function setUp() {
13 $client = $this->getMockBuilder( MultiHttpClient
::class )
14 ->setConstructorArgs( [ [] ] )
15 ->setMethods( [ 'isCurlEnabled' ] )->getMock();
16 $client->method( 'isCurlEnabled' )->willReturn( false );
17 $this->client
= $client;
20 private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) {
21 $httpRequest = $this->getMockBuilder( PhpHttpRequest
::class )
22 ->setConstructorArgs( [ '', [] ] )
24 $httpRequest->expects( $this->any() )
26 ->willReturn( Status
::wrap( $statusValue ) );
27 $httpRequest->expects( $this->any() )
28 ->method( 'getResponseHeaders' )
29 ->willReturn( $headers );
30 $httpRequest->expects( $this->any() )
31 ->method( 'getStatus' )
32 ->willReturn( $statusCode );
36 private function mockHttpRequestFactory( $httpRequest ) {
37 $factory = $this->getMockBuilder( MediaWiki\Http\HttpRequestFactory
::class )
39 $factory->expects( $this->any() )
41 ->willReturn( $httpRequest );
46 * Test call of a single url that should succeed
48 public function testMultiHttpClientSingleSuccess() {
50 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200 );
51 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
53 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client
->run( [
55 'url' => "http://example.test",
58 $this->assertEquals( 200, $rcode );
62 * Test call of a single url that should not exist, and therefore fail
64 public function testMultiHttpClientSingleFailure() {
65 // Mock an invalid tld
66 $httpRequest = $this->getHttpRequest(
67 StatusValue
::newFatal( 'http-invalid-url', 'http://www.example.test' ), 0 );
68 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
70 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client
->run( [
72 'url' => "http://www.example.test",
75 $failure = $rcode < 200 ||
$rcode >= 400;
76 $this->assertTrue( $failure );
80 * Test call of multiple urls that should all succeed
82 public function testMultiHttpClientMultipleSuccess() {
84 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200 );
85 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
90 'url' => 'http://example.test',
94 'url' => 'https://get.test',
97 $responses = $this->client
->runMulti( $reqs );
98 foreach ( $responses as $response ) {
99 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $response['response'];
100 $this->assertEquals( 200, $rcode );
105 * Test call of multiple urls that should all fail
107 public function testMultiHttpClientMultipleFailure() {
108 // Mock page not found
109 $httpRequest = $this->getHttpRequest(
110 StatusValue
::newFatal( "http-bad-status", 404, 'Not Found' ), 404 );
111 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
116 'url' => 'http://example.test/12345',
120 'url' => 'http://example.test/67890' ,
123 $responses = $this->client
->runMulti( $reqs );
124 foreach ( $responses as $response ) {
125 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $response['response'];
126 $failure = $rcode < 200 ||
$rcode >= 400;
127 $this->assertTrue( $failure );
132 * Test of response header handling
134 public function testMultiHttpClientHeaders() {
135 // Represenative headers for typical requests, per MWHttpRequest::getResponseHeaders()
138 'text/html; charset=utf-8',
141 'Wed, 18 Jul 2018 14:52:41 GMT',
144 'COUNTRY=NAe6; expires=Wed, 25-Jul-2018 14:52:41 GMT; path=/; domain=.example.test',
145 'LAST_NEWS=1531925562; expires=Thu, 18-Jul-2019 14:52:41 GMT; path=/; domain=.example.test',
149 // Mock success with specific headers
150 $httpRequest = $this->getHttpRequest( StatusValue
::newGood( 200 ), 200, $headers );
151 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
153 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client
->run( [
155 'url' => 'http://example.test',
158 $this->assertEquals( 200, $rcode );
159 $this->assertEquals( count( $headers ), count( $rhdrs ) );
160 foreach ( $headers as $name => $values ) {
161 $value = implode( ', ', $values );
162 $this->assertArrayHasKey( $name, $rhdrs );
163 $this->assertEquals( $value, $rhdrs[$name] );