File tree 3 files changed +18
-3
lines changed
3 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -1590,7 +1590,9 @@ public function isNoCache()
1590
1590
* Gets the preferred format for the response by inspecting, in the following order:
1591
1591
* * the request format set using setRequestFormat
1592
1592
* * the values of the Accept HTTP header
1593
- * * the content type of the body of the request.
1593
+ *
1594
+ * Note that if you use this method, you should send the "Vary: Accept" header
1595
+ * in the response to prevent any issues with intermediary HTTP caches.
1594
1596
*/
1595
1597
public function getPreferredFormat (?string $ default = 'html ' ): ?string
1596
1598
{
Original file line number Diff line number Diff line change @@ -275,7 +275,7 @@ public function prepare(Request $request)
275
275
} else {
276
276
// Content-type based on the Request
277
277
if (!$ headers ->has ('Content-Type ' )) {
278
- $ format = $ request ->getPreferredFormat (null );
278
+ $ format = $ request ->getRequestFormat (null );
279
279
if (null !== $ format && $ mimeType = $ request ->getMimeType ($ format )) {
280
280
$ headers ->set ('Content-Type ' , $ mimeType );
281
281
}
Original file line number Diff line number Diff line change @@ -497,12 +497,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
497
497
$ this ->assertEquals ('text/html; charset=UTF-8 ' , $ response ->headers ->get ('content-type ' ));
498
498
}
499
499
500
+ /**
501
+ * Same URL cannot produce different Content-Type based on the value of the Accept header,
502
+ * unless explicitly stated in the response object.
503
+ */
504
+ public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader ()
505
+ {
506
+ $ response = new Response ('foo ' );
507
+ $ request = Request::create ('/ ' );
508
+ $ request ->headers ->set ('Accept ' , 'application/json ' );
509
+ $ response ->prepare ($ request );
510
+
511
+ $ this ->assertSame ('text/html; charset=UTF-8 ' , $ response ->headers ->get ('content-type ' ));
512
+ }
513
+
500
514
public function testPrepareSetContentType ()
501
515
{
502
516
$ response = new Response ('foo ' );
503
517
$ request = Request::create ('/ ' );
504
518
$ request ->setRequestFormat ('json ' );
505
- $ request ->headers ->remove ('accept ' );
506
519
507
520
$ response ->prepare ($ request );
508
521
You can’t perform that action at this time.
0 commit comments