ResponseTest.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. /**
  257. * @dataProvider getStatusCodeFixtures
  258. */
  259. public function testSetStatusCode($code, $text, $expectedText)
  260. {
  261. $response = new Response();
  262. $response->setStatusCode($code, $text);
  263. $statusText = new \ReflectionProperty($response, 'statusText');
  264. $statusText->setAccessible(true);
  265. $this->assertEquals($expectedText, $statusText->getValue($response));
  266. }
  267. public function getStatusCodeFixtures()
  268. {
  269. return array(
  270. array('200', null, 'OK'),
  271. array('200', false, ''),
  272. array('200', 'foo', 'foo'),
  273. array('199', null, ''),
  274. array('199', false, ''),
  275. array('199', 'foo', 'foo')
  276. );
  277. }
  278. public function testIsInformational()
  279. {
  280. $response = new Response('', 100);
  281. $this->assertTrue($response->isInformational());
  282. $response = new Response('', 200);
  283. $this->assertFalse($response->isInformational());
  284. }
  285. public function testIsRedirectRedirection()
  286. {
  287. foreach (array(301, 302, 303, 307) as $code) {
  288. $response = new Response('', $code);
  289. $this->assertTrue($response->isRedirection());
  290. $this->assertTrue($response->isRedirect());
  291. }
  292. $response = new Response('', 304);
  293. $this->assertTrue($response->isRedirection());
  294. $this->assertFalse($response->isRedirect());
  295. $response = new Response('', 200);
  296. $this->assertFalse($response->isRedirection());
  297. $this->assertFalse($response->isRedirect());
  298. $response = new Response('', 404);
  299. $this->assertFalse($response->isRedirection());
  300. $this->assertFalse($response->isRedirect());
  301. $response = new Response('', 301, array('Location' => '/good-uri'));
  302. $this->assertFalse($response->isRedirect('/bad-uri'));
  303. $this->assertTrue($response->isRedirect('/good-uri'));
  304. }
  305. public function testIsNotFound()
  306. {
  307. $response = new Response('', 404);
  308. $this->assertTrue($response->isNotFound());
  309. $response = new Response('', 200);
  310. $this->assertFalse($response->isNotFound());
  311. }
  312. public function testIsEmpty()
  313. {
  314. foreach (array(201, 204, 304) as $code) {
  315. $response = new Response('', $code);
  316. $this->assertTrue($response->isEmpty());
  317. }
  318. $response = new Response('', 200);
  319. $this->assertFalse($response->isEmpty());
  320. }
  321. public function testIsForbidden()
  322. {
  323. $response = new Response('', 403);
  324. $this->assertTrue($response->isForbidden());
  325. $response = new Response('', 200);
  326. $this->assertFalse($response->isForbidden());
  327. }
  328. public function testIsOk()
  329. {
  330. $response = new Response('', 200);
  331. $this->assertTrue($response->isOk());
  332. $response = new Response('', 404);
  333. $this->assertFalse($response->isOk());
  334. }
  335. public function testIsServerOrClientError()
  336. {
  337. $response = new Response('', 404);
  338. $this->assertTrue($response->isClientError());
  339. $this->assertFalse($response->isServerError());
  340. $response = new Response('', 500);
  341. $this->assertFalse($response->isClientError());
  342. $this->assertTrue($response->isServerError());
  343. }
  344. public function testHasVary()
  345. {
  346. $response = new Response();
  347. $this->assertFalse($response->hasVary());
  348. $response->setVary('User-Agent');
  349. $this->assertTrue($response->hasVary());
  350. }
  351. public function testSetEtag()
  352. {
  353. $response = new Response('', 200, array('ETag' => '"12345"'));
  354. $response->setEtag();
  355. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  356. }
  357. /**
  358. * @dataProvider validContentProvider
  359. */
  360. public function testSetContent($content)
  361. {
  362. $response = new Response();
  363. $response->setContent($content);
  364. $this->assertEquals((string) $content, $response->getContent());
  365. }
  366. /**
  367. * @expectedException UnexpectedValueException
  368. * @dataProvider invalidContentProvider
  369. */
  370. public function testSetContentInvalid($content)
  371. {
  372. $response = new Response();
  373. $response->setContent($content);
  374. }
  375. public function validContentProvider()
  376. {
  377. return array(
  378. 'obj' => array(new StringableObject),
  379. 'string' => array('Foo'),
  380. 'int' => array(2),
  381. );
  382. }
  383. public function invalidContentProvider()
  384. {
  385. return array(
  386. 'obj' => array(new \stdClass),
  387. 'array' => array(array()),
  388. 'bool' => array(true, '1'),
  389. );
  390. }
  391. protected function createDateTimeOneHourAgo()
  392. {
  393. $date = new \DateTime();
  394. return $date->sub(new \DateInterval('PT1H'));
  395. }
  396. protected function createDateTimeOneHourLater()
  397. {
  398. $date = new \DateTime();
  399. return $date->add(new \DateInterval('PT1H'));
  400. }
  401. protected function createDateTimeNow()
  402. {
  403. return new \DateTime();
  404. }
  405. }
  406. class StringableObject
  407. {
  408. public function __toString()
  409. {
  410. return 'Foo';
  411. }
  412. }