public function testGetBestDoesNotMatchPriorities() { $acceptCharset = 'en, de'; $priorities = array('fr'); $this->assertNull($this->negotiator->getBest($acceptCharset, $priorities)); }
public function testGetBestRespectsQualityOfSource() { $accept = $this->negotiator->getBest('utf-8;q=0.5,iso-8859-1', array('iso-8859-1;q=0.3', 'utf-8;q=0.9', 'utf-16;q=1.0')); $this->assertInstanceOf('Negotiation\\AcceptCharset', $accept); $this->assertEquals('utf-8', $accept->getType()); }
/** * Handles a request to convert it to a response * * @param Request $request A Request instance * @param int $type The type of the request * @param bool $catch Whether to catch exceptions or not * * @return Response * * @throws Exception When an Exception occurs during processing */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { $acceptHeader = $request->headers->get('Accept'); if ($acceptHeader !== null && !empty($this->formatPriorities)) { $accept = $this->formatNegotiator->getBest($acceptHeader, $this->formatPriorities); $request->attributes->set('_accept', $accept); if ($accept !== null) { /** @var Accept $accept */ if (false === strpos($accept->getType(), '*')) { $mimeType = $accept->getType(); $format = $this->getFormat($mimeType); $request->attributes->set('_mime_type', $mimeType); $request->attributes->set('_format', $format); } } } $acceptLanguageHeader = $request->headers->get('Accept-Language'); if ($acceptLanguageHeader !== null && !empty($this->languagePriorities)) { $acceptLanguage = $this->languageNegotiator->getBest($acceptLanguageHeader, $this->languagePriorities); $request->attributes->set('_accept_language', $acceptLanguage); if ($acceptLanguage !== null) { /** @var AcceptLanguage $acceptLanguage */ $language = $acceptLanguage->getType(); $request->attributes->set('_language', $language); } } $acceptEncodingHeader = $request->headers->get('Accept-Encoding'); if ($acceptEncodingHeader !== null && !empty($this->encodingPriorities)) { $acceptEncoding = $this->encodingNegotiator->getBest($acceptEncodingHeader, $this->encodingPriorities); $request->attributes->set('_accept_encoding', $acceptEncoding); if ($acceptEncoding !== null) { /** @var AcceptEncoding $acceptEncoding */ $encoding = $acceptEncoding->getType(); $request->attributes->set('_encoding', $encoding); } } $acceptCharsetHeader = $request->headers->get('Accept-Charset'); if ($acceptCharsetHeader !== null && !empty($this->charsetPriorities)) { $acceptCharset = $this->charsetNegotiator->getBest($acceptCharsetHeader, $this->charsetPriorities); $request->attributes->set('_accept_charset', $acceptCharset); if ($acceptCharset !== null) { /** @var AcceptCharset $acceptCharset */ $charset = $acceptCharset->getType(); $request->attributes->set('_charset', $charset); } } try { $this->decodeBody($request); } catch (BadRequestHttpException $exception) { if ($catch === true) { return $this->handleException($exception, $request); } throw $exception; } return $this->kernel->handle($request, $type, $catch); }