79
78
$expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
80
79
$postdata = format_postdata_for_curlcall($postdatatoconvert);
81
$this->assertEquals($postdata, $expectedresult);
80
$this->assertEquals($expectedresult, $postdata);
84
83
public function test_download_file_content() {
85
$testhtml = "http://download.moodle.org/unittest/test.html";
86
$contents = download_file_content($testhtml);
87
$this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
86
// Test http success first.
87
$testhtml = $this->getExternalTestFileUrl('/test.html');
89
$contents = download_file_content($testhtml);
90
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
92
$tofile = "$CFG->tempdir/test.html";
94
$result = download_file_content($testhtml, null, null, false, 300, 20, false, $tofile);
95
$this->assertTrue($result);
96
$this->assertFileExists($tofile);
97
$this->assertSame(file_get_contents($tofile), $contents);
100
$result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true);
101
$this->assertSame($contents, $result);
103
$response = download_file_content($testhtml, null, null, true);
104
$this->assertInstanceOf('stdClass', $response);
105
$this->assertSame('200', $response->status);
106
$this->assertTrue(is_array($response->headers));
107
$this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
108
$this->assertSame($contents, $response->results);
109
$this->assertSame('', $response->error);
111
// Test https success.
112
$testhtml = $this->getExternalTestFileUrl('/test.html', true);
114
$contents = download_file_content($testhtml, null, null, false, 300, 20, true);
115
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
117
$contents = download_file_content($testhtml);
118
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
121
$testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent');
123
$contents = download_file_content($testhtml);
124
$this->assertFalse($contents);
125
$this->assertDebuggingCalled();
127
$response = download_file_content($testhtml, null, null, true);
128
$this->assertInstanceOf('stdClass', $response);
129
$this->assertSame('404', $response->status);
130
$this->assertTrue(is_array($response->headers));
131
$this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code));
132
// Do not test the response starts with DOCTYPE here because some servers may return different headers.
133
$this->assertSame('', $response->error);
136
$testhtml = $this->getExternalTestFileUrl('/test.html');
137
$testhtml = str_replace('http://', 'ftp://', $testhtml);
139
$contents = download_file_content($testhtml);
140
$this->assertFalse($contents);
142
// Test standard redirects.
143
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
145
$contents = download_file_content("$testurl?redir=2");
146
$this->assertSame('done', $contents);
148
$response = download_file_content("$testurl?redir=2", null, null, true);
149
$this->assertInstanceOf('stdClass', $response);
150
$this->assertSame('200', $response->status);
151
$this->assertTrue(is_array($response->headers));
152
$this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
153
$this->assertSame('done', $response->results);
154
$this->assertSame('', $response->error);
156
// Commented out this block if there are performance problems.
158
$contents = download_file_content("$testurl?redir=6");
159
$this->assertFalse(false, $contents);
160
$this->assertDebuggingCalled();
161
$response = download_file_content("$testurl?redir=6", null, null, true);
162
$this->assertInstanceOf('stdClass', $response);
163
$this->assertSame('0', $response->status);
164
$this->assertTrue(is_array($response->headers));
165
$this->assertFalse($response->results);
166
$this->assertNotEmpty($response->error);
169
// Test relative redirects.
170
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
172
$contents = download_file_content("$testurl");
173
$this->assertSame('done', $contents);
175
$contents = download_file_content("$testurl?unused=xxx");
176
$this->assertSame('done', $contents);
182
public function test_curl_basics() {
185
// Test HTTP success.
186
$testhtml = $this->getExternalTestFileUrl('/test.html');
189
$contents = $curl->get($testhtml);
190
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
191
$this->assertSame(0, $curl->get_errno());
194
$tofile = "$CFG->tempdir/test.html";
196
$fp = fopen($tofile, 'w');
197
$result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp));
198
$this->assertTrue($result);
200
$this->assertFileExists($tofile);
201
$this->assertSame($contents, file_get_contents($tofile));
205
$tofile = "$CFG->tempdir/test.html";
207
$result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile));
208
$this->assertTrue($result);
209
$this->assertFileExists($tofile);
210
$this->assertSame($contents, file_get_contents($tofile));
215
$contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist'));
216
$response = $curl->getResponse();
217
$this->assertSame('404 Not Found', reset($response));
218
$this->assertSame(0, $curl->get_errno());
221
public function test_curl_redirects() {
224
// Test full URL redirects.
225
$testurl = $this->getExternalTestFileUrl('/test_redir.php');
228
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
229
$response = $curl->getResponse();
230
$this->assertSame('200 OK', reset($response));
231
$this->assertSame(0, $curl->get_errno());
232
$this->assertSame(2, $curl->info['redirect_count']);
233
$this->assertSame('done', $contents);
236
$curl->emulateredirects = true;
237
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
238
$response = $curl->getResponse();
239
$this->assertSame('200 OK', reset($response));
240
$this->assertSame(0, $curl->get_errno());
241
$this->assertSame(2, $curl->info['redirect_count']);
242
$this->assertSame('done', $contents);
245
$contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
246
$response = $curl->getResponse();
247
$this->assertSame('302 Found', reset($response));
248
$this->assertSame(0, $curl->get_errno());
249
$this->assertSame(302, $curl->info['http_code']);
250
$this->assertSame('', $contents);
253
$curl->emulateredirects = true;
254
$contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
255
$response = $curl->getResponse();
256
$this->assertSame('302 Found', reset($response));
257
$this->assertSame(0, $curl->get_errno());
258
$this->assertSame(302, $curl->info['http_code']);
259
$this->assertSame('', $contents);
262
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
263
$this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
264
$this->assertNotEmpty($contents);
267
$curl->emulateredirects = true;
268
$contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
269
$this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
270
$this->assertNotEmpty($contents);
273
$tofile = "$CFG->tempdir/test.html";
275
$fp = fopen($tofile, 'w');
276
$result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
277
$this->assertTrue($result);
279
$this->assertFileExists($tofile);
280
$this->assertSame('done', file_get_contents($tofile));
284
$curl->emulateredirects = true;
285
$tofile = "$CFG->tempdir/test.html";
287
$fp = fopen($tofile, 'w');
288
$result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
289
$this->assertTrue($result);
291
$this->assertFileExists($tofile);
292
$this->assertSame('done', file_get_contents($tofile));
296
$tofile = "$CFG->tempdir/test.html";
298
$result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
299
$this->assertTrue($result);
300
$this->assertFileExists($tofile);
301
$this->assertSame('done', file_get_contents($tofile));
305
$curl->emulateredirects = true;
306
$tofile = "$CFG->tempdir/test.html";
308
$result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
309
$this->assertTrue($result);
310
$this->assertFileExists($tofile);
311
$this->assertSame('done', file_get_contents($tofile));
315
public function test_curl_relative_redirects() {
316
// Test relative location redirects.
317
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
320
$contents = $curl->get($testurl);
321
$response = $curl->getResponse();
322
$this->assertSame('200 OK', reset($response));
323
$this->assertSame(0, $curl->get_errno());
324
$this->assertSame(1, $curl->info['redirect_count']);
325
$this->assertSame('done', $contents);
328
$curl->emulateredirects = true;
329
$contents = $curl->get($testurl);
330
$response = $curl->getResponse();
331
$this->assertSame('200 OK', reset($response));
332
$this->assertSame(0, $curl->get_errno());
333
$this->assertSame(1, $curl->info['redirect_count']);
334
$this->assertSame('done', $contents);
336
// Test different redirect types.
337
$testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
340
$contents = $curl->get("$testurl?type=301");
341
$response = $curl->getResponse();
342
$this->assertSame('200 OK', reset($response));
343
$this->assertSame(0, $curl->get_errno());
344
$this->assertSame(1, $curl->info['redirect_count']);
345
$this->assertSame('done', $contents);
348
$curl->emulateredirects = true;
349
$contents = $curl->get("$testurl?type=301");
350
$response = $curl->getResponse();
351
$this->assertSame('200 OK', reset($response));
352
$this->assertSame(0, $curl->get_errno());
353
$this->assertSame(1, $curl->info['redirect_count']);
354
$this->assertSame('done', $contents);
357
$contents = $curl->get("$testurl?type=302");
358
$response = $curl->getResponse();
359
$this->assertSame('200 OK', reset($response));
360
$this->assertSame(0, $curl->get_errno());
361
$this->assertSame(1, $curl->info['redirect_count']);
362
$this->assertSame('done', $contents);
365
$curl->emulateredirects = true;
366
$contents = $curl->get("$testurl?type=302");
367
$response = $curl->getResponse();
368
$this->assertSame('200 OK', reset($response));
369
$this->assertSame(0, $curl->get_errno());
370
$this->assertSame(1, $curl->info['redirect_count']);
371
$this->assertSame('done', $contents);
374
$contents = $curl->get("$testurl?type=303");
375
$response = $curl->getResponse();
376
$this->assertSame('200 OK', reset($response));
377
$this->assertSame(0, $curl->get_errno());
378
$this->assertSame(1, $curl->info['redirect_count']);
379
$this->assertSame('done', $contents);
382
$curl->emulateredirects = true;
383
$contents = $curl->get("$testurl?type=303");
384
$response = $curl->getResponse();
385
$this->assertSame('200 OK', reset($response));
386
$this->assertSame(0, $curl->get_errno());
387
$this->assertSame(1, $curl->info['redirect_count']);
388
$this->assertSame('done', $contents);
391
$contents = $curl->get("$testurl?type=307");
392
$response = $curl->getResponse();
393
$this->assertSame('200 OK', reset($response));
394
$this->assertSame(0, $curl->get_errno());
395
$this->assertSame(1, $curl->info['redirect_count']);
396
$this->assertSame('done', $contents);
399
$curl->emulateredirects = true;
400
$contents = $curl->get("$testurl?type=307");
401
$response = $curl->getResponse();
402
$this->assertSame('200 OK', reset($response));
403
$this->assertSame(0, $curl->get_errno());
404
$this->assertSame(1, $curl->info['redirect_count']);
405
$this->assertSame('done', $contents);
408
$contents = $curl->get("$testurl?type=308");
409
$response = $curl->getResponse();
410
$this->assertSame('200 OK', reset($response));
411
$this->assertSame(0, $curl->get_errno());
412
$this->assertSame(1, $curl->info['redirect_count']);
413
$this->assertSame('done', $contents);
416
$curl->emulateredirects = true;
417
$contents = $curl->get("$testurl?type=308");
418
$response = $curl->getResponse();
419
$this->assertSame('200 OK', reset($response));
420
$this->assertSame(0, $curl->get_errno());
421
$this->assertSame(1, $curl->info['redirect_count']);
422
$this->assertSame('done', $contents);
426
public function test_curl_proxybypass() {
428
$testurl = $this->getExternalTestFileUrl('/test.html');
430
$oldproxy = $CFG->proxyhost;
431
$oldproxybypass = $CFG->proxybypass;
433
// Test without proxy bypass and inaccessible proxy.
434
$CFG->proxyhost = 'i.do.not.exist';
435
$CFG->proxybypass = '';
437
$contents = $curl->get($testurl);
438
$this->assertNotEquals(0, $curl->get_errno());
439
$this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
441
// Test with proxy bypass.
442
$testurlhost = parse_url($testurl, PHP_URL_HOST);
443
$CFG->proxybypass = $testurlhost;
445
$contents = $curl->get($testurl);
446
$this->assertSame(0, $curl->get_errno());
447
$this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
449
$CFG->proxyhost = $oldproxy;
450
$CFG->proxybypass = $oldproxybypass;
453
public function test_curl_post() {
454
$testurl = $this->getExternalTestFileUrl('/test_post.php');
456
// Test post request.
458
$contents = $curl->post($testurl, 'data=moodletest');
459
$response = $curl->getResponse();
460
$this->assertSame('200 OK', reset($response));
461
$this->assertSame(0, $curl->get_errno());
462
$this->assertSame('OK', $contents);
464
// Test 100 requests.
466
$curl->setHeader('Expect: 100-continue');
467
$contents = $curl->post($testurl, 'data=moodletest');
468
$response = $curl->getResponse();
469
$this->assertSame('200 OK', reset($response));
470
$this->assertSame(0, $curl->get_errno());
471
$this->assertSame('OK', $contents);
474
public function test_curl_file() {
475
$this->resetAfterTest();
476
$testurl = $this->getExternalTestFileUrl('/test_file.php');
478
$fs = get_file_storage();
480
'contextid' => context_system::instance()->id,
481
'component' => 'test',
482
'filearea' => 'curl_post',
485
'filename' => 'test.txt'
487
$teststring = 'moodletest';
488
$testfile = $fs->create_file_from_string($filerecord, $teststring);
490
// Test post with file.
491
$data = array('testfile' => $testfile);
493
$contents = $curl->post($testurl, $data);
494
$this->assertSame('OK', $contents);