Skip to content

Commit

Permalink
Fix BinaryFileResponse content type detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Coder264 committed Sep 30, 2022
1 parent 7acdc97 commit 9aeb286
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
*/
public function prepare(Request $request)
{
parent::prepare($request);

if ($this->isInformational() || $this->isEmpty()) {
parent::prepare($request);

$this->maxlen = 0;

return $this;
Expand All @@ -213,6 +213,8 @@ public function prepare(Request $request)
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
}

parent::prepare($request);

$this->offset = 0;
$this->maxlen = -1;

Expand Down
26 changes: 26 additions & 0 deletions Tests/BinaryFileResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,32 @@ public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
$this->assertFalse($response->headers->has('Content-Type'));
}

public function testContentTypeIsCorrectlyDetected()
{
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');

$request = Request::create('/');
$response->prepare($request);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('image/gif', $response->headers->get('Content-Type'));
}

public function testContentTypeIsNotGuessedWhenTheFileWasNotModified()
{
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
$response->setAutoLastModified();

$request = Request::create('/');
$request->headers->set('If-Modified-Since', $response->getLastModified()->format('D, d M Y H:i:s').' GMT');
$isNotModified = $response->isNotModified($request);
$this->assertTrue($isNotModified);
$response->prepare($request);

$this->assertSame(304, $response->getStatusCode());
$this->assertFalse($response->headers->has('Content-Type'));
}

protected function provideResponse()
{
return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);
Expand Down

1 comment on commit 9aeb286

@EgorGruzdev
Copy link

@EgorGruzdev EgorGruzdev commented on 9aeb286 Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeat fix for 6.0.13 version, please.

Please sign in to comment.