From 94733e96952a1f57db600582a8221807bb6f100a Mon Sep 17 00:00:00 2001 From: William DURAND Date: Fri, 16 May 2014 14:31:35 +0200 Subject: [PATCH] Check quality > 0.0 in FormatNegotiator::getBestFormat() The getBestFormat() method returns a format or null. When quality == 0.0, then it should return null rather than the negotiated format. Fix #29 See: https://github.com/willdurand/Negotiation/issues/29 --- src/Negotiation/FormatNegotiator.php | 4 +++- tests/Negotiation/Tests/FormatNegotiatorTest.php | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Negotiation/FormatNegotiator.php b/src/Negotiation/FormatNegotiator.php index 4147b6c..352ba4e 100644 --- a/src/Negotiation/FormatNegotiator.php +++ b/src/Negotiation/FormatNegotiator.php @@ -87,7 +87,9 @@ public function getBestFormat($acceptHeader, array $priorities = array()) $catchAllEnabled = $this->isCatchAllEnabled($priorities); if (null !== $accept = $this->getBest($acceptHeader, $mimeTypes)) { - if (null !== $format = $this->getFormat($accept->getValue())) { + if (0.0 < $accept->getQuality() && + null !== $format = $this->getFormat($accept->getValue()) + ) { if (in_array($format, $priorities) || $catchAllEnabled) { return $format; } diff --git a/tests/Negotiation/Tests/FormatNegotiatorTest.php b/tests/Negotiation/Tests/FormatNegotiatorTest.php index 762cf69..7750716 100644 --- a/tests/Negotiation/Tests/FormatNegotiatorTest.php +++ b/tests/Negotiation/Tests/FormatNegotiatorTest.php @@ -254,6 +254,7 @@ public static function dataProviderForGetBestFormat() array('text/html,application/xhtml+xml,application/xml', array('json'), null), array('text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c', array('*/*'), 'html'), array('text/html, application/json;q=0.8, text/csv;q=0.7', array(), 'html'), + array('text/html; q=0.0', array(), null), ); }