Skip to content

Commit

Permalink
Fixing tie breaker bug
Browse files Browse the repository at this point in the history
The tie breaker value was being used even in cases where the tie breaker
did not appear in the list of best match ties. This commit fixes this,
so that the tie breaker is used only if it appears within the list of
tied best matches.
  • Loading branch information
Ben Ramsey committed Sep 8, 2012
1 parent 8239b25 commit 8c53c4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Bitworking/Mimeparse.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public static function bestMatch($supported, $header, $tieBreaker = null)
$ties = array_filter($weightedMatches, function ($val) use ($a) {
return ($val[0] == $a[0]);
});
if (count($ties) > 1) {
if (count($ties) > 1 && in_array(array($a[0], $tieBreaker), $ties)) {
return $tieBreaker;
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Bitworking/MimeparseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,15 @@ public function testBestMatchWithTieBreakerAndNoTies()

$this->assertEquals('application/hal+json', Mimeparse::bestMatch($supportedMimeTypes, $httpAcceptHeader, 'application/hal+json'));
}

/**
* @covers Bitworking\Mimeparse::bestMatch
*/
public function testBestMatchWithTieBreakerNotMatchingTies()
{
$supportedMimeTypes = array('text/html', 'application/hal+xml', 'application/hal+json');
$httpAcceptHeader = 'application/*';

$this->assertEquals('application/hal+xml', Mimeparse::bestMatch($supportedMimeTypes, $httpAcceptHeader, 'text/html'));
}
}

0 comments on commit 8c53c4f

Please sign in to comment.