Bladeren bron

[HttpFoundation] fixed Request::getFormat() when the mime-type has some optional parameter (closes #1235)

Fabien Potencier 14 jaren geleden
bovenliggende
commit
0af4743583

+ 4 - 0
src/Symfony/Component/HttpFoundation/Request.php

@@ -674,6 +674,10 @@ class Request
      */
     public function getFormat($mimeType)
     {
+        if (false !== $pos = strpos($mimeType, ';')) {
+            $mimeType = substr($mimeType, 0, $pos);
+        }
+
         if (null === static::$formats) {
             static::initializeFormats();
         }

+ 9 - 0
tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php

@@ -146,6 +146,15 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * @covers Symfony\Component\HttpFoundation\Request::getFormat
+     */
+    public function testGetFormatFromMimeTypeWithParameters()
+    {
+        $request = new Request();
+        $this->assertEquals('json', $request->getFormat('application/json; charset=utf-8'));
+    }
+
     /**
      * @covers Symfony\Component\HttpFoundation\Request::getMimeType
      * @dataProvider getFormatToMimeTypeMapProvider