BinaryFileResponseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. public function testSetContent()
  40. {
  41. $this->expectException('LogicException');
  42. $response = new BinaryFileResponse(__FILE__);
  43. $response->setContent('foo');
  44. }
  45. public function testGetContent()
  46. {
  47. $response = new BinaryFileResponse(__FILE__);
  48. $this->assertFalse($response->getContent());
  49. }
  50. public function testSetContentDispositionGeneratesSafeFallbackFilename()
  51. {
  52. $response = new BinaryFileResponse(__FILE__);
  53. $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
  54. $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
  55. }
  56. public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
  57. {
  58. $response = new BinaryFileResponse(__FILE__);
  59. $iso88591EncodedFilename = utf8_decode('föö.html');
  60. $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
  61. // 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)
  62. $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
  63. }
  64. /**
  65. * @dataProvider provideRanges
  66. */
  67. public function testRequests($requestRange, $offset, $length, $responseRange)
  68. {
  69. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  70. // do a request to get the ETag
  71. $request = Request::create('/');
  72. $response->prepare($request);
  73. $etag = $response->headers->get('ETag');
  74. // prepare a request for a range of the testing file
  75. $request = Request::create('/');
  76. $request->headers->set('If-Range', $etag);
  77. $request->headers->set('Range', $requestRange);
  78. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  79. fseek($file, $offset);
  80. $data = fread($file, $length);
  81. fclose($file);
  82. $this->expectOutputString($data);
  83. $response = clone $response;
  84. $response->prepare($request);
  85. $response->sendContent();
  86. $this->assertEquals(206, $response->getStatusCode());
  87. $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
  88. $this->assertSame((string) $length, $response->headers->get('Content-Length'));
  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, ['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 [
  118. ['bytes=1-4', 1, 4, 'bytes 1-4/35'],
  119. ['bytes=-5', 30, 5, 'bytes 30-34/35'],
  120. ['bytes=30-', 30, 5, 'bytes 30-34/35'],
  121. ['bytes=30-30', 30, 1, 'bytes 30-30/35'],
  122. ['bytes=30-34', 30, 5, 'bytes 30-34/35'],
  123. ['bytes=30-40', 30, 5, 'bytes 30-34/35'],
  124. ];
  125. }
  126. public function testRangeRequestsWithoutLastModifiedDate()
  127. {
  128. // prevent auto last modified
  129. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
  130. // prepare a request for a range of the testing file
  131. $request = Request::create('/');
  132. $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
  133. $request->headers->set('Range', 'bytes=1-4');
  134. $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
  135. $response = clone $response;
  136. $response->prepare($request);
  137. $response->sendContent();
  138. $this->assertEquals(200, $response->getStatusCode());
  139. $this->assertNull($response->headers->get('Content-Range'));
  140. }
  141. /**
  142. * @dataProvider provideFullFileRanges
  143. */
  144. public function testFullFileRequests($requestRange)
  145. {
  146. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  147. // prepare a request for a range of the testing file
  148. $request = Request::create('/');
  149. $request->headers->set('Range', $requestRange);
  150. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  151. $data = fread($file, 35);
  152. fclose($file);
  153. $this->expectOutputString($data);
  154. $response = clone $response;
  155. $response->prepare($request);
  156. $response->sendContent();
  157. $this->assertEquals(200, $response->getStatusCode());
  158. }
  159. public function provideFullFileRanges()
  160. {
  161. return [
  162. ['bytes=0-'],
  163. ['bytes=0-34'],
  164. ['bytes=-35'],
  165. // Syntactical invalid range-request should also return the full resource
  166. ['bytes=20-10'],
  167. ['bytes=50-40'],
  168. // range units other than bytes must be ignored
  169. ['unknown=10-20'],
  170. ];
  171. }
  172. public function testRangeOnPostMethod()
  173. {
  174. $request = Request::create('/', 'POST');
  175. $request->headers->set('Range', 'bytes=10-20');
  176. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  177. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  178. $data = fread($file, 35);
  179. fclose($file);
  180. $this->expectOutputString($data);
  181. $response = clone $response;
  182. $response->prepare($request);
  183. $response->sendContent();
  184. $this->assertSame(200, $response->getStatusCode());
  185. $this->assertSame('35', $response->headers->get('Content-Length'));
  186. $this->assertNull($response->headers->get('Content-Range'));
  187. }
  188. public function testUnpreparedResponseSendsFullFile()
  189. {
  190. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
  191. $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
  192. $this->expectOutputString($data);
  193. $response = clone $response;
  194. $response->sendContent();
  195. $this->assertEquals(200, $response->getStatusCode());
  196. }
  197. /**
  198. * @dataProvider provideInvalidRanges
  199. */
  200. public function testInvalidRequests($requestRange)
  201. {
  202. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  203. // prepare a request for a range of the testing file
  204. $request = Request::create('/');
  205. $request->headers->set('Range', $requestRange);
  206. $response = clone $response;
  207. $response->prepare($request);
  208. $response->sendContent();
  209. $this->assertEquals(416, $response->getStatusCode());
  210. $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
  211. }
  212. public function provideInvalidRanges()
  213. {
  214. return [
  215. ['bytes=-40'],
  216. ['bytes=40-50'],
  217. ];
  218. }
  219. /**
  220. * @dataProvider provideXSendfileFiles
  221. */
  222. public function testXSendfile($file)
  223. {
  224. $request = Request::create('/');
  225. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  226. BinaryFileResponse::trustXSendfileTypeHeader();
  227. $response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
  228. $response->prepare($request);
  229. $this->expectOutputString('');
  230. $response->sendContent();
  231. $this->assertStringContainsString('README.md', $response->headers->get('X-Sendfile'));
  232. }
  233. public function provideXSendfileFiles()
  234. {
  235. return [
  236. [__DIR__.'/../README.md'],
  237. ['file://'.__DIR__.'/../README.md'],
  238. ];
  239. }
  240. /**
  241. * @dataProvider getSampleXAccelMappings
  242. */
  243. public function testXAccelMapping($realpath, $mapping, $virtual)
  244. {
  245. $request = Request::create('/');
  246. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  247. $request->headers->set('X-Accel-Mapping', $mapping);
  248. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  249. BinaryFileResponse::trustXSendfileTypeHeader();
  250. $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
  251. $reflection = new \ReflectionObject($response);
  252. $property = $reflection->getProperty('file');
  253. $property->setAccessible(true);
  254. $property->setValue($response, $file);
  255. $response->prepare($request);
  256. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  257. }
  258. public function testDeleteFileAfterSend()
  259. {
  260. $request = Request::create('/');
  261. $path = __DIR__.'/File/Fixtures/to_delete';
  262. touch($path);
  263. $realPath = realpath($path);
  264. $this->assertFileExists($realPath);
  265. $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']);
  266. $response->deleteFileAfterSend(true);
  267. $response->prepare($request);
  268. $response->sendContent();
  269. $this->assertFileDoesNotExist($path);
  270. }
  271. public function testAcceptRangeOnUnsafeMethods()
  272. {
  273. $request = Request::create('/', 'POST');
  274. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  275. $response->prepare($request);
  276. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  277. }
  278. public function testAcceptRangeNotOverriden()
  279. {
  280. $request = Request::create('/', 'POST');
  281. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  282. $response->headers->set('Accept-Ranges', 'foo');
  283. $response->prepare($request);
  284. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  285. }
  286. public function getSampleXAccelMappings()
  287. {
  288. return [
  289. ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
  290. ['/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'],
  291. ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
  292. ];
  293. }
  294. public function testStream()
  295. {
  296. $request = Request::create('/');
  297. $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
  298. $response->prepare($request);
  299. $this->assertNull($response->headers->get('Content-Length'));
  300. }
  301. protected function provideResponse()
  302. {
  303. return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
  304. }
  305. public static function tearDownAfterClass()
  306. {
  307. $path = __DIR__.'/../Fixtures/to_delete';
  308. if (file_exists($path)) {
  309. @unlink($path);
  310. }
  311. }
  312. }