ResponseTest.php 19KB

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