Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

   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                  $attribute->setAccessible(true);
  61  
  62                  try {
  63                      return $attribute->getValue($object);
  64                  } finally {
  65                      $attribute->setAccessible(false);
  66                  }
  67              } catch (\ReflectionException $e) {
  68                  // Do nothing.
  69              }
  70          } while ($reflector = $reflector->getParentClass());
  71  
  72          throw new \moodle_exception(sprintf('Attribute "%s" not found in object.', $attributename));
  73      }
  74  
  75      /**
  76       * Test http client can send request synchronously.
  77       *
  78       * @covers \core\http_client
  79       */
  80      public function test_http_client_can_send_synchronously() {
  81          $testhtml = $this->getExternalTestFileUrl('/test.html');
  82  
  83          $client = new \core\http_client(['handler' => new MockHandler([new Response()])]);
  84          $request = new Request('GET', $testhtml);
  85          $r = $client->send($request);
  86  
  87          $this->assertSame(200, $r->getStatusCode());
  88      }
  89  
  90      /**
  91       * Test http client can have options as a part of the request.
  92       *
  93       * @covers \core\http_client
  94       */
  95      public function test_http_client_has_options() {
  96          $testhtml = $this->getExternalTestFileUrl('/test.html');
  97  
  98          $client = new \core\http_client([
  99                  'base_uri' => $testhtml,
 100                  'timeout'  => 2,
 101                  'headers'  => ['bar' => 'baz'],
 102                  'mock'  => new MockHandler()
 103          ]);
 104          $config = self::read_object_attribute($client, 'config');
 105  
 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']);
 113      }
 114  
 115      /**
 116       * Test guzzle can have headers changed in the request.
 117       *
 118       * @covers \core\http_client
 119       */
 120      public function test_http_client_can_modify_the_header_for_each_request() {
 121          $testhtml = $this->getExternalTestFileUrl('/test.html');
 122  
 123          $mock = new MockHandler([new Response()]);
 124          $c = new \core\http_client([
 125                  'headers' => ['User-agent' => 'foo'],
 126                  'mock' => $mock
 127          ]);
 128          $c->get($testhtml, ['headers' => ['User-Agent' => 'bar']]);
 129          $this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
 130      }
 131  
 132      /**
 133       * Test guzzle can unset options.
 134       *
 135       * @covers \core\http_client
 136       */
 137      public function test_can_unset_request_option_with_null() {
 138          $testhtml = $this->getExternalTestFileUrl('/test.html');
 139  
 140          $mock = new MockHandler([new Response()]);
 141          $c = new \core\http_client([
 142                  'headers' => ['foo' => 'bar'],
 143                  'mock' => $mock
 144          ]);
 145          $c->get($testhtml, ['headers' => null]);
 146  
 147          $this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
 148      }
 149  
 150      /**
 151       * Test the basic cookiejar functionality.
 152       *
 153       * @covers \core\http_client
 154       */
 155      public function test_basic_cookie_jar() {
 156          $mock = new MockHandler([
 157                  new Response(200, ['Set-Cookie' => 'foo=bar']),
 158                  new Response()
 159          ]);
 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'));
 165      }
 166  
 167      /**
 168       * Test the basic shared cookiejar.
 169       *
 170       * @covers \core\http_client
 171       */
 172      public function test_shared_cookie_jar() {
 173          $mock = new MockHandler([
 174                  new Response(200, ['Set-Cookie' => 'foo=bar']),
 175                  new Response()
 176          ]);
 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'));
 181      }
 182  
 183      /**
 184       * Test guzzle security helper.
 185       *
 186       * @covers \core\http_client
 187       * @covers \core\local\guzzle\check_request
 188       */
 189      public function test_guzzle_basics_with_security_helper() {
 190          $this->resetAfterTest();
 191  
 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.
 197  
 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);
 203  
 204          $this->assertSame(200, $response->getStatusCode());
 205  
 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();
 208  
 209          // Make the mock return a different string.
 210          $mockhelper->expects($this->any())->method('get_blocked_url_string')->will($this->returnValue('You shall not pass'));
 211  
 212          // And make the mock security helper block all URLs. This helper instance doesn't care about config.
 213          $mockhelper->expects($this->any())->method('url_is_blocked')->will($this->returnValue(true));
 214  
 215          $mock = new MockHandler([new Response(200, [], 'You shall not pass')]);
 216          $client = new \core\http_client(['mock' => $mock, 'securityhelper' => $mockhelper]);
 217          $this->expectException(\GuzzleHttp\Exception\RequestException::class);
 218          $response = $client->request('GET', $testhtml);
 219  
 220          $this->assertSame('You shall not pass', $response->getBody()->getContents());
 221      }
 222  
 223      /**
 224       * Test guzzle proxy bypass with moodle.
 225       *
 226       * @covers \core\http_client
 227       * @covers \core\local\guzzle\check_request
 228       */
 229      public function test_http_client_proxy_bypass() {
 230          $this->resetAfterTest();
 231  
 232          global $CFG;
 233          $testurl = $this->getExternalTestFileUrl('/test.html');
 234  
 235          // Test without proxy bypass and inaccessible proxy.
 236          $CFG->proxyhost = 'i.do.not.exist';
 237          $CFG->proxybypass = '';
 238  
 239          $client = new \core\http_client();
 240          $this->expectException(\GuzzleHttp\Exception\RequestException::class);
 241          $response = $client->get($testurl);
 242  
 243          $this->assertNotEquals('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
 244  
 245          // Test with proxy bypass.
 246          $testurlhost = parse_url($testurl, PHP_URL_HOST);
 247          $CFG->proxybypass = $testurlhost;
 248          $client = new \core\http_client();
 249          $response = $client->get($testurl);
 250  
 251          $this->assertSame('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
 252      }
 253  
 254      /**
 255       * Test moodle redirect can be set with guzzle.
 256       *
 257       * @covers \core\http_client
 258       * @covers \core\local\guzzle\redirect_middleware
 259       */
 260      public function test_moodle_allow_redirects_can_be_true() {
 261          $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 262  
 263          $mock = new MockHandler([new Response(200, [], 'foo')]);
 264          $client = new \core\http_client(['mock' => $mock]);
 265          $client->get($testurl, ['moodle_allow_redirect' => true]);
 266  
 267          $this->assertSame(true, $mock->getLastOptions()['moodle_allow_redirect']);
 268      }
 269  
 270      /**
 271       * Test redirect with absolute url.
 272       *
 273       * @covers \core\http_client
 274       * @covers \core\local\guzzle\redirect_middleware
 275       */
 276      public function test_redirects_with_absolute_uri() {
 277          $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 278  
 279          $mock = new MockHandler([
 280                  new Response(302, ['Location' => 'http://moodle.com']),
 281                  new Response(200)
 282          ]);
 283          $client = new \core\http_client(['mock' => $mock]);
 284          $request = new Request('GET', "{$testurl}?redir=1&extdest=1");
 285          $response = $client->send($request);
 286  
 287          $this->assertSame(200, $response->getStatusCode());
 288          $this->assertSame('http://moodle.com', (string)$mock->getLastRequest()->getUri());
 289      }
 290  
 291      /**
 292       * Test redirect with relatetive url.
 293       *
 294       * @covers \core\http_client
 295       * @covers \core\local\guzzle\redirect_middleware
 296       */
 297      public function test_redirects_with_relative_uri() {
 298          $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 299  
 300          $mock = new MockHandler([
 301                  new Response(302, ['Location' => $testurl]),
 302                  new Response(200, [], 'done')
 303          ]);
 304          $client = new \core\http_client(['mock' => $mock]);
 305          $request = new Request('GET', $testurl);
 306          $response = $client->send($request);
 307  
 308          $this->assertSame(200, $response->getStatusCode());
 309          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 310          $this->assertSame('done', $response->getBody()->getContents());
 311  
 312          // Test different types of redirect types.
 313          $mock = new MockHandler([
 314                  new Response(302, ['Location' => $testurl]),
 315                  new Response(200, [], 'done')
 316          ]);
 317          $client = new \core\http_client(['mock' => $mock]);
 318          $request = new Request('GET', "$testurl?type=301");
 319          $response = $client->send($request);
 320  
 321          $this->assertSame(200, $response->getStatusCode());
 322          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 323          $this->assertSame('done', $response->getBody()->getContents());
 324  
 325          $mock = new MockHandler([
 326                  new Response(302, ['Location' => $testurl]),
 327                  new Response(200, [], 'done')
 328          ]);
 329          $client = new \core\http_client(['mock' => $mock]);
 330          $request = new Request('GET', "$testurl?type=302");
 331          $response = $client->send($request);
 332  
 333          $this->assertSame(200, $response->getStatusCode());
 334          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 335          $this->assertSame('done', $response->getBody()->getContents());
 336  
 337          $mock = new MockHandler([
 338                  new Response(302, ['Location' => $testurl]),
 339                  new Response(200, [], 'done')
 340          ]);
 341          $client = new \core\http_client(['mock' => $mock]);
 342          $request = new Request('GET', "$testurl?type=303");
 343          $response = $client->send($request);
 344  
 345          $this->assertSame(200, $response->getStatusCode());
 346          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 347          $this->assertSame('done', $response->getBody()->getContents());
 348  
 349          $mock = new MockHandler([
 350                  new Response(302, ['Location' => $testurl]),
 351                  new Response(200, [], 'done')
 352          ]);
 353          $client = new \core\http_client(['mock' => $mock]);
 354          $request = new Request('GET', "$testurl?type=307");
 355          $response = $client->send($request);
 356  
 357          $this->assertSame(200, $response->getStatusCode());
 358          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 359          $this->assertSame('done', $response->getBody()->getContents());
 360      }
 361  
 362      /**
 363       * Test guzzle cache middleware.
 364       *
 365       * @covers \core\local\guzzle\cache_item
 366       * @covers \core\local\guzzle\cache_handler
 367       * @covers \core\local\guzzle\cache_storage
 368       */
 369      public function test_http_client_cache_item() {
 370          global $CFG, $USER;
 371          $module = 'core_guzzle';
 372          $cachedir = "$CFG->cachedir/$module/";
 373  
 374          $testhtml = $this->getExternalTestFileUrl('/test.html');
 375  
 376          // Test item is cached in the specified module.
 377          $client = new \core\http_client([
 378                  'cache' => true,
 379                  'module_cache' => $module
 380          ]);
 381          $response = $client->get($testhtml);
 382  
 383          $cachecontent = '';
 384          if ($dir = opendir($cachedir)) {
 385              while (false !== ($file = readdir($dir))) {
 386                  if (!is_dir($file) && $file !== '.' && $file !== '..') {
 387                      if (strpos($file, 'u' . $USER->id . '_') !== false) {
 388                          $cachecontent = file_get_contents($cachedir . $file);
 389                      }
 390                  }
 391              }
 392          }
 393  
 394          $this->assertNotEmpty($cachecontent);
 395          @unlink($cachedir . $file);
 396  
 397          // Test cache item objects returns correct values.
 398          $key = 'sample_key';
 399          $cachefilename = 'u' . $USER->id . '_' . md5(serialize($key));
 400          $cachefile = $cachedir.$cachefilename;
 401  
 402          $content = $response->getBody()->getContents();
 403          file_put_contents($cachefile, serialize($content));
 404  
 405          $cacheitemobject = new \core\local\guzzle\cache_item($key, $module, null);
 406  
 407          // Test the cache item matches with the cached response.
 408          $this->assertSame($content, $cacheitemobject->get());
 409  
 410          @unlink($cachefile);
 411      }
 412  }