MediaWiki REL1_33
MultiHttpClientTest.php
Go to the documentation of this file.
1<?php
2
9 protected $client;
10
11 protected function setUp() {
12 parent::setUp();
13 $client = $this->getMockBuilder( MultiHttpClient::class )
14 ->setConstructorArgs( [ [] ] )
15 ->setMethods( [ 'isCurlEnabled' ] )->getMock();
16 $client->method( 'isCurlEnabled' )->willReturn( false );
17 $this->client = $client;
18 }
19
20 private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) {
21 $httpRequest = $this->getMockBuilder( PhpHttpRequest::class )
22 ->setConstructorArgs( [ '', [] ] )
23 ->getMock();
24 $httpRequest->expects( $this->any() )
25 ->method( 'execute' )
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 );
33 return $httpRequest;
34 }
35
36 private function mockHttpRequestFactory( $httpRequest ) {
37 $factory = $this->getMockBuilder( MediaWiki\Http\HttpRequestFactory::class )
38 ->getMock();
39 $factory->expects( $this->any() )
40 ->method( 'create' )
41 ->willReturn( $httpRequest );
42 return $factory;
43 }
44
49 // Mock success
50 $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
51 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
52
53 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client->run( [
54 'method' => 'GET',
55 'url' => "http://example.test",
56 ] );
57
58 $this->assertEquals( 200, $rcode );
59 }
60
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 ) );
69
70 list( $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ) = $this->client->run( [
71 'method' => 'GET',
72 'url' => "http://www.example.test",
73 ] );
74
75 $failure = $rcode < 200 || $rcode >= 400;
76 $this->assertTrue( $failure );
77 }
78
83 // Mock success
84 $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
85 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
86
87 $reqs = [
88 [
89 'method' => 'GET',
90 'url' => 'http://example.test',
91 ],
92 [
93 'method' => 'GET',
94 'url' => 'https://get.test',
95 ],
96 ];
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 );
101 }
102 }
103
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 ) );
112
113 $reqs = [
114 [
115 'method' => 'GET',
116 'url' => 'http://example.test/12345',
117 ],
118 [
119 'method' => 'GET',
120 'url' => 'http://example.test/67890' ,
121 ]
122 ];
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 );
128 }
129 }
130
134 public function testMultiHttpClientHeaders() {
135 // Represenative headers for typical requests, per MWHttpRequest::getResponseHeaders()
136 $headers = [
137 'content-type' => [
138 'text/html; charset=utf-8',
139 ],
140 'date' => [
141 'Wed, 18 Jul 2018 14:52:41 GMT',
142 ],
143 'set-cookie' => [
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',
146 ]
147 ];
148
149 // Mock success with specific headers
150 $httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200, $headers );
151 $this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
152
153 list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $this->client->run( [
154 'method' => 'GET',
155 'url' => 'http://example.test',
156 ] );
157
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] );
164 }
165 }
166}
Various HTTP related functions.
Definition Http.php:27
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
The urls herein are not actually called, because we mock the return results.
testMultiHttpClientMultipleSuccess()
Test call of multiple urls that should all succeed.
testMultiHttpClientSingleFailure()
Test call of a single url that should not exist, and therefore fail.
testMultiHttpClientSingleSuccess()
Test call of a single url that should succeed.
mockHttpRequestFactory( $httpRequest)
testMultiHttpClientHeaders()
Test of response header handling.
getHttpRequest( $statusValue, $statusCode, $headers=[])
testMultiHttpClientMultipleFailure()
Test call of multiple urls that should all fail.
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
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
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
this hook is for auditing only $response
Definition hooks.txt:780
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:37
A helper class for throttling authentication attempts.