ResponseTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class ResponseTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testIsValidateable()
  15. {
  16. $response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  17. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
  18. $response = new Response('', 200, array('ETag' => '"12345"'));
  19. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
  20. $response = new Response();
  21. $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
  22. }
  23. public function testGetDate()
  24. {
  25. $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  26. $this->assertEquals(0, $this->createDateTimeOneHourAgo()->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present');
  27. $response = new Response();
  28. $date = $response->getDate();
  29. $this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present');
  30. $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  31. $now = $this->createDateTimeNow();
  32. $response->headers->set('Date', $now->format(DATE_RFC2822));
  33. $this->assertEquals(0, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified');
  34. }
  35. public function testGetMaxAge()
  36. {
  37. $response = new Response();
  38. $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
  39. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
  40. $response = new Response();
  41. $response->headers->set('Cache-Control', 'max-age=600');
  42. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
  43. $response = new Response();
  44. $response->headers->set('Cache-Control', 'must-revalidate');
  45. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  46. $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
  47. $response = new Response();
  48. $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
  49. }
  50. public function testIsPrivate()
  51. {
  52. $response = new Response();
  53. $response->headers->set('Cache-Control', 'max-age=100');
  54. $response->setPrivate();
  55. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  56. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  57. $response = new Response();
  58. $response->headers->set('Cache-Control', 'public, max-age=100');
  59. $response->setPrivate();
  60. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  61. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  62. $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
  63. }
  64. public function testExpire()
  65. {
  66. $response = new Response();
  67. $response->headers->set('Cache-Control', 'max-age=100');
  68. $response->expire();
  69. $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
  70. $response = new Response();
  71. $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
  72. $response->expire();
  73. $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
  74. $response = new Response();
  75. $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
  76. $response->headers->set('Age', '1000');
  77. $response->expire();
  78. $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
  79. $response = new Response();
  80. $response->expire();
  81. $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
  82. }
  83. public function testGetTtl()
  84. {
  85. $response = new Response();
  86. $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
  87. $response = new Response();
  88. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  89. $this->assertLessThan(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
  90. $response = new Response();
  91. $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
  92. $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part');
  93. $response = new Response();
  94. $response->headers->set('Cache-Control', 'max-age=60');
  95. $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
  96. }
  97. public function testSetClientTtl()
  98. {
  99. $response = new Response();
  100. $response->setClientTtl(10);
  101. $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
  102. }
  103. public function testGetVary()
  104. {
  105. $response = new Response();
  106. $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
  107. $response = new Response();
  108. $response->headers->set('Vary', 'Accept-Language');
  109. $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
  110. $response = new Response();
  111. $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
  112. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
  113. $response = new Response();
  114. $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
  115. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
  116. }
  117. public function testSetVary()
  118. {
  119. $response = new Response();
  120. $response->setVary('Accept-Language');
  121. $this->assertEquals(array('Accept-Language'), $response->getVary());
  122. $response->setVary('Accept-Language, User-Agent');
  123. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
  124. $response->setVary('X-Foo', false);
  125. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() doesn\'t change the Vary header if replace is set to false');
  126. }
  127. public function testDefaultContentType()
  128. {
  129. $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
  130. $headerMock->expects($this->at(0))
  131. ->method('set')
  132. ->with('Content-Type', 'text/html; charset=UTF-8');
  133. $headerMock->expects($this->at(1))
  134. ->method('set')
  135. ->with('Content-Type', 'text/html; charset=Foo');
  136. $response = new Response();
  137. $response->headers = $headerMock;
  138. // verify first set()
  139. $response->__toString();
  140. $response->headers->remove('Content-Type');
  141. $response->setCharset('Foo');
  142. // verify second set()
  143. $response->__toString();
  144. }
  145. public function testSendContent()
  146. {
  147. $response = new Response('test response rendering', 200);
  148. $response->sendHeaders();
  149. ob_start();
  150. $response->sendContent();
  151. $string = ob_get_clean();
  152. $this->assertContains('test response rendering', $string);
  153. }
  154. public function testSetPublic()
  155. {
  156. $response = new Response();
  157. $response->setPublic();
  158. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  159. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  160. }
  161. public function testIsInvalid()
  162. {
  163. $response = new Response();
  164. try {
  165. $response->setStatusCode(99);
  166. $this->fail();
  167. } catch(\InvalidArgumentException $e) {
  168. $this->assertTrue($response->isInvalid());
  169. }
  170. try {
  171. $response->setStatusCode(650);
  172. $this->fail();
  173. } catch(\InvalidArgumentException $e) {
  174. $this->assertTrue($response->isInvalid());
  175. }
  176. $response = new Response('', 200);
  177. $this->assertFalse($response->isInvalid());
  178. }
  179. public function testIsInformational()
  180. {
  181. $response = new Response('', 100);
  182. $this->assertTrue($response->isInformational());
  183. $response = new Response('', 200);
  184. $this->assertFalse($response->isInformational());
  185. }
  186. public function testIsRedirectRedirection()
  187. {
  188. foreach (array(301, 302, 303, 307) as $code)
  189. {
  190. $response = new Response('', $code);
  191. $this->assertTrue($response->isRedirection());
  192. $this->assertTrue($response->isRedirect());
  193. }
  194. $response = new Response('', 304);
  195. $this->assertTrue($response->isRedirection());
  196. $this->assertFalse($response->isRedirect());
  197. $response = new Response('', 200);
  198. $this->assertFalse($response->isRedirection());
  199. $this->assertFalse($response->isRedirect());
  200. $response = new Response('', 404);
  201. $this->assertFalse($response->isRedirection());
  202. $this->assertFalse($response->isRedirect());
  203. }
  204. public function testIsNotFound()
  205. {
  206. $response = new Response('', 404);
  207. $this->assertTrue($response->isNotFound());
  208. $response = new Response('', 200);
  209. $this->assertFalse($response->isNotFound());
  210. }
  211. public function testIsEmpty()
  212. {
  213. foreach (array(201, 204, 304) as $code)
  214. {
  215. $response = new Response('', $code);
  216. $this->assertTrue($response->isEmpty());
  217. }
  218. $response = new Response('', 200);
  219. $this->assertFalse($response->isEmpty());
  220. }
  221. public function testIsForbidden()
  222. {
  223. $response = new Response('', 403);
  224. $this->assertTrue($response->isForbidden());
  225. $response = new Response('', 200);
  226. $this->assertFalse($response->isForbidden());
  227. }
  228. public function testIsOk()
  229. {
  230. $response = new Response('', 200);
  231. $this->assertTrue($response->isOk());
  232. $response = new Response('', 404);
  233. $this->assertFalse($response->isOk());
  234. }
  235. public function testIsServerOrClientError()
  236. {
  237. $response = new Response('', 404);
  238. $this->assertTrue($response->isClientError());
  239. $this->assertFalse($response->isServerError());
  240. $response = new Response('', 500);
  241. $this->assertFalse($response->isClientError());
  242. $this->assertTrue($response->isServerError());
  243. }
  244. public function testHasVary()
  245. {
  246. $response = new Response();
  247. $this->assertFalse($response->hasVary());
  248. $response->setVary('User-Agent');
  249. $this->assertTrue($response->hasVary());
  250. }
  251. public function testSetEtag()
  252. {
  253. $response = new Response('', 200, array('ETag' => '"12345"'));
  254. $response->setEtag();
  255. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  256. }
  257. protected function createDateTimeOneHourAgo()
  258. {
  259. $date = new \DateTime();
  260. return $date->sub(new \DateInterval('PT1H'));
  261. }
  262. protected function createDateTimeOneHourLater()
  263. {
  264. $date = new \DateTime();
  265. return $date->add(new \DateInterval('PT1H'));
  266. }
  267. protected function createDateTimeNow()
  268. {
  269. return new \DateTime();
  270. }
  271. }