Aucune description

BinaryFileResponseTest.php 11KB

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