Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 4.4:
  [HttpFoundation] Do not set the default Content-Type based on the Accept header
  [Security] Fix access_control behavior with unanimous decision strategy
  • Loading branch information
nicolas-grekas committed Mar 30, 2020
2 parents 98b44bd + 62f9250 commit 26fb006
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 @@ -1561,7 +1561,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 @@ -266,7 +266,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 26fb006

Please sign in to comment.