1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
<?php
use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\MockObject\MockObject;
use Wikimedia\TestingAccessWrapper;
/**
* The urls herein are not actually called, because we mock the return results.
*
* @covers MultiHttpClient
*/
class MultiHttpClientTest extends MediaWikiIntegrationTestCase {
/** @var MultiHttpClient|MockObject */
protected $client;
/**
* @param array $options
* @return MultiHttpClient|MockObject
*/
private function createClient( $options = [] ) {
$client = $this->getMockBuilder( MultiHttpClient::class )
->setConstructorArgs( [ $options ] )
->onlyMethods( [ 'isCurlEnabled' ] )->getMock();
$client->method( 'isCurlEnabled' )->willReturn( false );
return $client;
}
protected function setUp(): void {
parent::setUp();
$this->client = $this->createClient( [] );
}
private function getHttpRequest( $statusValue, $statusCode, $headers = [] ) {
$options = [
'timeout' => 1,
'connectTimeout' => 1
];
$httpRequest = $this->getMockBuilder( MWHttpRequest::class )
->setConstructorArgs( [ '', $options ] )
->getMock();
$httpRequest->method( 'execute' )
->willReturn( Status::wrap( $statusValue ) );
$httpRequest->method( 'getResponseHeaders' )
->willReturn( $headers );
$httpRequest->method( 'getStatus' )
->willReturn( $statusCode );
return $httpRequest;
}
private function mockHttpRequestFactory( $httpRequest ) {
$factory = $this->createMock( MediaWiki\Http\HttpRequestFactory::class );
$factory->method( 'create' )
->willReturn( $httpRequest );
return $factory;
}
/**
* Test call of a single url that should succeed
*/
public function testMultiHttpClientSingleSuccess() {
// Mock success
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
[ $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ] = $this->client->run( [
'method' => 'GET',
'url' => "http://example.test",
] );
$this->assertSame( 200, $rcode );
}
/**
* Test call of a single url that should not exist, and therefore fail
*/
public function testMultiHttpClientSingleFailure() {
// Mock an invalid tld
$httpRequest = $this->getHttpRequest(
StatusValue::newFatal( 'http-invalid-url', 'http://www.example.test' ), 0 );
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
[ $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ] = $this->client->run( [
'method' => 'GET',
'url' => "http://www.example.test",
] );
$this->assertSame( 0, $rcode );
}
/**
* Test call of multiple urls that should all succeed
*/
public function testMultiHttpClientMultipleSuccess() {
// Mock success
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
$reqs = [
[
'method' => 'GET',
'url' => 'http://example.test',
],
[
'method' => 'GET',
'url' => 'https://get.test',
],
];
$responses = $this->client->runMulti( $reqs );
foreach ( $responses as $response ) {
[ $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ] = $response['response'];
$this->assertSame( 200, $rcode );
}
}
/**
* Test call of multiple urls that should all fail
*/
public function testMultiHttpClientMultipleFailure() {
// Mock page not found
$httpRequest = $this->getHttpRequest(
StatusValue::newFatal( "http-bad-status", 404, 'Not Found' ), 404 );
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
$reqs = [
[
'method' => 'GET',
'url' => 'http://example.test/12345',
],
[
'method' => 'GET',
'url' => 'http://example.test/67890' ,
]
];
$responses = $this->client->runMulti( $reqs );
foreach ( $responses as $response ) {
[ $rcode, $rdesc, /* $rhdrs */, $rbody, $rerr ] = $response['response'];
$this->assertSame( 404, $rcode );
}
}
/**
* Test of response header handling
*/
public function testMultiHttpClientHeaders() {
// Represenative headers for typical requests, per MWHttpRequest::getResponseHeaders()
$headers = [
'content-type' => [
'text/html; charset=utf-8',
],
'date' => [
'Wed, 18 Jul 2018 14:52:41 GMT',
],
'set-cookie' => [
'COUNTRY=NAe6; expires=Wed, 25-Jul-2018 14:52:41 GMT; path=/; domain=.example.test',
'LAST_NEWS=1531925562; expires=Thu, 18-Jul-2019 14:52:41 GMT; path=/; domain=.example.test',
]
];
// Mock success with specific headers
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200, $headers );
$this->setService( 'HttpRequestFactory', $this->mockHttpRequestFactory( $httpRequest ) );
[ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $this->client->run( [
'method' => 'GET',
'url' => 'http://example.test',
] );
$this->assertSame( 200, $rcode );
$this->assertSame( count( $headers ), count( $rhdrs ) );
foreach ( $headers as $name => $values ) {
$value = implode( ', ', $values );
$this->assertArrayHasKey( $name, $rhdrs );
$this->assertEquals( $value, $rhdrs[$name] );
}
}
public static function provideMultiHttpTimeout() {
return [
'default 10/30' => [
[],
[],
10,
30
],
'constructor override' => [
[ 'connTimeout' => 2, 'reqTimeout' => 3 ],
[],
2,
3
],
'run override' => [
[],
[ 'connTimeout' => 2, 'reqTimeout' => 3 ],
2,
3
],
'constructor max option limits default' => [
[ 'maxConnTimeout' => 2, 'maxReqTimeout' => 3 ],
[],
2,
3
],
'constructor max option limits regular constructor option' => [
[
'maxConnTimeout' => 2,
'maxReqTimeout' => 3,
'connTimeout' => 100,
'reqTimeout' => 100
],
[],
2,
3
],
'constructor max option greater than regular constructor option' => [
[
'maxConnTimeout' => 2,
'maxReqTimeout' => 3,
'connTimeout' => 1,
'reqTimeout' => 1
],
[],
1,
1
],
'constructor max option limits run option' => [
[
'maxConnTimeout' => 2,
'maxReqTimeout' => 3,
],
[
'connTimeout' => 100,
'reqTimeout' => 100
],
2,
3
],
];
}
/**
* Test of timeout parameter handling
* @dataProvider provideMultiHttpTimeout
*/
public function testMultiHttpTimeout( $createOptions, $runOptions,
$expectedConnTimeout, $expectedReqTimeout
) {
$url = 'http://www.example.test';
$httpRequest = $this->getHttpRequest( StatusValue::newGood( 200 ), 200 );
$factory = $this->createMock( MediaWiki\Http\HttpRequestFactory::class );
$factory->method( 'create' )
->with(
$url,
$this->callback(
static function ( $options ) use ( $expectedReqTimeout, $expectedConnTimeout ) {
return $options['timeout'] === $expectedReqTimeout
&& $options['connectTimeout'] === $expectedConnTimeout;
}
)
)
->willReturn( $httpRequest );
$this->setService( 'HttpRequestFactory', $factory );
$client = $this->createClient( $createOptions );
$client->run(
[ 'method' => 'GET', 'url' => $url ],
$runOptions
);
$this->addToAssertionCount( 1 );
}
public function testUseReverseProxy() {
// TODO: Cannot use TestingAccessWrapper here because it doesn't
// support pass-by-reference (T287318)
$class = new ReflectionClass( MultiHttpClient::class );
$func = $class->getMethod( 'useReverseProxy' );
$func->setAccessible( true );
$req = [
'url' => 'https://example.org/path?query=string',
];
$func->invokeArgs( new MultiHttpClient( [] ), [ &$req, 'http://localhost:1234' ] );
$this->assertSame( 'http://localhost:1234/path?query=string', $req['url'] );
$this->assertSame( 'example.org', $req['headers']['Host'] );
}
public function testNormalizeRequests() {
// TODO: Cannot use TestingAccessWrapper here because it doesn't
// support pass-by-reference (T287318)
$class = new ReflectionClass( MultiHttpClient::class );
$func = $class->getMethod( 'normalizeRequests' );
$func->setAccessible( true );
$reqs = [
[ 'GET', 'https://example.org/path?query=string' ],
[
'method' => 'GET',
'url' => 'https://example.com/path?query=another%20string'
],
];
$client = new MultiHttpClient( [
'localVirtualHosts' => [ 'example.org' ],
'localProxy' => 'http://localhost:1234',
] );
$func->invokeArgs( $client, [ &$reqs ] );
// Req #0 transformed to use reverse proxy
$this->assertSame( 'http://localhost:1234/path?query=string', $reqs[0]['url'] );
$this->assertSame( 'example.org', $reqs[0]['headers']['host'] );
$this->assertFalse( $reqs[0]['proxy'] );
// Req #1 left alone, domain doesn't match
$this->assertSame( 'https://example.com/path?query=another%20string', $reqs[1]['url'] );
}
public function testGetCurlMulti() {
$cm = TestingAccessWrapper::newFromObject( new MultiHttpClient( [] ) );
$resource = $cm->getCurlMulti( [ 'usePipelining' => true ] );
$this->assertThat(
$resource,
$this->logicalOr(
$this->isType( IsType::TYPE_RESOURCE ),
$this->isInstanceOf( 'CurlMultiHandle' )
)
);
}
}
|