PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.3.1
GiveWP – Donation Plugin and Fundraising Platform v3.3.1
4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / vendor-prefixed / symfony / http-foundation / Tests / ResponseTest.php
give / vendor / vendor-prefixed / symfony / http-foundation / Tests Last commit date
File 2 years ago Fixtures 2 years ago Session 2 years ago schema 2 years ago AcceptHeaderItemTest.php 2 years ago AcceptHeaderTest.php 2 years ago ApacheRequestTest.php 2 years ago BinaryFileResponseTest.php 2 years ago CookieTest.php 2 years ago ExpressionRequestMatcherTest.php 2 years ago FileBagTest.php 2 years ago HeaderBagTest.php 2 years ago IpUtilsTest.php 2 years ago JsonResponseTest.php 2 years ago ParameterBagTest.php 2 years ago RedirectResponseTest.php 2 years ago RequestMatcherTest.php 2 years ago RequestStackTest.php 2 years ago RequestTest.php 2 years ago ResponseFunctionalTest.php 2 years ago ResponseHeaderBagTest.php 2 years ago ResponseTest.php 2 years ago ResponseTestCase.php 2 years ago ServerBagTest.php 2 years ago StreamedResponseTest.php 2 years ago
ResponseTest.php
1016 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * Modified by impress-org on 23-January-2024 using Strauss.
12 * @see https://github.com/BrianHenryIE/strauss
13 */
14
15 namespace Give\Vendors\Symfony\Component\HttpFoundation\Tests;
16
17 use Give\Vendors\Symfony\Component\HttpFoundation\Request;
18 use Give\Vendors\Symfony\Component\HttpFoundation\Response;
19
20 /**
21 * @group time-sensitive
22 */
23 class ResponseTest extends ResponseTestCase
24 {
25 public function testCreate()
26 {
27 $response = Response::create('foo', 301, ['Foo' => 'bar']);
28
29 $this->assertInstanceOf('Give\Vendors\Symfony\Component\HttpFoundation\Response', $response);
30 $this->assertEquals(301, $response->getStatusCode());
31 $this->assertEquals('bar', $response->headers->get('foo'));
32 }
33
34 public function testToString()
35 {
36 $response = new Response();
37 $response = explode("\r\n", $response);
38 $this->assertEquals('HTTP/1.0 200 OK', $response[0]);
39 $this->assertEquals('Cache-Control: no-cache, private', $response[1]);
40 }
41
42 public function testClone()
43 {
44 $response = new Response();
45 $responseClone = clone $response;
46 $this->assertEquals($response, $responseClone);
47 }
48
49 public function testSendHeaders()
50 {
51 $response = new Response();
52 $headers = $response->sendHeaders();
53 $this->assertObjectHasAttribute('headers', $headers);
54 $this->assertObjectHasAttribute('content', $headers);
55 $this->assertObjectHasAttribute('version', $headers);
56 $this->assertObjectHasAttribute('statusCode', $headers);
57 $this->assertObjectHasAttribute('statusText', $headers);
58 $this->assertObjectHasAttribute('charset', $headers);
59 }
60
61 public function testSend()
62 {
63 $response = new Response();
64 $responseSend = $response->send();
65 $this->assertObjectHasAttribute('headers', $responseSend);
66 $this->assertObjectHasAttribute('content', $responseSend);
67 $this->assertObjectHasAttribute('version', $responseSend);
68 $this->assertObjectHasAttribute('statusCode', $responseSend);
69 $this->assertObjectHasAttribute('statusText', $responseSend);
70 $this->assertObjectHasAttribute('charset', $responseSend);
71 }
72
73 public function testGetCharset()
74 {
75 $response = new Response();
76 $charsetOrigin = 'UTF-8';
77 $response->setCharset($charsetOrigin);
78 $charset = $response->getCharset();
79 $this->assertEquals($charsetOrigin, $charset);
80 }
81
82 public function testIsCacheable()
83 {
84 $response = new Response();
85 $this->assertFalse($response->isCacheable());
86 }
87
88 public function testIsCacheableWithErrorCode()
89 {
90 $response = new Response('', 500);
91 $this->assertFalse($response->isCacheable());
92 }
93
94 public function testIsCacheableWithNoStoreDirective()
95 {
96 $response = new Response();
97 $response->headers->set('cache-control', 'private');
98 $this->assertFalse($response->isCacheable());
99 }
100
101 public function testIsCacheableWithSetTtl()
102 {
103 $response = new Response();
104 $response->setTtl(10);
105 $this->assertTrue($response->isCacheable());
106 }
107
108 public function testMustRevalidate()
109 {
110 $response = new Response();
111 $this->assertFalse($response->mustRevalidate());
112 }
113
114 public function testMustRevalidateWithMustRevalidateCacheControlHeader()
115 {
116 $response = new Response();
117 $response->headers->set('cache-control', 'must-revalidate');
118
119 $this->assertTrue($response->mustRevalidate());
120 }
121
122 public function testMustRevalidateWithProxyRevalidateCacheControlHeader()
123 {
124 $response = new Response();
125 $response->headers->set('cache-control', 'proxy-revalidate');
126
127 $this->assertTrue($response->mustRevalidate());
128 }
129
130 public function testSetNotModified()
131 {
132 $response = new Response('foo');
133 $modified = $response->setNotModified();
134 $this->assertObjectHasAttribute('headers', $modified);
135 $this->assertObjectHasAttribute('content', $modified);
136 $this->assertObjectHasAttribute('version', $modified);
137 $this->assertObjectHasAttribute('statusCode', $modified);
138 $this->assertObjectHasAttribute('statusText', $modified);
139 $this->assertObjectHasAttribute('charset', $modified);
140 $this->assertEquals(304, $modified->getStatusCode());
141
142 ob_start();
143 $modified->sendContent();
144 $string = ob_get_clean();
145 $this->assertEmpty($string);
146 }
147
148 public function testIsSuccessful()
149 {
150 $response = new Response();
151 $this->assertTrue($response->isSuccessful());
152 }
153
154 public function testIsNotModified()
155 {
156 $response = new Response();
157 $modified = $response->isNotModified(new Request());
158 $this->assertFalse($modified);
159 }
160
161 public function testIsNotModifiedNotSafe()
162 {
163 $request = Request::create('/homepage', 'POST');
164
165 $response = new Response();
166 $this->assertFalse($response->isNotModified($request));
167 }
168
169 public function testIsNotModifiedLastModified()
170 {
171 $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
172 $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
173 $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
174
175 $request = new Request();
176 $request->headers->set('If-Modified-Since', $modified);
177
178 $response = new Response();
179
180 $response->headers->set('Last-Modified', $modified);
181 $this->assertTrue($response->isNotModified($request));
182
183 $response->headers->set('Last-Modified', $before);
184 $this->assertTrue($response->isNotModified($request));
185
186 $response->headers->set('Last-Modified', $after);
187 $this->assertFalse($response->isNotModified($request));
188
189 $response->headers->set('Last-Modified', '');
190 $this->assertFalse($response->isNotModified($request));
191 }
192
193 public function testIsNotModifiedEtag()
194 {
195 $etagOne = 'randomly_generated_etag';
196 $etagTwo = 'randomly_generated_etag_2';
197
198 $request = new Request();
199 $request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
200
201 $response = new Response();
202
203 $response->headers->set('ETag', $etagOne);
204 $this->assertTrue($response->isNotModified($request));
205
206 $response->headers->set('ETag', $etagTwo);
207 $this->assertTrue($response->isNotModified($request));
208
209 $response->headers->set('ETag', '');
210 $this->assertFalse($response->isNotModified($request));
211 }
212
213 public function testIsNotModifiedLastModifiedAndEtag()
214 {
215 $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
216 $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
217 $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
218 $etag = 'randomly_generated_etag';
219
220 $request = new Request();
221 $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
222 $request->headers->set('If-Modified-Since', $modified);
223
224 $response = new Response();
225
226 $response->headers->set('ETag', $etag);
227 $response->headers->set('Last-Modified', $after);
228 $this->assertFalse($response->isNotModified($request));
229
230 $response->headers->set('ETag', 'non-existent-etag');
231 $response->headers->set('Last-Modified', $before);
232 $this->assertFalse($response->isNotModified($request));
233
234 $response->headers->set('ETag', $etag);
235 $response->headers->set('Last-Modified', $modified);
236 $this->assertTrue($response->isNotModified($request));
237 }
238
239 public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
240 {
241 $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
242 $etag = 'randomly_generated_etag';
243
244 $request = new Request();
245 $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
246 $request->headers->set('If-Modified-Since', $modified);
247
248 $response = new Response();
249
250 $response->headers->set('ETag', $etag);
251 $this->assertTrue($response->isNotModified($request));
252
253 $response->headers->set('ETag', 'non-existent-etag');
254 $this->assertFalse($response->isNotModified($request));
255 }
256
257 public function testIsValidateable()
258 {
259 $response = new Response('', 200, ['Last-Modified' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);
260 $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
261
262 $response = new Response('', 200, ['ETag' => '"12345"']);
263 $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
264
265 $response = new Response();
266 $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
267 }
268
269 public function testGetDate()
270 {
271 $oneHourAgo = $this->createDateTimeOneHourAgo();
272 $response = new Response('', 200, ['Date' => $oneHourAgo->format(\DATE_RFC2822)]);
273 $date = $response->getDate();
274 $this->assertEquals($oneHourAgo->getTimestamp(), $date->getTimestamp(), '->getDate() returns the Date header if present');
275
276 $response = new Response();
277 $date = $response->getDate();
278 $this->assertEquals(time(), $date->getTimestamp(), '->getDate() returns the current Date if no Date header present');
279
280 $response = new Response('', 200, ['Date' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);
281 $now = $this->createDateTimeNow();
282 $response->headers->set('Date', $now->format(\DATE_RFC2822));
283 $date = $response->getDate();
284 $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
285
286 $response = new Response('', 200);
287 $now = $this->createDateTimeNow();
288 $response->headers->remove('Date');
289 $date = $response->getDate();
290 $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed');
291 }
292
293 public function testGetMaxAge()
294 {
295 $response = new Response();
296 $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
297 $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
298
299 $response = new Response();
300 $response->headers->set('Cache-Control', 'max-age=600');
301 $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
302
303 $response = new Response();
304 $response->headers->set('Cache-Control', 'must-revalidate');
305 $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(\DATE_RFC2822));
306 $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
307
308 $response = new Response();
309 $response->headers->set('Cache-Control', 'must-revalidate');
310 $response->headers->set('Expires', -1);
311 $this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(\DATE_RFC822));
312
313 $response = new Response();
314 $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
315 }
316
317 public function testSetSharedMaxAge()
318 {
319 $response = new Response();
320 $response->setSharedMaxAge(20);
321
322 $cacheControl = $response->headers->get('Cache-Control');
323 $this->assertEquals('public, s-maxage=20', $cacheControl);
324 }
325
326 public function testIsPrivate()
327 {
328 $response = new Response();
329 $response->headers->set('Cache-Control', 'max-age=100');
330 $response->setPrivate();
331 $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
332 $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
333
334 $response = new Response();
335 $response->headers->set('Cache-Control', 'public, max-age=100');
336 $response->setPrivate();
337 $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
338 $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
339 $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
340 }
341
342 public function testExpire()
343 {
344 $response = new Response();
345 $response->headers->set('Cache-Control', 'max-age=100');
346 $response->expire();
347 $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
348
349 $response = new Response();
350 $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
351 $response->expire();
352 $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
353
354 $response = new Response();
355 $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
356 $response->headers->set('Age', '1000');
357 $response->expire();
358 $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
359
360 $response = new Response();
361 $response->expire();
362 $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
363
364 $response = new Response();
365 $response->headers->set('Expires', -1);
366 $response->expire();
367 $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
368
369 $response = new Response();
370 $response->headers->set('Expires', date(\DATE_RFC2822, time() + 600));
371 $response->expire();
372 $this->assertNull($response->headers->get('Expires'), '->expire() removes the Expires header when the response is fresh');
373 }
374
375 public function testNullExpireHeader()
376 {
377 $response = new Response(null, 200, ['Expires' => null]);
378 $this->assertNull($response->getExpires());
379 }
380
381 public function testGetTtl()
382 {
383 $response = new Response();
384 $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
385
386 $response = new Response();
387 $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(\DATE_RFC2822));
388 $this->assertEquals(3600, $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
389
390 $response = new Response();
391 $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822));
392 $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
393
394 $response = new Response();
395 $response->headers->set('Expires', $response->getDate()->format(\DATE_RFC2822));
396 $response->headers->set('Age', 0);
397 $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
398
399 $response = new Response();
400 $response->headers->set('Cache-Control', 'max-age=60');
401 $this->assertEquals(60, $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
402 }
403
404 public function testSetClientTtl()
405 {
406 $response = new Response();
407 $response->setClientTtl(10);
408
409 $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
410 }
411
412 public function testGetSetProtocolVersion()
413 {
414 $response = new Response();
415
416 $this->assertEquals('1.0', $response->getProtocolVersion());
417
418 $response->setProtocolVersion('1.1');
419
420 $this->assertEquals('1.1', $response->getProtocolVersion());
421 }
422
423 public function testGetVary()
424 {
425 $response = new Response();
426 $this->assertEquals([], $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
427
428 $response = new Response();
429 $response->headers->set('Vary', 'Accept-Language');
430 $this->assertEquals(['Accept-Language'], $response->getVary(), '->getVary() parses a single header name value');
431
432 $response = new Response();
433 $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
434 $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
435
436 $response = new Response();
437 $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
438 $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->getVary() parses multiple header name values separated by commas');
439
440 $vary = ['Accept-Language', 'User-Agent', 'X-foo'];
441
442 $response = new Response();
443 $response->headers->set('Vary', $vary);
444 $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
445
446 $response = new Response();
447 $response->headers->set('Vary', 'Accept-Language, User-Agent, X-foo');
448 $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
449 }
450
451 public function testSetVary()
452 {
453 $response = new Response();
454 $response->setVary('Accept-Language');
455 $this->assertEquals(['Accept-Language'], $response->getVary());
456
457 $response->setVary('Accept-Language, User-Agent');
458 $this->assertEquals(['Accept-Language', 'User-Agent'], $response->getVary(), '->setVary() replace the vary header by default');
459
460 $response->setVary('X-Foo', false);
461 $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->setVary() doesn\'t wipe out earlier Vary headers if replace is set to false');
462 }
463
464 public function testDefaultContentType()
465 {
466 $headerMock = $this->getMockBuilder('Give\Vendors\Symfony\Component\HttpFoundation\ResponseHeaderBag')->setMethods(['set'])->getMock();
467 $headerMock->expects($this->exactly(2))
468 ->method('set')
469 ->withConsecutive(
470 ['Content-Type', 'text/html'],
471 ['Content-Type', 'text/html; charset=UTF-8']
472 );
473
474 $response = new Response('foo');
475 $response->headers = $headerMock;
476
477 $response->prepare(new Request());
478 }
479
480 public function testContentTypeCharset()
481 {
482 $response = new Response();
483 $response->headers->set('Content-Type', 'text/css');
484
485 // force fixContentType() to be called
486 $response->prepare(new Request());
487
488 $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
489 }
490
491 public function testPrepareDoesNothingIfContentTypeIsSet()
492 {
493 $response = new Response('foo');
494 $response->headers->set('Content-Type', 'text/plain');
495
496 $response->prepare(new Request());
497
498 $this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
499 }
500
501 public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
502 {
503 $response = new Response('foo');
504
505 $response->prepare(new Request());
506
507 $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
508 }
509
510 public function testPrepareSetContentType()
511 {
512 $response = new Response('foo');
513 $request = Request::create('/');
514 $request->setRequestFormat('json');
515
516 $response->prepare($request);
517
518 $this->assertEquals('application/json', $response->headers->get('content-type'));
519 }
520
521 public function testPrepareRemovesContentForHeadRequests()
522 {
523 $response = new Response('foo');
524 $request = Request::create('/', 'HEAD');
525
526 $length = 12345;
527 $response->headers->set('Content-Length', $length);
528 $response->prepare($request);
529
530 $this->assertEquals('', $response->getContent());
531 $this->assertEquals($length, $response->headers->get('Content-Length'), 'Content-Length should be as if it was GET; see RFC2616 14.13');
532 }
533
534 public function testPrepareRemovesContentForInformationalResponse()
535 {
536 $response = new Response('foo');
537 $request = Request::create('/');
538
539 $response->setContent('content');
540 $response->setStatusCode(101);
541 $response->prepare($request);
542 $this->assertEquals('', $response->getContent());
543 $this->assertFalse($response->headers->has('Content-Type'));
544
545 $response->setContent('content');
546 $response->setStatusCode(304);
547 $response->prepare($request);
548 $this->assertEquals('', $response->getContent());
549 $this->assertFalse($response->headers->has('Content-Type'));
550 $this->assertFalse($response->headers->has('Content-Length'));
551 }
552
553 public function testPrepareRemovesContentLength()
554 {
555 $response = new Response('foo');
556 $request = Request::create('/');
557
558 $response->headers->set('Content-Length', 12345);
559 $response->prepare($request);
560 $this->assertEquals(12345, $response->headers->get('Content-Length'));
561
562 $response->headers->set('Transfer-Encoding', 'chunked');
563 $response->prepare($request);
564 $this->assertFalse($response->headers->has('Content-Length'));
565 }
566
567 public function testPrepareSetsPragmaOnHttp10Only()
568 {
569 $request = Request::create('/', 'GET');
570 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
571
572 $response = new Response('foo');
573 $response->prepare($request);
574 $this->assertEquals('no-cache', $response->headers->get('pragma'));
575 $this->assertEquals('-1', $response->headers->get('expires'));
576
577 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
578 $response = new Response('foo');
579 $response->prepare($request);
580 $this->assertFalse($response->headers->has('pragma'));
581 $this->assertFalse($response->headers->has('expires'));
582 }
583
584 public function testSetCache()
585 {
586 $response = new Response();
587 // ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public']
588 try {
589 $response->setCache(['wrong option' => 'value']);
590 $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
591 } catch (\Exception $e) {
592 $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
593 $this->assertStringContainsString('"wrong option"', $e->getMessage());
594 }
595
596 $options = ['etag' => '"whatever"'];
597 $response->setCache($options);
598 $this->assertEquals('"whatever"', $response->getEtag());
599
600 $now = $this->createDateTimeNow();
601 $options = ['last_modified' => $now];
602 $response->setCache($options);
603 $this->assertEquals($now->getTimestamp(), $response->getLastModified()->getTimestamp());
604
605 $options = ['max_age' => 100];
606 $response->setCache($options);
607 $this->assertEquals(100, $response->getMaxAge());
608
609 $options = ['s_maxage' => 200];
610 $response->setCache($options);
611 $this->assertEquals(200, $response->getMaxAge());
612
613 $this->assertTrue($response->headers->hasCacheControlDirective('public'));
614 $this->assertFalse($response->headers->hasCacheControlDirective('private'));
615
616 $response->setCache(['public' => true]);
617 $this->assertTrue($response->headers->hasCacheControlDirective('public'));
618 $this->assertFalse($response->headers->hasCacheControlDirective('private'));
619
620 $response->setCache(['public' => false]);
621 $this->assertFalse($response->headers->hasCacheControlDirective('public'));
622 $this->assertTrue($response->headers->hasCacheControlDirective('private'));
623
624 $response->setCache(['private' => true]);
625 $this->assertFalse($response->headers->hasCacheControlDirective('public'));
626 $this->assertTrue($response->headers->hasCacheControlDirective('private'));
627
628 $response->setCache(['private' => false]);
629 $this->assertTrue($response->headers->hasCacheControlDirective('public'));
630 $this->assertFalse($response->headers->hasCacheControlDirective('private'));
631
632 $response->setCache(['immutable' => true]);
633 $this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
634
635 $response->setCache(['immutable' => false]);
636 $this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
637 }
638
639 public function testSendContent()
640 {
641 $response = new Response('test response rendering', 200);
642
643 ob_start();
644 $response->sendContent();
645 $string = ob_get_clean();
646 $this->assertStringContainsString('test response rendering', $string);
647 }
648
649 public function testSetPublic()
650 {
651 $response = new Response();
652 $response->setPublic();
653
654 $this->assertTrue($response->headers->hasCacheControlDirective('public'));
655 $this->assertFalse($response->headers->hasCacheControlDirective('private'));
656 }
657
658 public function testSetImmutable()
659 {
660 $response = new Response();
661 $response->setImmutable();
662
663 $this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
664 }
665
666 public function testIsImmutable()
667 {
668 $response = new Response();
669 $response->setImmutable();
670
671 $this->assertTrue($response->isImmutable());
672 }
673
674 public function testSetExpires()
675 {
676 $response = new Response();
677 $response->setExpires(null);
678
679 $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
680
681 $now = $this->createDateTimeNow();
682 $response->setExpires($now);
683
684 $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
685 }
686
687 public function testSetLastModified()
688 {
689 $response = new Response();
690 $response->setLastModified($this->createDateTimeNow());
691 $this->assertNotNull($response->getLastModified());
692
693 $response->setLastModified(null);
694 $this->assertNull($response->getLastModified());
695 }
696
697 public function testIsInvalid()
698 {
699 $response = new Response();
700
701 try {
702 $response->setStatusCode(99);
703 $this->fail();
704 } catch (\InvalidArgumentException $e) {
705 $this->assertTrue($response->isInvalid());
706 }
707
708 try {
709 $response->setStatusCode(650);
710 $this->fail();
711 } catch (\InvalidArgumentException $e) {
712 $this->assertTrue($response->isInvalid());
713 }
714
715 $response = new Response('', 200);
716 $this->assertFalse($response->isInvalid());
717 }
718
719 /**
720 * @dataProvider getStatusCodeFixtures
721 */
722 public function testSetStatusCode($code, $text, $expectedText)
723 {
724 $response = new Response();
725
726 $response->setStatusCode($code, $text);
727
728 $statusText = new \ReflectionProperty($response, 'statusText');
729 $statusText->setAccessible(true);
730
731 $this->assertEquals($expectedText, $statusText->getValue($response));
732 }
733
734 public function getStatusCodeFixtures()
735 {
736 return [
737 ['200', null, 'OK'],
738 ['200', false, ''],
739 ['200', 'foo', 'foo'],
740 ['199', null, 'unknown status'],
741 ['199', false, ''],
742 ['199', 'foo', 'foo'],
743 ];
744 }
745
746 public function testIsInformational()
747 {
748 $response = new Response('', 100);
749 $this->assertTrue($response->isInformational());
750
751 $response = new Response('', 200);
752 $this->assertFalse($response->isInformational());
753 }
754
755 public function testIsRedirectRedirection()
756 {
757 foreach ([301, 302, 303, 307] as $code) {
758 $response = new Response('', $code);
759 $this->assertTrue($response->isRedirection());
760 $this->assertTrue($response->isRedirect());
761 }
762
763 $response = new Response('', 304);
764 $this->assertTrue($response->isRedirection());
765 $this->assertFalse($response->isRedirect());
766
767 $response = new Response('', 200);
768 $this->assertFalse($response->isRedirection());
769 $this->assertFalse($response->isRedirect());
770
771 $response = new Response('', 404);
772 $this->assertFalse($response->isRedirection());
773 $this->assertFalse($response->isRedirect());
774
775 $response = new Response('', 301, ['Location' => '/good-uri']);
776 $this->assertFalse($response->isRedirect('/bad-uri'));
777 $this->assertTrue($response->isRedirect('/good-uri'));
778 }
779
780 public function testIsNotFound()
781 {
782 $response = new Response('', 404);
783 $this->assertTrue($response->isNotFound());
784
785 $response = new Response('', 200);
786 $this->assertFalse($response->isNotFound());
787 }
788
789 public function testIsEmpty()
790 {
791 foreach ([204, 304] as $code) {
792 $response = new Response('', $code);
793 $this->assertTrue($response->isEmpty());
794 }
795
796 $response = new Response('', 200);
797 $this->assertFalse($response->isEmpty());
798 }
799
800 public function testIsForbidden()
801 {
802 $response = new Response('', 403);
803 $this->assertTrue($response->isForbidden());
804
805 $response = new Response('', 200);
806 $this->assertFalse($response->isForbidden());
807 }
808
809 public function testIsOk()
810 {
811 $response = new Response('', 200);
812 $this->assertTrue($response->isOk());
813
814 $response = new Response('', 404);
815 $this->assertFalse($response->isOk());
816 }
817
818 public function testIsServerOrClientError()
819 {
820 $response = new Response('', 404);
821 $this->assertTrue($response->isClientError());
822 $this->assertFalse($response->isServerError());
823
824 $response = new Response('', 500);
825 $this->assertFalse($response->isClientError());
826 $this->assertTrue($response->isServerError());
827 }
828
829 public function testHasVary()
830 {
831 $response = new Response();
832 $this->assertFalse($response->hasVary());
833
834 $response->setVary('User-Agent');
835 $this->assertTrue($response->hasVary());
836 }
837
838 public function testSetEtag()
839 {
840 $response = new Response('', 200, ['ETag' => '"12345"']);
841 $response->setEtag();
842
843 $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
844 }
845
846 /**
847 * @dataProvider validContentProvider
848 */
849 public function testSetContent($content)
850 {
851 $response = new Response();
852 $response->setContent($content);
853 $this->assertEquals((string) $content, $response->getContent());
854 }
855
856 /**
857 * @dataProvider invalidContentProvider
858 */
859 public function testSetContentInvalid($content)
860 {
861 $this->expectException('UnexpectedValueException');
862 $response = new Response();
863 $response->setContent($content);
864 }
865
866 public function testSettersAreChainable()
867 {
868 $response = new Response();
869
870 $setters = [
871 'setProtocolVersion' => '1.0',
872 'setCharset' => 'UTF-8',
873 'setPublic' => null,
874 'setPrivate' => null,
875 'setDate' => $this->createDateTimeNow(),
876 'expire' => null,
877 'setMaxAge' => 1,
878 'setSharedMaxAge' => 1,
879 'setTtl' => 1,
880 'setClientTtl' => 1,
881 ];
882
883 foreach ($setters as $setter => $arg) {
884 $this->assertEquals($response, $response->{$setter}($arg));
885 }
886 }
887
888 public function testNoDeprecationsAreTriggered()
889 {
890 new DefaultResponse();
891 $this->getMockBuilder(Response::class)->getMock();
892
893 // we just need to ensure that subclasses of Response can be created without any deprecations
894 // being triggered if the subclass does not override any final methods
895 $this->addToAssertionCount(1);
896 }
897
898 public function validContentProvider()
899 {
900 return [
901 'obj' => [new StringableObject()],
902 'string' => ['Foo'],
903 'int' => [2],
904 ];
905 }
906
907 public function invalidContentProvider()
908 {
909 return [
910 'obj' => [new \stdClass()],
911 'array' => [[]],
912 'bool' => [true, '1'],
913 ];
914 }
915
916 protected function createDateTimeOneHourAgo()
917 {
918 return $this->createDateTimeNow()->sub(new \DateInterval('PT1H'));
919 }
920
921 protected function createDateTimeOneHourLater()
922 {
923 return $this->createDateTimeNow()->add(new \DateInterval('PT1H'));
924 }
925
926 protected function createDateTimeNow()
927 {
928 $date = new \DateTime();
929
930 return $date->setTimestamp(time());
931 }
932
933 protected function provideResponse()
934 {
935 return new Response();
936 }
937
938 /**
939 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
940 *
941 * @author Fábio Pacheco
942 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
943 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
944 */
945 public function ianaCodesReasonPhrasesProvider()
946 {
947 if (!\in_array('https', stream_get_wrappers(), true)) {
948 $this->markTestSkipped('The "https" wrapper is not available');
949 }
950
951 $ianaHttpStatusCodes = new \DOMDocument();
952
953 $context = stream_context_create([
954 'http' => [
955 'method' => 'GET',
956 'timeout' => 30,
957 'user_agent' => __METHOD__,
958 ],
959 ]);
960
961 if (!$rawStatusCodes = file_get_contents('https://www.iana.org/assignments/http-status-codes/http-status-codes.xml', false, $context)) {
962 $this->markTestSkipped('The IANA server is throttling the list of status codes');
963 }
964
965 $ianaHttpStatusCodes->loadXML($rawStatusCodes);
966 if (!$ianaHttpStatusCodes->relaxNGValidate(__DIR__.'/schema/http-status-codes.rng')) {
967 self::fail('Invalid IANA\'s HTTP status code list.');
968 }
969
970 $ianaCodesReasonPhrases = [];
971
972 $xpath = new \DOMXPath($ianaHttpStatusCodes);
973 $xpath->registerNamespace('ns', 'http://www.iana.org/assignments');
974
975 $records = $xpath->query('//ns:record');
976 foreach ($records as $record) {
977 $value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
978 $description = $xpath->query('.//ns:description', $record)->item(0)->nodeValue;
979
980 if (\in_array($description, ['Unassigned', '(Unused)'], true)) {
981 continue;
982 }
983
984 if (preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches)) {
985 for ($value = $matches[1]; $value <= $matches[2]; ++$value) {
986 $ianaCodesReasonPhrases[] = [$value, $description];
987 }
988 } else {
989 $ianaCodesReasonPhrases[] = [$value, $description];
990 }
991 }
992
993 return $ianaCodesReasonPhrases;
994 }
995
996 /**
997 * @dataProvider ianaCodesReasonPhrasesProvider
998 */
999 public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase)
1000 {
1001 $this->assertEquals($reasonPhrase, Response::$statusTexts[$code]);
1002 }
1003 }
1004
1005 class StringableObject
1006 {
1007 public function __toString()
1008 {
1009 return 'Foo';
1010 }
1011 }
1012
1013 class DefaultResponse extends Response
1014 {
1015 }
1016