Skip to content

Commit 0cf0ec2

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: Fix BinaryFileResponse content type detection logic Bump Symfony version to 4.4.47 Update VERSION for 4.4.46 Update CONTRIBUTORS for 4.4.46 Update CHANGELOG for 4.4.46
2 parents 54be067 + 9aeb286 commit 0cf0ec2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

BinaryFileResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ public function setContentDisposition(string $disposition, string $filename = ''
206206
*/
207207
public function prepare(Request $request)
208208
{
209-
parent::prepare($request);
210-
211209
if ($this->isInformational() || $this->isEmpty()) {
210+
parent::prepare($request);
211+
212212
$this->maxlen = 0;
213213

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

221+
parent::prepare($request);
222+
221223
$this->offset = 0;
222224
$this->maxlen = -1;
223225

Tests/BinaryFileResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,32 @@ public function testPrepareNotAddingContentTypeHeaderIfNoContentResponse()
411411
$this->assertFalse($response->headers->has('Content-Type'));
412412
}
413413

414+
public function testContentTypeIsCorrectlyDetected()
415+
{
416+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
417+
418+
$request = Request::create('/');
419+
$response->prepare($request);
420+
421+
$this->assertSame(200, $response->getStatusCode());
422+
$this->assertSame('image/gif', $response->headers->get('Content-Type'));
423+
}
424+
425+
public function testContentTypeIsNotGuessedWhenTheFileWasNotModified()
426+
{
427+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
428+
$response->setAutoLastModified();
429+
430+
$request = Request::create('/');
431+
$request->headers->set('If-Modified-Since', $response->getLastModified()->format('D, d M Y H:i:s').' GMT');
432+
$isNotModified = $response->isNotModified($request);
433+
$this->assertTrue($isNotModified);
434+
$response->prepare($request);
435+
436+
$this->assertSame(304, $response->getStatusCode());
437+
$this->assertFalse($response->headers->has('Content-Type'));
438+
}
439+
414440
protected function provideResponse()
415441
{
416442
return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);

0 commit comments

Comments
 (0)