Merge branch 'wip-mdl-33618' of https://github.com/rajeshtaneja/moodle
[moodle.git] / lib / tests / filelib_test.php
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/>.
17 /**
18  * Unit tests for /lib/filelib.php.
19  *
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
24  */
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
59                     (
60                         'type' => 'Color',
61                         'value' => 'violet'
62                     )
63                 )
64             )
65         )
66         );
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)
76         )
77         );
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);
81     }
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.
157         /*
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);
167         */
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);
177     }
179     /**
180      * Test curl class.
181      */
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);
379         $oldproxy = $CFG->proxyhost;
380         $oldproxybypass = $CFG->proxybypass;
382         // Test without proxy bypass and inaccessible proxy.
383         $CFG->proxyhost = 'i.do.not.exist';
384         $CFG->proxybypass = '';
385         $curl = new curl();
386         $contents = $curl->get($testhtml);
387         $this->assertNotEquals(0, $curl->get_errno());
388         $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
390         // Test with proxy bypass.
391         $testhtmlhost = parse_url($testhtml, PHP_URL_HOST);
392         $CFG->proxybypass = $testhtmlhost;
393         $curl = new curl();
394         $contents = $curl->get($testhtml);
395         $this->assertSame(0, $curl->get_errno());
396         $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
398         $CFG->proxyhost = $oldproxy;
399         $CFG->proxybypass = $oldproxybypass;
400     }
402     /**
403      * Testing prepare draft area
404      *
405      * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
406      * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
407      */
408     public function test_prepare_draft_area() {
409         global $USER, $DB;
411         $this->resetAfterTest(true);
413         $generator = $this->getDataGenerator();
414         $user = $generator->create_user();
415         $usercontext = context_user::instance($user->id);
416         $USER = $DB->get_record('user', array('id'=>$user->id));
418         $repositorypluginname = 'user';
420         $args = array();
421         $args['type'] = $repositorypluginname;
422         $repos = repository::get_instances($args);
423         $userrepository = reset($repos);
424         $this->assertInstanceOf('repository', $userrepository);
426         $fs = get_file_storage();
428         $syscontext = context_system::instance();
429         $component = 'core';
430         $filearea  = 'unittest';
431         $itemid    = 0;
432         $filepath  = '/';
433         $filename  = 'test.txt';
434         $sourcefield = 'Copyright stuff';
436         $filerecord = array(
437             'contextid' => $syscontext->id,
438             'component' => $component,
439             'filearea'  => $filearea,
440             'itemid'    => $itemid,
441             'filepath'  => $filepath,
442             'filename'  => $filename,
443             'source'    => $sourcefield,
444         );
445         $ref = $fs->pack_reference($filerecord);
446         $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
447         $fileid = $originalfile->get_id();
448         $this->assertInstanceOf('stored_file', $originalfile);
450         // Create a user private file.
451         $userfilerecord = new stdClass;
452         $userfilerecord->contextid = $usercontext->id;
453         $userfilerecord->component = 'user';
454         $userfilerecord->filearea  = 'private';
455         $userfilerecord->itemid    = 0;
456         $userfilerecord->filepath  = '/';
457         $userfilerecord->filename  = 'userfile.txt';
458         $userfilerecord->source    = 'test';
459         $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
460         $userfileref = $fs->pack_reference($userfilerecord);
462         $filerefrecord = clone((object)$filerecord);
463         $filerefrecord->filename = 'testref.txt';
465         // Create a file reference.
466         $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
467         $this->assertInstanceOf('stored_file', $fileref);
468         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
469         $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
470         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
471         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
473         $draftitemid = 0;
474         file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
476         $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
477         $this->assertCount(3, $draftfiles);
479         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
480         $source = unserialize($draftfile->get_source());
481         $this->assertSame($ref, $source->original);
482         $this->assertSame($sourcefield, $source->source);
484         $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
485         $this->assertInstanceOf('stored_file', $draftfileref);
486         $this->assertTrue($draftfileref->is_external_file());
488         // Change some information.
489         $author = 'Dongsheng Cai';
490         $draftfile->set_author($author);
491         $newsourcefield = 'Get from Flickr';
492         $license = 'GPLv3';
493         $draftfile->set_license($license);
494         // If you want to really just change source field, do this.
495         $source = unserialize($draftfile->get_source());
496         $newsourcefield = 'From flickr';
497         $source->source = $newsourcefield;
498         $draftfile->set_source(serialize($source));
500         // Save changed file.
501         file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
503         $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
505         // Make sure it's the original file id.
506         $this->assertEquals($fileid, $file->get_id());
507         $this->assertInstanceOf('stored_file', $file);
508         $this->assertSame($author, $file->get_author());
509         $this->assertSame($license, $file->get_license());
510         $this->assertEquals($newsourcefield, $file->get_source());
511     }
513     /**
514      * Testing deleting original files.
515      *
516      * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
517      * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
518      */
519     public function test_delete_original_file_from_draft() {
520         global $USER, $DB;
522         $this->resetAfterTest(true);
524         $generator = $this->getDataGenerator();
525         $user = $generator->create_user();
526         $usercontext = context_user::instance($user->id);
527         $USER = $DB->get_record('user', array('id'=>$user->id));
529         $repositorypluginname = 'user';
531         $args = array();
532         $args['type'] = $repositorypluginname;
533         $repos = repository::get_instances($args);
534         $userrepository = reset($repos);
535         $this->assertInstanceOf('repository', $userrepository);
537         $fs = get_file_storage();
538         $syscontext = context_system::instance();
540         $filecontent = 'User file content';
542         // Create a user private file.
543         $userfilerecord = new stdClass;
544         $userfilerecord->contextid = $usercontext->id;
545         $userfilerecord->component = 'user';
546         $userfilerecord->filearea  = 'private';
547         $userfilerecord->itemid    = 0;
548         $userfilerecord->filepath  = '/';
549         $userfilerecord->filename  = 'userfile.txt';
550         $userfilerecord->source    = 'test';
551         $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
552         $userfileref = $fs->pack_reference($userfilerecord);
553         $contenthash = $userfile->get_contenthash();
555         $filerecord = array(
556             'contextid' => $syscontext->id,
557             'component' => 'core',
558             'filearea'  => 'phpunit',
559             'itemid'    => 0,
560             'filepath'  => '/',
561             'filename'  => 'test.txt',
562         );
563         // Create a file reference.
564         $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
565         $this->assertInstanceOf('stored_file', $fileref);
566         $this->assertEquals($userrepository->id, $fileref->get_repository_id());
567         $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash());
568         $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
569         $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
571         $draftitemid = 0;
572         file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
573         $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
574         $this->assertCount(2, $draftfiles);
575         $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
576         $draftfile->delete();
577         // Save changed file.
578         file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
580         // The file reference should be a regular moodle file now.
581         $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
582         $this->assertFalse($fileref->is_external_file());
583         $this->assertSame($contenthash, $fileref->get_contenthash());
584         $this->assertEquals($filecontent, $fileref->get_content());
585     }
587     /**
588      * Tests the strip_double_headers function in the curl class.
589      */
590     public function test_curl_strip_double_headers() {
591         // Example from issue tracker.
592         $mdl30648example = <<<EOF
593 HTTP/1.0 407 Proxy Authentication Required
594 Server: squid/2.7.STABLE9
595 Date: Thu, 08 Dec 2011 14:44:33 GMT
596 Content-Type: text/html
597 Content-Length: 1275
598 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
599 Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
600 X-Cache: MISS from homer.lancs.ac.uk
601 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
602 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
603 Connection: close
605 HTTP/1.0 200 OK
606 Server: Apache
607 X-Lb-Nocache: true
608 Cache-Control: private, max-age=15
609 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
610 Content-Type: text/html
611 Date: Thu, 08 Dec 2011 14:44:53 GMT
612 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
613 X-Cache-Action: MISS
614 X-Cache-Age: 0
615 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
616 X-Cache: MISS from ww
618 <html>...
619 EOF;
620         $mdl30648expected = <<<EOF
621 HTTP/1.0 200 OK
622 Server: Apache
623 X-Lb-Nocache: true
624 Cache-Control: private, max-age=15
625 ETag: "4d69af5d8ba873ea9192c489e151bd7b"
626 Content-Type: text/html
627 Date: Thu, 08 Dec 2011 14:44:53 GMT
628 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
629 X-Cache-Action: MISS
630 X-Cache-Age: 0
631 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
632 X-Cache: MISS from ww
634 <html>...
635 EOF;
636         // For HTTP, replace the \n with \r\n.
637         $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
638         $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
640         // Test stripping works OK.
641         $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example));
642         // Test it does nothing to the 'plain' data.
643         $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected));
645         // Example from OU proxy.
646         $httpsexample = <<<EOF
647 HTTP/1.0 200 Connection established
649 HTTP/1.1 200 OK
650 Date: Fri, 22 Feb 2013 17:14:23 GMT
651 Server: Apache/2
652 X-Powered-By: PHP/5.3.3-7+squeeze14
653 Content-Type: text/xml
654 Connection: close
655 Content-Encoding: gzip
656 Transfer-Encoding: chunked
658 <?xml version="1.0" encoding="ISO-8859-1" ?>
659 <rss version="2.0">...
660 EOF;
661         $httpsexpected = <<<EOF
662 HTTP/1.1 200 OK
663 Date: Fri, 22 Feb 2013 17:14:23 GMT
664 Server: Apache/2
665 X-Powered-By: PHP/5.3.3-7+squeeze14
666 Content-Type: text/xml
667 Connection: close
668 Content-Encoding: gzip
669 Transfer-Encoding: chunked
671 <?xml version="1.0" encoding="ISO-8859-1" ?>
672 <rss version="2.0">...
673 EOF;
674         // For HTTP, replace the \n with \r\n.
675         $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
676         $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
678         // Test stripping works OK.
679         $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample));
680         // Test it does nothing to the 'plain' data.
681         $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected));
682     }