Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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          $blocked = "http://blocked.com";
 211          $mockhelper->expects($this->any())->method('get_blocked_url_string')->will($this->returnValue($blocked));
 212  
 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));
 215  
 216          $client = new \core\http_client(['securityhelper' => $mockhelper]);
 217  
 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);
 224          }
 225  
 226      }
 227  
 228      /**
 229       * Test guzzle proxy bypass with moodle.
 230       *
 231       * @covers \core\http_client
 232       * @covers \core\local\guzzle\check_request
 233       */
 234      public function test_http_client_proxy_bypass() {
 235          $this->resetAfterTest();
 236  
 237          global $CFG;
 238          $testurl = $this->getExternalTestFileUrl('/test.html');
 239  
 240          // Test without proxy bypass and inaccessible proxy.
 241          $CFG->proxyhost = 'i.do.not.exist';
 242          $CFG->proxybypass = '';
 243  
 244          $client = new \core\http_client();
 245          $this->expectException(\GuzzleHttp\Exception\RequestException::class);
 246          $response = $client->get($testurl);
 247  
 248          $this->assertNotEquals('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
 249  
 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);
 255  
 256          $this->assertSame('99914b932bd37a50b983c5e7c90ae93b', md5(json_encode($response)));
 257      }
 258  
 259      /**
 260       * Test moodle redirect can be set with guzzle.
 261       *
 262       * @covers \core\http_client
 263       * @covers \core\local\guzzle\redirect_middleware
 264       */
 265      public function test_moodle_allow_redirects_can_be_true() {
 266          $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 267  
 268          $mock = new MockHandler([new Response(200, [], 'foo')]);
 269          $client = new \core\http_client(['mock' => $mock]);
 270          $client->get($testurl, ['moodle_allow_redirect' => true]);
 271  
 272          $this->assertSame(true, $mock->getLastOptions()['moodle_allow_redirect']);
 273      }
 274  
 275      /**
 276       * Test redirect with absolute url.
 277       *
 278       * @covers \core\http_client
 279       * @covers \core\local\guzzle\redirect_middleware
 280       */
 281      public function test_redirects_with_absolute_uri() {
 282          $testurl = $this->getExternalTestFileUrl('/test_redir.php');
 283  
 284          $mock = new MockHandler([
 285                  new Response(302, ['Location' => 'http://moodle.com']),
 286                  new Response(200)
 287          ]);
 288          $client = new \core\http_client(['mock' => $mock]);
 289          $request = new Request('GET', "{$testurl}?redir=1&extdest=1");
 290          $response = $client->send($request);
 291  
 292          $this->assertSame(200, $response->getStatusCode());
 293          $this->assertSame('http://moodle.com', (string)$mock->getLastRequest()->getUri());
 294      }
 295  
 296      /**
 297       * Test redirect with relatetive url.
 298       *
 299       * @covers \core\http_client
 300       * @covers \core\local\guzzle\redirect_middleware
 301       */
 302      public function test_redirects_with_relative_uri() {
 303          $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
 304  
 305          $mock = new MockHandler([
 306                  new Response(302, ['Location' => $testurl]),
 307                  new Response(200, [], 'done')
 308          ]);
 309          $client = new \core\http_client(['mock' => $mock]);
 310          $request = new Request('GET', $testurl);
 311          $response = $client->send($request);
 312  
 313          $this->assertSame(200, $response->getStatusCode());
 314          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 315          $this->assertSame('done', $response->getBody()->getContents());
 316  
 317          // Test different types of redirect types.
 318          $mock = new MockHandler([
 319                  new Response(302, ['Location' => $testurl]),
 320                  new Response(200, [], 'done')
 321          ]);
 322          $client = new \core\http_client(['mock' => $mock]);
 323          $request = new Request('GET', "$testurl?type=301");
 324          $response = $client->send($request);
 325  
 326          $this->assertSame(200, $response->getStatusCode());
 327          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 328          $this->assertSame('done', $response->getBody()->getContents());
 329  
 330          $mock = new MockHandler([
 331                  new Response(302, ['Location' => $testurl]),
 332                  new Response(200, [], 'done')
 333          ]);
 334          $client = new \core\http_client(['mock' => $mock]);
 335          $request = new Request('GET', "$testurl?type=302");
 336          $response = $client->send($request);
 337  
 338          $this->assertSame(200, $response->getStatusCode());
 339          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 340          $this->assertSame('done', $response->getBody()->getContents());
 341  
 342          $mock = new MockHandler([
 343                  new Response(302, ['Location' => $testurl]),
 344                  new Response(200, [], 'done')
 345          ]);
 346          $client = new \core\http_client(['mock' => $mock]);
 347          $request = new Request('GET', "$testurl?type=303");
 348          $response = $client->send($request);
 349  
 350          $this->assertSame(200, $response->getStatusCode());
 351          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 352          $this->assertSame('done', $response->getBody()->getContents());
 353  
 354          $mock = new MockHandler([
 355                  new Response(302, ['Location' => $testurl]),
 356                  new Response(200, [], 'done')
 357          ]);
 358          $client = new \core\http_client(['mock' => $mock]);
 359          $request = new Request('GET', "$testurl?type=307");
 360          $response = $client->send($request);
 361  
 362          $this->assertSame(200, $response->getStatusCode());
 363          $this->assertSame($testurl, (string)$mock->getLastRequest()->getUri());
 364          $this->assertSame('done', $response->getBody()->getContents());
 365      }
 366  
 367      /**
 368       * Test guzzle cache middleware.
 369       *
 370       * @covers \core\local\guzzle\cache_item
 371       * @covers \core\local\guzzle\cache_handler
 372       * @covers \core\local\guzzle\cache_storage
 373       */
 374      public function test_http_client_cache_item() {
 375          global $CFG, $USER;
 376          $module = 'core_guzzle';
 377          $cachedir = "$CFG->cachedir/$module/";
 378  
 379          $testhtml = $this->getExternalTestFileUrl('/test.html');
 380  
 381          // Test item is cached in the specified module.
 382          $client = new \core\http_client([
 383                  'cache' => true,
 384                  'module_cache' => $module
 385          ]);
 386          $response = $client->get($testhtml);
 387  
 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);
 394                      }
 395                  }
 396              }
 397          }
 398  
 399          $this->assertNotEmpty($cachecontent);
 400          @unlink($cachedir . $file);
 401  
 402          // Test cache item objects returns correct values.
 403          $key = 'sample_key';
 404          $cachefilename = 'u' . $USER->id . '_' . md5(serialize($key));
 405          $cachefile = $cachedir.$cachefilename;
 406  
 407          $content = $response->getBody()->getContents();
 408          file_put_contents($cachefile, serialize($content));
 409  
 410          $cacheitemobject = new \core\local\guzzle\cache_item($key, $module, null);
 411  
 412          // Test the cache item matches with the cached response.
 413          $this->assertSame($content, $cacheitemobject->get());
 414  
 415          @unlink($cachefile);
 416      }
 417  }