3 use Wikimedia\Http\HttpAcceptNegotiator
;
6 * @covers Wikimedia\Http\HttpAcceptNegotiator
9 * @author Daniel Kinzler
11 class HttpAcceptNegotiatorTest
extends \PHPUnit_Framework_TestCase
{
13 public function provideGetFirstSupportedValue() {
22 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
23 [ 'text/xzy', 'text/bar' ], // accepted
25 'text/BAR', // expected
28 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
29 [ 'text/xzy', 'text/xoo' ], // accepted
34 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
35 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
37 'text/bar', // expected
40 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
41 [ 'text/xoo', '*' ], // accepted
43 'text/foo', // expected
46 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
47 [ 'text/xoo', '*/*' ], // accepted
49 'text/foo', // expected
51 [ // #6: text/* wildcard
52 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
53 [ 'application/*', 'text/foo' ], // accepted
55 'application/zuul', // expected
61 * @dataProvider provideGetFirstSupportedValue
63 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
64 $negotiator = new HttpAcceptNegotiator( $supported );
65 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
67 $this->assertEquals( $expected, $actual );
70 public function provideGetBestSupportedKey() {
79 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
80 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
82 'text/BAR', // expected
85 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
86 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
91 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
92 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
94 'text/BAR', // expected
97 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
98 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
103 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
104 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
106 'text/foo', // expected
108 [ // #6: */* wildcard
109 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
110 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
112 'text/foo', // expected
114 [ // #7: text/* wildcard
115 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
116 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
118 'application/zuul', // expected
120 [ // #8: Test specific format preferred over wildcard (T133314)
121 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
122 [ '*/*' => 1, 'text/html' => 1 ], // accepted
124 'text/html', // expected
126 [ // #9: Test specific format preferred over range (T133314)
127 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
128 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
130 'text/html', // expected
132 [ // #10: Test range preferred over wildcard (T133314)
133 [ 'application/rdf+xml', 'text/html' ], // supported
134 [ '*/*' => 1, 'text/*' => 1 ], // accepted
136 'text/html', // expected
142 * @dataProvider provideGetBestSupportedKey
144 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
145 $negotiator = new HttpAcceptNegotiator( $supported );
146 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
148 $this->assertEquals( $expected, $actual );