Skip to content

Commit

Permalink
Re-introduce "Add $strict parameter to getBest() to enable except…
Browse files Browse the repository at this point in the history
…ions for invalid media types.""
  • Loading branch information
willdurand committed Oct 14, 2016
1 parent 1f210db commit 93c3cbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Negotiation/AbstractNegotiator.php
Expand Up @@ -13,7 +13,7 @@ abstract class AbstractNegotiator
* *
* @return AcceptHeader|null best matching type * @return AcceptHeader|null best matching type
*/ */
public function getBest($header, array $priorities) public function getBest($header, array $priorities, $strict = false)
{ {
if (empty($priorities)) { if (empty($priorities)) {
throw new InvalidArgument('A set of server priorities should be given.'); throw new InvalidArgument('A set of server priorities should be given.');
Expand All @@ -33,7 +33,9 @@ public function getBest($header, array $priorities)
try { try {
$acceptedHeaders[] = $this->acceptFactory($h); $acceptedHeaders[] = $this->acceptFactory($h);
} catch (Exception\Exception $e) { } catch (Exception\Exception $e) {
// silently skip in case of invalid headers coming in from a client if ($strict) {
throw $e;
}
} }
} }
$acceptedPriorities = array(); $acceptedPriorities = array();
Expand Down
11 changes: 11 additions & 0 deletions tests/Negotiation/Tests/NegotiatorTest.php
Expand Up @@ -104,6 +104,17 @@ public function testGetBestRespectsQualityOfSource()
$this->assertEquals('text/plain', $accept->getType()); $this->assertEquals('text/plain', $accept->getType());
} }


/**
* @expectedException Negotiation\Exception\InvalidMediaType
*/
public function testGetBestInvalidMediaType()
{
$header = 'sdlfkj20ff; wdf';
$priorities = array('foo/qwer');

$this->negotiator->getBest($header, $priorities, true);
}

/** /**
* @dataProvider dataProviderForTestParseHeader * @dataProvider dataProviderForTestParseHeader
*/ */
Expand Down

0 comments on commit 93c3cbf

Please sign in to comment.