No Description

BinaryFileResponseTest.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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')->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. public function provideRanges()
  70. {
  71. return array(
  72. array('bytes=1-4', 1, 4, 'bytes 1-4/35'),
  73. array('bytes=-5', 30, 5, 'bytes 30-34/35'),
  74. array('bytes=30-', 30, 5, 'bytes 30-34/35'),
  75. array('bytes=30-30', 30, 1, 'bytes 30-30/35'),
  76. array('bytes=30-34', 30, 5, 'bytes 30-34/35'),
  77. );
  78. }
  79. /**
  80. * @dataProvider provideFullFileRanges
  81. */
  82. public function testFullFileRequests($requestRange)
  83. {
  84. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif')->setAutoEtag();
  85. // prepare a request for a range of the testing file
  86. $request = Request::create('/');
  87. $request->headers->set('Range', $requestRange);
  88. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  89. $data = fread($file, 35);
  90. fclose($file);
  91. $this->expectOutputString($data);
  92. $response = clone $response;
  93. $response->prepare($request);
  94. $response->sendContent();
  95. $this->assertEquals(200, $response->getStatusCode());
  96. }
  97. public function provideFullFileRanges()
  98. {
  99. return array(
  100. array('bytes=0-'),
  101. array('bytes=0-34'),
  102. array('bytes=-35'),
  103. // Syntactical invalid range-request should also return the full resource
  104. array('bytes=20-10'),
  105. array('bytes=50-40'),
  106. );
  107. }
  108. /**
  109. * @dataProvider provideInvalidRanges
  110. */
  111. public function testInvalidRequests($requestRange)
  112. {
  113. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif')->setAutoEtag();
  114. // prepare a request for a range of the testing file
  115. $request = Request::create('/');
  116. $request->headers->set('Range', $requestRange);
  117. $response = clone $response;
  118. $response->prepare($request);
  119. $response->sendContent();
  120. $this->assertEquals(416, $response->getStatusCode());
  121. #$this->assertEquals('', $response->headers->get('Content-Range'));
  122. }
  123. public function provideInvalidRanges()
  124. {
  125. return array(
  126. array('bytes=-40'),
  127. array('bytes=30-40'),
  128. );
  129. }
  130. public function testXSendfile()
  131. {
  132. $request = Request::create('/');
  133. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  134. BinaryFileResponse::trustXSendfileTypeHeader();
  135. $response = BinaryFileResponse::create(__DIR__.'/../README.md');
  136. $response->prepare($request);
  137. $this->expectOutputString('');
  138. $response->sendContent();
  139. $this->assertContains('README.md', $response->headers->get('X-Sendfile'));
  140. }
  141. /**
  142. * @dataProvider getSampleXAccelMappings
  143. */
  144. public function testXAccelMapping($realpath, $mapping, $virtual)
  145. {
  146. $request = Request::create('/');
  147. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  148. $request->headers->set('X-Accel-Mapping', $mapping);
  149. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  150. BinaryFileResponse::trustXSendFileTypeHeader();
  151. $response = new BinaryFileResponse($file);
  152. $reflection = new \ReflectionObject($response);
  153. $property = $reflection->getProperty('file');
  154. $property->setAccessible(true);
  155. $property->setValue($response, $file);
  156. $response->prepare($request);
  157. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  158. }
  159. public function testDeleteFileAfterSend()
  160. {
  161. $request = Request::create('/');
  162. $path = __DIR__.'/File/Fixtures/to_delete';
  163. touch($path);
  164. $realPath = realpath($path);
  165. $this->assertFileExists($realPath);
  166. $response = new BinaryFileResponse($realPath);
  167. $response->deleteFileAfterSend(true);
  168. $response->prepare($request);
  169. $response->sendContent();
  170. $this->assertFileNotExists($path);
  171. }
  172. public function testAcceptRangeOnUnsafeMethods()
  173. {
  174. $request = Request::create('/', 'POST');
  175. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif');
  176. $response->prepare($request);
  177. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  178. }
  179. public function testAcceptRangeNotOverriden()
  180. {
  181. $request = Request::create('/', 'POST');
  182. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif');
  183. $response->headers->set('Accept-Ranges', 'foo');
  184. $response->prepare($request);
  185. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  186. }
  187. public function getSampleXAccelMappings()
  188. {
  189. return array(
  190. array('/var/www/var/www/files/foo.txt', '/files/=/var/www/', '/files/var/www/files/foo.txt'),
  191. array('/home/foo/bar.txt', '/files/=/var/www/,/baz/=/home/foo/', '/baz/bar.txt'),
  192. );
  193. }
  194. protected function provideResponse()
  195. {
  196. return new BinaryFileResponse(__DIR__.'/../README.md');
  197. }
  198. public static function tearDownAfterClass()
  199. {
  200. $path = __DIR__.'/../Fixtures/to_delete';
  201. if (file_exists($path)) {
  202. @unlink($path);
  203. }
  204. }
  205. }