1b9e58991cc6d5ba033d7c753ee9391c942fadc5
[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     /**
212      * @dataProvider provideInvalidRanges
213      */
214     public function testInvalidRequests($requestRange)
215     {
216         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
217
218         // prepare a request for a range of the testing file
219         $request = Request::create('/');
220         $request->headers->set('Range', $requestRange);
221
222         $response = clone $response;
223         $response->prepare($request);
224         $response->sendContent();
225
226         $this->assertEquals(416, $response->getStatusCode());
227         $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
228     }
229
230     public function provideInvalidRanges()
231     {
232         return array(
233             array('bytes=-40'),
234             array('bytes=30-40'),
235         );
236     }
237
238     /**
239      * @dataProvider provideXSendfileFiles
240      */
241     public function testXSendfile($file)
242     {
243         $request = Request::create('/');
244         $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
245
246         BinaryFileResponse::trustXSendfileTypeHeader();
247         $response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
248         $response->prepare($request);
249
250         $this->expectOutputString('');
251         $response->sendContent();
252
253         $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
254     }
255
256     public function provideXSendfileFiles()
257     {
258         return array(
259             array(__DIR__.'/../README.md'),
260             array('file://'.__DIR__.'/../README.md'),
261         );
262     }
263
264     /**
265      * @dataProvider getSampleXAccelMappings
266      */
267     public function testXAccelMapping($realpath, $mapping, $virtual)
268     {
269         $request = Request::create('/');
270         $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
271         $request->headers->set('X-Accel-Mapping', $mapping);
272
273         $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
274
275         BinaryFileResponse::trustXSendfileTypeHeader();
276         $response = new BinaryFileResponse($file, 200, array('Content-Type' => 'application/octet-stream'));
277         $reflection = new \ReflectionObject($response);
278         $property = $reflection->getProperty('file');
279         $property->setAccessible(true);
280         $property->setValue($response, $file);
281
282         $response->prepare($request);
283         $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
284     }
285
286     public function testDeleteFileAfterSend()
287     {
288         $request = Request::create('/');
289
290         $path = __DIR__.'/File/Fixtures/to_delete';
291         touch($path);
292         $realPath = realpath($path);
293         $this->assertFileExists($realPath);
294
295         $response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
296         $response->deleteFileAfterSend(true);
297
298         $response->prepare($request);
299         $response->sendContent();
300
301         $this->assertFileNotExists($path);
302     }
303
304     public function testAcceptRangeOnUnsafeMethods()
305     {
306         $request = Request::create('/', 'POST');
307         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
308         $response->prepare($request);
309
310         $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
311     }
312
313     public function testAcceptRangeNotOverriden()
314     {
315         $request = Request::create('/', 'POST');
316         $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
317         $response->headers->set('Accept-Ranges', 'foo');
318         $response->prepare($request);
319
320         $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
321     }
322
323     public function getSampleXAccelMappings()
324     {
325         return array(
326             array('/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'),
327             array('/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'),
328         );
329     }
330
331     public function testStream()
332     {
333         $request = Request::create('/');
334         $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
335         $response->prepare($request);
336
337         $this->assertNull($response->headers->get('Content-Length'));
338     }
339
340     protected function provideResponse()
341     {
342         return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
343     }
344
345     public static function tearDownAfterClass()
346     {
347         $path = __DIR__.'/../Fixtures/to_delete';
348         if (file_exists($path)) {
349             @unlink($path);
350         }
351     }
352 }