1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\Tests;
13:
14: use Symfony\Component\HttpFoundation\Request;
15: use Symfony\Component\HttpFoundation\Response;
16:
17: class ResponseTest extends ResponseTestCase
18: {
19: public function testCreate()
20: {
21: $response = Response::create('foo', 301, array('Foo' => 'bar'));
22:
23: $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
24: $this->assertEquals(301, $response->getStatusCode());
25: $this->assertEquals('bar', $response->headers->get('foo'));
26: }
27:
28: public function testToString()
29: {
30: $response = new Response();
31: $response = explode("\r\n", $response);
32: $this->assertEquals("HTTP/1.0 200 OK", $response[0]);
33: $this->assertEquals("Cache-Control: no-cache", $response[1]);
34: }
35:
36: public function testClone()
37: {
38: $response = new Response();
39: $responseClone = clone $response;
40: $this->assertEquals($response, $responseClone);
41: }
42:
43: public function testSendHeaders()
44: {
45: $response = new Response();
46: $headers = $response->sendHeaders();
47: $this->assertObjectHasAttribute('headers', $headers);
48: $this->assertObjectHasAttribute('content', $headers);
49: $this->assertObjectHasAttribute('version', $headers);
50: $this->assertObjectHasAttribute('statusCode', $headers);
51: $this->assertObjectHasAttribute('statusText', $headers);
52: $this->assertObjectHasAttribute('charset', $headers);
53: }
54:
55: public function testSend()
56: {
57: $response = new Response();
58: $responseSend = $response->send();
59: $this->assertObjectHasAttribute('headers', $responseSend);
60: $this->assertObjectHasAttribute('content', $responseSend);
61: $this->assertObjectHasAttribute('version', $responseSend);
62: $this->assertObjectHasAttribute('statusCode', $responseSend);
63: $this->assertObjectHasAttribute('statusText', $responseSend);
64: $this->assertObjectHasAttribute('charset', $responseSend);
65: }
66:
67: public function testGetCharset()
68: {
69: $response = new Response();
70: $charsetOrigin = 'UTF-8';
71: $response->setCharset($charsetOrigin);
72: $charset = $response->getCharset();
73: $this->assertEquals($charsetOrigin, $charset);
74: }
75:
76: public function testIsCacheable()
77: {
78: $response = new Response();
79: $this->assertFalse($response->isCacheable());
80: }
81:
82: public function testIsCacheableWithErrorCode()
83: {
84: $response = new Response('', 500);
85: $this->assertFalse($response->isCacheable());
86: }
87:
88: public function testIsCacheableWithNoStoreDirective()
89: {
90: $response = new Response();
91: $response->headers->set('cache-control', 'private');
92: $this->assertFalse($response->isCacheable());
93: }
94:
95: public function testIsCacheableWithSetTtl()
96: {
97: $response = new Response();
98: $response->setTtl(10);
99: $this->assertTrue($response->isCacheable());
100: }
101:
102: public function testMustRevalidate()
103: {
104: $response = new Response();
105: $this->assertFalse($response->mustRevalidate());
106: }
107:
108: public function testSetNotModified()
109: {
110: $response = new Response();
111: $modified = $response->setNotModified();
112: $this->assertObjectHasAttribute('headers', $modified);
113: $this->assertObjectHasAttribute('content', $modified);
114: $this->assertObjectHasAttribute('version', $modified);
115: $this->assertObjectHasAttribute('statusCode', $modified);
116: $this->assertObjectHasAttribute('statusText', $modified);
117: $this->assertObjectHasAttribute('charset', $modified);
118: $this->assertEquals(304, $modified->getStatusCode());
119: }
120:
121: public function testIsSuccessful()
122: {
123: $response = new Response();
124: $this->assertTrue($response->isSuccessful());
125: }
126:
127: public function testIsNotModified()
128: {
129: $response = new Response();
130: $modified = $response->isNotModified(new Request());
131: $this->assertFalse($modified);
132: }
133:
134: public function testIsNotModifiedNotSafe()
135: {
136: $request = Request::create('/homepage', 'POST');
137:
138: $response = new Response();
139: $this->assertFalse($response->isNotModified($request));
140: }
141:
142: public function testIsNotModifiedLastModified()
143: {
144: $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
145: $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
146: $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
147:
148: $request = new Request();
149: $request->headers->set('If-Modified-Since', $modified);
150:
151: $response = new Response();
152:
153: $response->headers->set('Last-Modified', $modified);
154: $this->assertTrue($response->isNotModified($request));
155:
156: $response->headers->set('Last-Modified', $before);
157: $this->assertTrue($response->isNotModified($request));
158:
159: $response->headers->set('Last-Modified', $after);
160: $this->assertFalse($response->isNotModified($request));
161:
162: $response->headers->set('Last-Modified', '');
163: $this->assertFalse($response->isNotModified($request));
164: }
165:
166: public function testIsNotModifiedEtag()
167: {
168: $etagOne = 'randomly_generated_etag';
169: $etagTwo = 'randomly_generated_etag_2';
170:
171: $request = new Request();
172: $request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
173:
174: $response = new Response();
175:
176: $response->headers->set('ETag', $etagOne);
177: $this->assertTrue($response->isNotModified($request));
178:
179: $response->headers->set('ETag', $etagTwo);
180: $this->assertTrue($response->isNotModified($request));
181:
182: $response->headers->set('ETag', '');
183: $this->assertFalse($response->isNotModified($request));
184: }
185:
186: public function testIsNotModifiedLastModifiedAndEtag()
187: {
188: $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
189: $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
190: $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
191: $etag = 'randomly_generated_etag';
192:
193: $request = new Request();
194: $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
195: $request->headers->set('If-Modified-Since', $modified);
196:
197: $response = new Response();
198:
199: $response->headers->set('ETag', $etag);
200: $response->headers->set('Last-Modified', $after);
201: $this->assertFalse($response->isNotModified($request));
202:
203: $response->headers->set('ETag', 'non-existent-etag');
204: $response->headers->set('Last-Modified', $before);
205: $this->assertFalse($response->isNotModified($request));
206:
207: $response->headers->set('ETag', $etag);
208: $response->headers->set('Last-Modified', $modified);
209: $this->assertTrue($response->isNotModified($request));
210: }
211:
212: public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
213: {
214: $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
215: $etag = 'randomly_generated_etag';
216:
217: $request = new Request();
218: $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
219: $request->headers->set('If-Modified-Since', $modified);
220:
221: $response = new Response();
222:
223: $response->headers->set('ETag', $etag);
224: $this->assertTrue($response->isNotModified($request));
225:
226: $response->headers->set('ETag', 'non-existent-etag');
227: $this->assertFalse($response->isNotModified($request));
228: }
229:
230: public function testIsValidateable()
231: {
232: $response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
233: $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
234:
235: $response = new Response('', 200, array('ETag' => '"12345"'));
236: $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
237:
238: $response = new Response();
239: $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
240: }
241:
242: public function testGetDate()
243: {
244: $oneHourAgo = $this->createDateTimeOneHourAgo();
245: $response = new Response('', 200, array('Date' => $oneHourAgo->format(DATE_RFC2822)));
246: $this->assertEquals(0, $oneHourAgo->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present');
247:
248: $response = new Response();
249: $date = $response->getDate();
250: $this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present');
251:
252: $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
253: $now = $this->createDateTimeNow();
254: $response->headers->set('Date', $now->format(DATE_RFC2822));
255: $this->assertLessThanOrEqual(1, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified');
256:
257: $response = new Response('', 200);
258: $response->headers->remove('Date');
259: $this->assertInstanceOf('\DateTime', $response->getDate());
260: }
261:
262: public function testGetMaxAge()
263: {
264: $response = new Response();
265: $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
266: $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
267:
268: $response = new Response();
269: $response->headers->set('Cache-Control', 'max-age=600');
270: $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
271:
272: $response = new Response();
273: $response->headers->set('Cache-Control', 'must-revalidate');
274: $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
275: $this->assertLessThanOrEqual(1, $response->getMaxAge() - 3600, '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
276:
277: $response = new Response();
278: $response->headers->set('Cache-Control', 'must-revalidate');
279: $response->headers->set('Expires', -1);
280: $this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822));
281:
282: $response = new Response();
283: $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
284: }
285:
286: public function testSetSharedMaxAge()
287: {
288: $response = new Response();
289: $response->setSharedMaxAge(20);
290:
291: $cacheControl = $response->headers->get('Cache-Control');
292: $this->assertEquals('public, s-maxage=20', $cacheControl);
293: }
294:
295: public function testIsPrivate()
296: {
297: $response = new Response();
298: $response->headers->set('Cache-Control', 'max-age=100');
299: $response->setPrivate();
300: $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
301: $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
302:
303: $response = new Response();
304: $response->headers->set('Cache-Control', 'public, max-age=100');
305: $response->setPrivate();
306: $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
307: $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
308: $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
309: }
310:
311: public function testExpire()
312: {
313: $response = new Response();
314: $response->headers->set('Cache-Control', 'max-age=100');
315: $response->expire();
316: $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
317:
318: $response = new Response();
319: $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
320: $response->expire();
321: $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
322:
323: $response = new Response();
324: $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
325: $response->headers->set('Age', '1000');
326: $response->expire();
327: $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
328:
329: $response = new Response();
330: $response->expire();
331: $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
332:
333: $response = new Response();
334: $response->headers->set('Expires', -1);
335: $response->expire();
336: $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
337: }
338:
339: public function testGetTtl()
340: {
341: $response = new Response();
342: $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
343:
344: $response = new Response();
345: $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
346: $this->assertLessThanOrEqual(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
347:
348: $response = new Response();
349: $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
350: $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
351:
352: $response = new Response();
353: $response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
354: $response->headers->set('Age', 0);
355: $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
356:
357: $response = new Response();
358: $response->headers->set('Cache-Control', 'max-age=60');
359: $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
360: }
361:
362: public function testSetClientTtl()
363: {
364: $response = new Response();
365: $response->setClientTtl(10);
366:
367: $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
368: }
369:
370: public function testGetSetProtocolVersion()
371: {
372: $response = new Response();
373:
374: $this->assertEquals('1.0', $response->getProtocolVersion());
375:
376: $response->setProtocolVersion('1.1');
377:
378: $this->assertEquals('1.1', $response->getProtocolVersion());
379: }
380:
381: public function testGetVary()
382: {
383: $response = new Response();
384: $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
385:
386: $response = new Response();
387: $response->headers->set('Vary', 'Accept-Language');
388: $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
389:
390: $response = new Response();
391: $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
392: $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
393:
394: $response = new Response();
395: $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
396: $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
397:
398: $vary = array('Accept-Language', 'User-Agent', 'X-foo');
399:
400: $response = new Response();
401: $response->headers->set('Vary', $vary);
402: $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
403:
404: $response = new Response();
405: $response->headers->set('Vary', 'Accept-Language, User-Agent, X-foo');
406: $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
407: }
408:
409: public function testSetVary()
410: {
411: $response = new Response();
412: $response->setVary('Accept-Language');
413: $this->assertEquals(array('Accept-Language'), $response->getVary());
414:
415: $response->setVary('Accept-Language, User-Agent');
416: $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
417:
418: $response->setVary('X-Foo', false);
419: $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->setVary() doesn\'t wipe out earlier Vary headers if replace is set to false');
420: }
421:
422: public function testDefaultContentType()
423: {
424: $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
425: $headerMock->expects($this->at(0))
426: ->method('set')
427: ->with('Content-Type', 'text/html');
428: $headerMock->expects($this->at(1))
429: ->method('set')
430: ->with('Content-Type', 'text/html; charset=UTF-8');
431:
432: $response = new Response('foo');
433: $response->headers = $headerMock;
434:
435: $response->prepare(new Request());
436: }
437:
438: public function testContentTypeCharset()
439: {
440: $response = new Response();
441: $response->headers->set('Content-Type', 'text/css');
442:
443:
444: $response->prepare(new Request());
445:
446: $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
447: }
448:
449: public function testPrepareDoesNothingIfContentTypeIsSet()
450: {
451: $response = new Response('foo');
452: $response->headers->set('Content-Type', 'text/plain');
453:
454: $response->prepare(new Request());
455:
456: $this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
457: }
458:
459: public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
460: {
461: $response = new Response('foo');
462:
463: $response->prepare(new Request());
464:
465: $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
466: }
467:
468: public function testPrepareSetContentType()
469: {
470: $response = new Response('foo');
471: $request = Request::create('/');
472: $request->setRequestFormat('json');
473:
474: $response->prepare($request);
475:
476: $this->assertEquals('application/json', $response->headers->get('content-type'));
477: }
478:
479: public function testPrepareRemovesContentForHeadRequests()
480: {
481: $response = new Response('foo');
482: $request = Request::create('/', 'HEAD');
483:
484: $length = 12345;
485: $response->headers->set('Content-Length', $length);
486: $response->prepare($request);
487:
488: $this->assertEquals('', $response->getContent());
489: $this->assertEquals($length, $response->headers->get('Content-Length'), 'Content-Length should be as if it was GET; see RFC2616 14.13');
490: }
491:
492: public function testPrepareRemovesContentForInformationalResponse()
493: {
494: $response = new Response('foo');
495: $request = Request::create('/');
496:
497: $response->setContent('content');
498: $response->setStatusCode(101);
499: $response->prepare($request);
500: $this->assertEquals('', $response->getContent());
501: $this->assertFalse($response->headers->has('Content-Type'));
502: $this->assertFalse($response->headers->has('Content-Type'));
503:
504: $response->setContent('content');
505: $response->setStatusCode(304);
506: $response->prepare($request);
507: $this->assertEquals('', $response->getContent());
508: $this->assertFalse($response->headers->has('Content-Type'));
509: $this->assertFalse($response->headers->has('Content-Length'));
510: }
511:
512: public function testPrepareRemovesContentLength()
513: {
514: $response = new Response('foo');
515: $request = Request::create('/');
516:
517: $response->headers->set('Content-Length', 12345);
518: $response->prepare($request);
519: $this->assertEquals(12345, $response->headers->get('Content-Length'));
520:
521: $response->headers->set('Transfer-Encoding', 'chunked');
522: $response->prepare($request);
523: $this->assertFalse($response->headers->has('Content-Length'));
524: }
525:
526: public function testPrepareSetsPragmaOnHttp10Only()
527: {
528: $request = Request::create('/', 'GET');
529: $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
530:
531: $response = new Response('foo');
532: $response->prepare($request);
533: $this->assertEquals('no-cache', $response->headers->get('pragma'));
534: $this->assertEquals('-1', $response->headers->get('expires'));
535:
536: $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
537: $response = new Response('foo');
538: $response->prepare($request);
539: $this->assertFalse($response->headers->has('pragma'));
540: $this->assertFalse($response->headers->has('expires'));
541: }
542:
543: public function testSetCache()
544: {
545: $response = new Response();
546:
547: try {
548: $response->setCache(array("wrong option" => "value"));
549: $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
550: } catch (\Exception $e) {
551: $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
552: $this->assertContains('"wrong option"', $e->getMessage());
553: }
554:
555: $options = array('etag' => '"whatever"');
556: $response->setCache($options);
557: $this->assertEquals($response->getEtag(), '"whatever"');
558:
559: $now = new \DateTime();
560: $options = array('last_modified' => $now);
561: $response->setCache($options);
562: $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
563:
564: $options = array('max_age' => 100);
565: $response->setCache($options);
566: $this->assertEquals($response->getMaxAge(), 100);
567:
568: $options = array('s_maxage' => 200);
569: $response->setCache($options);
570: $this->assertEquals($response->getMaxAge(), 200);
571:
572: $this->assertTrue($response->headers->hasCacheControlDirective('public'));
573: $this->assertFalse($response->headers->hasCacheControlDirective('private'));
574:
575: $response->setCache(array('public' => true));
576: $this->assertTrue($response->headers->hasCacheControlDirective('public'));
577: $this->assertFalse($response->headers->hasCacheControlDirective('private'));
578:
579: $response->setCache(array('public' => false));
580: $this->assertFalse($response->headers->hasCacheControlDirective('public'));
581: $this->assertTrue($response->headers->hasCacheControlDirective('private'));
582:
583: $response->setCache(array('private' => true));
584: $this->assertFalse($response->headers->hasCacheControlDirective('public'));
585: $this->assertTrue($response->headers->hasCacheControlDirective('private'));
586:
587: $response->setCache(array('private' => false));
588: $this->assertTrue($response->headers->hasCacheControlDirective('public'));
589: $this->assertFalse($response->headers->hasCacheControlDirective('private'));
590: }
591:
592: public function testSendContent()
593: {
594: $response = new Response('test response rendering', 200);
595:
596: ob_start();
597: $response->sendContent();
598: $string = ob_get_clean();
599: $this->assertContains('test response rendering', $string);
600: }
601:
602: public function testSetPublic()
603: {
604: $response = new Response();
605: $response->setPublic();
606:
607: $this->assertTrue($response->headers->hasCacheControlDirective('public'));
608: $this->assertFalse($response->headers->hasCacheControlDirective('private'));
609: }
610:
611: public function testSetExpires()
612: {
613: $response = new Response();
614: $response->setExpires(null);
615:
616: $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
617:
618: $now = new \DateTime();
619: $response->setExpires($now);
620:
621: $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
622: }
623:
624: public function testSetLastModified()
625: {
626: $response = new Response();
627: $response->setLastModified(new \DateTime());
628: $this->assertNotNull($response->getLastModified());
629:
630: $response->setLastModified(null);
631: $this->assertNull($response->getLastModified());
632: }
633:
634: public function testIsInvalid()
635: {
636: $response = new Response();
637:
638: try {
639: $response->setStatusCode(99);
640: $this->fail();
641: } catch (\InvalidArgumentException $e) {
642: $this->assertTrue($response->isInvalid());
643: }
644:
645: try {
646: $response->setStatusCode(650);
647: $this->fail();
648: } catch (\InvalidArgumentException $e) {
649: $this->assertTrue($response->isInvalid());
650: }
651:
652: $response = new Response('', 200);
653: $this->assertFalse($response->isInvalid());
654: }
655:
656: 657: 658:
659: public function testSetStatusCode($code, $text, $expectedText)
660: {
661: $response = new Response();
662:
663: $response->setStatusCode($code, $text);
664:
665: $statusText = new \ReflectionProperty($response, 'statusText');
666: $statusText->setAccessible(true);
667:
668: $this->assertEquals($expectedText, $statusText->getValue($response));
669: }
670:
671: public function getStatusCodeFixtures()
672: {
673: return array(
674: array('200', null, 'OK'),
675: array('200', false, ''),
676: array('200', 'foo', 'foo'),
677: array('199', null, ''),
678: array('199', false, ''),
679: array('199', 'foo', 'foo'),
680: );
681: }
682:
683: public function testIsInformational()
684: {
685: $response = new Response('', 100);
686: $this->assertTrue($response->isInformational());
687:
688: $response = new Response('', 200);
689: $this->assertFalse($response->isInformational());
690: }
691:
692: public function testIsRedirectRedirection()
693: {
694: foreach (array(301, 302, 303, 307) as $code) {
695: $response = new Response('', $code);
696: $this->assertTrue($response->isRedirection());
697: $this->assertTrue($response->isRedirect());
698: }
699:
700: $response = new Response('', 304);
701: $this->assertTrue($response->isRedirection());
702: $this->assertFalse($response->isRedirect());
703:
704: $response = new Response('', 200);
705: $this->assertFalse($response->isRedirection());
706: $this->assertFalse($response->isRedirect());
707:
708: $response = new Response('', 404);
709: $this->assertFalse($response->isRedirection());
710: $this->assertFalse($response->isRedirect());
711:
712: $response = new Response('', 301, array('Location' => '/good-uri'));
713: $this->assertFalse($response->isRedirect('/bad-uri'));
714: $this->assertTrue($response->isRedirect('/good-uri'));
715: }
716:
717: public function testIsNotFound()
718: {
719: $response = new Response('', 404);
720: $this->assertTrue($response->isNotFound());
721:
722: $response = new Response('', 200);
723: $this->assertFalse($response->isNotFound());
724: }
725:
726: public function testIsEmpty()
727: {
728: foreach (array(204, 304) as $code) {
729: $response = new Response('', $code);
730: $this->assertTrue($response->isEmpty());
731: }
732:
733: $response = new Response('', 200);
734: $this->assertFalse($response->isEmpty());
735: }
736:
737: public function testIsForbidden()
738: {
739: $response = new Response('', 403);
740: $this->assertTrue($response->isForbidden());
741:
742: $response = new Response('', 200);
743: $this->assertFalse($response->isForbidden());
744: }
745:
746: public function testIsOk()
747: {
748: $response = new Response('', 200);
749: $this->assertTrue($response->isOk());
750:
751: $response = new Response('', 404);
752: $this->assertFalse($response->isOk());
753: }
754:
755: public function testIsServerOrClientError()
756: {
757: $response = new Response('', 404);
758: $this->assertTrue($response->isClientError());
759: $this->assertFalse($response->isServerError());
760:
761: $response = new Response('', 500);
762: $this->assertFalse($response->isClientError());
763: $this->assertTrue($response->isServerError());
764: }
765:
766: public function testHasVary()
767: {
768: $response = new Response();
769: $this->assertFalse($response->hasVary());
770:
771: $response->setVary('User-Agent');
772: $this->assertTrue($response->hasVary());
773: }
774:
775: public function testSetEtag()
776: {
777: $response = new Response('', 200, array('ETag' => '"12345"'));
778: $response->setEtag();
779:
780: $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
781: }
782:
783: 784: 785:
786: public function testSetContent($content)
787: {
788: $response = new Response();
789: $response->setContent($content);
790: $this->assertEquals((string) $content, $response->getContent());
791: }
792:
793: 794: 795: 796:
797: public function testSetContentInvalid($content)
798: {
799: $response = new Response();
800: $response->setContent($content);
801: }
802:
803: public function testSettersAreChainable()
804: {
805: $response = new Response();
806:
807: $setters = array(
808: 'setProtocolVersion' => '1.0',
809: 'setCharset' => 'UTF-8',
810: 'setPublic' => null,
811: 'setPrivate' => null,
812: 'setDate' => new \DateTime(),
813: 'expire' => null,
814: 'setMaxAge' => 1,
815: 'setSharedMaxAge' => 1,
816: 'setTtl' => 1,
817: 'setClientTtl' => 1,
818: );
819:
820: foreach ($setters as $setter => $arg) {
821: $this->assertEquals($response, $response->{$setter}($arg));
822: }
823: }
824:
825: public function validContentProvider()
826: {
827: return array(
828: 'obj' => array(new StringableObject()),
829: 'string' => array('Foo'),
830: 'int' => array(2),
831: );
832: }
833:
834: public function invalidContentProvider()
835: {
836: return array(
837: 'obj' => array(new \stdClass()),
838: 'array' => array(array()),
839: 'bool' => array(true, '1'),
840: );
841: }
842:
843: protected function createDateTimeOneHourAgo()
844: {
845: $date = new \DateTime();
846:
847: return $date->sub(new \DateInterval('PT1H'));
848: }
849:
850: protected function createDateTimeOneHourLater()
851: {
852: $date = new \DateTime();
853:
854: return $date->add(new \DateInterval('PT1H'));
855: }
856:
857: protected function createDateTimeNow()
858: {
859: return new \DateTime();
860: }
861:
862: protected function provideResponse()
863: {
864: return new Response();
865: }
866: }
867:
868: class StringableObject
869: {
870: public function __toString()
871: {
872: return 'Foo';
873: }
874: }
875: