8000 Fix BinaryFileResponse content type detection logic · symfony/symfony@87f58ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 87f58ae

Browse files
committed
Fix BinaryFileResponse content type detection logic
1 parent 146e0ac commit 87f58ae

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
201201
*/
202202
public function prepare(Request $request)
203203
{
204-
parent::prepare($request);
205-
206204
if ($this->isInformational() || $this->isEmpty()) {
205+
parent::prepare($request);
206+
207207
$this->maxlen = 0;
208208

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

216+
parent::prepare($request);
217+
216218
$this->offset = 0;
217219
$this->maxlen = -1;
218220

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

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

391+
public function testContentTypeIsCorrectlyDetected()
392+
{
393+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
394+
395+
$request = Request::create('/');
396+
$response->prepare($request);
397+
398+
$this->assertSame(200, $response->getStatusCode());
399+
$this->assertSame('image/gif', $response->headers->get('Content-Type'));
400+
}
401+
402+
public function testContentTypeIsNotGuessedWhenTheFileWasNotModified()
403+
{
404+
$response = new BinaryFileResponse(__DIR__.'/File/Fixtures/test.gif');
405+
$response->setAutoLastModified();
406+
407+
$request = Request::create('/');
408+
$request->headers->set('If-Modified-Since', $response->getLastModified()->format('D, d M Y H:i:s').' GMT');
409+
$isNotModified = $response->isNotModified($request);
410+
$this->assertTrue($isNotModified);
411+
$response->prepare($request);
412+
413+
$this->assertSame(304, $response->getStatusCode());
414+
$this->assertFalse($response->headers->has('Content-Type'));
415+
}
416+
391417
protected function provideResponse()
392418
{
393419
return new BinaryFileResponse(__DIR__.'/../README.md', 200, ['Content-Type' => 'application/octet-stream']);

0 commit comments

Comments
 (0)
0