MediaWiki  1.32.0
MultiHttpClientTest.php
Go to the documentation of this file.
1 <?php
2 
11  protected $client;
12 
13  protected function setUp() {
14  parent::setUp();
15  $client = $this->getMockBuilder( MultiHttpClient::class )
16  ->setConstructorArgs( [ [] ] )
17  ->setMethods( [ 'isCurlEnabled' ] )->getMock();
18  $client->method( 'isCurlEnabled' )->willReturn( false );
19  $this->client = $client;
20  }
21 
22  private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) {
23  $httpRequest = $this->getMockBuilder( PhpHttpRequest::class )
24  ->setConstructorArgs( [ '', [] ] )
25  ->getMock();
26  $httpRequest->expects( $this->any() )
27  ->method( 'execute' )
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 );
35  return $httpRequest;
36  }
37 
38  private function mockHttpRequestFactory( $httpRequest ) {
39  $factory = $this->getMockBuilder( MediaWiki\Http\HttpRequestFactory::class )
40  ->getMock();
41  $factory->expects( $this->any() )
42  ->method( 'create' )
43  ->willReturn( $httpRequest );
44  return $factory;
45  }
46 
51  // Mock success
52  $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
53  $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
54 
55  list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client->run( [
56  'method' => 'GET',
57  'url' => "http://example.test",
58  ] );
59 
60  $this->assertEquals( 200, $rcode );
61  }
62 
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 ) );
71 
72  list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client->run( [
73  'method' => 'GET',
74  'url' => "http://www.example.test",
75  ] );
76 
77  $failure = $rcode < 200 || $rcode >= 400;
78  $this->assertTrue( $failure );
79  }
80 
85  // Mock success
86  $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
87  $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
88 
89  $reqs = [
90  [
91  'method' => 'GET',
92  'url' => 'http://example.test',
93  ],
94  [
95  'method' => 'GET',
96  'url' => 'https://get.test',
97  ],
98  ];
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 );
103  }
104  }
105 
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 ) );
114 
115  $reqs = [
116  [
117  'method' => 'GET',
118  'url' => 'http://example.test/12345',
119  ],
120  [
121  'method' => 'GET',
122  'url' => 'http://example.test/67890' ,
123  ]
124  ];
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 );
130  }
131  }
132 
136  public function testMultiHttpClientHeaders() {
137  // Represenative headers for typical requests, per MWHttpRequest::getResponseHeaders()
138  $headers = [
139  'content-type' => [
140  'text/html; charset=utf-8',
141  ],
142  'date' => [
143  'Wed, 18 Jul 2018 14:52:41 GMT',
144  ],
145  'set-cookie' => [
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',
148  ]
149  ];
150 
151  // Mock success with specific headers
152  $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200, $headers );
153  $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
154 
155  list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( [
156  'method' => 'GET',
157  'url' => 'http://example.test',
158  ] );
159 
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] );
166  }
167  }
168 }
MultiHttpClientTest\testMultiHttpClientSingleSuccess
testMultiHttpClientSingleSuccess()
Test call of a single url that should succeed.
Definition: MultiHttpClientTest.php:50
captcha-old.count
count
Definition: captcha-old.py:249
MultiHttpClientTest\testMultiHttpClientMultipleFailure
testMultiHttpClientMultipleFailure()
Test call of multiple urls that should all fail.
Definition: MultiHttpClientTest.php:109
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
MultiHttpClientTest\testMultiHttpClientMultipleSuccess
testMultiHttpClientMultipleSuccess()
Test call of multiple urls that should all succeed.
Definition: MultiHttpClientTest.php:84
MultiHttpClientTest\setUp
setUp()
Definition: MultiHttpClientTest.php:13
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
MultiHttpClientTest\$client
$client
Definition: MultiHttpClientTest.php:11
MediaWiki
A helper class for throttling authentication attempts.
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
$value
$value
Definition: styleTest.css.php:49
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
MultiHttpClientTest\testMultiHttpClientSingleFailure
testMultiHttpClientSingleFailure()
Test call of a single url that should not exist, and therefore fail.
Definition: MultiHttpClientTest.php:66
MultiHttpClientTest\mockHttpRequestFactory
mockHttpRequestFactory( $httpRequest)
Definition: MultiHttpClientTest.php:38
$response
this hook is for auditing only $response
Definition: hooks.txt:813
MultiHttpClientTest\getHttpRequest
getHttpRequest( $statusValue, $statusCode, $headers=[])
Definition: MultiHttpClientTest.php:22
MultiHttpClientTest\testMultiHttpClientHeaders
testMultiHttpClientHeaders()
Test of response header handling.
Definition: MultiHttpClientTest.php:136
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MultiHttpClientTest
Tests for MultiHttpClient.
Definition: MultiHttpClientTest.php:10
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:646
Http
Various HTTP related functions.
Definition: Http.php:27