2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for /lib/filelib.php.
22 * @copyright 2009 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/filelib.php');
30 require_once($CFG->dirroot . '/repository/lib.php');
32 class core_filelib_testcase extends advanced_testcase {
33 public function test_format_postdata_for_curlcall() {
35 // POST params with just simple types.
36 $postdatatoconvert = array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
37 $expectedresult = "userid=1&roleid=22&name=john";
38 $postdata = format_postdata_for_curlcall($postdatatoconvert);
39 $this->assertEquals($expectedresult, $postdata);
41 // POST params with a string containing & character.
42 $postdatatoconvert = array( 'name' => 'john&emilie', 'roleid' => 22);
43 $expectedresult = "name=john%26emilie&roleid=22"; // Urlencode: '%26' => '&'.
44 $postdata = format_postdata_for_curlcall($postdatatoconvert);
45 $this->assertEquals($expectedresult, $postdata);
47 // POST params with an empty value.
48 $postdatatoconvert = array( 'name' => null, 'roleid' => 22);
49 $expectedresult = "name=&roleid=22";
50 $postdata = format_postdata_for_curlcall($postdatatoconvert);
51 $this->assertEquals($expectedresult, $postdata);
53 // POST params with complex types.
54 $postdatatoconvert = array( 'users' => array(
57 'customfields' => array(
67 $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet";
68 $postdata = format_postdata_for_curlcall($postdatatoconvert);
69 $this->assertEquals($expectedresult, $postdata);
71 // POST params with other complex types.
72 $postdatatoconvert = array ('members' =>
74 array('groupid' => 1, 'userid' => 1)
75 , array('groupid' => 1, 'userid' => 2)
78 $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
79 $postdata = format_postdata_for_curlcall($postdatatoconvert);
80 $this->assertEquals($expectedresult, $postdata);
83 public function test_download_file_content() {
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);
475 * Testing prepare draft area
477 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
478 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
480 public function test_prepare_draft_area() {
483 $this->resetAfterTest(true);
485 $generator = $this->getDataGenerator();
486 $user = $generator->create_user();
487 $usercontext = context_user::instance($user->id);
488 $USER = $DB->get_record('user', array('id'=>$user->id));
490 $repositorypluginname = 'user';
493 $args['type'] = $repositorypluginname;
494 $repos = repository::get_instances($args);
495 $userrepository = reset($repos);
496 $this->assertInstanceOf('repository', $userrepository);
498 $fs = get_file_storage();
500 $syscontext = context_system::instance();
502 $filearea = 'unittest';
505 $filename = 'test.txt';
506 $sourcefield = 'Copyright stuff';
509 'contextid' => $syscontext->id,
510 'component' => $component,
511 'filearea' => $filearea,
513 'filepath' => $filepath,
514 'filename' => $filename,
515 'source' => $sourcefield,
517 $ref = $fs->pack_reference($filerecord);
518 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
519 $fileid = $originalfile->get_id();
520 $this->assertInstanceOf('stored_file', $originalfile);
522 // Create a user private file.
523 $userfilerecord = new stdClass;
524 $userfilerecord->contextid = $usercontext->id;
525 $userfilerecord->component = 'user';
526 $userfilerecord->filearea = 'private';
527 $userfilerecord->itemid = 0;
528 $userfilerecord->filepath = '/';
529 $userfilerecord->filename = 'userfile.txt';
530 $userfilerecord->source = 'test';
531 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
532 $userfileref = $fs->pack_reference($userfilerecord);
534 $filerefrecord = clone((object)$filerecord);
535 $filerefrecord->filename = 'testref.txt';
537 // Create a file reference.
538 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
539 $this->assertInstanceOf('stored_file', $fileref);
540 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
541 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
542 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
543 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
546 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
548 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
549 $this->assertCount(3, $draftfiles);
551 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
552 $source = unserialize($draftfile->get_source());
553 $this->assertSame($ref, $source->original);
554 $this->assertSame($sourcefield, $source->source);
556 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
557 $this->assertInstanceOf('stored_file', $draftfileref);
558 $this->assertTrue($draftfileref->is_external_file());
560 // Change some information.
561 $author = 'Dongsheng Cai';
562 $draftfile->set_author($author);
563 $newsourcefield = 'Get from Flickr';
565 $draftfile->set_license($license);
566 // If you want to really just change source field, do this.
567 $source = unserialize($draftfile->get_source());
568 $newsourcefield = 'From flickr';
569 $source->source = $newsourcefield;
570 $draftfile->set_source(serialize($source));
572 // Save changed file.
573 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
575 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
577 // Make sure it's the original file id.
578 $this->assertEquals($fileid, $file->get_id());
579 $this->assertInstanceOf('stored_file', $file);
580 $this->assertSame($author, $file->get_author());
581 $this->assertSame($license, $file->get_license());
582 $this->assertEquals($newsourcefield, $file->get_source());
586 * Testing deleting original files.
588 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
589 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
591 public function test_delete_original_file_from_draft() {
594 $this->resetAfterTest(true);
596 $generator = $this->getDataGenerator();
597 $user = $generator->create_user();
598 $usercontext = context_user::instance($user->id);
599 $USER = $DB->get_record('user', array('id'=>$user->id));
601 $repositorypluginname = 'user';
604 $args['type'] = $repositorypluginname;
605 $repos = repository::get_instances($args);
606 $userrepository = reset($repos);
607 $this->assertInstanceOf('repository', $userrepository);
609 $fs = get_file_storage();
610 $syscontext = context_system::instance();
612 $filecontent = 'User file content';
614 // Create a user private file.
615 $userfilerecord = new stdClass;
616 $userfilerecord->contextid = $usercontext->id;
617 $userfilerecord->component = 'user';
618 $userfilerecord->filearea = 'private';
619 $userfilerecord->itemid = 0;
620 $userfilerecord->filepath = '/';
621 $userfilerecord->filename = 'userfile.txt';
622 $userfilerecord->source = 'test';
623 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
624 $userfileref = $fs->pack_reference($userfilerecord);
625 $contenthash = $userfile->get_contenthash();
628 'contextid' => $syscontext->id,
629 'component' => 'core',
630 'filearea' => 'phpunit',
633 'filename' => 'test.txt',
635 // Create a file reference.
636 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
637 $this->assertInstanceOf('stored_file', $fileref);
638 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
639 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
640 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
641 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
644 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
645 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
646 $this->assertCount(2, $draftfiles);
647 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
648 $draftfile->delete();
649 // Save changed file.
650 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
652 // The file reference should be a regular moodle file now.
653 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
654 $this->assertFalse($fileref->is_external_file());
655 $this->assertSame($contenthash, $fileref->get_contenthash());
656 $this->assertEquals($filecontent, $fileref->get_content());
660 * Tests the strip_double_headers function in the curl class.
662 public function test_curl_strip_double_headers() {
663 // Example from issue tracker.
664 $mdl30648example = <<<EOF
665 HTTP/1.0 407 Proxy Authentication Required
666 Server: squid/2.7.STABLE9
667 Date: Thu, 08 Dec 2011 14:44:33 GMT
668 Content-Type: text/html
670 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
671 Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
672 X-Cache: MISS from homer.lancs.ac.uk
673 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
674 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
680 Cache-Control: private, max-age=15
681 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
682 Content-Type: text/html
683 Date: Thu, 08 Dec 2011 14:44:53 GMT
684 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
687 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP
688 X-Cache: MISS from ww
692 $mdl30648expected = <<<EOF
696 Cache-Control: private, max-age=15
697 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
698 Content-Type: text/html
699 Date: Thu, 08 Dec 2011 14:44:53 GMT
700 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
703 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP
704 X-Cache: MISS from ww
708 // For HTTP, replace the \n with \r\n.
709 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
710 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
712 // Test stripping works OK.
713 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
714 // Test it does nothing to the 'plain' data.
715 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
717 // Example from OU proxy.
718 $httpsexample = <<<EOF
719 HTTP/1.0 200 Connection established
722 Date: Fri, 22 Feb 2013 17:14:23 GMT
724 X-Powered-By: PHP/5.3.3-7+squeeze14
725 Content-Type: text/xml
727 Content-Encoding: gzip
728 Transfer-Encoding: chunked
730 <?xml version="1.0" encoding="ISO-8859-1" ?>
731 <rss version="2.0">...
733 $httpsexpected = <<<EOF
735 Date: Fri, 22 Feb 2013 17:14:23 GMT
737 X-Powered-By: PHP/5.3.3-7+squeeze14
738 Content-Type: text/xml
740 Content-Encoding: gzip
741 Transfer-Encoding: chunked
743 <?xml version="1.0" encoding="ISO-8859-1" ?>
744 <rss version="2.0">...
746 // For HTTP, replace the \n with \r\n.
747 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
748 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
750 // Test stripping works OK.
751 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
752 // Test it does nothing to the 'plain' data.
753 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));