MDL-67271 core: Add test to find missing SVG icons
[moodle.git] / lib / tests / http_client_test.php
blob8d405ff9a46fcbc7fd5c7c1025e329a05ce3ebf0
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.
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/>.
17 namespace core;
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;
25 /**
26 * Unit tests for guzzle integration in core.
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
39 class http_client_test extends \advanced_testcase {
41 /**
42 * Read the object attributes and return the configs for test.
44 * @param object $object
45 * @param string $attributename
46 * @return mixed
47 * @covers \core\http_client
49 public static function read_object_attribute(object $object, string $attributename) {
50 $reflector = new \ReflectionObject($object);
52 do {
53 try {
54 $attribute = $reflector->getProperty($attributename);
56 if (!$attribute || $attribute->isPublic()) {
57 return $object->$attributename;
60 $attribute->setAccessible(true);
62 try {
63 return $attribute->getValue($object);
64 } finally {
65 $attribute->setAccessible(false);
67 } catch (\ReflectionException $e) {
68 // Do nothing.
70 } while ($reflector = $reflector->getParentClass());
72 throw new \moodle_exception(sprintf('Attribute "%s" not found in object.', $attributename));
75 /**
76 * Test http client can send request synchronously.
78 * @covers \core\http_client
80 public function test_http_client_can_send_synchronously() {
81 $testhtml = $this->getExternalTestFileUrl('/test.html');
83 $client = new \core\http_client(['handler' => new MockHandler([new Response()])]);
84 $request = new Request('GET', $testhtml);
85 $r = $client->send($request);
87 $this->assertSame(200, $r->getStatusCode());
90 /**
91 * Test http client can have options as a part of the request.
93 * @covers \core\http_client
95 public function test_http_client_has_options() {
96 $testhtml = $this->getExternalTestFileUrl('/test.html');
98 $client = new \core\http_client([
99 'base_uri' => $testhtml,
100 'timeout' => 2,
101 'headers' => ['bar' => 'baz'],
102 'mock' => new MockHandler()
104 $config = self::read_object_attribute($client, 'config');
106 $this->assertArrayHasKey('base_uri', $config);
107 $this->assertInstanceOf(Uri::class, $config['base_uri']);
108 $this->assertSame($testhtml, (string) $config['base_uri']);
109 $this->assertArrayHasKey('handler', $config);
110 $this->assertNotNull($config['handler']);
111 $this->assertArrayHasKey('timeout', $config);
112 $this->assertSame(2, $config['timeout']);
116 * Test guzzle can have headers changed in the request.
118 * @covers \core\http_client
120 public function test_http_client_can_modify_the_header_for_each_request() {
121 $testhtml = $this->getExternalTestFileUrl('/test.html');
123 $mock = new MockHandler([new Response()]);
124 $c = new \core\http_client([
125 'headers' => ['User-agent' => 'foo'],
126 'mock' => $mock
128 $c->get($testhtml, ['headers' => ['User-Agent' => 'bar']]);
129 $this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
133 * Test guzzle can unset options.
135 * @covers \core\http_client
137 public function test_can_unset_request_option_with_null() {
138 $testhtml = $this->getExternalTestFileUrl('/test.html');
140 $mock = new MockHandler([new Response()]);
141 $c = new \core\http_client([
142 'headers' => ['foo' => 'bar'],
143 'mock' => $mock
145 $c->get($testhtml, ['headers' => null]);
147 $this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
151 * Test the basic cookiejar functionality.
153 * @covers \core\http_client
155 public function test_basic_cookie_jar() {
156 $mock = new MockHandler([
157 new Response(200, ['Set-Cookie' => 'foo=bar']),
158 new Response()
160 $client = new \core\http_client(['mock' => $mock]);
161 $jar = new CookieJar();
162 $client->get('http://foo.com', ['cookies' => $jar]);
163 $client->get('http://foo.com', ['cookies' => $jar]);
164 $this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
168 * Test the basic shared cookiejar.
170 * @covers \core\http_client
172 public function test_shared_cookie_jar() {
173 $mock = new MockHandler([
174 new Response(200, ['Set-Cookie' => 'foo=bar']),
175 new Response()
177 $client = new \core\http_client(['mock' => $mock, 'cookies' => true]);
178 $client->get('http://foo.com');
179 $client->get('http://foo.com');
180 self::assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
184 * Test guzzle security helper.
186 * @covers \core\http_client
187 * @covers \core\local\guzzle\check_request
189 public function test_guzzle_basics_with_security_helper() {
190 $this->resetAfterTest();
192 // Test a request with a basic hostname filter applied.
193 $testhtml = $this->getExternalTestFileUrl('/test.html');
194 $url = new \moodle_url($testhtml);
195 $host = $url->get_host();
196 set_config('curlsecurityblockedhosts', $host); // Blocks $host.
198 // Now, create a request using the 'ignoresecurity' override.
199 // We expect this request to pass, despite the admin setting having been set earlier.
200 $mock = new MockHandler([new Response(200, [], 'foo')]);
201 $client = new \core\http_client(['mock' => $mock, 'ignoresecurity' => true]);
202 $response = $client->request('GET', $testhtml);
204 $this->assertSame(200, $response->getStatusCode());
206 // Now, try injecting a mock security helper into curl. This will override the default helper.
207 $mockhelper = $this->getMockBuilder('\core\files\curl_security_helper')->getMock();
209 // Make the mock return a different string.
210 $blocked = "http://blocked.com";
211 $mockhelper->expects($this->any())->method('get_blocked_url_string')->will($this->returnValue($blocked));
213 // And make the mock security helper block all URLs. This helper instance doesn't care about config.
214 $mockhelper->expects($this->any())->method('url_is_blocked')->will($this->returnValue(true));
216 $client = new \core\http_client(['securityhelper' => $mockhelper]);
218 $this->resetDebugging();
219 try {
220 $client->request('GET', $testhtml);
221 $this->fail("Blocked Request should have thrown an exception");
222 } catch (\GuzzleHttp\Exception\RequestException $e) {
223 $this->assertDebuggingCalled("Blocked $blocked [user 0]", DEBUG_NONE);
229 * Test guzzle proxy bypass with moodle.
231 * @covers \core\http_client
232 * @covers \core\local\guzzle\check_request
234 public function test_http_client_proxy_bypass() {
235 $this->resetAfterTest();
237 global $CFG;
238 $testurl = $this->getExternalTestFileUrl('/test.html');
240 // Test without proxy bypass and inaccessible proxy.
241 $CFG->proxyhost = 'i.do.not.exist';
242 $CFG->proxybypass = '';
244 $client = new \core\http_client();
245 $this->expectException(\GuzzleHttp\Exception\RequestException::class);
246 $response = $client->get($testurl);
248 $this->assertNotEquals('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
250 // Test with proxy bypass.
251 $testurlhost = parse_url($testurl, PHP_URL_HOST);
252 $CFG->proxybypass = $testurlhost;
253 $client = new \core\http_client();
254 $response = $client->get($testurl);
256 $this->assertSame('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
260 * Test moodle redirect can be set with guzzle.
262 * @covers \core\http_client
263 * @covers \core\local\guzzle\redirect_middleware
265 public function test_moodle_allow_redirects_can_be_true() {
266 $testurl = $this->getExternalTestFileUrl('/test_redir.php');
268 $mock = new MockHandler([new Response(200, [], 'foo')]);
269 $client = new \core\http_client(['mock' => $mock]);
270 $client->get($testurl, ['moodle_allow_redirect' => true]);
272 $this->assertSame(true, $mock->getLastOptions()['moodle_allow_redirect']);
276 * Test redirect with absolute url.
278 * @covers \core\http_client
279 * @covers \core\local\guzzle\redirect_middleware
281 public function test_redirects_with_absolute_uri() {
282 $testurl = $this->getExternalTestFileUrl('/test_redir.php');
284 $mock = new MockHandler([
285 new Response(302, ['Location' => 'http://moodle.com']),
286 new Response(200)
288 $client = new \core\http_client(['mock' => $mock]);
289 $request = new Request('GET', "{$testurl}?redir=1&extdest=1");
290 $response = $client->send($request);
292 $this->assertSame(200, $response->getStatusCode());
293 $this->assertSame('http://moodle.com', (string)$mock->getLastRequest()->getUri());
297 * Test redirect with relatetive url.
299 * @covers \core\http_client
300 * @covers \core\local\guzzle\redirect_middleware
302 public function test_redirects_with_relative_uri() {
303 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
305 $mock = new MockHandler([
306 new Response(302, ['Location' => $testurl]),
307 new Response(200, [], 'done')
309 $client = new \core\http_client(['mock' => $mock]);
310 $request = new Request('GET', $testurl);
311 $response = $client->send($request);
313 $this->assertSame(200, $response->getStatusCode());
314 $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
315 $this->assertSame('done', $response->getBody()->getContents());
317 // Test different types of redirect types.
318 $mock = new MockHandler([
319 new Response(302, ['Location' => $testurl]),
320 new Response(200, [], 'done')
322 $client = new \core\http_client(['mock' => $mock]);
323 $request = new Request('GET', "$testurl?type=301");
324 $response = $client->send($request);
326 $this->assertSame(200, $response->getStatusCode());
327 $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
328 $this->assertSame('done', $response->getBody()->getContents());
330 $mock = new MockHandler([
331 new Response(302, ['Location' => $testurl]),
332 new Response(200, [], 'done')
334 $client = new \core\http_client(['mock' => $mock]);
335 $request = new Request('GET', "$testurl?type=302");
336 $response = $client->send($request);
338 $this->assertSame(200, $response->getStatusCode());
339 $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
340 $this->assertSame('done', $response->getBody()->getContents());
342 $mock = new MockHandler([
343 new Response(302, ['Location' => $testurl]),
344 new Response(200, [], 'done')
346 $client = new \core\http_client(['mock' => $mock]);
347 $request = new Request('GET', "$testurl?type=303");
348 $response = $client->send($request);
350 $this->assertSame(200, $response->getStatusCode());
351 $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
352 $this->assertSame('done', $response->getBody()->getContents());
354 $mock = new MockHandler([
355 new Response(302, ['Location' => $testurl]),
356 new Response(200, [], 'done')
358 $client = new \core\http_client(['mock' => $mock]);
359 $request = new Request('GET', "$testurl?type=307");
360 $response = $client->send($request);
362 $this->assertSame(200, $response->getStatusCode());
363 $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
364 $this->assertSame('done', $response->getBody()->getContents());
368 * Test guzzle cache middleware.
370 * @covers \core\local\guzzle\cache_item
371 * @covers \core\local\guzzle\cache_handler
372 * @covers \core\local\guzzle\cache_storage
374 public function test_http_client_cache_item() {
375 global $CFG, $USER;
376 $module = 'core_guzzle';
377 $cachedir = "$CFG->cachedir/$module/";
379 $testhtml = $this->getExternalTestFileUrl('/test.html');
381 // Test item is cached in the specified module.
382 $client = new \core\http_client([
383 'cache' => true,
384 'module_cache' => $module
386 $response = $client->get($testhtml);
388 $cachecontent = '';
389 if ($dir = opendir($cachedir)) {
390 while (false !== ($file = readdir($dir))) {
391 if (!is_dir($file) && $file !== '.' && $file !== '..') {
392 if (strpos($file, 'u' . $USER->id . '_') !== false) {
393 $cachecontent = file_get_contents($cachedir . $file);
399 $this->assertNotEmpty($cachecontent);
400 @unlink($cachedir . $file);
402 // Test cache item objects returns correct values.
403 $key = 'sample_key';
404 $cachefilename = 'u' . $USER->id . '_' . md5(serialize($key));
405 $cachefile = $cachedir.$cachefilename;
407 $content = $response->getBody()->getContents();
408 file_put_contents($cachefile, serialize($content));
410 $cacheitemobject = new \core\local\guzzle\cache_item($key, $module, null);
412 // Test the cache item matches with the cached response.
413 $this->assertSame($content, $cacheitemobject->get());
415 @unlink($cachefile);