Pull merge.
[yaffs-website] / vendor / symfony / http-foundation / Tests / BinaryFileResponseTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use Symfony\Component\HttpFoundation\BinaryFileResponse;
15 use Symfony\Component\HttpFoundation\File\Stream;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
18 use Symfony\Component\HttpFoundation\Tests\File\FakeFile;
19
20 class BinaryFileResponseTest extends ResponseTestCase
21 {
22     public function testConstruction()
23     {
24         $file = __DIR__.'/../README.md';
25         $response = new BinaryFileResponse($file, 404, array('X-Header' => 'Foo'), true, null, true, true);
26         $this->assertEquals(404, $response->getStatusCode());
27         $this->assertEquals('Foo', $response->headers->get('X-Header'));
28         $this->assertTrue($response->headers->has('ETag'));
29         $this->assertTrue($response->headers->has('Last-Modified'));
30         $this->assertFalse($response->headers->has('Content-Disposition'));
31
32         $response = BinaryFileResponse::create($file, 404, array(), true, ResponseHeaderBag::DISPOSITION_INLINE);
33         $this->assertEquals(404, $response->getStatusCode());
34         $this->assertFalse($response->headers->has('ETag'));
35         $this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
36     }
37
38     public function testConstructWithNonAsciiFilename()
39     {
40         touch(sys_get_temp_dir().'/fööö.html');
41
42         $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, array(), true, 'attachment');
43
44         @unlink(sys_get_temp_dir().'/fööö.html');
45
46         $this->assertSame('fööö.html', $response->getFile()->getFilename());
47     }
48
49     /**
50      * @expectedException \LogicException
51      */
52     public function testSetContent()
53     {
54         $response = new BinaryFileResponse(__FILE__);
55         $response->setContent('foo');
56     }
57
58     public function testGetContent()
59     {
60         $response = new BinaryFileResponse(__FILE__);
61         $this->assertFalse($response->getContent());
62     }
63
64     public function testSetContentDispositionGeneratesSafeFallbackFilename()
65     {
66         $response = new BinaryFileResponse(__FILE__);
67         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
68
69         $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
70     }
71
72     public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
73     {
74         $response = new BinaryFileResponse(__FILE__);
75
76         $iso88591EncodedFilename = utf8_decode('föö.html');
77         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
78
79         // the parameter filename* is invalid in this case (rawurldecode('f%F6%F6') does not provide a UTF-8 string but an ISO-8859-1 encoded one)
80         $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
81     }
82
83     /**
84      * @dataProvider provideRanges
85      */
86     public function testRequests($requestRange, $offset, $length, $responseRange)
87     {
88         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
89
90         // do a request to get the ETag
91         $request = Request::create('/');
92         $response->prepare($request);
93         $etag = $response->headers->get('ETag');
94
95         // prepare a request for a range of the testing file
96         $request = Request::create('/');
97         $request->headers->set('If-Range', $etag);
98         $request->headers->set('Range', $requestRange);
99
100         $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
101         fseek($file, $offset);
102         $data = fread($file, $length);
103         fclose($file);
104
105         $this->expectOutputString($data);
106         $response = clone $response;
107         $response->prepare($request);
108         $response->sendContent();
109
110         $this->assertEquals(206, $response->getStatusCode());
111         $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
112         $this->assertSame($length, $response->headers->get('Content-Length'));
113     }
114
115     /**
116      * @dataProvider provideRanges
117      */
118     public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
119     {
120         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
121
122         // do a request to get the LastModified
123         $request = Request::create('/');
124         $response->prepare($request);
125         $lastModified = $response->headers->get('Last-Modified');
126
127         // prepare a request for a range of the testing file
128         $request = Request::create('/');
129         $request->headers->set('If-Range', $lastModified);
130         $request->headers->set('Range', $requestRange);
131
132         $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
133         fseek($file, $offset);
134         $data = fread($file, $length);
135         fclose($file);
136
137         $this->expectOutputString($data);
138         $response = clone $response;
139         $response->prepare($request);
140         $response->sendContent();
141
142         $this->assertEquals(206, $response->getStatusCode());
143         $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
144     }
145
146     public function provideRanges()
147     {
148         return array(
149             array('bytes=1-4', 1, 4, 'bytes 1-4/35'),
150             array('bytes=-5', 30, 5, 'bytes 30-34/35'),
151             array('bytes=30-', 30, 5, 'bytes 30-34/35'),
152             array('bytes=30-30', 30, 1, 'bytes 30-30/35'),
153             array('bytes=30-34', 30, 5, 'bytes 30-34/35'),
154         );
155     }
156
157     public function testRangeRequestsWithoutLastModifiedDate()
158     {
159         // prevent auto last modified
160         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);
161
162         // prepare a request for a range of the testing file
163         $request = Request::create('/');
164         $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
165         $request->headers->set('Range', 'bytes=1-4');
166
167         $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
168         $response = clone $response;
169         $response->prepare($request);
170         $response->sendContent();
171
172         $this->assertEquals(200, $response->getStatusCode());
173         $this->assertNull($response->headers->get('Content-Range'));
174     }
175
176     /**
177      * @dataProvider provideFullFileRanges
178      */
179     public function testFullFileRequests($requestRange)
180     {
181         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
182
183         // prepare a request for a range of the testing file
184         $request = Request::create('/');
185         $request->headers->set('Range', $requestRange);
186
187         $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
188         $data = fread($file, 35);
189         fclose($file);
190
191         $this->expectOutputString($data);
192         $response = clone $response;
193         $response->prepare($request);
194         $response->sendContent();
195
196         $this->assertEquals(200, $response->getStatusCode());
197     }
198
199     public function provideFullFileRanges()
200     {
201         return array(
202             array('bytes=0-'),
203             array('bytes=0-34'),
204             array('bytes=-35'),
205             // Syntactical invalid range-request should also return the full resource
206             array('bytes=20-10'),
207             array('bytes=50-40'),
208         );
209     }
210
211     public function testUnpreparedResponseSendsFullFile()
212     {
213         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
214
215         $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
216
217         $this->expectOutputString($data);
218         $response = clone $response;
219         $response->sendContent();
220
221         $this->assertEquals(200, $response->getStatusCode());
222     }
223
224     /**
225      * @dataProvider provideInvalidRanges
226      */
227     public function testInvalidRequests($requestRange)
228     {
229         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
230
231         // prepare a request for a range of the testing file
232         $request = Request::create('/');
233         $request->headers->set('Range', $requestRange);
234
235         $response = clone $response;
236         $response->prepare($request);
237         $response->sendContent();
238
239         $this->assertEquals(416, $response->getStatusCode());
240         $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
241     }
242
243     public function provideInvalidRanges()
244     {
245         return array(
246             array('bytes=-40'),
247             array('bytes=30-40'),
248         );
249     }
250
251     /**
252      * @dataProvider provideXSendfileFiles
253      */
254     public function testXSendfile($file)
255     {
256         $request = Request::create('/');
257         $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
258
259         BinaryFileResponse::trustXSendfileTypeHeader();
260         $response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
261         $response->prepare($request);
262
263         $this->expectOutputString('');
264         $response->sendContent();
265
266         $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
267     }
268
269     public function provideXSendfileFiles()
270     {
271         return array(
272             array(__DIR__.'/../README.md'),
273             array('file://'.__DIR__.'/../README.md'),
274         );
275     }
276
277     /**
278      * @dataProvider getSampleXAccelMappings
279      */
280     public function testXAccelMapping($realpath, $mapping, $virtual)
281     {
282         $request = Request::create('/');
283         $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
284         $request->headers->set('X-Accel-Mapping', $mapping);
285
286         $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
287
288         BinaryFileResponse::trustXSendfileTypeHeader();
289         $response = new BinaryFileResponse($file, 200, array('Content-Type' => 'application/octet-stream'));
290         $reflection = new \ReflectionObject($response);
291         $property = $reflection->getProperty('file');
292         $property->setAccessible(true);
293         $property->setValue($response, $file);
294
295         $response->prepare($request);
296         $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
297     }
298
299     public function testDeleteFileAfterSend()
300     {
301         $request = Request::create('/');
302
303         $path = __DIR__.'/File/Fixtures/to_delete';
304         touch($path);
305         $realPath = realpath($path);
306         $this->assertFileExists($realPath);
307
308         $response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
309         $response->deleteFileAfterSend(true);
310
311         $response->prepare($request);
312         $response->sendContent();
313
314         $this->assertFileNotExists($path);
315     }
316
317     public function testAcceptRangeOnUnsafeMethods()
318     {
319         $request = Request::create('/', 'POST');
320         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
321         $response->prepare($request);
322
323         $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
324     }
325
326     public function testAcceptRangeNotOverriden()
327     {
328         $request = Request::create('/', 'POST');
329         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
330         $response->headers->set('Accept-Ranges', 'foo');
331         $response->prepare($request);
332
333         $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
334     }
335
336     public function getSampleXAccelMappings()
337     {
338         return array(
339             array('/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'),
340             array('/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'),
341         );
342     }
343
344     public function testStream()
345     {
346         $request = Request::create('/');
347         $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
348         $response->prepare($request);
349
350         $this->assertNull($response->headers->get('Content-Length'));
351     }
352
353     protected function provideResponse()
354     {
355         return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
356     }
357
358     public static function tearDownAfterClass()
359     {
360         $path = __DIR__.'/../Fixtures/to_delete';
361         if (file_exists($path)) {
362             @unlink($path);
363         }
364     }
365 }