ResponseTest.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 testSetSharedMaxAge()
  51. {
  52. $response = new Response();
  53. $response->setSharedMaxAge(20);
  54. $cacheControl = $response->headers->get('Cache-Control');
  55. $this->assertEquals('public, s-maxage=20', $cacheControl);
  56. }
  57. public function testIsPrivate()
  58. {
  59. $response = new Response();
  60. $response->headers->set('Cache-Control', 'max-age=100');
  61. $response->setPrivate();
  62. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  63. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  64. $response = new Response();
  65. $response->headers->set('Cache-Control', 'public, max-age=100');
  66. $response->setPrivate();
  67. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  68. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  69. $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
  70. }
  71. public function testExpire()
  72. {
  73. $response = new Response();
  74. $response->headers->set('Cache-Control', 'max-age=100');
  75. $response->expire();
  76. $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
  77. $response = new Response();
  78. $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
  79. $response->expire();
  80. $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
  81. $response = new Response();
  82. $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
  83. $response->headers->set('Age', '1000');
  84. $response->expire();
  85. $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
  86. $response = new Response();
  87. $response->expire();
  88. $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
  89. }
  90. public function testGetTtl()
  91. {
  92. $response = new Response();
  93. $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
  94. $response = new Response();
  95. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  96. $this->assertLessThan(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
  97. $response = new Response();
  98. $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
  99. $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part');
  100. $response = new Response();
  101. $response->headers->set('Cache-Control', 'max-age=60');
  102. $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
  103. }
  104. public function testSetClientTtl()
  105. {
  106. $response = new Response();
  107. $response->setClientTtl(10);
  108. $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
  109. }
  110. public function testGetSetProtocolVersion()
  111. {
  112. $response = new Response();
  113. $this->assertEquals('1.0', $response->getProtocolVersion());
  114. $response->setProtocolVersion('1.1');
  115. $this->assertEquals('1.1', $response->getProtocolVersion());
  116. }
  117. public function testGetVary()
  118. {
  119. $response = new Response();
  120. $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
  121. $response = new Response();
  122. $response->headers->set('Vary', 'Accept-Language');
  123. $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
  124. $response = new Response();
  125. $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
  126. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
  127. $response = new Response();
  128. $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
  129. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
  130. }
  131. public function testSetVary()
  132. {
  133. $response = new Response();
  134. $response->setVary('Accept-Language');
  135. $this->assertEquals(array('Accept-Language'), $response->getVary());
  136. $response->setVary('Accept-Language, User-Agent');
  137. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
  138. $response->setVary('X-Foo', false);
  139. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() doesn\'t change the Vary header if replace is set to false');
  140. }
  141. public function testDefaultContentType()
  142. {
  143. $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
  144. $headerMock->expects($this->at(0))
  145. ->method('set')
  146. ->with('Content-Type', 'text/html; charset=UTF-8');
  147. $headerMock->expects($this->at(1))
  148. ->method('set')
  149. ->with('Content-Type', 'text/html; charset=Foo');
  150. $response = new Response('foo');
  151. $response->headers = $headerMock;
  152. // verify first set()
  153. $response->__toString();
  154. $response->headers->remove('Content-Type');
  155. $response->setCharset('Foo');
  156. // verify second set()
  157. $response->__toString();
  158. }
  159. public function testContentTypeCharset()
  160. {
  161. $response = new Response();
  162. $response->headers->set('Content-Type', 'text/css');
  163. // force fixContentType() to be called
  164. $response->__toString();
  165. $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
  166. }
  167. public function testSetCache()
  168. {
  169. $response = new Response();
  170. //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
  171. try {
  172. $response->setCache(array("wrong option" => "value"));
  173. $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  174. } catch (\Exception $e) {
  175. $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
  176. $this->assertContains('"wrong option"', $e->getMessage());
  177. }
  178. $options = array('etag' => '"whatever"');
  179. $response->setCache($options);
  180. $this->assertEquals($response->getEtag(), '"whatever"');
  181. $now = new \DateTime();
  182. $options = array('last_modified' => $now);
  183. $response->setCache($options);
  184. $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
  185. $options = array('max_age' => 100);
  186. $response->setCache($options);
  187. $this->assertEquals($response->getMaxAge(), 100);
  188. $options = array('s_maxage' => 200);
  189. $response->setCache($options);
  190. $this->assertEquals($response->getMaxAge(), 200);
  191. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  192. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  193. $response->setCache(array('public' => true));
  194. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  195. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  196. $response->setCache(array('public' => false));
  197. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  198. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  199. $response->setCache(array('private' => true));
  200. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  201. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  202. $response->setCache(array('private' => false));
  203. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  204. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  205. }
  206. public function testSendContent()
  207. {
  208. $response = new Response('test response rendering', 200);
  209. ob_start();
  210. $response->sendContent();
  211. $string = ob_get_clean();
  212. $this->assertContains('test response rendering', $string);
  213. }
  214. public function testSetPublic()
  215. {
  216. $response = new Response();
  217. $response->setPublic();
  218. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  219. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  220. }
  221. public function testSetExpires()
  222. {
  223. $response = new Response();
  224. $response->setExpires(null);
  225. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  226. $now = new \DateTime();
  227. $response->setExpires($now);
  228. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  229. }
  230. public function testSetLastModified()
  231. {
  232. $response = new Response();
  233. $response->setLastModified(new \DateTime());
  234. $this->assertNotNull($response->getLastModified());
  235. $response->setLastModified(null);
  236. $this->assertNull($response->getLastModified());
  237. }
  238. public function testIsInvalid()
  239. {
  240. $response = new Response();
  241. try {
  242. $response->setStatusCode(99);
  243. $this->fail();
  244. } catch(\InvalidArgumentException $e) {
  245. $this->assertTrue($response->isInvalid());
  246. }
  247. try {
  248. $response->setStatusCode(650);
  249. $this->fail();
  250. } catch(\InvalidArgumentException $e) {
  251. $this->assertTrue($response->isInvalid());
  252. }
  253. $response = new Response('', 200);
  254. $this->assertFalse($response->isInvalid());
  255. }
  256. public function testIsInformational()
  257. {
  258. $response = new Response('', 100);
  259. $this->assertTrue($response->isInformational());
  260. $response = new Response('', 200);
  261. $this->assertFalse($response->isInformational());
  262. }
  263. public function testIsRedirectRedirection()
  264. {
  265. foreach (array(301, 302, 303, 307) as $code)
  266. {
  267. $response = new Response('', $code);
  268. $this->assertTrue($response->isRedirection());
  269. $this->assertTrue($response->isRedirect());
  270. }
  271. $response = new Response('', 304);
  272. $this->assertTrue($response->isRedirection());
  273. $this->assertFalse($response->isRedirect());
  274. $response = new Response('', 200);
  275. $this->assertFalse($response->isRedirection());
  276. $this->assertFalse($response->isRedirect());
  277. $response = new Response('', 404);
  278. $this->assertFalse($response->isRedirection());
  279. $this->assertFalse($response->isRedirect());
  280. $response = new Response('', 301, array('Location' => '/good-uri'));
  281. $this->assertFalse($response->isRedirect('/bad-uri'));
  282. $this->assertTrue($response->isRedirect('/good-uri'));
  283. }
  284. public function testIsNotFound()
  285. {
  286. $response = new Response('', 404);
  287. $this->assertTrue($response->isNotFound());
  288. $response = new Response('', 200);
  289. $this->assertFalse($response->isNotFound());
  290. }
  291. public function testIsEmpty()
  292. {
  293. foreach (array(201, 204, 304) as $code)
  294. {
  295. $response = new Response('', $code);
  296. $this->assertTrue($response->isEmpty());
  297. }
  298. $response = new Response('', 200);
  299. $this->assertFalse($response->isEmpty());
  300. }
  301. public function testIsForbidden()
  302. {
  303. $response = new Response('', 403);
  304. $this->assertTrue($response->isForbidden());
  305. $response = new Response('', 200);
  306. $this->assertFalse($response->isForbidden());
  307. }
  308. public function testIsOk()
  309. {
  310. $response = new Response('', 200);
  311. $this->assertTrue($response->isOk());
  312. $response = new Response('', 404);
  313. $this->assertFalse($response->isOk());
  314. }
  315. public function testIsServerOrClientError()
  316. {
  317. $response = new Response('', 404);
  318. $this->assertTrue($response->isClientError());
  319. $this->assertFalse($response->isServerError());
  320. $response = new Response('', 500);
  321. $this->assertFalse($response->isClientError());
  322. $this->assertTrue($response->isServerError());
  323. }
  324. public function testHasVary()
  325. {
  326. $response = new Response();
  327. $this->assertFalse($response->hasVary());
  328. $response->setVary('User-Agent');
  329. $this->assertTrue($response->hasVary());
  330. }
  331. public function testSetEtag()
  332. {
  333. $response = new Response('', 200, array('ETag' => '"12345"'));
  334. $response->setEtag();
  335. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  336. }
  337. /**
  338. * @dataProvider validContentProvider
  339. */
  340. public function testSetContent($content)
  341. {
  342. $response = new Response();
  343. $response->setContent($content);
  344. $this->assertEquals((string) $content, $response->getContent());
  345. }
  346. /**
  347. * @expectedException UnexpectedValueException
  348. * @dataProvider invalidContentProvider
  349. */
  350. public function testSetContentInvalid($content)
  351. {
  352. $response = new Response();
  353. $response->setContent($content);
  354. }
  355. public function validContentProvider()
  356. {
  357. return array(
  358. 'obj' => array(new StringableObject),
  359. 'string' => array('Foo'),
  360. 'int' => array(2),
  361. );
  362. }
  363. public function invalidContentProvider()
  364. {
  365. return array(
  366. 'obj' => array(new \stdClass),
  367. 'array' => array(array()),
  368. 'bool' => array(true, '1'),
  369. );
  370. }
  371. protected function createDateTimeOneHourAgo()
  372. {
  373. $date = new \DateTime();
  374. return $date->sub(new \DateInterval('PT1H'));
  375. }
  376. protected function createDateTimeOneHourLater()
  377. {
  378. $date = new \DateTime();
  379. return $date->add(new \DateInterval('PT1H'));
  380. }
  381. protected function createDateTimeNow()
  382. {
  383. return new \DateTime();
  384. }
  385. }
  386. class StringableObject
  387. {
  388. public function __toString()
  389. {
  390. return 'Foo';
  391. }
  392. }