| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Http; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\Http\HttpAcceptParser; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class HttpAcceptParserTest extends TestCase { |
| 14 |
|
| 15 |
public static function provideParseWeights() { |
| 16 |
return [ |
| 17 |
[ |
| 18 |
'', |
| 19 |
[] |
| 20 |
], |
| 21 |
[ |
| 22 |
'Foo/Bar', |
| 23 |
[ 'foo/bar' => 1 ] |
| 24 |
], |
| 25 |
[ |
| 26 |
'Accept: text/plain', |
| 27 |
[ 'text/plain' => 1 ] |
| 28 |
], |
| 29 |
[ |
| 30 |
'Accept: application/vnd.php.serialized, application/rdf+xml', |
| 31 |
[ 'application/vnd.php.serialized' => 1, 'application/rdf+xml' => 1 ] |
| 32 |
], |
| 33 |
[ |
| 34 |
'foo/*; q=0.2, xoo; q=0,text/n3', |
| 35 |
[ 'text/n3' => 1, 'foo/*' => 0.2 ] |
| 36 |
], |
| 37 |
[ |
| 38 |
'foo/*; q=0.2, */*; q=0.1,text/*', |
| 39 |
[ 'text/*' => 1, 'foo/*' => 0.2, '*/*' => 0.1 ] |
| 40 |
], |
| 41 |
[ |
| 42 |
'Foo/*; q=0.2, Xoo/*; level=3, Bar/*; charset=xyz; q=0.4', |
| 43 |
[ 'xoo/*' => 1, 'bar/*' => 0.4, 'foo/*' => 0.2 ] |
| 44 |
], |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testParseWeights( $header, $expected ) { |
| 52 |
$parser = new HttpAcceptParser(); |
| 53 |
$actual = $parser->parseWeights( $header ); |
| 54 |
|
| 55 |
$this->assertEquals( $expected, $actual ); |
| 56 |
} |
| 57 |
|
| 58 |
public static function provideParseAccept() { |
| 59 |
return [ |
| 60 |
[ |
| 61 |
|
| 62 |
'test/123; q=0.5, test/456; q=0.8', |
| 63 |
[ |
| 64 |
[ |
| 65 |
'type' => 'test', |
| 66 |
'subtype' => '456', |
| 67 |
'q' => 0.8, |
| 68 |
'i' => 1, |
| 69 |
'params' => [] |
| 70 |
], |
| 71 |
[ |
| 72 |
'type' => 'test', |
| 73 |
'subtype' => '123', |
| 74 |
'q' => 0.5, |
| 75 |
'i' => 0, |
| 76 |
'params' => [] |
| 77 |
], |
| 78 |
] |
| 79 |
], |
| 80 |
[ |
| 81 |
|
| 82 |
'test/123; q=0.5, test/789; q=0.8, test/456; q=0.8', |
| 83 |
[ |
| 84 |
[ |
| 85 |
'type' => 'test', |
| 86 |
'subtype' => '789', |
| 87 |
'q' => 0.8, |
| 88 |
'i' => 1, |
| 89 |
'params' => [] |
| 90 |
], |
| 91 |
[ |
| 92 |
'type' => 'test', |
| 93 |
'subtype' => '456', |
| 94 |
'q' => 0.8, |
| 95 |
'i' => 2, |
| 96 |
'params' => [] |
| 97 |
], |
| 98 |
[ |
| 99 |
'type' => 'test', |
| 100 |
'subtype' => '123', |
| 101 |
'q' => 0.5, |
| 102 |
'i' => 0, |
| 103 |
'params' => [] |
| 104 |
] |
| 105 |
] |
| 106 |
], |
| 107 |
[ |
| 108 |
|
| 109 |
'hi-ho/12.3; q=0.5, hi/ho+456; q=0.8', |
| 110 |
[ |
| 111 |
[ |
| 112 |
'type' => 'hi', |
| 113 |
'subtype' => 'ho+456', |
| 114 |
'q' => 0.8, |
| 115 |
'i' => 1, |
| 116 |
'params' => [] |
| 117 |
], |
| 118 |
[ |
| 119 |
'type' => 'hi-ho', |
| 120 |
'subtype' => '12.3', |
| 121 |
'q' => 0.5, |
| 122 |
'i' => 0, |
| 123 |
'params' => [] |
| 124 |
] |
| 125 |
] |
| 126 |
], |
| 127 |
[ |
| 128 |
|
| 129 |
'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"', |
| 130 |
[ |
| 131 |
[ |
| 132 |
'type' => 'text', |
| 133 |
'subtype' => 'html', |
| 134 |
'q' => 1, |
| 135 |
'i' => 0, |
| 136 |
'params' => [ |
| 137 |
'profile' => 'https://www.mediawiki.org/wiki/Specs/HTML/0.0.0' |
| 138 |
] |
| 139 |
] |
| 140 |
] |
| 141 |
], |
| 142 |
[ |
| 143 |
|
| 144 |
'test/123; q=0.5, test/789; q', |
| 145 |
[ |
| 146 |
[ |
| 147 |
'type' => 'test', |
| 148 |
'subtype' => '789', |
| 149 |
'q' => 1, |
| 150 |
'i' => 1, |
| 151 |
'params' => [] |
| 152 |
], |
| 153 |
[ |
| 154 |
'type' => 'test', |
| 155 |
'subtype' => '123', |
| 156 |
'q' => 0.5, |
| 157 |
'i' => 0, |
| 158 |
'params' => [] |
| 159 |
], |
| 160 |
] |
| 161 |
], |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function testParseAccept( $header, $expected ) { |
| 169 |
$parser = new HttpAcceptParser(); |
| 170 |
$actual = $parser->parseAccept( $header ); |
| 171 |
$this->assertEquals( $expected, $actual ); |
| 172 |
} |
| 173 |
|
| 174 |
} |