BinaryFileResponseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /**
  59. * @dataProvider provideRanges
  60. */
  61. public function testRequests($requestRange, $offset, $length, $responseRange)
  62. {
  63. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
  64. // do a request to get the ETag
  65. $request = Request::create('/');
  66. $response->prepare($request);
  67. $etag = $response->headers->get('ETag');
  68. // prepare a request for a range of the testing file
  69. $request = Request::create('/');
  70. $request->headers->set('If-Range', $etag);
  71. $request->headers->set('Range', $requestRange);
  72. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  73. fseek($file, $offset);
  74. $data = fread($file, $length);
  75. fclose($file);
  76. $this->expectOutputString($data);
  77. $response = clone $response;
  78. $response->prepare($request);
  79. $response->sendContent();
  80. $this->assertEquals(206, $response->getStatusCode());
  81. $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
  82. $this->assertSame($length, $response->headers->get('Content-Length'));
  83. }
  84. /**
  85. * @dataProvider provideRanges
  86. */
  87. public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
  88. {
  89. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
  90. // do a request to get the LastModified
  91. $request = Request::create('/');
  92. $response->prepare($request);
  93. $lastModified = $response->headers->get('Last-Modified');
  94. // prepare a request for a range of the testing file
  95. $request = Request::create('/');
  96. $request->headers->set('If-Range', $lastModified);
  97. $request->headers->set('Range', $requestRange);
  98. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  99. fseek($file, $offset);
  100. $data = fread($file, $length);
  101. fclose($file);
  102. $this->expectOutputString($data);
  103. $response = clone $response;
  104. $response->prepare($request);
  105. $response->sendContent();
  106. $this->assertEquals(206, $response->getStatusCode());
  107. $this->assertEquals($responseRange, $response->headers->get('Content-Range'));
  108. }
  109. public function provideRanges()
  110. {
  111. return array(
  112. array('bytes=1-4', 1, 4, 'bytes 1-4/35'),
  113. array('bytes=-5', 30, 5, 'bytes 30-34/35'),
  114. array('bytes=30-', 30, 5, 'bytes 30-34/35'),
  115. array('bytes=30-30', 30, 1, 'bytes 30-30/35'),
  116. array('bytes=30-34', 30, 5, 'bytes 30-34/35'),
  117. );
  118. }
  119. public function testRangeRequestsWithoutLastModifiedDate()
  120. {
  121. // prevent auto last modified
  122. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);
  123. // prepare a request for a range of the testing file
  124. $request = Request::create('/');
  125. $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
  126. $request->headers->set('Range', 'bytes=1-4');
  127. $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
  128. $response = clone $response;
  129. $response->prepare($request);
  130. $response->sendContent();
  131. $this->assertEquals(200, $response->getStatusCode());
  132. $this->assertNull($response->headers->get('Content-Range'));
  133. }
  134. /**
  135. * @dataProvider provideFullFileRanges
  136. */
  137. public function testFullFileRequests($requestRange)
  138. {
  139. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
  140. // prepare a request for a range of the testing file
  141. $request = Request::create('/');
  142. $request->headers->set('Range', $requestRange);
  143. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  144. $data = fread($file, 35);
  145. fclose($file);
  146. $this->expectOutputString($data);
  147. $response = clone $response;
  148. $response->prepare($request);
  149. $response->sendContent();
  150. $this->assertEquals(200, $response->getStatusCode());
  151. }
  152. public function provideFullFileRanges()
  153. {
  154. return array(
  155. array('bytes=0-'),
  156. array('bytes=0-34'),
  157. array('bytes=-35'),
  158. // Syntactical invalid range-request should also return the full resource
  159. array('bytes=20-10'),
  160. array('bytes=50-40'),
  161. );
  162. }
  163. /**
  164. * @dataProvider provideInvalidRanges
  165. */
  166. public function testInvalidRequests($requestRange)
  167. {
  168. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'))->setAutoEtag();
  169. // prepare a request for a range of the testing file
  170. $request = Request::create('/');
  171. $request->headers->set('Range', $requestRange);
  172. $response = clone $response;
  173. $response->prepare($request);
  174. $response->sendContent();
  175. $this->assertEquals(416, $response->getStatusCode());
  176. $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
  177. }
  178. public function provideInvalidRanges()
  179. {
  180. return array(
  181. array('bytes=-40'),
  182. array('bytes=30-40'),
  183. );
  184. }
  185. /**
  186. * @dataProvider provideXSendfileFiles
  187. */
  188. public function testXSendfile($file)
  189. {
  190. $request = Request::create('/');
  191. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  192. BinaryFileResponse::trustXSendfileTypeHeader();
  193. $response = BinaryFileResponse::create($file, 200, array('Content-Type' => 'application/octet-stream'));
  194. $response->prepare($request);
  195. $this->expectOutputString('');
  196. $response->sendContent();
  197. $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
  198. }
  199. public function provideXSendfileFiles()
  200. {
  201. return array(
  202. array(__DIR__.'/../README.md'),
  203. array('file://'.__DIR__.'/../README.md'),
  204. );
  205. }
  206. /**
  207. * @dataProvider getSampleXAccelMappings
  208. */
  209. public function testXAccelMapping($realpath, $mapping, $virtual)
  210. {
  211. $request = Request::create('/');
  212. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  213. $request->headers->set('X-Accel-Mapping', $mapping);
  214. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  215. BinaryFileResponse::trustXSendfileTypeHeader();
  216. $response = new BinaryFileResponse($file, 200, array('Content-Type' => 'application/octet-stream'));
  217. $reflection = new \ReflectionObject($response);
  218. $property = $reflection->getProperty('file');
  219. $property->setAccessible(true);
  220. $property->setValue($response, $file);
  221. $response->prepare($request);
  222. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  223. }
  224. public function testDeleteFileAfterSend()
  225. {
  226. $request = Request::create('/');
  227. $path = __DIR__.'/File/Fixtures/to_delete';
  228. touch($path);
  229. $realPath = realpath($path);
  230. $this->assertFileExists($realPath);
  231. $response = new BinaryFileResponse($realPath, 200, array('Content-Type' => 'application/octet-stream'));
  232. $response->deleteFileAfterSend(true);
  233. $response->prepare($request);
  234. $response->sendContent();
  235. $this->assertFileNotExists($path);
  236. }
  237. public function testAcceptRangeOnUnsafeMethods()
  238. {
  239. $request = Request::create('/', 'POST');
  240. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
  241. $response->prepare($request);
  242. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  243. }
  244. public function testAcceptRangeNotOverriden()
  245. {
  246. $request = Request::create('/', 'POST');
  247. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));
  248. $response->headers->set('Accept-Ranges', 'foo');
  249. $response->prepare($request);
  250. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  251. }
  252. public function getSampleXAccelMappings()
  253. {
  254. return array(
  255. array('/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'),
  256. array('/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'),
  257. );
  258. }
  259. public function testStream()
  260. {
  261. $request = Request::create('/');
  262. $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
  263. $response->prepare($request);
  264. $this->assertNull($response->headers->get('Content-Length'));
  265. }
  266. protected function provideResponse()
  267. {
  268. return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
  269. }
  270. public static function tearDownAfterClass()
  271. {
  272. $path = __DIR__.'/../Fixtures/to_delete';
  273. if (file_exists($path)) {
  274. @unlink($path);
  275. }
  276. }
  277. }