BinaryFileResponseTest.php 12KB

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