4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
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;
20 class BinaryFileResponseTest extends ResponseTestCase
22 public function testConstruction()
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'));
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'));
38 public function testConstructWithNonAsciiFilename()
40 touch(sys_get_temp_dir().'/fööö.html');
42 $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, array(), true, 'attachment');
44 @unlink(sys_get_temp_dir().'/fööö.html');
46 $this->assertSame('fööö.html', $response->getFile()->getFilename());
50 * @expectedException \LogicException
52 public function testSetContent()
54 $response = new BinaryFileResponse(__FILE__);
55 $response->setContent('foo');
58 public function testGetContent()
60 $response = new BinaryFileResponse(__FILE__);
61 $this->assertFalse($response->getContent());
64 public function testSetContentDispositionGeneratesSafeFallbackFilename()
66 $response = new BinaryFileResponse(__FILE__);
67 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
69 $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
72 public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
74 $response = new BinaryFileResponse(__FILE__);
76 $iso88591EncodedFilename = utf8_decode('föö.html');
77 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
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'));
84 * @dataProvider provideRanges
86 public function testRequests($requestRange, $offset, $length, $responseRange)
88 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
90 // do a request to get the ETag
91 $request = Request::create('/');
92 $response->prepare($request);
93 $etag = $response->headers->get('ETag');
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);
100 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
101 fseek($file, $offset);
102 $data = fread($file, $length);
105 $this->expectOutputString($data);
106 $response = clone $response;
107 $response->prepare($request);
108 $response->sendContent();
110 $this->assertEquals(206, $response->getStatusCode());
111 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
112 $this->assertSame($length, $response->headers->get('Content-Length'));
116 * @dataProvider provideRanges
118 public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
120 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
122 // do a request to get the LastModified
123 $request = Request::create('/');
124 $response->prepare($request);
125 $lastModified = $response->headers->get('Last-Modified');
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);
132 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
133 fseek($file, $offset);
134 $data = fread($file, $length);
137 $this->expectOutputString($data);
138 $response = clone $response;
139 $response->prepare($request);
140 $response->sendContent();
142 $this->assertEquals(206, $response->getStatusCode());
143 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
146 public function provideRanges()
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'),
157 public function testRangeRequestsWithoutLastModifiedDate()
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);
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');
167 $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
168 $response = clone $response;
169 $response->prepare($request);
170 $response->sendContent();
172 $this->assertEquals(200, $response->getStatusCode());
173 $this->assertNull($response->headers->get('Content-Range'));
177 * @dataProvider provideFullFileRanges
179 public function testFullFileRequests($requestRange)
181 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
183 // prepare a request for a range of the testing file
184 $request = Request::create('/');
185 $request->headers->set('Range', $requestRange);
187 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
188 $data = fread($file, 35);
191 $this->expectOutputString($data);
192 $response = clone $response;
193 $response->prepare($request);
194 $response->sendContent();
196 $this->assertEquals(200, $response->getStatusCode());
199 public function provideFullFileRanges()
205 // Syntactical invalid range-request should also return the full resource
206 array('bytes=20-10'),
207 array('bytes=50-40'),
212 * @dataProvider provideInvalidRanges
214 public function testInvalidRequests($requestRange)
216 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
218 // prepare a request for a range of the testing file
219 $request = Request::create('/');
220 $request->headers->set('Range', $requestRange);
222 $response = clone $response;
223 $response->prepare($request);
224 $response->sendContent();
226 $this->assertEquals(416, $response->getStatusCode());
227 $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
230 public function provideInvalidRanges()
234 array('bytes=30-40'),
239 * @dataProvider provideXSendfileFiles
241 public function testXSendfile($file)
243 $request = Request::create('/');
244 $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
246 BinaryFileResponse::trustXSendfileTypeHeader();
247 $response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
248 $response->prepare($request);
250 $this->expectOutputString('');
251 $response->sendContent();
253 $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
256 public function provideXSendfileFiles()
259 array(__DIR__.'/../README.md'),
260 array('file://'.__DIR__.'/../README.md'),
265 * @dataProvider getSampleXAccelMappings
267 public function testXAccelMapping($realpath, $mapping, $virtual)
269 $request = Request::create('/');
270 $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
271 $request->headers->set('X-Accel-Mapping', $mapping);
273 $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
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);
282 $response->prepare($request);
283 $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
286 public function testDeleteFileAfterSend()
288 $request = Request::create('/');
290 $path = __DIR__.'/File/Fixtures/to_delete';
292 $realPath = realpath($path);
293 $this->assertFileExists($realPath);
295 $response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
296 $response->deleteFileAfterSend(true);
298 $response->prepare($request);
299 $response->sendContent();
301 $this->assertFileNotExists($path);
304 public function testAcceptRangeOnUnsafeMethods()
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);
310 $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
313 public function testAcceptRangeNotOverriden()
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);
320 $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
323 public function getSampleXAccelMappings()
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'),
331 public function testStream()
333 $request = Request::create('/');
334 $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
335 $response->prepare($request);
337 $this->assertNull($response->headers->get('Content-Length'));
340 protected function provideResponse()
342 return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
345 public static function tearDownAfterClass()
347 $path = __DIR__.'/../Fixtures/to_delete';
348 if (file_exists($path)) {