MDL-39915 cleanup core_externallib_testcase
[moodle.git] / lib / tests / filelib_test.php
CommitLineData
a3d5830a
PS
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/>.
16
17
18/**
19 * Unit tests for /lib/filelib.php.
20 *
21 * @package core_files
22 * @category phpunit
23 * @copyright 2009 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->libdir . '/filelib.php');
ac29403e 31require_once($CFG->dirroot . '/repository/lib.php');
a3d5830a 32
67233725 33class filelib_testcase extends advanced_testcase {
a3d5830a
PS
34 public function test_format_postdata_for_curlcall() {
35
36 //POST params with just simple types
37 $postdatatoconvert =array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
38 $expectedresult = "userid=1&roleid=22&name=john";
39 $postdata = format_postdata_for_curlcall($postdatatoconvert);
40 $this->assertEquals($postdata, $expectedresult);
41
42 //POST params with a string containing & character
43 $postdatatoconvert =array( 'name' => 'john&emilie', 'roleid' => 22);
44 $expectedresult = "name=john%26emilie&roleid=22"; //urlencode: '%26' => '&'
45 $postdata = format_postdata_for_curlcall($postdatatoconvert);
46 $this->assertEquals($postdata, $expectedresult);
47
48 //POST params with an empty value
49 $postdatatoconvert =array( 'name' => null, 'roleid' => 22);
50 $expectedresult = "name=&roleid=22";
51 $postdata = format_postdata_for_curlcall($postdatatoconvert);
52 $this->assertEquals($postdata, $expectedresult);
53
54 //POST params with complex types
55 $postdatatoconvert =array( 'users' => array(
56 array(
57 'id' => 2,
58 'customfields' => array(
59 array
60 (
61 'type' => 'Color',
62 'value' => 'violet'
63 )
64 )
65 )
66 )
67 );
68 $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet";
69 $postdata = format_postdata_for_curlcall($postdatatoconvert);
70 $this->assertEquals($postdata, $expectedresult);
71
72 //POST params with other complex types
73 $postdatatoconvert = array ('members' =>
74 array(
75 array('groupid' => 1, 'userid' => 1)
76 , array('groupid' => 1, 'userid' => 2)
77 )
78 );
79 $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
80 $postdata = format_postdata_for_curlcall($postdatatoconvert);
81 $this->assertEquals($postdata, $expectedresult);
82 }
83
84 public function test_download_file_content() {
35ea5db0
PS
85 global $CFG;
86
87 // Test http success first.
a3d5830a 88 $testhtml = "http://download.moodle.org/unittest/test.html";
35ea5db0
PS
89
90 $contents = download_file_content($testhtml);
91 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
92
93 $tofile = "$CFG->tempdir/test.html";
94 @unlink($tofile);
95 $result = download_file_content($testhtml, null, null, false, 300, 20, false, $tofile);
96 $this->assertTrue($result);
97 $this->assertFileExists($tofile);
98 $this->assertSame($contents, file_get_contents($tofile));
99 @unlink($tofile);
100
101 $result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true);
102 $this->assertSame($contents, $result);
103
104 $response = download_file_content($testhtml, null, null, true);
105 $this->assertInstanceOf('stdClass', $response);
106 $this->assertSame('200', $response->status);
107 $this->assertTrue(is_array($response->headers));
846f6fbe 108 $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
35ea5db0
PS
109 $this->assertSame($contents, $response->results);
110 $this->assertSame('', $response->error);
111
112
113 // Test https success.
114 $testhtml = "https://download.moodle.org/unittest/test.html";
115
116 $contents = download_file_content($testhtml, null, null, false, 300, 20, true);
117 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
118
119 $contents = download_file_content($testhtml);
120 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
121
122
123 // Now 404.
124 $testhtml = "http://download.moodle.org/unittest/test.html_nonexistent";
125
126 $contents = download_file_content($testhtml);
127 $this->assertFalse($contents);
128 $this->assertDebuggingCalled();
129
130 $response = download_file_content($testhtml, null, null, true);
131 $this->assertInstanceOf('stdClass', $response);
132 $this->assertSame('404', $response->status);
133 $this->assertTrue(is_array($response->headers));
846f6fbe 134 $this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code));
35ea5db0
PS
135 $this->assertStringStartsWith('<!DOCTYPE', $response->results);
136 $this->assertSame('', $response->error);
137
138
139 // Invalid url.
140 $testhtml = "ftp://download.moodle.org/unittest/test.html";
141
a3d5830a 142 $contents = download_file_content($testhtml);
35ea5db0
PS
143 $this->assertFalse($contents);
144
145
146 // Test standard redirects.
147 $testurl = 'http://download.moodle.org/unittest/test_redir.php';
148
149 $contents = download_file_content("$testurl?redir=2");
150 $this->assertSame('done', $contents);
151
152 $response = download_file_content("$testurl?redir=2", null, null, true);
153 $this->assertInstanceOf('stdClass', $response);
154 $this->assertSame('200', $response->status);
155 $this->assertTrue(is_array($response->headers));
846f6fbe 156 $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code));
35ea5db0
PS
157 $this->assertSame('done', $response->results);
158 $this->assertSame('', $response->error);
159
160/*
161 // Commented out for performance reasons.
162
163 $contents = download_file_content("$testurl?redir=6");
164 $this->assertFalse(false, $contents);
165 $this->assertDebuggingCalled();
166
167 $response = download_file_content("$testurl?redir=6", null, null, true);
168 $this->assertInstanceOf('stdClass', $response);
169 $this->assertSame('0', $response->status);
170 $this->assertTrue(is_array($response->headers));
171 $this->assertFalse($response->results);
172 $this->assertNotEmpty($response->error);
173*/
174
175 // Test relative redirects.
176 $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
177
178 $contents = download_file_content("$testurl");
179 $this->assertSame('done', $contents);
180
181 $contents = download_file_content("$testurl?unused=xxx");
182 $this->assertSame('done', $contents);
183 }
184
185 /**
186 * Test curl class.
187 */
188 public function test_curl_class() {
189 global $CFG;
190
191 // Test https success.
192 $testhtml = "https://download.moodle.org/unittest/test.html";
193
194 $curl = new curl();
195 $contents = $curl->get($testhtml);
196 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
197 $this->assertSame(0, $curl->get_errno());
198
199 $curl = new curl();
200 $tofile = "$CFG->tempdir/test.html";
201 @unlink($tofile);
202 $fp = fopen($tofile, 'w');
203 $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp));
204 $this->assertTrue($result);
205 fclose($fp);
206 $this->assertFileExists($tofile);
207 $this->assertSame($contents, file_get_contents($tofile));
208 @unlink($tofile);
209
210 $curl = new curl();
211 $tofile = "$CFG->tempdir/test.html";
212 @unlink($tofile);
213 $result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile));
214 $this->assertTrue($result);
215 $this->assertFileExists($tofile);
216 $this->assertSame($contents, file_get_contents($tofile));
217 @unlink($tofile);
218
219 // Test full URL redirects.
220 $testurl = 'http://download.moodle.org/unittest/test_redir.php';
221
222 $curl = new curl();
223 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
224 $this->assertSame(0, $curl->get_errno());
225 $this->assertSame(2, $curl->info['redirect_count']);
226 $this->assertSame('done', $contents);
227
228 $curl = new curl();
229 $curl->emulateredirects = true;
230 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2));
231 $this->assertSame(0, $curl->get_errno());
232 $this->assertSame(2, $curl->info['redirect_count']);
233 $this->assertSame('done', $contents);
234
235 $curl = new curl();
236 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
237 $this->assertSame(0, $curl->get_errno());
238 $this->assertSame(302, $curl->info['http_code']);
239 $this->assertSame('', $contents);
240
241 $curl = new curl();
242 $curl->emulateredirects = true;
243 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0));
244 $this->assertSame(0, $curl->get_errno());
245 $this->assertSame(302, $curl->info['http_code']);
246 $this->assertSame('', $contents);
247
248 $curl = new curl();
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);
252
253 $curl = new curl();
254 $curl->emulateredirects = true;
255 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1));
256 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno());
257 $this->assertNotEmpty($contents);
258
259 $curl = new curl();
260 $tofile = "$CFG->tempdir/test.html";
261 @unlink($tofile);
262 $fp = fopen($tofile, 'w');
263 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
264 $this->assertTrue($result);
265 fclose($fp);
266 $this->assertFileExists($tofile);
267 $this->assertSame('done', file_get_contents($tofile));
268 @unlink($tofile);
269
270 $curl = new curl();
271 $curl->emulateredirects = true;
272 $tofile = "$CFG->tempdir/test.html";
273 @unlink($tofile);
274 $fp = fopen($tofile, 'w');
275 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp));
276 $this->assertTrue($result);
277 fclose($fp);
278 $this->assertFileExists($tofile);
279 $this->assertSame('done', file_get_contents($tofile));
280 @unlink($tofile);
281
282 $curl = new curl();
283 $tofile = "$CFG->tempdir/test.html";
284 @unlink($tofile);
285 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
286 $this->assertTrue($result);
287 $this->assertFileExists($tofile);
288 $this->assertSame('done', file_get_contents($tofile));
289 @unlink($tofile);
290
291 $curl = new curl();
292 $curl->emulateredirects = true;
293 $tofile = "$CFG->tempdir/test.html";
294 @unlink($tofile);
295 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile));
296 $this->assertTrue($result);
297 $this->assertFileExists($tofile);
298 $this->assertSame('done', file_get_contents($tofile));
299 @unlink($tofile);
300
301
302 // Test relative location redirects.
303 $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
304
305 $curl = new curl();
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);
310
311 $curl = new curl();
312 $curl->emulateredirects = true;
313 $contents = $curl->get($testurl);
314 $this->assertSame(0, $curl->get_errno());
315 $this->assertSame(1, $curl->info['redirect_count']);
316 $this->assertSame('done', $contents);
317
318
319 // Test different redirect types.
320 $testurl = 'http://download.moodle.org/unittest/test_relative_redir.php';
321
322 $curl = new curl();
323 $contents = $curl->get("$testurl?type=301");
324 $this->assertSame(0, $curl->get_errno());
325 $this->assertSame(1, $curl->info['redirect_count']);
326 $this->assertSame('done', $contents);
327
328 $curl = new curl();
329 $curl->emulateredirects = true;
330 $contents = $curl->get("$testurl?type=301");
331 $this->assertSame(0, $curl->get_errno());
332 $this->assertSame(1, $curl->info['redirect_count']);
333 $this->assertSame('done', $contents);
334
335 $curl = new curl();
336 $contents = $curl->get("$testurl?type=302");
337 $this->assertSame(0, $curl->get_errno());
338 $this->assertSame(1, $curl->info['redirect_count']);
339 $this->assertSame('done', $contents);
340
341 $curl = new curl();
342 $curl->emulateredirects = true;
343 $contents = $curl->get("$testurl?type=302");
344 $this->assertSame(0, $curl->get_errno());
345 $this->assertSame(1, $curl->info['redirect_count']);
346 $this->assertSame('done', $contents);
347
348 $curl = new curl();
349 $contents = $curl->get("$testurl?type=303");
350 $this->assertSame(0, $curl->get_errno());
351 $this->assertSame(1, $curl->info['redirect_count']);
352 $this->assertSame('done', $contents);
353
354 $curl = new curl();
355 $curl->emulateredirects = true;
356 $contents = $curl->get("$testurl?type=303");
357 $this->assertSame(0, $curl->get_errno());
358 $this->assertSame(1, $curl->info['redirect_count']);
359 $this->assertSame('done', $contents);
360
361 $curl = new curl();
362 $contents = $curl->get("$testurl?type=307");
363 $this->assertSame(0, $curl->get_errno());
364 $this->assertSame(1, $curl->info['redirect_count']);
365 $this->assertSame('done', $contents);
366
367 $curl = new curl();
368 $curl->emulateredirects = true;
369 $contents = $curl->get("$testurl?type=307");
370 $this->assertSame(0, $curl->get_errno());
371 $this->assertSame(1, $curl->info['redirect_count']);
372 $this->assertSame('done', $contents);
373
374 $curl = new curl();
375 $contents = $curl->get("$testurl?type=308");
376 $this->assertSame(0, $curl->get_errno());
377 $this->assertSame(1, $curl->info['redirect_count']);
378 $this->assertSame('done', $contents);
379
380 $curl = new curl();
381 $curl->emulateredirects = true;
382 $contents = $curl->get("$testurl?type=308");
383 $this->assertSame(0, $curl->get_errno());
384 $this->assertSame(1, $curl->info['redirect_count']);
385 $this->assertSame('done', $contents);
386
a3d5830a 387 }
67233725
DC
388
389 /**
390 * Testing prepare draft area
391 *
392 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
393 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
394 */
395 public function test_prepare_draft_area() {
396 global $USER, $DB;
397
398 $this->resetAfterTest(true);
399
400 $generator = $this->getDataGenerator();
401 $user = $generator->create_user();
402 $usercontext = context_user::instance($user->id);
403 $USER = $DB->get_record('user', array('id'=>$user->id));
404
7051415c
DC
405 $repositorypluginname = 'user';
406
407 $args = array();
408 $args['type'] = $repositorypluginname;
409 $repos = repository::get_instances($args);
410 $userrepository = reset($repos);
411 $this->assertInstanceOf('repository', $userrepository);
67233725
DC
412
413 $fs = get_file_storage();
7051415c 414
67233725
DC
415 $syscontext = context_system::instance();
416 $component = 'core';
417 $filearea = 'unittest';
418 $itemid = 0;
419 $filepath = '/';
420 $filename = 'test.txt';
421 $sourcefield = 'Copyright stuff';
422
423 $filerecord = array(
424 'contextid' => $syscontext->id,
425 'component' => $component,
426 'filearea' => $filearea,
427 'itemid' => $itemid,
428 'filepath' => $filepath,
429 'filename' => $filename,
430 'source' => $sourcefield,
431 );
432 $ref = $fs->pack_reference($filerecord);
67233725 433 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
67233725
DC
434 $fileid = $originalfile->get_id();
435 $this->assertInstanceOf('stored_file', $originalfile);
436
7051415c
DC
437 // create a user private file
438 $userfilerecord = new stdClass;
439 $userfilerecord->contextid = $usercontext->id;
440 $userfilerecord->component = 'user';
441 $userfilerecord->filearea = 'private';
442 $userfilerecord->itemid = 0;
443 $userfilerecord->filepath = '/';
444 $userfilerecord->filename = 'userfile.txt';
445 $userfilerecord->source = 'test';
446 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
447 $userfileref = $fs->pack_reference($userfilerecord);
448
449 $filerefrecord = clone((object)$filerecord);
450 $filerefrecord->filename = 'testref.txt';
451 // create a file reference
452 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
453 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 454 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
7051415c
DC
455 $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
456 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
457 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
458
67233725
DC
459 $draftitemid = 0;
460 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
461
462 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
7051415c 463 $this->assertEquals(3, count($draftfiles));
67233725
DC
464
465 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
466 $source = unserialize($draftfile->get_source());
467 $this->assertEquals($ref, $source->original);
468 $this->assertEquals($sourcefield, $source->source);
469
7051415c
DC
470 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
471 $this->assertInstanceOf('stored_file', $draftfileref);
472 $this->assertEquals(true, $draftfileref->is_external_file());
67233725
DC
473
474 // change some information
475 $author = 'Dongsheng Cai';
476 $draftfile->set_author($author);
7051415c 477 $newsourcefield = 'Get from Flickr';
67233725
DC
478 $license = 'GPLv3';
479 $draftfile->set_license($license);
480 // if you want to really just change source field, do this:
481 $source = unserialize($draftfile->get_source());
482 $newsourcefield = 'From flickr';
483 $source->source = $newsourcefield;
484 $draftfile->set_source(serialize($source));
485
486 // Save changed file
487 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
488
489 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
490
491 // Make sure it's the original file id
492 $this->assertEquals($fileid, $file->get_id());
493 $this->assertInstanceOf('stored_file', $file);
494 $this->assertEquals($author, $file->get_author());
495 $this->assertEquals($license, $file->get_license());
496 $this->assertEquals($newsourcefield, $file->get_source());
497 }
61506a0a
DC
498
499 /**
500 * Testing deleting original files
501 *
502 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
503 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
504 */
505 public function test_delete_original_file_from_draft() {
506 global $USER, $DB;
507
508 $this->resetAfterTest(true);
509
510 $generator = $this->getDataGenerator();
511 $user = $generator->create_user();
512 $usercontext = context_user::instance($user->id);
513 $USER = $DB->get_record('user', array('id'=>$user->id));
514
515 $repositorypluginname = 'user';
516
517 $args = array();
518 $args['type'] = $repositorypluginname;
519 $repos = repository::get_instances($args);
520 $userrepository = reset($repos);
521 $this->assertInstanceOf('repository', $userrepository);
522
523 $fs = get_file_storage();
524 $syscontext = context_system::instance();
525
526 $filecontent = 'User file content';
527
528 // create a user private file
529 $userfilerecord = new stdClass;
530 $userfilerecord->contextid = $usercontext->id;
531 $userfilerecord->component = 'user';
532 $userfilerecord->filearea = 'private';
533 $userfilerecord->itemid = 0;
534 $userfilerecord->filepath = '/';
535 $userfilerecord->filename = 'userfile.txt';
536 $userfilerecord->source = 'test';
537 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent);
538 $userfileref = $fs->pack_reference($userfilerecord);
539 $contenthash = $userfile->get_contenthash();
540
541 $filerecord = array(
542 'contextid' => $syscontext->id,
543 'component' => 'core',
544 'filearea' => 'phpunit',
545 'itemid' => 0,
546 'filepath' => '/',
547 'filename' => 'test.txt',
548 );
549 // create a file reference
550 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref);
551 $this->assertInstanceOf('stored_file', $fileref);
291a3d1f 552 $this->assertEquals($userrepository->id, $fileref->get_repository_id());
61506a0a
DC
553 $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
554 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
555 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
556
557 $draftitemid = 0;
558 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0);
559 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
560 $this->assertEquals(2, count($draftfiles));
561 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename);
562 $draftfile->delete();
563 // Save changed file
564 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0);
565
566 // The file reference should be a regular moodle file now
567 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt');
568 $this->assertEquals(false, $fileref->is_external_file());
569 $this->assertEquals($contenthash, $fileref->get_contenthash());
570 $this->assertEquals($filecontent, $fileref->get_content());
571 }
85cb4b65 572
573 /**
574 * Tests the strip_double_headers function in the curl class.
575 */
576 public function test_curl_strip_double_headers() {
577 // Example from issue tracker.
578 $mdl30648example = <<<EOF
579HTTP/1.0 407 Proxy Authentication Required
580Server: squid/2.7.STABLE9
581Date: Thu, 08 Dec 2011 14:44:33 GMT
582Content-Type: text/html
583Content-Length: 1275
584X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
585Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
586X-Cache: MISS from homer.lancs.ac.uk
587X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128
588Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9)
589Connection: close
590
591HTTP/1.0 200 OK
592Server: Apache
593X-Lb-Nocache: true
594Cache-Control: private, max-age=15
595ETag: "4d69af5d8ba873ea9192c489e151bd7b"
596Content-Type: text/html
597Date: Thu, 08 Dec 2011 14:44:53 GMT
598Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
599X-Cache-Action: MISS
600X-Cache-Age: 0
601Vary: 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
602X-Cache: MISS from ww
603
604<html>...
605EOF;
606 $mdl30648expected = <<<EOF
607HTTP/1.0 200 OK
608Server: Apache
609X-Lb-Nocache: true
610Cache-Control: private, max-age=15
611ETag: "4d69af5d8ba873ea9192c489e151bd7b"
612Content-Type: text/html
613Date: Thu, 08 Dec 2011 14:44:53 GMT
614Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk
615X-Cache-Action: MISS
616X-Cache-Age: 0
617Vary: 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
618X-Cache: MISS from ww
619
620<html>...
621EOF;
622 // For HTTP, replace the \n with \r\n.
623 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example);
624 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected);
625
626 // Test stripping works OK.
627 $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648example));
628 // Test it does nothing to the 'plain' data.
629 $this->assertEquals($mdl30648expected, curl::strip_double_headers($mdl30648expected));
630
631 // Example from OU proxy.
632 $httpsexample = <<<EOF
633HTTP/1.0 200 Connection established
634
635HTTP/1.1 200 OK
636Date: Fri, 22 Feb 2013 17:14:23 GMT
637Server: Apache/2
638X-Powered-By: PHP/5.3.3-7+squeeze14
639Content-Type: text/xml
640Connection: close
641Content-Encoding: gzip
642Transfer-Encoding: chunked
643
644<?xml version="1.0" encoding="ISO-8859-1" ?>
645<rss version="2.0">...
646EOF;
647 $httpsexpected = <<<EOF
648HTTP/1.1 200 OK
649Date: Fri, 22 Feb 2013 17:14:23 GMT
650Server: Apache/2
651X-Powered-By: PHP/5.3.3-7+squeeze14
652Content-Type: text/xml
653Connection: close
654Content-Encoding: gzip
655Transfer-Encoding: chunked
656
657<?xml version="1.0" encoding="ISO-8859-1" ?>
658<rss version="2.0">...
659EOF;
660 // For HTTP, replace the \n with \r\n.
661 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample);
662 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected);
663
664 // Test stripping works OK.
665 $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexample));
666 // Test it does nothing to the 'plain' data.
667 $this->assertEquals($httpsexpected, curl::strip_double_headers($httpsexpected));
668 }
a3d5830a 669}