MDL-42754 Messages: Show noreply user notifications
[moodle.git] / lib / tests / filelib_test.php
blob0dc7a6491ff198d4c287eb229396a52916eab3c6
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.
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/>.
17 /**
18 * Unit tests for /lib/filelib.php.
20 * @package core_files
21 * @category phpunit
22 * @copyright 2009 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
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(
55 array(
56 'id' => 2,
57 'customfields' => array(
58 array
60 'type' => 'Color',
61 'value' => 'violet'
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' =>
73 array(
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() {
84 global $CFG;
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";
93 @unlink($tofile);
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);
98 @unlink($tofile);
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));
120 // Now 404.
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 $this->assertStringStartsWith('<!DOCTYPE', $response->results);
133 $this->assertSame('', $response->error);
135 // Invalid url.
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);
180 * Test curl class.
182 public function test_curl_class() {
183 global $CFG;
185 // Test https success.
186 $testhtml = $this->getExternalTestFileUrl('/test.html');
188 $curl = new curl();
189 $contents = $curl->get($testhtml);
190 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
191 $this->assertSame(0, $curl->get_errno());
193 $curl = new curl();
194 $tofile = "$CFG->tempdir/test.html";
195 @unlink($tofile);
196 $fp = fopen($tofile, 'w');
197 $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp));
198 $this->assertTrue($result);
199 fclose($fp);
200 $this->assertFileExists($tofile);
201 $this->assertSame($contents, file_get_contents($tofile));
202 @unlink($tofile);
204 $curl = new curl();
205 $tofile = "$CFG->tempdir/test.html";
206 @unlink($tofile);
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));
211 @unlink($tofile);
213 // Test full URL redirects.
214 $testurl = $this->getExternalTestFileUrl('/test_redir.php');
216 $curl = new curl();
217 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
218 $this->assertSame(0, $curl->get_errno());
219 $this->assertSame(2, $curl->info['redirect_count']);
220 $this->assertSame('done', $contents);
222 $curl = new curl();
223 $curl->emulateredirects = true;
224 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
225 $this->assertSame(0, $curl->get_errno());
226 $this->assertSame(2, $curl->info['redirect_count']);
227 $this->assertSame('done', $contents);
229 $curl = new curl();
230 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
231 $this->assertSame(0, $curl->get_errno());
232 $this->assertSame(302, $curl->info['http_code']);
233 $this->assertSame('', $contents);
235 $curl = new curl();
236 $curl->emulateredirects = true;
237 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
238 $this->assertSame(0, $curl->get_errno());
239 $this->assertSame(302, $curl->info['http_code']);
240 $this->assertSame('', $contents);
242 $curl = new curl();
243 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
244 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
245 $this->assertNotEmpty($contents);
247 $curl = new curl();
248 $curl->emulateredirects = true;
249 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
250 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
251 $this->assertNotEmpty($contents);
253 $curl = new curl();
254 $tofile = "$CFG->tempdir/test.html";
255 @unlink($tofile);
256 $fp = fopen($tofile, 'w');
257 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
258 $this->assertTrue($result);
259 fclose($fp);
260 $this->assertFileExists($tofile);
261 $this->assertSame('done', file_get_contents($tofile));
262 @unlink($tofile);
264 $curl = new curl();
265 $curl->emulateredirects = true;
266 $tofile = "$CFG->tempdir/test.html";
267 @unlink($tofile);
268 $fp = fopen($tofile, 'w');
269 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
270 $this->assertTrue($result);
271 fclose($fp);
272 $this->assertFileExists($tofile);
273 $this->assertSame('done', file_get_contents($tofile));
274 @unlink($tofile);
276 $curl = new curl();
277 $tofile = "$CFG->tempdir/test.html";
278 @unlink($tofile);
279 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
280 $this->assertTrue($result);
281 $this->assertFileExists($tofile);
282 $this->assertSame('done', file_get_contents($tofile));
283 @unlink($tofile);
285 $curl = new curl();
286 $curl->emulateredirects = true;
287 $tofile = "$CFG->tempdir/test.html";
288 @unlink($tofile);
289 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
290 $this->assertTrue($result);
291 $this->assertFileExists($tofile);
292 $this->assertSame('done', file_get_contents($tofile));
293 @unlink($tofile);
295 // Test relative location redirects.
296 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
298 $curl = new curl();
299 $contents = $curl->get($testurl);
300 $this->assertSame(0, $curl->get_errno());
301 $this->assertSame(1, $curl->info['redirect_count']);
302 $this->assertSame('done', $contents);
304 $curl = new curl();
305 $curl->emulateredirects = true;
306 $contents = $curl->get($testurl);
307 $this->assertSame(0, $curl->get_errno());
308 $this->assertSame(1, $curl->info['redirect_count']);
309 $this->assertSame('done', $contents);
311 // Test different redirect types.
312 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php');
314 $curl = new curl();
315 $contents = $curl->get("$testurl?type=301");
316 $this->assertSame(0, $curl->get_errno());
317 $this->assertSame(1, $curl->info['redirect_count']);
318 $this->assertSame('done', $contents);
320 $curl = new curl();
321 $curl->emulateredirects = true;
322 $contents = $curl->get("$testurl?type=301");
323 $this->assertSame(0, $curl->get_errno());
324 $this->assertSame(1, $curl->info['redirect_count']);
325 $this->assertSame('done', $contents);
327 $curl = new curl();
328 $contents = $curl->get("$testurl?type=302");
329 $this->assertSame(0, $curl->get_errno());
330 $this->assertSame(1, $curl->info['redirect_count']);
331 $this->assertSame('done', $contents);
333 $curl = new curl();
334 $curl->emulateredirects = true;
335 $contents = $curl->get("$testurl?type=302");
336 $this->assertSame(0, $curl->get_errno());
337 $this->assertSame(1, $curl->info['redirect_count']);
338 $this->assertSame('done', $contents);
340 $curl = new curl();
341 $contents = $curl->get("$testurl?type=303");
342 $this->assertSame(0, $curl->get_errno());
343 $this->assertSame(1, $curl->info['redirect_count']);
344 $this->assertSame('done', $contents);
346 $curl = new curl();
347 $curl->emulateredirects = true;
348 $contents = $curl->get("$testurl?type=303");
349 $this->assertSame(0, $curl->get_errno());
350 $this->assertSame(1, $curl->info['redirect_count']);
351 $this->assertSame('done', $contents);
353 $curl = new curl();
354 $contents = $curl->get("$testurl?type=307");
355 $this->assertSame(0, $curl->get_errno());
356 $this->assertSame(1, $curl->info['redirect_count']);
357 $this->assertSame('done', $contents);
359 $curl = new curl();
360 $curl->emulateredirects = true;
361 $contents = $curl->get("$testurl?type=307");
362 $this->assertSame(0, $curl->get_errno());
363 $this->assertSame(1, $curl->info['redirect_count']);
364 $this->assertSame('done', $contents);
366 $curl = new curl();
367 $contents = $curl->get("$testurl?type=308");
368 $this->assertSame(0, $curl->get_errno());
369 $this->assertSame(1, $curl->info['redirect_count']);
370 $this->assertSame('done', $contents);
372 $curl = new curl();
373 $curl->emulateredirects = true;
374 $contents = $curl->get("$testurl?type=308");
375 $this->assertSame(0, $curl->get_errno());
376 $this->assertSame(1, $curl->info['redirect_count']);
377 $this->assertSame('done', $contents);
381 * Testing prepare draft area
383 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
384 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
386 public function test_prepare_draft_area() {
387 global $USER, $DB;
389 $this->resetAfterTest(true);
391 $generator = $this->getDataGenerator();
392 $user = $generator->create_user();
393 $usercontext = context_user::instance($user->id);
394 $USER = $DB->get_record('user', array('id'=>$user->id));
396 $repositorypluginname = 'user';
398 $args = array();
399 $args['type'] = $repositorypluginname;
400 $repos = repository::get_instances($args);
401 $userrepository = reset($repos);
402 $this->assertInstanceOf('repository', $userrepository);
404 $fs = get_file_storage();
406 $syscontext = context_system::instance();
407 $component = 'core';
408 $filearea = 'unittest';
409 $itemid = 0;
410 $filepath = '/';
411 $filename = 'test.txt';
412 $sourcefield = 'Copyright stuff';
414 $filerecord = array(
415 'contextid' => $syscontext->id,
416 'component' => $component,
417 'filearea' => $filearea,
418 'itemid' => $itemid,
419 'filepath' => $filepath,
420 'filename' => $filename,
421 'source' => $sourcefield,
423 $ref = $fs->pack_reference($filerecord);
424 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
425 $fileid = $originalfile->get_id();
426 $this->assertInstanceOf('stored_file', $originalfile);
428 // Create a user private file.
429 $userfilerecord = new stdClass;
430 $userfilerecord->contextid = $usercontext->id;
431 $userfilerecord->component = 'user';
432 $userfilerecord->filearea = 'private';
433 $userfilerecord->itemid = 0;
434 $userfilerecord->filepath = '/';
435 $userfilerecord->filename = 'userfile.txt';
436 $userfilerecord->source = 'test';
437 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
438 $userfileref = $fs->pack_reference($userfilerecord);
440 $filerefrecord = clone((object)$filerecord);
441 $filerefrecord->filename = 'testref.txt';
443 // Create a file reference.
444 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
445 $this->assertInstanceOf('stored_file', $fileref);
446 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
447 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
448 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
449 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
451 $draftitemid = 0;
452 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
454 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
455 $this->assertCount(3, $draftfiles);
457 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
458 $source = unserialize($draftfile->get_source());
459 $this->assertSame($ref, $source->original);
460 $this->assertSame($sourcefield, $source->source);
462 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
463 $this->assertInstanceOf('stored_file', $draftfileref);
464 $this->assertTrue($draftfileref->is_external_file());
466 // Change some information.
467 $author = 'Dongsheng Cai';
468 $draftfile->set_author($author);
469 $newsourcefield = 'Get from Flickr';
470 $license = 'GPLv3';
471 $draftfile->set_license($license);
472 // If you want to really just change source field, do this.
473 $source = unserialize($draftfile->get_source());
474 $newsourcefield = 'From flickr';
475 $source->source = $newsourcefield;
476 $draftfile->set_source(serialize($source));
478 // Save changed file.
479 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
481 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
483 // Make sure it's the original file id.
484 $this->assertEquals($fileid, $file->get_id());
485 $this->assertInstanceOf('stored_file', $file);
486 $this->assertSame($author, $file->get_author());
487 $this->assertSame($license, $file->get_license());
488 $this->assertEquals($newsourcefield, $file->get_source());
492 * Testing deleting original files.
494 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
495 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
497 public function test_delete_original_file_from_draft() {
498 global $USER, $DB;
500 $this->resetAfterTest(true);
502 $generator = $this->getDataGenerator();
503 $user = $generator->create_user();
504 $usercontext = context_user::instance($user->id);
505 $USER = $DB->get_record('user', array('id'=>$user->id));
507 $repositorypluginname = 'user';
509 $args = array();
510 $args['type'] = $repositorypluginname;
511 $repos = repository::get_instances($args);
512 $userrepository = reset($repos);
513 $this->assertInstanceOf('repository', $userrepository);
515 $fs = get_file_storage();
516 $syscontext = context_system::instance();
518 $filecontent = 'User file content';
520 // Create a user private file.
521 $userfilerecord = new stdClass;
522 $userfilerecord->contextid = $usercontext->id;
523 $userfilerecord->component = 'user';
524 $userfilerecord->filearea = 'private';
525 $userfilerecord->itemid = 0;
526 $userfilerecord->filepath = '/';
527 $userfilerecord->filename = 'userfile.txt';
528 $userfilerecord->source = 'test';
529 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
530 $userfileref = $fs->pack_reference($userfilerecord);
531 $contenthash = $userfile->get_contenthash();
533 $filerecord = array(
534 'contextid' => $syscontext->id,
535 'component' => 'core',
536 'filearea' => 'phpunit',
537 'itemid' => 0,
538 'filepath' => '/',
539 'filename' => 'test.txt',
541 // Create a file reference.
542 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
543 $this->assertInstanceOf('stored_file', $fileref);
544 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
545 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
546 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
547 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
549 $draftitemid = 0;
550 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
551 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
552 $this->assertCount(2, $draftfiles);
553 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
554 $draftfile->delete();
555 // Save changed file.
556 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
558 // The file reference should be a regular moodle file now.
559 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
560 $this->assertFalse($fileref->is_external_file());
561 $this->assertSame($contenthash, $fileref->get_contenthash());
562 $this->assertEquals($filecontent, $fileref->get_content());
566 * Tests the strip_double_headers function in the curl class.
568 public function test_curl_strip_double_headers() {
569 // Example from issue tracker.
570 $mdl30648example = <<<EOF
571 HTTP/1.0 407 Proxy Authentication Required
572 Server: squid/2.7.STABLE9
573 Date: Thu, 08 Dec 2011 14:44:33 GMT
574 Content-Type: text/html
575 Content-Length: 1275
576 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
577 Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
578 X-Cache: MISS from homer.lancs.ac.uk
579 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
580 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
581 Connection: close
583 HTTP/1.0 200 OK
584 Server: Apache
585 X-Lb-Nocache: true
586 Cache-Control: private, max-age=15
587 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
588 Content-Type: text/html
589 Date: Thu, 08 Dec 2011 14:44:53 GMT
590 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
591 X-Cache-Action: MISS
592 X-Cache-Age: 0
593 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
594 X-Cache: MISS from ww
596 <html>...
597 EOF;
598 $mdl30648expected = <<<EOF
599 HTTP/1.0 200 OK
600 Server: Apache
601 X-Lb-Nocache: true
602 Cache-Control: private, max-age=15
603 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
604 Content-Type: text/html
605 Date: Thu, 08 Dec 2011 14:44:53 GMT
606 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
607 X-Cache-Action: MISS
608 X-Cache-Age: 0
609 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
610 X-Cache: MISS from ww
612 <html>...
613 EOF;
614 // For HTTP, replace the \n with \r\n.
615 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
616 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
618 // Test stripping works OK.
619 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
620 // Test it does nothing to the 'plain' data.
621 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
623 // Example from OU proxy.
624 $httpsexample = <<<EOF
625 HTTP/1.0 200 Connection established
627 HTTP/1.1 200 OK
628 Date: Fri, 22 Feb 2013 17:14:23 GMT
629 Server: Apache/2
630 X-Powered-By: PHP/5.3.3-7+squeeze14
631 Content-Type: text/xml
632 Connection: close
633 Content-Encoding: gzip
634 Transfer-Encoding: chunked
636 <?xml version="1.0" encoding="ISO-8859-1" ?>
637 <rss version="2.0">...
638 EOF;
639 $httpsexpected = <<<EOF
640 HTTP/1.1 200 OK
641 Date: Fri, 22 Feb 2013 17:14:23 GMT
642 Server: Apache/2
643 X-Powered-By: PHP/5.3.3-7+squeeze14
644 Content-Type: text/xml
645 Connection: close
646 Content-Encoding: gzip
647 Transfer-Encoding: chunked
649 <?xml version="1.0" encoding="ISO-8859-1" ?>
650 <rss version="2.0">...
651 EOF;
652 // For HTTP, replace the \n with \r\n.
653 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
654 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
656 // Test stripping works OK.
657 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
658 // Test it does nothing to the 'plain' data.
659 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));