ResponseTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 testSetCache()
  146. {
  147. $response = new Response();
  148. //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
  149. try {
  150. $response->setCache(array("wrong option" => "value"));
  151. $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  152. } catch (\Exception $e) {
  153. $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
  154. }
  155. $options = array('etag' => '"whatever"');
  156. $response->setCache($options);
  157. $this->assertEquals($response->getEtag(), '"whatever"');
  158. $now = new \DateTime();
  159. $options = array('last_modified' => $now);
  160. $response->setCache($options);
  161. $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
  162. $options = array('max_age' => 100);
  163. $response->setCache($options);
  164. $this->assertEquals($response->getMaxAge(), 100);
  165. $options = array('s_maxage' => 200);
  166. $response->setCache($options);
  167. $this->assertEquals($response->getMaxAge(), 200);
  168. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  169. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  170. $response->setCache(array('public' => true));
  171. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  172. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  173. $response->setCache(array('public' => false));
  174. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  175. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  176. $response->setCache(array('private' => true));
  177. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  178. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  179. $response->setCache(array('private' => false));
  180. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  181. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  182. }
  183. public function testSendContent()
  184. {
  185. $response = new Response('test response rendering', 200);
  186. ob_start();
  187. $response->sendContent();
  188. $string = ob_get_clean();
  189. $this->assertContains('test response rendering', $string);
  190. }
  191. public function testSetPublic()
  192. {
  193. $response = new Response();
  194. $response->setPublic();
  195. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  196. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  197. }
  198. public function testSetExpires()
  199. {
  200. $response = new Response();
  201. $response->setExpires(null);
  202. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  203. $now = new \DateTime();
  204. $response->setExpires($now);
  205. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  206. }
  207. public function testSetLastModified()
  208. {
  209. $response = new Response();
  210. $response->setLastModified(new \DateTime());
  211. $this->assertNotNull($response->getLastModified());
  212. $response->setLastModified(null);
  213. $this->assertNull($response->getLastModified());
  214. }
  215. public function testIsInvalid()
  216. {
  217. $response = new Response();
  218. try {
  219. $response->setStatusCode(99);
  220. $this->fail();
  221. } catch(\InvalidArgumentException $e) {
  222. $this->assertTrue($response->isInvalid());
  223. }
  224. try {
  225. $response->setStatusCode(650);
  226. $this->fail();
  227. } catch(\InvalidArgumentException $e) {
  228. $this->assertTrue($response->isInvalid());
  229. }
  230. $response = new Response('', 200);
  231. $this->assertFalse($response->isInvalid());
  232. }
  233. public function testIsInformational()
  234. {
  235. $response = new Response('', 100);
  236. $this->assertTrue($response->isInformational());
  237. $response = new Response('', 200);
  238. $this->assertFalse($response->isInformational());
  239. }
  240. public function testIsRedirectRedirection()
  241. {
  242. foreach (array(301, 302, 303, 307) as $code)
  243. {
  244. $response = new Response('', $code);
  245. $this->assertTrue($response->isRedirection());
  246. $this->assertTrue($response->isRedirect());
  247. }
  248. $response = new Response('', 304);
  249. $this->assertTrue($response->isRedirection());
  250. $this->assertFalse($response->isRedirect());
  251. $response = new Response('', 200);
  252. $this->assertFalse($response->isRedirection());
  253. $this->assertFalse($response->isRedirect());
  254. $response = new Response('', 404);
  255. $this->assertFalse($response->isRedirection());
  256. $this->assertFalse($response->isRedirect());
  257. }
  258. public function testIsNotFound()
  259. {
  260. $response = new Response('', 404);
  261. $this->assertTrue($response->isNotFound());
  262. $response = new Response('', 200);
  263. $this->assertFalse($response->isNotFound());
  264. }
  265. public function testIsEmpty()
  266. {
  267. foreach (array(201, 204, 304) as $code)
  268. {
  269. $response = new Response('', $code);
  270. $this->assertTrue($response->isEmpty());
  271. }
  272. $response = new Response('', 200);
  273. $this->assertFalse($response->isEmpty());
  274. }
  275. public function testIsForbidden()
  276. {
  277. $response = new Response('', 403);
  278. $this->assertTrue($response->isForbidden());
  279. $response = new Response('', 200);
  280. $this->assertFalse($response->isForbidden());
  281. }
  282. public function testIsOk()
  283. {
  284. $response = new Response('', 200);
  285. $this->assertTrue($response->isOk());
  286. $response = new Response('', 404);
  287. $this->assertFalse($response->isOk());
  288. }
  289. public function testIsServerOrClientError()
  290. {
  291. $response = new Response('', 404);
  292. $this->assertTrue($response->isClientError());
  293. $this->assertFalse($response->isServerError());
  294. $response = new Response('', 500);
  295. $this->assertFalse($response->isClientError());
  296. $this->assertTrue($response->isServerError());
  297. }
  298. public function testHasVary()
  299. {
  300. $response = new Response();
  301. $this->assertFalse($response->hasVary());
  302. $response->setVary('User-Agent');
  303. $this->assertTrue($response->hasVary());
  304. }
  305. public function testSetEtag()
  306. {
  307. $response = new Response('', 200, array('ETag' => '"12345"'));
  308. $response->setEtag();
  309. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  310. }
  311. protected function createDateTimeOneHourAgo()
  312. {
  313. $date = new \DateTime();
  314. return $date->sub(new \DateInterval('PT1H'));
  315. }
  316. protected function createDateTimeOneHourLater()
  317. {
  318. $date = new \DateTime();
  319. return $date->add(new \DateInterval('PT1H'));
  320. }
  321. protected function createDateTimeNow()
  322. {
  323. return new \DateTime();
  324. }
  325. }