You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

365 regels
13 KiB

  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. ];
  124. }
  125. public function testRangeRequestsWithoutLastModifiedDate()
  126. {
  127. // prevent auto last modified
  128. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
  129. // prepare a request for a range of the testing file
  130. $request = Request::create('/');
  131. $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
  132. $request->headers->set('Range', 'bytes=1-4');
  133. $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
  134. $response = clone $response;
  135. $response->prepare($request);
  136. $response->sendContent();
  137. $this->assertEquals(200, $response->getStatusCode());
  138. $this->assertNull($response->headers->get('Content-Range'));
  139. }
  140. /**
  141. * @dataProvider provideFullFileRanges
  142. */
  143. public function testFullFileRequests($requestRange)
  144. {
  145. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  146. // prepare a request for a range of the testing file
  147. $request = Request::create('/');
  148. $request->headers->set('Range', $requestRange);
  149. $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
  150. $data = fread($file, 35);
  151. fclose($file);
  152. $this->expectOutputString($data);
  153. $response = clone $response;
  154. $response->prepare($request);
  155. $response->sendContent();
  156. $this->assertEquals(200, $response->getStatusCode());
  157. }
  158. public function provideFullFileRanges()
  159. {
  160. return [
  161. ['bytes=0-'],
  162. ['bytes=0-34'],
  163. ['bytes=-35'],
  164. // Syntactical invalid range-request should also return the full resource
  165. ['bytes=20-10'],
  166. ['bytes=50-40'],
  167. ];
  168. }
  169. public function testUnpreparedResponseSendsFullFile()
  170. {
  171. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
  172. $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');
  173. $this->expectOutputString($data);
  174. $response = clone $response;
  175. $response->sendContent();
  176. $this->assertEquals(200, $response->getStatusCode());
  177. }
  178. /**
  179. * @dataProvider provideInvalidRanges
  180. */
  181. public function testInvalidRequests($requestRange)
  182. {
  183. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
  184. // prepare a request for a range of the testing file
  185. $request = Request::create('/');
  186. $request->headers->set('Range', $requestRange);
  187. $response = clone $response;
  188. $response->prepare($request);
  189. $response->sendContent();
  190. $this->assertEquals(416, $response->getStatusCode());
  191. $this->assertEquals('bytes */35', $response->headers->get('Content-Range'));
  192. }
  193. public function provideInvalidRanges()
  194. {
  195. return [
  196. ['bytes=-40'],
  197. ['bytes=30-40'],
  198. ];
  199. }
  200. /**
  201. * @dataProvider provideXSendfileFiles
  202. */
  203. public function testXSendfile($file)
  204. {
  205. $request = Request::create('/');
  206. $request->headers->set('X-Sendfile-Type', 'X-Sendfile');
  207. BinaryFileResponse::trustXSendfileTypeHeader();
  208. $response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
  209. $response->prepare($request);
  210. $this->expectOutputString('');
  211. $response->sendContent();
  212. $this->assertStringContainsString('README.md', $response->headers->get('X-Sendfile'));
  213. }
  214. public function provideXSendfileFiles()
  215. {
  216. return [
  217. [__DIR__.'/../README.md'],
  218. ['file://'.__DIR__.'/../README.md'],
  219. ];
  220. }
  221. /**
  222. * @dataProvider getSampleXAccelMappings
  223. */
  224. public function testXAccelMapping($realpath, $mapping, $virtual)
  225. {
  226. $request = Request::create('/');
  227. $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
  228. $request->headers->set('X-Accel-Mapping', $mapping);
  229. $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test');
  230. BinaryFileResponse::trustXSendfileTypeHeader();
  231. $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
  232. $reflection = new \ReflectionObject($response);
  233. $property = $reflection->getProperty('file');
  234. $property->setAccessible(true);
  235. $property->setValue($response, $file);
  236. $response->prepare($request);
  237. $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
  238. }
  239. public function testDeleteFileAfterSend()
  240. {
  241. $request = Request::create('/');
  242. $path = __DIR__.'/File/Fixtures/to_delete';
  243. touch($path);
  244. $realPath = realpath($path);
  245. $this->assertFileExists($realPath);
  246. $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']);
  247. $response->deleteFileAfterSend(true);
  248. $response->prepare($request);
  249. $response->sendContent();
  250. $this->assertFileNotExists($path);
  251. }
  252. public function testAcceptRangeOnUnsafeMethods()
  253. {
  254. $request = Request::create('/', 'POST');
  255. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  256. $response->prepare($request);
  257. $this->assertEquals('none', $response->headers->get('Accept-Ranges'));
  258. }
  259. public function testAcceptRangeNotOverriden()
  260. {
  261. $request = Request::create('/', 'POST');
  262. $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
  263. $response->headers->set('Accept-Ranges', 'foo');
  264. $response->prepare($request);
  265. $this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
  266. }
  267. public function getSampleXAccelMappings()
  268. {
  269. return [
  270. ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
  271. ['/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'],
  272. ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
  273. ];
  274. }
  275. public function testStream()
  276. {
  277. $request = Request::create('/');
  278. $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']);
  279. $response->prepare($request);
  280. $this->assertNull($response->headers->get('Content-Length'));
  281. }
  282. protected function provideResponse()
  283. {
  284. return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
  285. }
  286. public static function tearDownAfterClass()
  287. {
  288. $path = __DIR__.'/../Fixtures/to_delete';
  289. if (file_exists($path)) {
  290. @unlink($path);
  291. }
  292. }
  293. }