Debian Codegraph @

tests/phpunit/unit/includes/Rest/ConditionalHeaderUtilTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use MediaWiki\Rest\ConditionalHeaderUtil;
6 use MediaWiki\Rest\RequestData;
7 use MediaWiki\Rest\Response;
8 use MediaWikiUnitTestCase;
9
10 /**
11 * @covers \MediaWiki\Rest\ConditionalHeaderUtil
12 */
13 class ConditionalHeaderUtilTest extends MediaWikiUnitTestCase {
14 public static function provider() {
15 return [
16 'nothing' => [
17 'GET',
18 null,
19 null,
20 null,
21 [],
22 null,
23 []
24 ],
25 'inm true' => [
26 'GET',
27 '"a"',
28 null,
29 null,
30 [ 'If-None-Match' => '"b"' ],
31 null,
32 [ 'ETag' => [ '"a"' ] ]
33 ],
34 'inm false' => [
35 'GET',
36 '"a"',
37 null,
38 null,
39 [ 'If-None-Match' => '"a"' ],
40 304,
41 [ 'ETag' => [ '"a"' ] ]
42 ],
43 'ims true' => [
44 'GET',
45 null,
46 'Mon, 14 Oct 2019 00:00:01 GMT',
47 null,
48 [ 'If-Modified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
49 null,
50 [ 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:01 GMT' ] ]
51 ],
52 'ims false' => [
53 'GET',
54 null,
55 'Mon, 14 Oct 2019 00:00:00 GMT',
56 null,
57 [ 'If-Modified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
58 304,
59 [ 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:00 GMT' ] ]
60 ],
61 'im true' => [
62 'GET',
63 '"a"',
64 null,
65 null,
66 [ 'If-Match' => '"a"' ],
67 null,
68 [ 'ETag' => [ '"a"' ] ]
69 ],
70 'im false' => [
71 'GET',
72 '"a"',
73 null,
74 null,
75 [ 'If-Match' => '"b"' ],
76 412,
77 [ 'ETag' => [ '"a"' ] ]
78 ],
79 'ius true' => [
80 'GET',
81 null,
82 'Mon, 14 Oct 2019 00:00:00 GMT',
83 null,
84 [ 'If-Unmodified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
85 null,
86 [ 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:00 GMT' ] ]
87 ],
88 'ius false' => [
89 'GET',
90 null,
91 'Mon, 14 Oct 2019 00:00:01 GMT',
92 null,
93 [ 'If-Unmodified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
94 412,
95 [ 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:01 GMT' ] ]
96 ],
97 'im true, ius false' => [
98 'GET',
99 '"a"',
100 'Mon, 14 Oct 2019 00:00:01 GMT',
101 null,
102 [
103 'If-Match' => '"a"',
104 'If-Unmodified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT'
105 ],
106 null,
107 [
108 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:01 GMT' ],
109 'ETag' => [ '"a"' ]
110 ]
111 ],
112 'inm true, ims false' => [
113 'GET',
114 '"a"',
115 'Mon, 14 Oct 2019 00:00:00 GMT',
116 null,
117 [
118 'If-None-Match' => '"b"',
119 'If-Modified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT'
120 ],
121 null,
122 [
123 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:00 GMT' ],
124 'ETag' => [ '"a"' ]
125 ]
126 ],
127 'im true, inm false' => [
128 'GET',
129 '"a"',
130 null,
131 null,
132 [
133 'If-Match' => '"a"',
134 'If-None-Match' => '"a"'
135 ],
136 304,
137 [ 'ETag' => [ '"a"' ] ]
138 ],
139 'ius true, inm false' => [
140 'GET',
141 '"a"',
142 'Mon, 14 Oct 2019 00:00:00 GMT',
143 null,
144 [
145 'If-Unmodified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT',
146 'If-None-Match' => '"a"'
147 ],
148 304,
149 [
150 'Last-Modified' => [ 'Mon, 14 Oct 2019 00:00:00 GMT' ],
151 'ETag' => [ '"a"' ]
152 ]
153 ],
154 'im star true' => [
155 'GET',
156 '"a"',
157 null,
158 true,
159 [ 'If-Match' => '*' ],
160 null,
161 [ 'ETag' => [ '"a"' ] ]
162 ],
163 'im star false' => [
164 'GET',
165 null,
166 null,
167 false,
168 [ 'If-Match' => '*' ],
169 412,
170 []
171 ],
172 'inm false post' => [
173 'POST',
174 '"a"',
175 null,
176 null,
177 [ 'If-None-Match' => '"a"' ],
178 412,
179 [ 'ETag' => [ '"a"' ] ]
180 ],
181 'im multiple true' => [
182 'GET',
183 '"a"',
184 null,
185 null,
186 [ 'If-Match' => '"b", "a"' ],
187 null,
188 [ 'ETag' => [ '"a"' ] ]
189 ],
190 'im multiple false' => [
191 'GET',
192 '"a"',
193 null,
194 null,
195 [ 'If-Match' => '"b", "c"' ],
196 412,
197 [ 'ETag' => [ '"a"' ] ]
198 ],
199 // A strong ETag returned by the server may have been "weakened" by
200 // a proxy or middleware, e.g. when applying compression and setting
201 // content-encoding (rather than transfer-encoding). We want to still accept
202 // such ETags, even though that's against the HTTP spec (T238849).
203 // See ConditionalHeaderUtil::setVarnishETagHack.
204 'If-Match weak vs strong' => [
205 'GET',
206 '"a"',
207 null,
208 null,
209 [ 'If-Match' => 'W/"a"' ],
210 null,
211 [ 'ETag' => [ '"a"' ] ]
212 ],
213 'If-Match strong vs weak' => [
214 'GET',
215 'W/"a"',
216 null,
217 null,
218 [ 'If-Match' => '"a"' ],
219 412,
220 [ 'ETag' => [ 'W/"a"' ] ]
221 ],
222 'If-Match weak vs weak' => [
223 'GET',
224 'W/"a"',
225 null,
226 null,
227 [ 'If-Match' => 'W/"a"' ],
228 412,
229 [ 'ETag' => [ 'W/"a"' ] ]
230 ],
231 'ims with resource unknown' => [
232 'GET',
233 null,
234 null,
235 null,
236 [ 'If-Modified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
237 null,
238 []
239 ],
240 'ius with resource unknown' => [
241 'GET',
242 null,
243 null,
244 null,
245 [ 'If-Unmodified-Since' => 'Mon, 14 Oct 2019 00:00:00 GMT' ],
246 412,
247 []
248 ],
249 'If-None-Match wildcard GET request, resource has representation' => [
250 'GET',
251 null,
252 null,
253 true,
254 [ 'If-None-Match' => '*' ],
255 304,
256 []
257 ],
258 'If-None-Match wildcard non-GET request, resource has representation' => [
259 'POST',
260 null,
261 null,
262 true,
263 [ 'If-None-Match' => '*' ],
264 412,
265 []
266 ],
267 ];
268 }
269
270 /** @dataProvider provider */
271 public function testConditionalHeaderUtil( $method, $eTag, $lastModified, $hasRepresentation,
272 $requestHeaders, $expectedStatus, $expectedResponseHeaders
273 ) {
274 $util = new ConditionalHeaderUtil;
275 $util->setValidators( $eTag, $lastModified, $hasRepresentation );
276 $request = new RequestData( [
277 'method' => $method,
278 'headers' => $requestHeaders
279 ] );
280 $status = $util->checkPreconditions( $request );
281 $this->assertSame( $expectedStatus, $status );
282
283 $response = new Response;
284 $util->applyResponseHeaders( $response );
285 $this->assertSame( $expectedResponseHeaders, $response->getHeaders() );
286 }
287 }