BinaryFileResponseTest.php 11 KB

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