Skip to content

Commit

Permalink
[HttpFoundation] Do not set the default Content-Type based on the Acc…
Browse files Browse the repository at this point in the history
…ept header
  • Loading branch information
yceruto authored and nicolas-grekas committed Mar 30, 2020
1 parent 67d0196 commit 62f9250
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,9 @@ public function isNoCache()
* Gets the preferred format for the response by inspecting, in the following order:
* * the request format set using setRequestFormat
* * the values of the Accept HTTP header
* * the content type of the body of the request.
*
* Note that if you use this method, you should send the "Vary: Accept" header
* in the response to prevent any issues with intermediary HTTP caches.
*/
public function getPreferredFormat(?string $default = 'html'): ?string
{
Expand Down
2 changes: 1 addition & 1 deletion Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function prepare(Request $request)
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getPreferredFormat(null);
$format = $request->getRequestFormat(null);
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
Expand Down
15 changes: 14 additions & 1 deletion Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
}

/**
* Same URL cannot produce different Content-Type based on the value of the Accept header,
* unless explicitly stated in the response object.
*/
public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader()
{
$response = new Response('foo');
$request = Request::create('/');
$request->headers->set('Accept', 'application/json');
$response->prepare($request);

$this->assertSame('text/html; charset=UTF-8', $response->headers->get('content-type'));
}

public function testPrepareSetContentType()
{
$response = new Response('foo');
$request = Request::create('/');
$request->setRequestFormat('json');
$request->headers->remove('accept');

$response->prepare($request);

Expand Down

0 comments on commit 62f9250

Please sign in to comment.