File tree Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -1566,7 +1566,9 @@ public function isNoCache()
15661566 * Gets the preferred format for the response by inspecting, in the following order:
15671567 * * the request format set using setRequestFormat
15681568 * * the values of the Accept HTTP header
1569- * * the content type of the body of the request.
1569+ *
1570+ * Note that if you use this method, you should send the "Vary: Accept" header
1571+ * in the response to prevent any issues with intermediary HTTP caches.
15701572 */
15711573 public function getPreferredFormat (?string $ default = 'html ' ): ?string
15721574 {
Original file line number Diff line number Diff line change @@ -270,7 +270,7 @@ public function prepare(Request $request)
270270 } else {
271271 // Content-type based on the Request
272272 if (!$ headers ->has ('Content-Type ' )) {
273- $ format = $ request ->getPreferredFormat (null );
273+ $ format = $ request ->getRequestFormat (null );
274274 if (null !== $ format && $ mimeType = $ request ->getMimeType ($ format )) {
275275 $ headers ->set ('Content-Type ' , $ mimeType );
276276 }
Original file line number Diff line number Diff line change @@ -500,12 +500,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
500500 $ this ->assertEquals ('text/html; charset=UTF-8 ' , $ response ->headers ->get ('content-type ' ));
501501 }
502502
503+ /**
504+ * Same URL cannot produce different Content-Type based on the value of the Accept header,
505+ * unless explicitly stated in the response object.
506+ */
507+ public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader ()
508+ {
509+ $ response = new Response ('foo ' );
510+ $ request = Request::create ('/ ' );
511+ $ request ->headers ->set ('Accept ' , 'application/json ' );
512+ $ response ->prepare ($ request );
513+
514+ $ this ->assertSame ('text/html; charset=UTF-8 ' , $ response ->headers ->get ('content-type ' ));
515+ }
516+
503517 public function testPrepareSetContentType ()
504518 {
505519 $ response = new Response ('foo ' );
506520 $ request = Request::create ('/ ' );
507521 $ request ->setRequestFormat ('json ' );
508- $ request ->headers ->remove ('accept ' );
509522
510523 $ response ->prepare ($ request );
511524
You can’t perform that action at this time.
0 commit comments