BinaryFileResponseTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, ['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, [], 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, [], 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, ['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, ['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 [
  120. ['bytes=1-4', 1, 4, 'bytes 1-4/35'],
  121. ['bytes=-5', 30, 5, 'bytes 30-34/35'],
  122. ['bytes=30-', 30, 5, 'bytes 30-34/35'],
  123. ['bytes=30-30', 30, 1, 'bytes 30-30/35'],
  124. ['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, ['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, ['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 [
  163. ['bytes=0-'],
  164. ['bytes=0-34'],
  165. ['bytes=-35'],
  166. // Syntactical invalid range-request should also return the full resource
  167. ['bytes=20-10'],
  168. ['bytes=50-40'],
  169. ];
  170. }
  171. public function testUnpreparedResponseSendsFullFile()
  172. {
  173. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
  174. $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
  175. $this->expectOutputString($data);
  176. $response = clone $response;
  177. $response->sendContent();
  178. $this->assertEquals(200, $response->getStatusCode());
  179. }
  180. /**
  181. * @dataProvider provideInvalidRanges
  182. */
  183. public function testInvalidRequests($requestRange)
  184. {
  185. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  186. // prepare a request for a range of the testing file
  187. $request = Request::create('/');
  188. $request->headers->set('Range', $requestRange);
  189. $response = clone $response;
  190. $response->prepare($request);
  191. $response->sendContent();
  192. $this->assertEquals(416, $response->getStatusCode());
  193. $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
  194. }
  195. public function provideInvalidRanges()
  196. {
  197. return [
  198. ['bytes=-40'],
  199. ['bytes=30-40'],
  200. ];
  201. }
  202. /**
  203. * @dataProvider provideXSendfileFiles
  204. */
  205. public function testXSendfile($file)
  206. {
  207. $request = Request::create('/');
  208. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  209. BinaryFileResponse::trustXSendfileTypeHeader();
  210. $response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
  211. $response->prepare($request);
  212. $this->expectOutputString('');
  213. $response->sendContent();
  214. $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
  215. }
  216. public function provideXSendfileFiles()
  217. {
  218. return [
  219. [__DIR__.'/../README.md'],
  220. ['file://'.__DIR__.'/../README.md'],
  221. ];
  222. }
  223. /**
  224. * @dataProvider getSampleXAccelMappings
  225. */
  226. public function testXAccelMapping($realpath, $mapping, $virtual)
  227. {
  228. $request = Request::create('/');
  229. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  230. $request->headers->set('X-Accel-Mapping', $mapping);
  231. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  232. BinaryFileResponse::trustXSendfileTypeHeader();
  233. $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
  234. $reflection = new \ReflectionObject($response);
  235. $property = $reflection->getProperty('file');
  236. $property->setAccessible(true);
  237. $property->setValue($response, $file);
  238. $response->prepare($request);
  239. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  240. }
  241. public function testDeleteFileAfterSend()
  242. {
  243. $request = Request::create('/');
  244. $path = __DIR__.'/File/Fixtures/to_delete';
  245. touch($path);
  246. $realPath = realpath($path);
  247. $this->assertFileExists($realPath);
  248. $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']);
  249. $response->deleteFileAfterSend(true);
  250. $response->prepare($request);
  251. $response->sendContent();
  252. $this->assertFileNotExists($path);
  253. }
  254. public function testAcceptRangeOnUnsafeMethods()
  255. {
  256. $request = Request::create('/', 'POST');
  257. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  258. $response->prepare($request);
  259. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  260. }
  261. public function testAcceptRangeNotOverriden()
  262. {
  263. $request = Request::create('/', 'POST');
  264. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  265. $response->headers->set('Accept-Ranges', 'foo');
  266. $response->prepare($request);
  267. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  268. }
  269. public function getSampleXAccelMappings()
  270. {
  271. return [
  272. ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
  273. ['/home/Foo/bar.txt', '/var/www/=/files/,/home/Foo/=/baz/', '/baz/bar.txt'],
  274. ['/home/Foo/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', '/baz/bar.txt'],
  275. ];
  276. }
  277. public function testStream()
  278. {
  279. $request = Request::create('/');
  280. $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
  281. $response->prepare($request);
  282. $this->assertNull($response->headers->get('Content-Length'));
  283. }
  284. protected function provideResponse()
  285. {
  286. return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
  287. }
  288. public static function tearDownAfterClass()
  289. {
  290. $path = __DIR__.'/../Fixtures/to_delete';
  291. if (file_exists($path)) {
  292. @unlink($path);
  293. }
  294. }
  295. }