3 use Wikimedia\Http\HttpAcceptNegotiator
;
6 * @covers Wikimedia\Http\HttpAcceptNegotiator
8 * @author Daniel Kinzler
10 class HttpAcceptNegotiatorTest
extends \PHPUnit_Framework_TestCase
{
12 public function provideGetFirstSupportedValue() {
21 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
22 [ 'text/xzy', 'text/bar' ], // accepted
24 'text/BAR', // expected
27 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
28 [ 'text/xzy', 'text/xoo' ], // accepted
33 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
34 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
36 'text/bar', // expected
39 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
40 [ 'text/xoo', '*' ], // accepted
42 'text/foo', // expected
45 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
46 [ 'text/xoo', '*/*' ], // accepted
48 'text/foo', // expected
50 [ // #6: text/* wildcard
51 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
52 [ 'application/*', 'text/foo' ], // accepted
54 'application/zuul', // expected
60 * @dataProvider provideGetFirstSupportedValue
62 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
63 $negotiator = new HttpAcceptNegotiator( $supported );
64 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
66 $this->assertEquals( $expected, $actual );
69 public function provideGetBestSupportedKey() {
78 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
79 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
81 'text/BAR', // expected
84 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
85 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
90 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
91 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
93 'text/BAR', // expected
96 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
97 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
102 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
103 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
105 'text/foo', // expected
107 [ // #6: */* wildcard
108 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
109 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
111 'text/foo', // expected
113 [ // #7: text/* wildcard
114 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
115 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
117 'application/zuul', // expected
119 [ // #8: Test specific format preferred over wildcard (T133314)
120 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
121 [ '*/*' => 1, 'text/html' => 1 ], // accepted
123 'text/html', // expected
125 [ // #9: Test specific format preferred over range (T133314)
126 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
127 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
129 'text/html', // expected
131 [ // #10: Test range preferred over wildcard (T133314)
132 [ 'application/rdf+xml', 'text/html' ], // supported
133 [ '*/*' => 1, 'text/*' => 1 ], // accepted
135 'text/html', // expected
141 * @dataProvider provideGetBestSupportedKey
143 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
144 $negotiator = new HttpAcceptNegotiator( $supported );
145 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
147 $this->assertEquals( $expected, $actual );