MediaWiki REL1_33
HttpAcceptNegotiatorTest.php
Go to the documentation of this file.
1<?php
2
4
10class HttpAcceptNegotiatorTest extends \PHPUnit\Framework\TestCase {
11
13 return [
14 [ // #0: empty
15 [], // supported
16 [], // accepted
17 null, // default
18 null, // expected
19 ],
20 [ // #1: simple
21 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
22 [ 'text/xzy', 'text/bar' ], // accepted
23 null, // default
24 'text/BAR', // expected
25 ],
26 [ // #2: default
27 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
28 [ 'text/xzy', 'text/xoo' ], // accepted
29 'X', // default
30 'X', // expected
31 ],
32 [ // #3: preference
33 [ 'text/foo', 'text/bar', 'application/zuul' ], // supported
34 [ 'text/xoo', 'text/BAR', 'text/foo' ], // accepted
35 null, // default
36 'text/bar', // expected
37 ],
38 [ // #4: * wildcard
39 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
40 [ 'text/xoo', '*' ], // accepted
41 null, // default
42 'text/foo', // expected
43 ],
44 [ // #5: */* wildcard
45 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
46 [ 'text/xoo', '*/*' ], // accepted
47 null, // default
48 'text/foo', // expected
49 ],
50 [ // #6: text/* wildcard
51 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
52 [ 'application/*', 'text/foo' ], // accepted
53 null, // default
54 'application/zuul', // expected
55 ],
56 ];
57 }
58
62 public function testGetFirstSupportedValue( $supported, $accepted, $default, $expected ) {
63 $negotiator = new HttpAcceptNegotiator( $supported );
64 $actual = $negotiator->getFirstSupportedValue( $accepted, $default );
65
66 $this->assertEquals( $expected, $actual );
67 }
68
69 public function provideGetBestSupportedKey() {
70 return [
71 [ // #0: empty
72 [], // supported
73 [], // accepted
74 null, // default
75 null, // expected
76 ],
77 [ // #1: simple
78 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
79 [ 'text/xzy' => 1, 'text/bar' => 0.5 ], // accepted
80 null, // default
81 'text/BAR', // expected
82 ],
83 [ // #2: default
84 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
85 [ 'text/xzy' => 1, 'text/xoo' => 0.5 ], // accepted
86 'X', // default
87 'X', // expected
88 ],
89 [ // #3: weighted
90 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
91 [ 'text/foo' => 0.3, 'text/BAR' => 0.8, 'application/zuul' => 0.5 ], // accepted
92 null, // default
93 'text/BAR', // expected
94 ],
95 [ // #4: zero weight
96 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
97 [ 'text/foo' => 0, 'text/xoo' => 1 ], // accepted
98 null, // default
99 null, // expected
100 ],
101 [ // #5: * wildcard
102 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
103 [ 'text/xoo' => 0.5, '*' => 0.1 ], // accepted
104 null, // default
105 'text/foo', // expected
106 ],
107 [ // #6: */* wildcard
108 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
109 [ 'text/xoo' => 0.5, '*/*' => 0.1 ], // accepted
110 null, // default
111 'text/foo', // expected
112 ],
113 [ // #7: text/* wildcard
114 [ 'text/foo', 'text/BAR', 'application/zuul' ], // supported
115 [ 'text/foo' => 0.3, 'application/*' => 0.8 ], // accepted
116 null, // default
117 'application/zuul', // expected
118 ],
119 [ // #8: Test specific format preferred over wildcard (T133314)
120 [ 'application/rdf+xml', 'text/json', 'text/html' ], // supported
121 [ '*/*' => 1, 'text/html' => 1 ], // accepted
122 null, // default
123 'text/html', // expected
124 ],
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
128 null, // default
129 'text/html', // expected
130 ],
131 [ // #10: Test range preferred over wildcard (T133314)
132 [ 'application/rdf+xml', 'text/html' ], // supported
133 [ '*/*' => 1, 'text/*' => 1 ], // accepted
134 null, // default
135 'text/html', // expected
136 ],
137 ];
138 }
139
143 public function testGetBestSupportedKey( $supported, $accepted, $default, $expected ) {
144 $negotiator = new HttpAcceptNegotiator( $supported );
145 $actual = $negotiator->getBestSupportedKey( $accepted, $default );
146
147 $this->assertEquals( $expected, $actual );
148 }
149
150}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Wikimedia\Http\HttpAcceptNegotiator.
testGetBestSupportedKey( $supported, $accepted, $default, $expected)
provideGetBestSupportedKey
testGetFirstSupportedValue( $supported, $accepted, $default, $expected)
provideGetFirstSupportedValue
Utility for negotiating a value from a set of supported values using a preference list.