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 @@ -1590,7 +1590,9 @@ public function isNoCache()
15901590     * Gets the preferred format for the response by inspecting, in the following order: 
15911591     *   * the request format set using setRequestFormat 
15921592     *   * 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. 
15941596     */ 
15951597    public  function  getPreferredFormat (?string  $ default'html ' ): ?string 
15961598    {
Original file line number Diff line number Diff line change @@ -275,7 +275,7 @@ public function prepare(Request $request)
275275        } else  {
276276            // Content-type based on the Request 
277277            if  (!$ headershas ('Content-Type ' )) {
278-                 $ format$ requestgetPreferredFormat (null );
278+                 $ format$ requestgetRequestFormat (null );
279279                if  (null  !== $ format$ mimeType$ requestgetMimeType ($ format
280280                    $ headersset ('Content-Type ' , $ mimeType
281281                }
Original file line number Diff line number Diff line change @@ -497,12 +497,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
497497        $ this assertEquals ('text/html; charset=UTF-8 ' , $ responseheaders ->get ('content-type ' ));
498498    }
499499
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+         $ responsenew  Response ('foo ' );
507+         $ requestcreate ('/ ' );
508+         $ requestheaders ->set ('Accept ' , 'application/json ' );
509+         $ responseprepare ($ request
510+ 
511+         $ this assertSame ('text/html; charset=UTF-8 ' , $ responseheaders ->get ('content-type ' ));
512+     }
513+ 
500514    public  function  testPrepareSetContentType ()
501515    {
502516        $ responsenew  Response ('foo ' );
503517        $ requestcreate ('/ ' );
504518        $ requestsetRequestFormat ('json ' );
505-         $ requestheaders ->remove ('accept ' );
506519
507520        $ responseprepare ($ request
508521
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments