Skip to content

Commit

Permalink
[HttpFoundation] Deprecate BinaryFileResponse::create().
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Nov 20, 2020
1 parent c13d5b5 commit 2c3918f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions UPGRADE-5.2.md
Expand Up @@ -44,6 +44,7 @@ HttpFoundation

* Deprecated not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`; wrap your filter in a closure instead.
* Deprecated the `Request::HEADER_X_FORWARDED_ALL` constant, use either `Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO` or `Request::HEADER_X_FORWARDED_AWS_ELB` or `Request::HEADER_X_FORWARDED_TRAEFIK`constants instead.
* Deprecated `BinaryFileResponse::create()`, use `__construct()` instead

Lock
----
Expand Down
4 changes: 2 additions & 2 deletions UPGRADE-6.0.md
Expand Up @@ -64,8 +64,8 @@ HttpFoundation
--------------

* Removed `Response::create()`, `JsonResponse::create()`,
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
`__construct()` instead)
`RedirectResponse::create()`, `StreamedResponse::create()` and
`BinaryFileResponse::create()` methods (use `__construct()` instead)
* Not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()` throws an `InvalidArgumentException`; wrap your filter in a closure instead.
* Removed the `Request::HEADER_X_FORWARDED_ALL` constant, use either `Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO` or `Request::HEADER_X_FORWARDED_AWS_ELB` or `Request::HEADER_X_FORWARDED_TRAEFIK`constants instead.

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Expand Up @@ -65,9 +65,13 @@ public function __construct($file, int $status = 200, array $headers = [], bool
* @param bool $autoLastModified Whether the Last-Modified header should be automatically set
*
* @return static
*
* @deprecated since Symfony 5.2, use __construct() instead.
*/
public static function create($file = null, int $status = 200, array $headers = [], bool $public = true, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
{
trigger_deprecation('symfony/http-foundation', '5.2', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, \get_called_class());

return new static($file, $status, $headers, $public, $contentDisposition, $autoEtag, $autoLastModified);
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* added `RateLimiter\RequestRateLimiterInterface` and `RateLimiter\AbstractRequestRateLimiter`
* deprecated not passing a `Closure` together with `FILTER_CALLBACK` to `ParameterBag::filter()`; wrap your filter in a closure instead.
* Deprecated the `Request::HEADER_X_FORWARDED_ALL` constant, use either `HEADER_X_FORWARDED_FOR | HEADER_X_FORWARDED_HOST | HEADER_X_FORWARDED_PORT | HEADER_X_FORWARDED_PROTO` or `HEADER_X_FORWARDED_AWS_ELB` or `HEADER_X_FORWARDED_TRAEFIK` constants instead.
* Deprecated `BinaryFileResponse::create()`, use `__construct()` instead


5.1.0
Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpFoundation\Tests;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Stream;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -19,6 +20,8 @@

class BinaryFileResponseTest extends ResponseTestCase
{
use ExpectDeprecationTrait;

public function testConstruction()
{
$file = __DIR__.'/../README.md';
Expand All @@ -29,6 +32,26 @@ public function testConstruction()
$this->assertTrue($response->headers->has('Last-Modified'));
$this->assertFalse($response->headers->has('Content-Disposition'));

$response = new BinaryFileResponse($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
$this->assertEquals(404, $response->getStatusCode());
$this->assertFalse($response->headers->has('ETag'));
$this->assertEquals('inline; filename=README.md', $response->headers->get('Content-Disposition'));
}

/**
* @group legacy
*/
public function testConstructionLegacy()
{
$file = __DIR__.'/../README.md';
$this->expectDeprecation('Since symfony/http-foundation 5.2: The "Symfony\Component\HttpFoundation\BinaryFileResponse::create()" method is deprecated, use "new Symfony\Component\HttpFoundation\BinaryFileResponse()" instead.');
$response = BinaryFileResponse::create($file, 404, ['X-Header' => 'Foo'], true, null, true, true);
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('Foo', $response->headers->get('X-Header'));
$this->assertTrue($response->headers->has('ETag'));
$this->assertTrue($response->headers->has('Last-Modified'));
$this->assertFalse($response->headers->has('Content-Disposition'));

$response = BinaryFileResponse::create($file, 404, [], true, ResponseHeaderBag::DISPOSITION_INLINE);
$this->assertEquals(404, $response->getStatusCode());
$this->assertFalse($response->headers->has('ETag'));
Expand Down Expand Up @@ -83,7 +106,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
*/
public function testRequests($requestRange, $offset, $length, $responseRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();

// do a request to get the ETag
$request = Request::create('/');
Expand Down Expand Up @@ -115,7 +138,7 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
*/
public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);

// do a request to get the LastModified
$request = Request::create('/');
Expand Down Expand Up @@ -156,7 +179,7 @@ public function provideRanges()
public function testRangeRequestsWithoutLastModifiedDate()
{
// prevent auto last modified
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'], true, null, false, false);

// prepare a request for a range of the testing file
$request = Request::create('/');
Expand All @@ -177,7 +200,7 @@ public function testRangeRequestsWithoutLastModifiedDate()
*/
public function testFullFileRequests($requestRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();

// prepare a request for a range of the testing file
$request = Request::create('/');
Expand Down Expand Up @@ -213,7 +236,7 @@ public function testRangeOnPostMethod()
{
$request = Request::create('/', 'POST');
$request->headers->set('Range', 'bytes=10-20');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);

$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
$data = fread($file, 35);
Expand All @@ -231,7 +254,7 @@ public function testRangeOnPostMethod()

public function testUnpreparedResponseSendsFullFile()
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200);

$data = file_get_contents(__DIR__.'/File/Fixtures/test.gif');

Expand All @@ -247,7 +270,7 @@ public function testUnpreparedResponseSendsFullFile()
*/
public function testInvalidRequests($requestRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
$response = (new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']))->setAutoEtag();

// prepare a request for a range of the testing file
$request = Request::create('/');
Expand Down Expand Up @@ -278,7 +301,7 @@ public function testXSendfile($file)
$request->headers->set('X-Sendfile-Type', 'X-Sendfile');

BinaryFileResponse::trustXSendfileTypeHeader();
$response = BinaryFileResponse::create($file, 200, ['Content-Type' => 'application/octet-stream']);
$response = new BinaryFileResponse($file, 200, ['Content-Type' => 'application/octet-stream']);
$response->prepare($request);

$this->expectOutputString('');
Expand Down Expand Up @@ -338,7 +361,7 @@ public function testDeleteFileAfterSend()
public function testAcceptRangeOnUnsafeMethods()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response->prepare($request);

$this->assertEquals('none', $response->headers->get('Accept-Ranges'));
Expand All @@ -347,7 +370,7 @@ public function testAcceptRangeOnUnsafeMethods()
public function testAcceptRangeNotOverriden()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);
$response->headers->set('Accept-Ranges', 'foo');
$response->prepare($request);

Expand Down

0 comments on commit 2c3918f

Please sign in to comment.