File
2 years ago
Fixtures
2 years ago
Session
2 years ago
schema
2 years ago
AcceptHeaderItemTest.php
2 years ago
AcceptHeaderTest.php
2 years ago
ApacheRequestTest.php
2 years ago
BinaryFileResponseTest.php
2 years ago
CookieTest.php
2 years ago
ExpressionRequestMatcherTest.php
2 years ago
FileBagTest.php
2 years ago
HeaderBagTest.php
2 years ago
IpUtilsTest.php
2 years ago
JsonResponseTest.php
2 years ago
ParameterBagTest.php
2 years ago
RedirectResponseTest.php
2 years ago
RequestMatcherTest.php
2 years ago
RequestStackTest.php
2 years ago
RequestTest.php
2 years ago
ResponseFunctionalTest.php
2 years ago
ResponseHeaderBagTest.php
2 years ago
ResponseTest.php
2 years ago
ResponseTestCase.php
2 years ago
ServerBagTest.php
2 years ago
StreamedResponseTest.php
2 years ago
BinaryFileResponseTest.php
391 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | * |
| 11 | * Modified by impress-org on 23-January-2024 using Strauss. |
| 12 | * @see https://github.com/BrianHenryIE/strauss |
| 13 | */ |
| 14 | |
| 15 | namespace Give\Vendors\Symfony\Component\HttpFoundation\Tests; |
| 16 | |
| 17 | use Give\Vendors\Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 18 | use Give\Vendors\Symfony\Component\HttpFoundation\File\Stream; |
| 19 | use Give\Vendors\Symfony\Component\HttpFoundation\Request; |
| 20 | use Give\Vendors\Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 21 | use Give\Vendors\Symfony\Component\HttpFoundation\Tests\File\FakeFile; |
| 22 | |
| 23 | class BinaryFileResponseTest extends ResponseTestCase |
| 24 | { |
| 25 | public function testConstruction() |
| 26 | { |
| 27 | $file = __DIR__.'/../README.md'; |
| 28 | $response = new BinaryFileResponse($file, 404, ['X-Header' => 'Foo'], true, null, true, true); |
| 29 | $this->assertEquals(404, $response->getStatusCode()); |
| 30 | $this->assertEquals('Foo', $response->headers->get('X-Header')); |
| 31 | $this->assertTrue($response->headers->has('ETag')); |
| 32 | $this->assertTrue($response->headers->has('Last-Modified')); |
| 33 | $this->assertFalse($response->headers->has('Content-Disposition')); |
| 34 | |
| 35 | $response = BinaryFileResponse::create($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE); |
| 36 | $this->assertEquals(404, $response->getStatusCode()); |
| 37 | $this->assertFalse($response->headers->has('ETag')); |
| 38 | $this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition')); |
| 39 | } |
| 40 | |
| 41 | public function testConstructWithNonAsciiFilename() |
| 42 | { |
| 43 | touch(sys_get_temp_dir().'/fööö.html'); |
| 44 | |
| 45 | $response = new BinaryFileResponse(sys_get_temp_dir().'/fööö.html', 200, [], true, 'attachment'); |
| 46 | |
| 47 | @unlink(sys_get_temp_dir().'/fööö.html'); |
| 48 | |
| 49 | $this->assertSame('fööö.html', $response->getFile()->getFilename()); |
| 50 | } |
| 51 | |
| 52 | public function testSetContent() |
| 53 | { |
| 54 | $this->expectException('LogicException'); |
| 55 | $response = new BinaryFileResponse(__FILE__); |
| 56 | $response->setContent('foo'); |
| 57 | } |
| 58 | |
| 59 | public function testGetContent() |
| 60 | { |
| 61 | $response = new BinaryFileResponse(__FILE__); |
| 62 | $this->assertFalse($response->getContent()); |
| 63 | } |
| 64 | |
| 65 | public function testSetContentDispositionGeneratesSafeFallbackFilename() |
| 66 | { |
| 67 | $response = new BinaryFileResponse(__FILE__); |
| 68 | $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html'); |
| 69 | |
| 70 | $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition')); |
| 71 | } |
| 72 | |
| 73 | public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename() |
| 74 | { |
| 75 | $response = new BinaryFileResponse(__FILE__); |
| 76 | |
| 77 | $iso88591EncodedFilename = utf8_decode('föö.html'); |
| 78 | $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename); |
| 79 | |
| 80 | // 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) |
| 81 | $this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition')); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @dataProvider provideRanges |
| 86 | */ |
| 87 | public function testRequests($requestRange, $offset, $length, $responseRange) |
| 88 | { |
| 89 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag(); |
| 90 | |
| 91 | // do a request to get the ETag |
| 92 | $request = Request::create('/'); |
| 93 | $response->prepare($request); |
| 94 | $etag = $response->headers->get('ETag'); |
| 95 | |
| 96 | // prepare a request for a range of the testing file |
| 97 | $request = Request::create('/'); |
| 98 | $request->headers->set('If-Range', $etag); |
| 99 | $request->headers->set('Range', $requestRange); |
| 100 | |
| 101 | $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r'); |
| 102 | fseek($file, $offset); |
| 103 | $data = fread($file, $length); |
| 104 | fclose($file); |
| 105 | |
| 106 | $this->expectOutputString($data); |
| 107 | $response = clone $response; |
| 108 | $response->prepare($request); |
| 109 | $response->sendContent(); |
| 110 | |
| 111 | $this->assertEquals(206, $response->getStatusCode()); |
| 112 | $this->assertEquals($responseRange, $response->headers->get('Content-Range')); |
| 113 | $this->assertSame((string) $length, $response->headers->get('Content-Length')); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @dataProvider provideRanges |
| 118 | */ |
| 119 | public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange) |
| 120 | { |
| 121 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']); |
| 122 | |
| 123 | // do a request to get the LastModified |
| 124 | $request = Request::create('/'); |
| 125 | $response->prepare($request); |
| 126 | $lastModified = $response->headers->get('Last-Modified'); |
| 127 | |
| 128 | // prepare a request for a range of the testing file |
| 129 | $request = Request::create('/'); |
| 130 | $request->headers->set('If-Range', $lastModified); |
| 131 | $request->headers->set('Range', $requestRange); |
| 132 | |
| 133 | $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r'); |
| 134 | fseek($file, $offset); |
| 135 | $data = fread($file, $length); |
| 136 | fclose($file); |
| 137 | |
| 138 | $this->expectOutputString($data); |
| 139 | $response = clone $response; |
| 140 | $response->prepare($request); |
| 141 | $response->sendContent(); |
| 142 | |
| 143 | $this->assertEquals(206, $response->getStatusCode()); |
| 144 | $this->assertEquals($responseRange, $response->headers->get('Content-Range')); |
| 145 | } |
| 146 | |
| 147 | public function provideRanges() |
| 148 | { |
| 149 | return [ |
| 150 | ['bytes=1-4', 1, 4, 'bytes 1-4/35'], |
| 151 | ['bytes=-5', 30, 5, 'bytes 30-34/35'], |
| 152 | ['bytes=30-', 30, 5, 'bytes 30-34/35'], |
| 153 | ['bytes=30-30', 30, 1, 'bytes 30-30/35'], |
| 154 | ['bytes=30-34', 30, 5, 'bytes 30-34/35'], |
| 155 | ['bytes=30-40', 30, 5, 'bytes 30-34/35'], |
| 156 | ]; |
| 157 | } |
| 158 | |
| 159 | public function testRangeRequestsWithoutLastModifiedDate() |
| 160 | { |
| 161 | // prevent auto last modified |
| 162 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false); |
| 163 | |
| 164 | // prepare a request for a range of the testing file |
| 165 | $request = Request::create('/'); |
| 166 | $request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT'); |
| 167 | $request->headers->set('Range', 'bytes=1-4'); |
| 168 | |
| 169 | $this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif')); |
| 170 | $response = clone $response; |
| 171 | $response->prepare($request); |
| 172 | $response->sendContent(); |
| 173 | |
| 174 | $this->assertEquals(200, $response->getStatusCode()); |
| 175 | $this->assertNull($response->headers->get('Content-Range')); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @dataProvider provideFullFileRanges |
| 180 | */ |
| 181 | public function testFullFileRequests($requestRange) |
| 182 | { |
| 183 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag(); |
| 184 | |
| 185 | // prepare a request for a range of the testing file |
| 186 | $request = Request::create('/'); |
| 187 | $request->headers->set('Range', $requestRange); |
| 188 | |
| 189 | $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r'); |
| 190 | $data = fread($file, 35); |
| 191 | fclose($file); |
| 192 | |
| 193 | $this->expectOutputString($data); |
| 194 | $response = clone $response; |
| 195 | $response->prepare($request); |
| 196 | $response->sendContent(); |
| 197 | |
| 198 | $this->assertEquals(200, $response->getStatusCode()); |
| 199 | } |
| 200 | |
| 201 | public function provideFullFileRanges() |
| 202 | { |
| 203 | return [ |
| 204 | ['bytes=0-'], |
| 205 | ['bytes=0-34'], |
| 206 | ['bytes=-35'], |
| 207 | // Syntactical invalid range-request should also return the full resource |
| 208 | ['bytes=20-10'], |
| 209 | ['bytes=50-40'], |
| 210 | // range units other than bytes must be ignored |
| 211 | ['unknown=10-20'], |
| 212 | ]; |
| 213 | } |
| 214 | |
| 215 | public function testRangeOnPostMethod() |
| 216 | { |
| 217 | $request = Request::create('/', 'POST'); |
| 218 | $request->headers->set('Range', 'bytes=10-20'); |
| 219 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']); |
| 220 | |
| 221 | $file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r'); |
| 222 | $data = fread($file, 35); |
| 223 | fclose($file); |
| 224 | |
| 225 | $this->expectOutputString($data); |
| 226 | $response = clone $response; |
| 227 | $response->prepare($request); |
| 228 | $response->sendContent(); |
| 229 | |
| 230 | $this->assertSame(200, $response->getStatusCode()); |
| 231 | $this->assertSame('35', $response->headers->get('Content-Length')); |
| 232 | $this->assertNull($response->headers->get('Content-Range')); |
| 233 | } |
| 234 | |
| 235 | public function testUnpreparedResponseSendsFullFile() |
| 236 | { |
| 237 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200); |
| 238 | |
| 239 | $data = file_get_contents(__DIR__.'/File/Fixtures/test.gif'); |
| 240 | |
| 241 | $this->expectOutputString($data); |
| 242 | $response = clone $response; |
| 243 | $response->sendContent(); |
| 244 | |
| 245 | $this->assertEquals(200, $response->getStatusCode()); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @dataProvider provideInvalidRanges |
| 250 | */ |
| 251 | public function testInvalidRequests($requestRange) |
| 252 | { |
| 253 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag(); |
| 254 | |
| 255 | // prepare a request for a range of the testing file |
| 256 | $request = Request::create('/'); |
| 257 | $request->headers->set('Range', $requestRange); |
| 258 | |
| 259 | $response = clone $response; |
| 260 | $response->prepare($request); |
| 261 | $response->sendContent(); |
| 262 | |
| 263 | $this->assertEquals(416, $response->getStatusCode()); |
| 264 | $this->assertEquals('bytes */35', $response->headers->get('Content-Range')); |
| 265 | } |
| 266 | |
| 267 | public function provideInvalidRanges() |
| 268 | { |
| 269 | return [ |
| 270 | ['bytes=-40'], |
| 271 | ['bytes=40-50'], |
| 272 | ]; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @dataProvider provideXSendfileFiles |
| 277 | */ |
| 278 | public function testXSendfile($file) |
| 279 | { |
| 280 | $request = Request::create('/'); |
| 281 | $request->headers->set('X-Sendfile-Type', 'X-Sendfile'); |
| 282 | |
| 283 | BinaryFileResponse::trustXSendfileTypeHeader(); |
| 284 | $response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']); |
| 285 | $response->prepare($request); |
| 286 | |
| 287 | $this->expectOutputString(''); |
| 288 | $response->sendContent(); |
| 289 | |
| 290 | $this->assertStringContainsString('README.md', $response->headers->get('X-Sendfile')); |
| 291 | } |
| 292 | |
| 293 | public function provideXSendfileFiles() |
| 294 | { |
| 295 | return [ |
| 296 | [__DIR__.'/../README.md'], |
| 297 | ['file://'.__DIR__.'/../README.md'], |
| 298 | ]; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @dataProvider getSampleXAccelMappings |
| 303 | */ |
| 304 | public function testXAccelMapping($realpath, $mapping, $virtual) |
| 305 | { |
| 306 | $request = Request::create('/'); |
| 307 | $request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect'); |
| 308 | $request->headers->set('X-Accel-Mapping', $mapping); |
| 309 | |
| 310 | $file = new FakeFile($realpath, __DIR__.'/File/Fixtures/test'); |
| 311 | |
| 312 | BinaryFileResponse::trustXSendfileTypeHeader(); |
| 313 | $response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']); |
| 314 | $reflection = new \ReflectionObject($response); |
| 315 | $property = $reflection->getProperty('file'); |
| 316 | $property->setAccessible(true); |
| 317 | $property->setValue($response, $file); |
| 318 | |
| 319 | $response->prepare($request); |
| 320 | $this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect')); |
| 321 | } |
| 322 | |
| 323 | public function testDeleteFileAfterSend() |
| 324 | { |
| 325 | $request = Request::create('/'); |
| 326 | |
| 327 | $path = __DIR__.'/File/Fixtures/to_delete'; |
| 328 | touch($path); |
| 329 | $realPath = realpath($path); |
| 330 | $this->assertFileExists($realPath); |
| 331 | |
| 332 | $response = new BinaryFileResponse($realPath, 200, ['Content-Type' => 'application/octet-stream']); |
| 333 | $response->deleteFileAfterSend(true); |
| 334 | |
| 335 | $response->prepare($request); |
| 336 | $response->sendContent(); |
| 337 | |
| 338 | $this->assertFileDoesNotExist($path); |
| 339 | } |
| 340 | |
| 341 | public function testAcceptRangeOnUnsafeMethods() |
| 342 | { |
| 343 | $request = Request::create('/', 'POST'); |
| 344 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']); |
| 345 | $response->prepare($request); |
| 346 | |
| 347 | $this->assertEquals('none', $response->headers->get('Accept-Ranges')); |
| 348 | } |
| 349 | |
| 350 | public function testAcceptRangeNotOverriden() |
| 351 | { |
| 352 | $request = Request::create('/', 'POST'); |
| 353 | $response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']); |
| 354 | $response->headers->set('Accept-Ranges', 'foo'); |
| 355 | $response->prepare($request); |
| 356 | |
| 357 | $this->assertEquals('foo', $response->headers->get('Accept-Ranges')); |
| 358 | } |
| 359 | |
| 360 | public function getSampleXAccelMappings() |
| 361 | { |
| 362 | return [ |
| 363 | ['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'], |
| 364 | ['/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'], |
| 365 | ['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null], |
| 366 | ]; |
| 367 | } |
| 368 | |
| 369 | public function testStream() |
| 370 | { |
| 371 | $request = Request::create('/'); |
| 372 | $response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, ['Content-Type' => 'text/plain']); |
| 373 | $response->prepare($request); |
| 374 | |
| 375 | $this->assertNull($response->headers->get('Content-Length')); |
| 376 | } |
| 377 | |
| 378 | protected function provideResponse() |
| 379 | { |
| 380 | return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']); |
| 381 | } |
| 382 | |
| 383 | public static function tearDownAfterClass() |
| 384 | { |
| 385 | $path = __DIR__.'/../Fixtures/to_delete'; |
| 386 | if (file_exists($path)) { |
| 387 | @unlink($path); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 |