1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core;
|
|
|
18 |
|
|
|
19 |
use GuzzleHttp\Cookie\CookieJar;
|
|
|
20 |
use GuzzleHttp\Handler\MockHandler;
|
|
|
21 |
use GuzzleHttp\Psr7\Request;
|
|
|
22 |
use GuzzleHttp\Psr7\Response;
|
|
|
23 |
use GuzzleHttp\Psr7\Uri;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit tests for guzzle integration in core.
|
|
|
27 |
*
|
|
|
28 |
* @package core
|
|
|
29 |
* @category test
|
|
|
30 |
* @copyright 2022 Safat Shahin <safat.shahin@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
* @coversDefaultClass \core\http_client
|
|
|
33 |
* @coversDefaultClass \core\local\guzzle\redirect_middleware
|
|
|
34 |
* @coversDefaultClass \core\local\guzzle\check_request
|
|
|
35 |
* @coversDefaultClass \core\local\guzzle\cache_item
|
|
|
36 |
* @coversDefaultClass \core\local\guzzle\cache_handler
|
|
|
37 |
* @coversDefaultClass \core\local\guzzle\cache_storage
|
|
|
38 |
*/
|
|
|
39 |
class http_client_test extends \advanced_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Read the object attributes and return the configs for test.
|
|
|
43 |
*
|
|
|
44 |
* @param object $object
|
|
|
45 |
* @param string $attributename
|
|
|
46 |
* @return mixed
|
|
|
47 |
* @covers \core\http_client
|
|
|
48 |
*/
|
|
|
49 |
public static function read_object_attribute(object $object, string $attributename) {
|
|
|
50 |
$reflector = new \ReflectionObject($object);
|
|
|
51 |
|
|
|
52 |
do {
|
|
|
53 |
try {
|
|
|
54 |
$attribute = $reflector->getProperty($attributename);
|
|
|
55 |
|
|
|
56 |
if (!$attribute || $attribute->isPublic()) {
|
|
|
57 |
return $object->$attributename;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
try {
|
|
|
62 |
return $attribute->getValue($object);
|
|
|
63 |
} finally {
|
|
|
64 |
}
|
|
|
65 |
} catch (\ReflectionException $e) {
|
|
|
66 |
// Do nothing.
|
|
|
67 |
}
|
|
|
68 |
} while ($reflector = $reflector->getParentClass());
|
|
|
69 |
|
|
|
70 |
throw new \moodle_exception(sprintf('Attribute "%s" not found in object.', $attributename));
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Test http client can send request synchronously.
|
|
|
75 |
*
|
|
|
76 |
* @covers \core\http_client
|
|
|
77 |
*/
|
|
|
78 |
public function test_http_client_can_send_synchronously() {
|
|
|
79 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
80 |
|
|
|
81 |
$client = new \core\http_client(['handler' => new MockHandler([new Response()])]);
|
|
|
82 |
$request = new Request('GET', $testhtml);
|
|
|
83 |
$r = $client->send($request);
|
|
|
84 |
|
|
|
85 |
$this->assertSame(200, $r->getStatusCode());
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Test http client can have options as a part of the request.
|
|
|
90 |
*
|
|
|
91 |
* @covers \core\http_client
|
|
|
92 |
*/
|
|
|
93 |
public function test_http_client_has_options() {
|
|
|
94 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
95 |
|
|
|
96 |
$client = new \core\http_client([
|
|
|
97 |
'base_uri' => $testhtml,
|
|
|
98 |
'timeout' => 2,
|
|
|
99 |
'headers' => ['bar' => 'baz'],
|
|
|
100 |
'mock' => new MockHandler()
|
|
|
101 |
]);
|
|
|
102 |
$config = self::read_object_attribute($client, 'config');
|
|
|
103 |
|
|
|
104 |
$this->assertArrayHasKey('base_uri', $config);
|
|
|
105 |
$this->assertInstanceOf(Uri::class, $config['base_uri']);
|
|
|
106 |
$this->assertSame($testhtml, (string) $config['base_uri']);
|
|
|
107 |
$this->assertArrayHasKey('handler', $config);
|
|
|
108 |
$this->assertNotNull($config['handler']);
|
|
|
109 |
$this->assertArrayHasKey('timeout', $config);
|
|
|
110 |
$this->assertSame(2, $config['timeout']);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Test guzzle can have headers changed in the request.
|
|
|
115 |
*
|
|
|
116 |
* @covers \core\http_client
|
|
|
117 |
*/
|
|
|
118 |
public function test_http_client_can_modify_the_header_for_each_request() {
|
|
|
119 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
120 |
|
|
|
121 |
$mock = new MockHandler([new Response()]);
|
|
|
122 |
$c = new \core\http_client([
|
|
|
123 |
'headers' => ['User-agent' => 'foo'],
|
|
|
124 |
'mock' => $mock
|
|
|
125 |
]);
|
|
|
126 |
$c->get($testhtml, ['headers' => ['User-Agent' => 'bar']]);
|
|
|
127 |
$this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Test guzzle can unset options.
|
|
|
132 |
*
|
|
|
133 |
* @covers \core\http_client
|
|
|
134 |
*/
|
|
|
135 |
public function test_can_unset_request_option_with_null() {
|
|
|
136 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
137 |
|
|
|
138 |
$mock = new MockHandler([new Response()]);
|
|
|
139 |
$c = new \core\http_client([
|
|
|
140 |
'headers' => ['foo' => 'bar'],
|
|
|
141 |
'mock' => $mock
|
|
|
142 |
]);
|
|
|
143 |
$c->get($testhtml, ['headers' => null]);
|
|
|
144 |
|
|
|
145 |
$this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Test the basic cookiejar functionality.
|
|
|
150 |
*
|
|
|
151 |
* @covers \core\http_client
|
|
|
152 |
*/
|
|
|
153 |
public function test_basic_cookie_jar() {
|
|
|
154 |
$mock = new MockHandler([
|
|
|
155 |
new Response(200, ['Set-Cookie' => 'foo=bar']),
|
|
|
156 |
new Response()
|
|
|
157 |
]);
|
|
|
158 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
159 |
$jar = new CookieJar();
|
|
|
160 |
$client->get('http://foo.com', ['cookies' => $jar]);
|
|
|
161 |
$client->get('http://foo.com', ['cookies' => $jar]);
|
|
|
162 |
$this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Test the basic shared cookiejar.
|
|
|
167 |
*
|
|
|
168 |
* @covers \core\http_client
|
|
|
169 |
*/
|
|
|
170 |
public function test_shared_cookie_jar() {
|
|
|
171 |
$mock = new MockHandler([
|
|
|
172 |
new Response(200, ['Set-Cookie' => 'foo=bar']),
|
|
|
173 |
new Response()
|
|
|
174 |
]);
|
|
|
175 |
$client = new \core\http_client(['mock' => $mock, 'cookies' => true]);
|
|
|
176 |
$client->get('http://foo.com');
|
|
|
177 |
$client->get('http://foo.com');
|
|
|
178 |
self::assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Test guzzle security helper.
|
|
|
183 |
*
|
|
|
184 |
* @covers \core\http_client
|
|
|
185 |
* @covers \core\local\guzzle\check_request
|
|
|
186 |
*/
|
|
|
187 |
public function test_guzzle_basics_with_security_helper() {
|
|
|
188 |
$this->resetAfterTest();
|
|
|
189 |
|
|
|
190 |
// Test a request with a basic hostname filter applied.
|
|
|
191 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
192 |
$url = new \moodle_url($testhtml);
|
|
|
193 |
$host = $url->get_host();
|
|
|
194 |
set_config('curlsecurityblockedhosts', $host); // Blocks $host.
|
|
|
195 |
|
|
|
196 |
// Now, create a request using the 'ignoresecurity' override.
|
|
|
197 |
// We expect this request to pass, despite the admin setting having been set earlier.
|
|
|
198 |
$mock = new MockHandler([new Response(200, [], 'foo')]);
|
|
|
199 |
$client = new \core\http_client(['mock' => $mock, 'ignoresecurity' => true]);
|
|
|
200 |
$response = $client->request('GET', $testhtml);
|
|
|
201 |
|
|
|
202 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
203 |
|
|
|
204 |
// Now, try injecting a mock security helper into curl. This will override the default helper.
|
|
|
205 |
$mockhelper = $this->getMockBuilder('\core\files\curl_security_helper')->getMock();
|
|
|
206 |
|
|
|
207 |
// Make the mock return a different string.
|
|
|
208 |
$blocked = "http://blocked.com";
|
|
|
209 |
$mockhelper->expects($this->any())->method('get_blocked_url_string')->will($this->returnValue($blocked));
|
|
|
210 |
|
|
|
211 |
// And make the mock security helper block all URLs. This helper instance doesn't care about config.
|
|
|
212 |
$mockhelper->expects($this->any())->method('url_is_blocked')->will($this->returnValue(true));
|
|
|
213 |
|
|
|
214 |
$client = new \core\http_client(['securityhelper' => $mockhelper]);
|
|
|
215 |
|
|
|
216 |
$this->resetDebugging();
|
|
|
217 |
try {
|
|
|
218 |
$client->request('GET', $testhtml);
|
|
|
219 |
$this->fail("Blocked Request should have thrown an exception");
|
|
|
220 |
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
|
|
221 |
$this->assertDebuggingCalled("Blocked $blocked [user 0]", DEBUG_NONE);
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Test guzzle proxy bypass with moodle.
|
|
|
228 |
*
|
|
|
229 |
* @covers \core\http_client
|
|
|
230 |
* @covers \core\local\guzzle\check_request
|
|
|
231 |
*/
|
|
|
232 |
public function test_http_client_proxy_bypass() {
|
|
|
233 |
$this->resetAfterTest();
|
|
|
234 |
|
|
|
235 |
global $CFG;
|
|
|
236 |
$testurl = $this->getExternalTestFileUrl('/test.html');
|
|
|
237 |
|
|
|
238 |
// Test without proxy bypass and inaccessible proxy.
|
|
|
239 |
$CFG->proxyhost = 'i.do.not.exist';
|
|
|
240 |
$CFG->proxybypass = '';
|
|
|
241 |
|
|
|
242 |
$client = new \core\http_client();
|
|
|
243 |
$this->expectException(\GuzzleHttp\Exception\RequestException::class);
|
|
|
244 |
$response = $client->get($testurl);
|
|
|
245 |
|
|
|
246 |
$this->assertNotEquals('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
|
|
|
247 |
|
|
|
248 |
// Test with proxy bypass.
|
|
|
249 |
$testurlhost = parse_url($testurl, PHP_URL_HOST);
|
|
|
250 |
$CFG->proxybypass = $testurlhost;
|
|
|
251 |
$client = new \core\http_client();
|
|
|
252 |
$response = $client->get($testurl);
|
|
|
253 |
|
|
|
254 |
$this->assertSame('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Test moodle redirect can be set with guzzle.
|
|
|
259 |
*
|
|
|
260 |
* @covers \core\http_client
|
|
|
261 |
* @covers \core\local\guzzle\redirect_middleware
|
|
|
262 |
*/
|
|
|
263 |
public function test_moodle_allow_redirects_can_be_true() {
|
|
|
264 |
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
|
|
|
265 |
|
|
|
266 |
$mock = new MockHandler([new Response(200, [], 'foo')]);
|
|
|
267 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
268 |
$client->get($testurl, ['moodle_allow_redirect' => true]);
|
|
|
269 |
|
|
|
270 |
$this->assertSame(true, $mock->getLastOptions()['moodle_allow_redirect']);
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* Test redirect with absolute url.
|
|
|
275 |
*
|
|
|
276 |
* @covers \core\http_client
|
|
|
277 |
* @covers \core\local\guzzle\redirect_middleware
|
|
|
278 |
*/
|
|
|
279 |
public function test_redirects_with_absolute_uri() {
|
|
|
280 |
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
|
|
|
281 |
|
|
|
282 |
$mock = new MockHandler([
|
|
|
283 |
new Response(302, ['Location' => 'http://moodle.com']),
|
|
|
284 |
new Response(200)
|
|
|
285 |
]);
|
|
|
286 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
287 |
$request = new Request('GET', "{$testurl}?redir=1&extdest=1");
|
|
|
288 |
$response = $client->send($request);
|
|
|
289 |
|
|
|
290 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
291 |
$this->assertSame('http://moodle.com', (string)$mock->getLastRequest()->getUri());
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Test redirect with relatetive url.
|
|
|
296 |
*
|
|
|
297 |
* @covers \core\http_client
|
|
|
298 |
* @covers \core\local\guzzle\redirect_middleware
|
|
|
299 |
*/
|
|
|
300 |
public function test_redirects_with_relative_uri() {
|
|
|
301 |
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
|
|
|
302 |
|
|
|
303 |
$mock = new MockHandler([
|
|
|
304 |
new Response(302, ['Location' => $testurl]),
|
|
|
305 |
new Response(200, [], 'done')
|
|
|
306 |
]);
|
|
|
307 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
308 |
$request = new Request('GET', $testurl);
|
|
|
309 |
$response = $client->send($request);
|
|
|
310 |
|
|
|
311 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
312 |
$this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
|
|
|
313 |
$this->assertSame('done', $response->getBody()->getContents());
|
|
|
314 |
|
|
|
315 |
// Test different types of redirect types.
|
|
|
316 |
$mock = new MockHandler([
|
|
|
317 |
new Response(302, ['Location' => $testurl]),
|
|
|
318 |
new Response(200, [], 'done')
|
|
|
319 |
]);
|
|
|
320 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
321 |
$request = new Request('GET', "$testurl?type=301");
|
|
|
322 |
$response = $client->send($request);
|
|
|
323 |
|
|
|
324 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
325 |
$this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
|
|
|
326 |
$this->assertSame('done', $response->getBody()->getContents());
|
|
|
327 |
|
|
|
328 |
$mock = new MockHandler([
|
|
|
329 |
new Response(302, ['Location' => $testurl]),
|
|
|
330 |
new Response(200, [], 'done')
|
|
|
331 |
]);
|
|
|
332 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
333 |
$request = new Request('GET', "$testurl?type=302");
|
|
|
334 |
$response = $client->send($request);
|
|
|
335 |
|
|
|
336 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
337 |
$this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
|
|
|
338 |
$this->assertSame('done', $response->getBody()->getContents());
|
|
|
339 |
|
|
|
340 |
$mock = new MockHandler([
|
|
|
341 |
new Response(302, ['Location' => $testurl]),
|
|
|
342 |
new Response(200, [], 'done')
|
|
|
343 |
]);
|
|
|
344 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
345 |
$request = new Request('GET', "$testurl?type=303");
|
|
|
346 |
$response = $client->send($request);
|
|
|
347 |
|
|
|
348 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
349 |
$this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
|
|
|
350 |
$this->assertSame('done', $response->getBody()->getContents());
|
|
|
351 |
|
|
|
352 |
$mock = new MockHandler([
|
|
|
353 |
new Response(302, ['Location' => $testurl]),
|
|
|
354 |
new Response(200, [], 'done')
|
|
|
355 |
]);
|
|
|
356 |
$client = new \core\http_client(['mock' => $mock]);
|
|
|
357 |
$request = new Request('GET', "$testurl?type=307");
|
|
|
358 |
$response = $client->send($request);
|
|
|
359 |
|
|
|
360 |
$this->assertSame(200, $response->getStatusCode());
|
|
|
361 |
$this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
|
|
|
362 |
$this->assertSame('done', $response->getBody()->getContents());
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* Test guzzle cache middleware.
|
|
|
367 |
*
|
|
|
368 |
* @covers \core\local\guzzle\cache_item
|
|
|
369 |
* @covers \core\local\guzzle\cache_handler
|
|
|
370 |
* @covers \core\local\guzzle\cache_storage
|
|
|
371 |
*/
|
|
|
372 |
public function test_http_client_cache_item() {
|
|
|
373 |
global $CFG, $USER;
|
|
|
374 |
$module = 'core_guzzle';
|
|
|
375 |
$cachedir = "$CFG->cachedir/$module/";
|
|
|
376 |
|
|
|
377 |
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
|
|
378 |
|
|
|
379 |
// Test item is cached in the specified module.
|
|
|
380 |
$client = new \core\http_client([
|
|
|
381 |
'cache' => true,
|
|
|
382 |
'module_cache' => $module
|
|
|
383 |
]);
|
|
|
384 |
$response = $client->get($testhtml);
|
|
|
385 |
|
|
|
386 |
$cachecontent = '';
|
|
|
387 |
if ($dir = opendir($cachedir)) {
|
|
|
388 |
while (false !== ($file = readdir($dir))) {
|
|
|
389 |
if (!is_dir($file) && $file !== '.' && $file !== '..') {
|
|
|
390 |
if (strpos($file, 'u' . $USER->id . '_') !== false) {
|
|
|
391 |
$cachecontent = file_get_contents($cachedir . $file);
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
}
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
$this->assertNotEmpty($cachecontent);
|
|
|
398 |
@unlink($cachedir . $file);
|
|
|
399 |
|
|
|
400 |
// Test cache item objects returns correct values.
|
|
|
401 |
$key = 'sample_key';
|
|
|
402 |
$cachefilename = 'u' . $USER->id . '_' . md5(serialize($key));
|
|
|
403 |
$cachefile = $cachedir.$cachefilename;
|
|
|
404 |
|
|
|
405 |
$content = $response->getBody()->getContents();
|
|
|
406 |
file_put_contents($cachefile, serialize($content));
|
|
|
407 |
|
|
|
408 |
$cacheitemobject = new \core\local\guzzle\cache_item($key, $module, null);
|
|
|
409 |
|
|
|
410 |
// Test the cache item matches with the cached response.
|
|
|
411 |
$this->assertSame($content, $cacheitemobject->get());
|
|
|
412 |
|
|
|
413 |
@unlink($cachefile);
|
|
|
414 |
}
|
|
|
415 |
}
|