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\Request;
16 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
17 use Symfony\Component\HttpFoundation\Tests\File\FakeFile;
19 class BinaryFileResponseTest extends ResponseTestCase
21 public function testConstruction()
23 $file = __DIR__.'/../README.md';
24 $response = new BinaryFileResponse($file, 404, array('X-Header' => 'Foo'), true, null, true, true);
25 $this->assertEquals(404, $response->getStatusCode());
26 $this->assertEquals('Foo', $response->headers->get('X-Header'));
27 $this->assertTrue($response->headers->has('ETag'));
28 $this->assertTrue($response->headers->has('Last-Modified'));
29 $this->assertFalse($response->headers->has('Content-Disposition'));
31 $response = BinaryFileResponse::create($file, 404, array(), true, ResponseHeaderBag::DISPOSITION_INLINE);
32 $this->assertEquals(404, $response->getStatusCode());
33 $this->assertFalse($response->headers->has('ETag'));
34 $this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
37 public function testConstructWithNonAsciiFilename()
39 touch(sys_get_temp_dir().'/fööö.html');
41 $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, array(), true, 'attachment');
43 @unlink(sys_get_temp_dir().'/fööö.html');
45 $this->assertSame('fööö.html', $response->getFile()->getFilename());
49 * @expectedException \LogicException
51 public function testSetContent()
53 $response = new BinaryFileResponse(__FILE__);
54 $response->setContent('foo');
57 public function testGetContent()
59 $response = new BinaryFileResponse(__FILE__);
60 $this->assertFalse($response->getContent());
63 public function testSetContentDispositionGeneratesSafeFallbackFilename()
65 $response = new BinaryFileResponse(__FILE__);
66 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
68 $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
72 * @dataProvider provideRanges
74 public function testRequests($requestRange, $offset, $length, $responseRange)
76 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
78 // do a request to get the ETag
79 $request = Request::create('/');
80 $response->prepare($request);
81 $etag = $response->headers->get('ETag');
83 // prepare a request for a range of the testing file
84 $request = Request::create('/');
85 $request->headers->set('If-Range', $etag);
86 $request->headers->set('Range', $requestRange);
88 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
89 fseek($file, $offset);
90 $data = fread($file, $length);
93 $this->expectOutputString($data);
94 $response = clone $response;
95 $response->prepare($request);
96 $response->sendContent();
98 $this->assertEquals(206, $response->getStatusCode());
99 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
103 * @dataProvider provideRanges
105 public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
107 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
109 // do a request to get the LastModified
110 $request = Request::create('/');
111 $response->prepare($request);
112 $lastModified = $response->headers->get('Last-Modified');
114 // prepare a request for a range of the testing file
115 $request = Request::create('/');
116 $request->headers->set('If-Range', $lastModified);
117 $request->headers->set('Range', $requestRange);
119 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
120 fseek($file, $offset);
121 $data = fread($file, $length);
124 $this->expectOutputString($data);
125 $response = clone $response;
126 $response->prepare($request);
127 $response->sendContent();
129 $this->assertEquals(206, $response->getStatusCode());
130 $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
133 public function provideRanges()
136 array('bytes=1-4', 1, 4, 'bytes 1-4/35'),
137 array('bytes=-5', 30, 5, 'bytes 30-34/35'),
138 array('bytes=30-', 30, 5, 'bytes 30-34/35'),
139 array('bytes=30-30', 30, 1, 'bytes 30-30/35'),
140 array('bytes=30-34', 30, 5, 'bytes 30-34/35'),
144 public function testRangeRequestsWithoutLastModifiedDate()
146 // prevent auto last modified
147 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);
149 // prepare a request for a range of the testing file
150 $request = Request::create('/');
151 $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
152 $request->headers->set('Range', 'bytes=1-4');
154 $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
155 $response = clone $response;
156 $response->prepare($request);
157 $response->sendContent();
159 $this->assertEquals(200, $response->getStatusCode());
160 $this->assertNull($response->headers->get('Content-Range'));
164 * @dataProvider provideFullFileRanges
166 public function testFullFileRequests($requestRange)
168 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
170 // prepare a request for a range of the testing file
171 $request = Request::create('/');
172 $request->headers->set('Range', $requestRange);
174 $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
175 $data = fread($file, 35);
178 $this->expectOutputString($data);
179 $response = clone $response;
180 $response->prepare($request);
181 $response->sendContent();
183 $this->assertEquals(200, $response->getStatusCode());
186 public function provideFullFileRanges()
192 // Syntactical invalid range-request should also return the full resource
193 array('bytes=20-10'),
194 array('bytes=50-40'),
199 * @dataProvider provideInvalidRanges
201 public function testInvalidRequests($requestRange)
203 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
205 // prepare a request for a range of the testing file
206 $request = Request::create('/');
207 $request->headers->set('Range', $requestRange);
209 $response = clone $response;
210 $response->prepare($request);
211 $response->sendContent();
213 $this->assertEquals(416, $response->getStatusCode());
214 $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
217 public function provideInvalidRanges()
221 array('bytes=30-40'),
226 * @dataProvider provideXSendfileFiles
228 public function testXSendfile($file)
230 $request = Request::create('/');
231 $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
233 BinaryFileResponse::trustXSendfileTypeHeader();
234 $response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
235 $response->prepare($request);
237 $this->expectOutputString('');
238 $response->sendContent();
240 $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
243 public function provideXSendfileFiles()
246 array(__DIR__.'/../README.md'),
247 array('file://'.__DIR__.'/../README.md'),
252 * @dataProvider getSampleXAccelMappings
254 public function testXAccelMapping($realpath, $mapping, $virtual)
256 $request = Request::create('/');
257 $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
258 $request->headers->set('X-Accel-Mapping', $mapping);
260 $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
262 BinaryFileResponse::trustXSendfileTypeHeader();
263 $response = new BinaryFileResponse($file, 200, array('Content-Type' => 'application/octet-stream'));
264 $reflection = new \ReflectionObject($response);
265 $property = $reflection->getProperty('file');
266 $property->setAccessible(true);
267 $property->setValue($response, $file);
269 $response->prepare($request);
270 $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
273 public function testDeleteFileAfterSend()
275 $request = Request::create('/');
277 $path = __DIR__.'/File/Fixtures/to_delete';
279 $realPath = realpath($path);
280 $this->assertFileExists($realPath);
282 $response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
283 $response->deleteFileAfterSend(true);
285 $response->prepare($request);
286 $response->sendContent();
288 $this->assertFileNotExists($path);
291 public function testAcceptRangeOnUnsafeMethods()
293 $request = Request::create('/', 'POST');
294 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
295 $response->prepare($request);
297 $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
300 public function testAcceptRangeNotOverriden()
302 $request = Request::create('/', 'POST');
303 $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
304 $response->headers->set('Accept-Ranges', 'foo');
305 $response->prepare($request);
307 $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
310 public function getSampleXAccelMappings()
313 array('/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'),
314 array('/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'),
318 protected function provideResponse()
320 return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
323 public static function tearDownAfterClass()
325 $path = __DIR__.'/../Fixtures/to_delete';
326 if (file_exists($path)) {