Skip to content

Commit 8c53c4f

Browse files
author
Ben Ramsey
committed
Fixing tie breaker bug
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.
1 parent 8239b25 commit 8c53c4f

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/Bitworking/Mimeparse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public static function bestMatch($supported, $header, $tieBreaker = null)
243243
$ties = array_filter($weightedMatches, function ($val) use ($a) {
244244
return ($val[0] == $a[0]);
245245
});
246-
if (count($ties) > 1) {
246+
if (count($ties) > 1 && in_array(array($a[0], $tieBreaker), $ties)) {
247247
return $tieBreaker;
248248
}
249249
}

tests/Bitworking/MimeparseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,15 @@ public function testBestMatchWithTieBreakerAndNoTies()
214214

215215
$this->assertEquals('application/hal+json', Mimeparse::bestMatch($supportedMimeTypes, $httpAcceptHeader, 'application/hal+json'));
216216
}
217+
218+
/**
219+
* @covers Bitworking\Mimeparse::bestMatch
220+
*/
221+
public function testBestMatchWithTieBreakerNotMatchingTies()
222+
{
223+
$supportedMimeTypes = array('text/html', 'application/hal+xml', 'application/hal+json');
224+
$httpAcceptHeader = 'application/*';
225+
226+
$this->assertEquals('application/hal+xml', Mimeparse::bestMatch($supportedMimeTypes, $httpAcceptHeader, 'text/html'));
227+
}
217228
}

0 commit comments

Comments
 (0)