ResponseTest.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. }
  177. $options = array('etag' => '"whatever"');
  178. $response->setCache($options);
  179. $this->assertEquals($response->getEtag(), '"whatever"');
  180. $now = new \DateTime();
  181. $options = array('last_modified' => $now);
  182. $response->setCache($options);
  183. $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
  184. $options = array('max_age' => 100);
  185. $response->setCache($options);
  186. $this->assertEquals($response->getMaxAge(), 100);
  187. $options = array('s_maxage' => 200);
  188. $response->setCache($options);
  189. $this->assertEquals($response->getMaxAge(), 200);
  190. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  191. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  192. $response->setCache(array('public' => true));
  193. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  194. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  195. $response->setCache(array('public' => false));
  196. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  197. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  198. $response->setCache(array('private' => true));
  199. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  200. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  201. $response->setCache(array('private' => false));
  202. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  203. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  204. }
  205. public function testSendContent()
  206. {
  207. $response = new Response('test response rendering', 200);
  208. ob_start();
  209. $response->sendContent();
  210. $string = ob_get_clean();
  211. $this->assertContains('test response rendering', $string);
  212. }
  213. public function testSetPublic()
  214. {
  215. $response = new Response();
  216. $response->setPublic();
  217. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  218. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  219. }
  220. public function testSetExpires()
  221. {
  222. $response = new Response();
  223. $response->setExpires(null);
  224. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  225. $now = new \DateTime();
  226. $response->setExpires($now);
  227. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  228. }
  229. public function testSetLastModified()
  230. {
  231. $response = new Response();
  232. $response->setLastModified(new \DateTime());
  233. $this->assertNotNull($response->getLastModified());
  234. $response->setLastModified(null);
  235. $this->assertNull($response->getLastModified());
  236. }
  237. public function testIsInvalid()
  238. {
  239. $response = new Response();
  240. try {
  241. $response->setStatusCode(99);
  242. $this->fail();
  243. } catch(\InvalidArgumentException $e) {
  244. $this->assertTrue($response->isInvalid());
  245. }
  246. try {
  247. $response->setStatusCode(650);
  248. $this->fail();
  249. } catch(\InvalidArgumentException $e) {
  250. $this->assertTrue($response->isInvalid());
  251. }
  252. $response = new Response('', 200);
  253. $this->assertFalse($response->isInvalid());
  254. }
  255. public function testIsInformational()
  256. {
  257. $response = new Response('', 100);
  258. $this->assertTrue($response->isInformational());
  259. $response = new Response('', 200);
  260. $this->assertFalse($response->isInformational());
  261. }
  262. public function testIsRedirectRedirection()
  263. {
  264. foreach (array(301, 302, 303, 307) as $code)
  265. {
  266. $response = new Response('', $code);
  267. $this->assertTrue($response->isRedirection());
  268. $this->assertTrue($response->isRedirect());
  269. }
  270. $response = new Response('', 304);
  271. $this->assertTrue($response->isRedirection());
  272. $this->assertFalse($response->isRedirect());
  273. $response = new Response('', 200);
  274. $this->assertFalse($response->isRedirection());
  275. $this->assertFalse($response->isRedirect());
  276. $response = new Response('', 404);
  277. $this->assertFalse($response->isRedirection());
  278. $this->assertFalse($response->isRedirect());
  279. $response = new Response('', 301, array('Location' => '/good-uri'));
  280. $this->assertFalse($response->isRedirect('/bad-uri'));
  281. $this->assertTrue($response->isRedirect('/good-uri'));
  282. }
  283. public function testIsNotFound()
  284. {
  285. $response = new Response('', 404);
  286. $this->assertTrue($response->isNotFound());
  287. $response = new Response('', 200);
  288. $this->assertFalse($response->isNotFound());
  289. }
  290. public function testIsEmpty()
  291. {
  292. foreach (array(201, 204, 304) as $code)
  293. {
  294. $response = new Response('', $code);
  295. $this->assertTrue($response->isEmpty());
  296. }
  297. $response = new Response('', 200);
  298. $this->assertFalse($response->isEmpty());
  299. }
  300. public function testIsForbidden()
  301. {
  302. $response = new Response('', 403);
  303. $this->assertTrue($response->isForbidden());
  304. $response = new Response('', 200);
  305. $this->assertFalse($response->isForbidden());
  306. }
  307. public function testIsOk()
  308. {
  309. $response = new Response('', 200);
  310. $this->assertTrue($response->isOk());
  311. $response = new Response('', 404);
  312. $this->assertFalse($response->isOk());
  313. }
  314. public function testIsServerOrClientError()
  315. {
  316. $response = new Response('', 404);
  317. $this->assertTrue($response->isClientError());
  318. $this->assertFalse($response->isServerError());
  319. $response = new Response('', 500);
  320. $this->assertFalse($response->isClientError());
  321. $this->assertTrue($response->isServerError());
  322. }
  323. public function testHasVary()
  324. {
  325. $response = new Response();
  326. $this->assertFalse($response->hasVary());
  327. $response->setVary('User-Agent');
  328. $this->assertTrue($response->hasVary());
  329. }
  330. public function testSetEtag()
  331. {
  332. $response = new Response('', 200, array('ETag' => '"12345"'));
  333. $response->setEtag();
  334. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  335. }
  336. /**
  337. * @dataProvider validContentProvider
  338. */
  339. public function testSetContent($content)
  340. {
  341. $response = new Response();
  342. $response->setContent($content);
  343. $this->assertEquals((string) $content, $response->getContent());
  344. }
  345. /**
  346. * @expectedException UnexpectedValueException
  347. * @dataProvider invalidContentProvider
  348. */
  349. public function testSetContentInvalid($content)
  350. {
  351. $response = new Response();
  352. $response->setContent($content);
  353. }
  354. public function validContentProvider()
  355. {
  356. return array(
  357. 'obj' => array(new StringableObject),
  358. 'string' => array('Foo'),
  359. 'int' => array(2),
  360. );
  361. }
  362. public function invalidContentProvider()
  363. {
  364. return array(
  365. 'obj' => array(new \stdClass),
  366. 'array' => array(array()),
  367. 'bool' => array(true, '1'),
  368. );
  369. }
  370. protected function createDateTimeOneHourAgo()
  371. {
  372. $date = new \DateTime();
  373. return $date->sub(new \DateInterval('PT1H'));
  374. }
  375. protected function createDateTimeOneHourLater()
  376. {
  377. $date = new \DateTime();
  378. return $date->add(new \DateInterval('PT1H'));
  379. }
  380. protected function createDateTimeNow()
  381. {
  382. return new \DateTime();
  383. }
  384. }
  385. class StringableObject
  386. {
  387. public function __toString()
  388. {
  389. return 'Foo';
  390. }
  391. }