BinaryFileResponseTest.php 12 KB

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