Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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