Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 27... Línea 27...
27
 *
27
 *
28
 * @package    core
28
 * @package    core
29
 * @category   test
29
 * @category   test
30
 * @copyright  2022 Safat Shahin <safat.shahin@moodle.com>
30
 * @copyright  2022 Safat Shahin <safat.shahin@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
32
 *
32
 * @coversDefaultClass \core\http_client
33
 * @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
 */
34
 */
39
class http_client_test extends \advanced_testcase {
35
class http_client_test extends \advanced_testcase {
Línea 40... Línea 36...
40
 
36
 
41
    /**
37
    /**
Línea 73... Línea 69...
73
    /**
69
    /**
74
     * Test http client can send request synchronously.
70
     * Test http client can send request synchronously.
75
     *
71
     *
76
     * @covers \core\http_client
72
     * @covers \core\http_client
77
     */
73
     */
78
    public function test_http_client_can_send_synchronously() {
74
    public function test_http_client_can_send_synchronously(): void {
79
        $testhtml = $this->getExternalTestFileUrl('/test.html');
75
        $testhtml = $this->getExternalTestFileUrl('/test.html');
Línea 80... Línea 76...
80
 
76
 
81
        $client = new \core\http_client(['handler' => new MockHandler([new Response()])]);
77
        $client = new \core\http_client(['handler' => new MockHandler([new Response()])]);
82
        $request = new Request('GET', $testhtml);
78
        $request = new Request('GET', $testhtml);
Línea 88... Línea 84...
88
    /**
84
    /**
89
     * Test http client can have options as a part of the request.
85
     * Test http client can have options as a part of the request.
90
     *
86
     *
91
     * @covers \core\http_client
87
     * @covers \core\http_client
92
     */
88
     */
93
    public function test_http_client_has_options() {
89
    public function test_http_client_has_options(): void {
94
        $testhtml = $this->getExternalTestFileUrl('/test.html');
90
        $testhtml = $this->getExternalTestFileUrl('/test.html');
Línea 95... Línea 91...
95
 
91
 
96
        $client = new \core\http_client([
92
        $client = new \core\http_client([
97
                'base_uri' => $testhtml,
93
                'base_uri' => $testhtml,
Línea 113... Línea 109...
113
    /**
109
    /**
114
     * Test guzzle can have headers changed in the request.
110
     * Test guzzle can have headers changed in the request.
115
     *
111
     *
116
     * @covers \core\http_client
112
     * @covers \core\http_client
117
     */
113
     */
118
    public function test_http_client_can_modify_the_header_for_each_request() {
114
    public function test_http_client_can_modify_the_header_for_each_request(): void {
119
        $testhtml = $this->getExternalTestFileUrl('/test.html');
115
        $testhtml = $this->getExternalTestFileUrl('/test.html');
Línea 120... Línea 116...
120
 
116
 
121
        $mock = new MockHandler([new Response()]);
117
        $mock = new MockHandler([new Response()]);
122
        $c = new \core\http_client([
118
        $c = new \core\http_client([
Línea 130... Línea 126...
130
    /**
126
    /**
131
     * Test guzzle can unset options.
127
     * Test guzzle can unset options.
132
     *
128
     *
133
     * @covers \core\http_client
129
     * @covers \core\http_client
134
     */
130
     */
135
    public function test_can_unset_request_option_with_null() {
131
    public function test_can_unset_request_option_with_null(): void {
136
        $testhtml = $this->getExternalTestFileUrl('/test.html');
132
        $testhtml = $this->getExternalTestFileUrl('/test.html');
Línea 137... Línea 133...
137
 
133
 
138
        $mock = new MockHandler([new Response()]);
134
        $mock = new MockHandler([new Response()]);
139
        $c = new \core\http_client([
135
        $c = new \core\http_client([
Línea 148... Línea 144...
148
    /**
144
    /**
149
     * Test the basic cookiejar functionality.
145
     * Test the basic cookiejar functionality.
150
     *
146
     *
151
     * @covers \core\http_client
147
     * @covers \core\http_client
152
     */
148
     */
153
    public function test_basic_cookie_jar() {
149
    public function test_basic_cookie_jar(): void {
154
        $mock = new MockHandler([
150
        $mock = new MockHandler([
155
                new Response(200, ['Set-Cookie' => 'foo=bar']),
151
                new Response(200, ['Set-Cookie' => 'foo=bar']),
156
                new Response()
152
                new Response()
157
        ]);
153
        ]);
158
        $client = new \core\http_client(['mock' => $mock]);
154
        $client = new \core\http_client(['mock' => $mock]);
Línea 165... Línea 161...
165
    /**
161
    /**
166
     * Test the basic shared cookiejar.
162
     * Test the basic shared cookiejar.
167
     *
163
     *
168
     * @covers \core\http_client
164
     * @covers \core\http_client
169
     */
165
     */
170
    public function test_shared_cookie_jar() {
166
    public function test_shared_cookie_jar(): void {
171
        $mock = new MockHandler([
167
        $mock = new MockHandler([
172
                new Response(200, ['Set-Cookie' => 'foo=bar']),
168
                new Response(200, ['Set-Cookie' => 'foo=bar']),
173
                new Response()
169
                new Response()
174
        ]);
170
        ]);
175
        $client = new \core\http_client(['mock' => $mock, 'cookies' => true]);
171
        $client = new \core\http_client(['mock' => $mock, 'cookies' => true]);
Línea 182... Línea 178...
182
     * Test guzzle security helper.
178
     * Test guzzle security helper.
183
     *
179
     *
184
     * @covers \core\http_client
180
     * @covers \core\http_client
185
     * @covers \core\local\guzzle\check_request
181
     * @covers \core\local\guzzle\check_request
186
     */
182
     */
187
    public function test_guzzle_basics_with_security_helper() {
183
    public function test_guzzle_basics_with_security_helper(): void {
188
        $this->resetAfterTest();
184
        $this->resetAfterTest();
Línea 189... Línea 185...
189
 
185
 
190
        // Test a request with a basic hostname filter applied.
186
        // Test a request with a basic hostname filter applied.
191
        $testhtml = $this->getExternalTestFileUrl('/test.html');
187
        $testhtml = $this->getExternalTestFileUrl('/test.html');
Línea 227... Línea 223...
227
     * Test guzzle proxy bypass with moodle.
223
     * Test guzzle proxy bypass with moodle.
228
     *
224
     *
229
     * @covers \core\http_client
225
     * @covers \core\http_client
230
     * @covers \core\local\guzzle\check_request
226
     * @covers \core\local\guzzle\check_request
231
     */
227
     */
232
    public function test_http_client_proxy_bypass() {
228
    public function test_http_client_proxy_bypass(): void {
233
        $this->resetAfterTest();
229
        $this->resetAfterTest();
Línea 234... Línea 230...
234
 
230
 
235
        global $CFG;
231
        global $CFG;
Línea 258... Línea 254...
258
     * Test moodle redirect can be set with guzzle.
254
     * Test moodle redirect can be set with guzzle.
259
     *
255
     *
260
     * @covers \core\http_client
256
     * @covers \core\http_client
261
     * @covers \core\local\guzzle\redirect_middleware
257
     * @covers \core\local\guzzle\redirect_middleware
262
     */
258
     */
263
    public function test_moodle_allow_redirects_can_be_true() {
259
    public function test_moodle_allow_redirects_can_be_true(): void {
264
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
260
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
Línea 265... Línea 261...
265
 
261
 
266
        $mock = new MockHandler([new Response(200, [], 'foo')]);
262
        $mock = new MockHandler([new Response(200, [], 'foo')]);
267
        $client = new \core\http_client(['mock' => $mock]);
263
        $client = new \core\http_client(['mock' => $mock]);
Línea 274... Línea 270...
274
     * Test redirect with absolute url.
270
     * Test redirect with absolute url.
275
     *
271
     *
276
     * @covers \core\http_client
272
     * @covers \core\http_client
277
     * @covers \core\local\guzzle\redirect_middleware
273
     * @covers \core\local\guzzle\redirect_middleware
278
     */
274
     */
279
    public function test_redirects_with_absolute_uri() {
275
    public function test_redirects_with_absolute_uri(): void {
280
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
276
        $testurl = $this->getExternalTestFileUrl('/test_redir.php');
Línea 281... Línea 277...
281
 
277
 
282
        $mock = new MockHandler([
278
        $mock = new MockHandler([
283
                new Response(302, ['Location' => 'http://moodle.com']),
279
                new Response(302, ['Location' => 'http://moodle.com']),
Línea 295... Línea 291...
295
     * Test redirect with relatetive url.
291
     * Test redirect with relatetive url.
296
     *
292
     *
297
     * @covers \core\http_client
293
     * @covers \core\http_client
298
     * @covers \core\local\guzzle\redirect_middleware
294
     * @covers \core\local\guzzle\redirect_middleware
299
     */
295
     */
300
    public function test_redirects_with_relative_uri() {
296
    public function test_redirects_with_relative_uri(): void {
301
        $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
297
        $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
Línea 302... Línea 298...
302
 
298
 
303
        $mock = new MockHandler([
299
        $mock = new MockHandler([
304
                new Response(302, ['Location' => $testurl]),
300
                new Response(302, ['Location' => $testurl]),
Línea 367... Línea 363...
367
     *
363
     *
368
     * @covers \core\local\guzzle\cache_item
364
     * @covers \core\local\guzzle\cache_item
369
     * @covers \core\local\guzzle\cache_handler
365
     * @covers \core\local\guzzle\cache_handler
370
     * @covers \core\local\guzzle\cache_storage
366
     * @covers \core\local\guzzle\cache_storage
371
     */
367
     */
372
    public function test_http_client_cache_item() {
368
    public function test_http_client_cache_item(): void {
373
        global $CFG, $USER;
369
        global $CFG, $USER;
374
        $module = 'core_guzzle';
370
        $module = 'core_guzzle';
375
        $cachedir = "$CFG->cachedir/$module/";
371
        $cachedir = "$CFG->cachedir/$module/";
Línea 376... Línea 372...
376
 
372