Debian Codegraph @

tests/phpunit/unit/includes/libs/http/HttpAcceptNegotiatorTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Http;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Http\HttpAcceptNegotiator;
7
8 /**
9 * @covers \Wikimedia\Http\HttpAcceptNegotiator
10 *
11 * @author Daniel Kinzler
12 */
13 class HttpAcceptNegotiatorTest extends TestCase {
14
15 public static function provideGetFirstSupportedValue() {
16 return [
17 [ // #0: empty
18 [], // supported
19 [], // accepted
20 null, // default
21 null, // expected
22 ],
23 [ // #1: simple
24 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
25 [ 'text/xzy', 'text/bar' ], // accepted
26 null, // default
27 'text/BAR', // expected
28 ],
29 [ // #2: default
30 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
31 [ 'text/xzy', 'text/xoo' ], // accepted
32 'X', // default
33 'X', // expected
34 ],
35 [ // #3: preference
36 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
37 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
38 null, // default
39 'text/bar', // expected
40 ],
41 [ // #4: * wildcard
42 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
43 [ 'text/xoo', '*' ], // accepted
44 null, // default
45 'text/foo', // expected
46 ],
47 [ // #5: */* wildcard
48 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
49 [ 'text/xoo', '*/*' ], // accepted
50 null, // default
51 'text/foo', // expected
52 ],
53 [ // #6: text/* wildcard
54 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
55 [ 'application/*', 'text/foo' ], // accepted
56 null, // default
57 'application/zuul', // expected
58 ],
59 ];
60 }
61
62 /**
63 * @dataProvider provideGetFirstSupportedValue
64 */
65 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
66 $negotiator = new HttpAcceptNegotiator( $supported );
67 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
68
69 $this->assertEquals( $expected, $actual );
70 }
71
72 public static function provideGetBestSupportedKey() {
73 return [
74 [ // #0: empty
75 [], // supported
76 [], // accepted
77 null, // default
78 null, // expected
79 ],
80 [ // #1: simple
81 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
82 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
83 null, // default
84 'text/BAR', // expected
85 ],
86 [ // #2: default
87 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
88 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
89 'X', // default
90 'X', // expected
91 ],
92 [ // #3: weighted
93 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
94 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
95 null, // default
96 'text/BAR', // expected
97 ],
98 [ // #4: zero weight
99 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
100 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
101 null, // default
102 null, // expected
103 ],
104 [ // #5: * wildcard
105 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
106 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
107 null, // default
108 'text/foo', // expected
109 ],
110 [ // #6: */* wildcard
111 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
112 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
113 null, // default
114 'text/foo', // expected
115 ],
116 [ // #7: text/* wildcard
117 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
118 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
119 null, // default
120 'application/zuul', // expected
121 ],
122 [ // #8: Test specific format preferred over wildcard (T133314)
123 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
124 [ '*/*' => 1, 'text/html' => 1 ], // accepted
125 null, // default
126 'text/html', // expected
127 ],
128 [ // #9: Test specific format preferred over range (T133314)
129 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
130 [ 'text/*' => 1, 'text/html' => 1 ], // accepted
131 null, // default
132 'text/html', // expected
133 ],
134 [ // #10: Test range preferred over wildcard (T133314)
135 [ 'application/rdf+xml', 'text/html' ], // supported
136 [ '*/*' => 1, 'text/*' => 1 ], // accepted
137 null, // default
138 'text/html', // expected
139 ],
140 ];
141 }
142
143 /**
144 * @dataProvider provideGetBestSupportedKey
145 */
146 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
147 $negotiator = new HttpAcceptNegotiator( $supported );
148 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
149
150 $this->assertEquals( $expected, $actual );
151 }
152
153 }