No Description

ResponseTest.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. class ResponseTest extends ResponseTestCase
  14. {
  15. public function testCreate()
  16. {
  17. $response = Response::create('foo', 301, array('Foo' => 'bar'));
  18. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
  19. $this->assertEquals(301, $response->getStatusCode());
  20. $this->assertEquals('bar', $response->headers->get('foo'));
  21. }
  22. public function testToString()
  23. {
  24. $response = new Response();
  25. $response = explode("\r\n", $response);
  26. $this->assertEquals("HTTP/1.0 200 OK", $response[0]);
  27. $this->assertEquals("Cache-Control: no-cache", $response[1]);
  28. }
  29. public function testClone()
  30. {
  31. $response = new Response();
  32. $responseClone = clone $response;
  33. $this->assertEquals($response, $responseClone);
  34. }
  35. public function testSendHeaders()
  36. {
  37. $response = new Response();
  38. $headers = $response->sendHeaders();
  39. $this->assertObjectHasAttribute('headers', $headers);
  40. $this->assertObjectHasAttribute('content', $headers);
  41. $this->assertObjectHasAttribute('version', $headers);
  42. $this->assertObjectHasAttribute('statusCode', $headers);
  43. $this->assertObjectHasAttribute('statusText', $headers);
  44. $this->assertObjectHasAttribute('charset', $headers);
  45. }
  46. public function testSend()
  47. {
  48. $response = new Response();
  49. $responseSend = $response->send();
  50. $this->assertObjectHasAttribute('headers', $responseSend);
  51. $this->assertObjectHasAttribute('content', $responseSend);
  52. $this->assertObjectHasAttribute('version', $responseSend);
  53. $this->assertObjectHasAttribute('statusCode', $responseSend);
  54. $this->assertObjectHasAttribute('statusText', $responseSend);
  55. $this->assertObjectHasAttribute('charset', $responseSend);
  56. }
  57. public function testGetCharset()
  58. {
  59. $response = new Response();
  60. $charsetOrigin = 'UTF-8';
  61. $response->setCharset($charsetOrigin);
  62. $charset = $response->getCharset();
  63. $this->assertEquals($charsetOrigin, $charset);
  64. }
  65. public function testIsCacheable()
  66. {
  67. $response = new Response();
  68. $this->assertFalse($response->isCacheable());
  69. }
  70. public function testIsCacheableWithErrorCode()
  71. {
  72. $response = new Response('', 500);
  73. $this->assertFalse($response->isCacheable());
  74. }
  75. public function testIsCacheableWithNoStoreDirective()
  76. {
  77. $response = new Response();
  78. $response->headers->set('cache-control', 'private');
  79. $this->assertFalse($response->isCacheable());
  80. }
  81. public function testIsCacheableWithSetTtl()
  82. {
  83. $response = new Response();
  84. $response->setTtl(10);
  85. $this->assertTrue($response->isCacheable());
  86. }
  87. public function testMustRevalidate()
  88. {
  89. $response = new Response();
  90. $this->assertFalse($response->mustRevalidate());
  91. }
  92. public function testSetNotModified()
  93. {
  94. $response = new Response();
  95. $modified = $response->setNotModified();
  96. $this->assertObjectHasAttribute('headers', $modified);
  97. $this->assertObjectHasAttribute('content', $modified);
  98. $this->assertObjectHasAttribute('version', $modified);
  99. $this->assertObjectHasAttribute('statusCode', $modified);
  100. $this->assertObjectHasAttribute('statusText', $modified);
  101. $this->assertObjectHasAttribute('charset', $modified);
  102. $this->assertEquals(304, $modified->getStatusCode());
  103. }
  104. public function testIsSuccessful()
  105. {
  106. $response = new Response();
  107. $this->assertTrue($response->isSuccessful());
  108. }
  109. public function testIsNotModified()
  110. {
  111. $response = new Response();
  112. $modified = $response->isNotModified(new Request());
  113. $this->assertFalse($modified);
  114. }
  115. public function testIsNotModifiedNotSafe()
  116. {
  117. $request = Request::create('/homepage', 'POST');
  118. $response = new Response();
  119. $this->assertFalse($response->isNotModified($request));
  120. }
  121. public function testIsNotModifiedLastModified()
  122. {
  123. $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
  124. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  125. $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
  126. $request = new Request();
  127. $request->headers->set('If-Modified-Since', $modified);
  128. $response = new Response();
  129. $response->headers->set('Last-Modified', $modified);
  130. $this->assertTrue($response->isNotModified($request));
  131. $response->headers->set('Last-Modified', $before);
  132. $this->assertTrue($response->isNotModified($request));
  133. $response->headers->set('Last-Modified', $after);
  134. $this->assertFalse($response->isNotModified($request));
  135. $response->headers->set('Last-Modified', '');
  136. $this->assertFalse($response->isNotModified($request));
  137. }
  138. public function testIsNotModifiedEtag()
  139. {
  140. $etagOne = 'randomly_generated_etag';
  141. $etagTwo = 'randomly_generated_etag_2';
  142. $request = new Request();
  143. $request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
  144. $response = new Response();
  145. $response->headers->set('ETag', $etagOne);
  146. $this->assertTrue($response->isNotModified($request));
  147. $response->headers->set('ETag', $etagTwo);
  148. $this->assertTrue($response->isNotModified($request));
  149. $response->headers->set('ETag', '');
  150. $this->assertFalse($response->isNotModified($request));
  151. }
  152. public function testIsNotModifiedLastModifiedAndEtag()
  153. {
  154. $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
  155. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  156. $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
  157. $etag = 'randomly_generated_etag';
  158. $request = new Request();
  159. $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
  160. $request->headers->set('If-Modified-Since', $modified);
  161. $response = new Response();
  162. $response->headers->set('ETag', $etag);
  163. $response->headers->set('Last-Modified', $after);
  164. $this->assertFalse($response->isNotModified($request));
  165. $response->headers->set('ETag', 'non-existent-etag');
  166. $response->headers->set('Last-Modified', $before);
  167. $this->assertFalse($response->isNotModified($request));
  168. $response->headers->set('ETag', $etag);
  169. $response->headers->set('Last-Modified', $modified);
  170. $this->assertTrue($response->isNotModified($request));
  171. }
  172. public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
  173. {
  174. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  175. $etag = 'randomly_generated_etag';
  176. $request = new Request();
  177. $request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
  178. $request->headers->set('If-Modified-Since', $modified);
  179. $response = new Response();
  180. $response->headers->set('ETag', $etag);
  181. $this->assertTrue($response->isNotModified($request));
  182. $response->headers->set('ETag', 'non-existent-etag');
  183. $this->assertFalse($response->isNotModified($request));
  184. }
  185. public function testIsValidateable()
  186. {
  187. $response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  188. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
  189. $response = new Response('', 200, array('ETag' => '"12345"'));
  190. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
  191. $response = new Response();
  192. $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
  193. }
  194. public function testGetDate()
  195. {
  196. $oneHourAgo = $this->createDateTimeOneHourAgo();
  197. $response = new Response('', 200, array('Date' => $oneHourAgo->format(DATE_RFC2822)));
  198. $this->assertEquals(0, $oneHourAgo->diff($response->getDate())->format('%s'), '->getDate() returns the Date header if present');
  199. $response = new Response();
  200. $date = $response->getDate();
  201. $this->assertLessThan(1, $date->diff(new \DateTime(), true)->format('%s'), '->getDate() returns the current Date if no Date header present');
  202. $response = new Response('', 200, array('Date' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));
  203. $now = $this->createDateTimeNow();
  204. $response->headers->set('Date', $now->format(DATE_RFC2822));
  205. $this->assertLessThanOrEqual(1, $now->diff($response->getDate())->format('%s'), '->getDate() returns the date when the header has been modified');
  206. $response = new Response('', 200);
  207. $response->headers->remove('Date');
  208. $this->assertInstanceOf('\DateTime', $response->getDate());
  209. }
  210. public function testGetMaxAge()
  211. {
  212. $response = new Response();
  213. $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
  214. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
  215. $response = new Response();
  216. $response->headers->set('Cache-Control', 'max-age=600');
  217. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
  218. $response = new Response();
  219. $response->headers->set('Cache-Control', 'must-revalidate');
  220. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  221. $this->assertLessThanOrEqual(1, $response->getMaxAge() - 3600, '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
  222. $response = new Response();
  223. $response->headers->set('Cache-Control', 'must-revalidate');
  224. $response->headers->set('Expires', -1);
  225. $this->assertEquals('Sat, 01 Jan 00 00:00:00 +0000', $response->getExpires()->format(DATE_RFC822));
  226. $response = new Response();
  227. $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
  228. }
  229. public function testSetSharedMaxAge()
  230. {
  231. $response = new Response();
  232. $response->setSharedMaxAge(20);
  233. $cacheControl = $response->headers->get('Cache-Control');
  234. $this->assertEquals('public, s-maxage=20', $cacheControl);
  235. }
  236. public function testIsPrivate()
  237. {
  238. $response = new Response();
  239. $response->headers->set('Cache-Control', 'max-age=100');
  240. $response->setPrivate();
  241. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  242. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  243. $response = new Response();
  244. $response->headers->set('Cache-Control', 'public, max-age=100');
  245. $response->setPrivate();
  246. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  247. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  248. $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
  249. }
  250. public function testExpire()
  251. {
  252. $response = new Response();
  253. $response->headers->set('Cache-Control', 'max-age=100');
  254. $response->expire();
  255. $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
  256. $response = new Response();
  257. $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
  258. $response->expire();
  259. $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
  260. $response = new Response();
  261. $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
  262. $response->headers->set('Age', '1000');
  263. $response->expire();
  264. $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
  265. $response = new Response();
  266. $response->expire();
  267. $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
  268. $response = new Response();
  269. $response->headers->set('Expires', -1);
  270. $response->expire();
  271. $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
  272. }
  273. public function testGetTtl()
  274. {
  275. $response = new Response();
  276. $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
  277. $response = new Response();
  278. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
  279. $this->assertLessThanOrEqual(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
  280. $response = new Response();
  281. $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
  282. $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
  283. $response = new Response();
  284. $response->headers->set('Expires', $response->getDate()->format(DATE_RFC2822));
  285. $response->headers->set('Age', 0);
  286. $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
  287. $response = new Response();
  288. $response->headers->set('Cache-Control', 'max-age=60');
  289. $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
  290. }
  291. public function testSetClientTtl()
  292. {
  293. $response = new Response();
  294. $response->setClientTtl(10);
  295. $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
  296. }
  297. public function testGetSetProtocolVersion()
  298. {
  299. $response = new Response();
  300. $this->assertEquals('1.0', $response->getProtocolVersion());
  301. $response->setProtocolVersion('1.1');
  302. $this->assertEquals('1.1', $response->getProtocolVersion());
  303. }
  304. public function testGetVary()
  305. {
  306. $response = new Response();
  307. $this->assertEquals(array(), $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
  308. $response = new Response();
  309. $response->headers->set('Vary', 'Accept-Language');
  310. $this->assertEquals(array('Accept-Language'), $response->getVary(), '->getVary() parses a single header name value');
  311. $response = new Response();
  312. $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
  313. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
  314. $response = new Response();
  315. $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
  316. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->getVary() parses multiple header name values separated by commas');
  317. $vary = array('Accept-Language', 'User-Agent', 'X-foo');
  318. $response = new Response();
  319. $response->headers->set('Vary', $vary);
  320. $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
  321. $response = new Response();
  322. $response->headers->set('Vary', 'Accept-Language, User-Agent, X-foo');
  323. $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
  324. }
  325. public function testSetVary()
  326. {
  327. $response = new Response();
  328. $response->setVary('Accept-Language');
  329. $this->assertEquals(array('Accept-Language'), $response->getVary());
  330. $response->setVary('Accept-Language, User-Agent');
  331. $this->assertEquals(array('Accept-Language', 'User-Agent'), $response->getVary(), '->setVary() replace the vary header by default');
  332. $response->setVary('X-Foo', false);
  333. $this->assertEquals(array('Accept-Language', 'User-Agent', 'X-Foo'), $response->getVary(), '->setVary() doesn\'t wipe out earlier Vary headers if replace is set to false');
  334. }
  335. public function testDefaultContentType()
  336. {
  337. $headerMock = $this->getMock('Symfony\Component\HttpFoundation\ResponseHeaderBag', array('set'));
  338. $headerMock->expects($this->at(0))
  339. ->method('set')
  340. ->with('Content-Type', 'text/html');
  341. $headerMock->expects($this->at(1))
  342. ->method('set')
  343. ->with('Content-Type', 'text/html; charset=UTF-8');
  344. $response = new Response('foo');
  345. $response->headers = $headerMock;
  346. $response->prepare(new Request());
  347. }
  348. public function testContentTypeCharset()
  349. {
  350. $response = new Response();
  351. $response->headers->set('Content-Type', 'text/css');
  352. // force fixContentType() to be called
  353. $response->prepare(new Request());
  354. $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
  355. }
  356. public function testPrepareDoesNothingIfContentTypeIsSet()
  357. {
  358. $response = new Response('foo');
  359. $response->headers->set('Content-Type', 'text/plain');
  360. $response->prepare(new Request());
  361. $this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
  362. }
  363. public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
  364. {
  365. $response = new Response('foo');
  366. $response->prepare(new Request());
  367. $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
  368. }
  369. public function testPrepareSetContentType()
  370. {
  371. $response = new Response('foo');
  372. $request = Request::create('/');
  373. $request->setRequestFormat('json');
  374. $response->prepare($request);
  375. $this->assertEquals('application/json', $response->headers->get('content-type'));
  376. }
  377. public function testPrepareRemovesContentForHeadRequests()
  378. {
  379. $response = new Response('foo');
  380. $request = Request::create('/', 'HEAD');
  381. $length = 12345;
  382. $response->headers->set('Content-Length', $length);
  383. $response->prepare($request);
  384. $this->assertEquals('', $response->getContent());
  385. $this->assertEquals($length, $response->headers->get('Content-Length'), 'Content-Length should be as if it was GET; see RFC2616 14.13');
  386. }
  387. public function testPrepareRemovesContentForInformationalResponse()
  388. {
  389. $response = new Response('foo');
  390. $request = Request::create('/');
  391. $response->setContent('content');
  392. $response->setStatusCode(101);
  393. $response->prepare($request);
  394. $this->assertEquals('', $response->getContent());
  395. $this->assertFalse($response->headers->has('Content-Type'));
  396. $this->assertFalse($response->headers->has('Content-Type'));
  397. $response->setContent('content');
  398. $response->setStatusCode(304);
  399. $response->prepare($request);
  400. $this->assertEquals('', $response->getContent());
  401. $this->assertFalse($response->headers->has('Content-Type'));
  402. $this->assertFalse($response->headers->has('Content-Length'));
  403. }
  404. public function testPrepareRemovesContentLength()
  405. {
  406. $response = new Response('foo');
  407. $request = Request::create('/');
  408. $response->headers->set('Content-Length', 12345);
  409. $response->prepare($request);
  410. $this->assertEquals(12345, $response->headers->get('Content-Length'));
  411. $response->headers->set('Transfer-Encoding', 'chunked');
  412. $response->prepare($request);
  413. $this->assertFalse($response->headers->has('Content-Length'));
  414. }
  415. public function testPrepareSetsPragmaOnHttp10Only()
  416. {
  417. $request = Request::create('/', 'GET');
  418. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
  419. $response = new Response('foo');
  420. $response->prepare($request);
  421. $this->assertEquals('no-cache', $response->headers->get('pragma'));
  422. $this->assertEquals('-1', $response->headers->get('expires'));
  423. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
  424. $response = new Response('foo');
  425. $response->prepare($request);
  426. $this->assertFalse($response->headers->has('pragma'));
  427. $this->assertFalse($response->headers->has('expires'));
  428. }
  429. public function testSetCache()
  430. {
  431. $response = new Response();
  432. //array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
  433. try {
  434. $response->setCache(array("wrong option" => "value"));
  435. $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  436. } catch (\Exception $e) {
  437. $this->assertInstanceOf('InvalidArgumentException', $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
  438. $this->assertContains('"wrong option"', $e->getMessage());
  439. }
  440. $options = array('etag' => '"whatever"');
  441. $response->setCache($options);
  442. $this->assertEquals($response->getEtag(), '"whatever"');
  443. $now = new \DateTime();
  444. $options = array('last_modified' => $now);
  445. $response->setCache($options);
  446. $this->assertEquals($response->getLastModified()->getTimestamp(), $now->getTimestamp());
  447. $options = array('max_age' => 100);
  448. $response->setCache($options);
  449. $this->assertEquals($response->getMaxAge(), 100);
  450. $options = array('s_maxage' => 200);
  451. $response->setCache($options);
  452. $this->assertEquals($response->getMaxAge(), 200);
  453. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  454. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  455. $response->setCache(array('public' => true));
  456. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  457. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  458. $response->setCache(array('public' => false));
  459. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  460. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  461. $response->setCache(array('private' => true));
  462. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  463. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  464. $response->setCache(array('private' => false));
  465. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  466. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  467. }
  468. public function testSendContent()
  469. {
  470. $response = new Response('test response rendering', 200);
  471. ob_start();
  472. $response->sendContent();
  473. $string = ob_get_clean();
  474. $this->assertContains('test response rendering', $string);
  475. }
  476. public function testSetPublic()
  477. {
  478. $response = new Response();
  479. $response->setPublic();
  480. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  481. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  482. }
  483. public function testSetExpires()
  484. {
  485. $response = new Response();
  486. $response->setExpires(null);
  487. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  488. $now = new \DateTime();
  489. $response->setExpires($now);
  490. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  491. }
  492. public function testSetLastModified()
  493. {
  494. $response = new Response();
  495. $response->setLastModified(new \DateTime());
  496. $this->assertNotNull($response->getLastModified());
  497. $response->setLastModified(null);
  498. $this->assertNull($response->getLastModified());
  499. }
  500. public function testIsInvalid()
  501. {
  502. $response = new Response();
  503. try {
  504. $response->setStatusCode(99);
  505. $this->fail();
  506. } catch (\InvalidArgumentException $e) {
  507. $this->assertTrue($response->isInvalid());
  508. }
  509. try {
  510. $response->setStatusCode(650);
  511. $this->fail();
  512. } catch (\InvalidArgumentException $e) {
  513. $this->assertTrue($response->isInvalid());
  514. }
  515. $response = new Response('', 200);
  516. $this->assertFalse($response->isInvalid());
  517. }
  518. /**
  519. * @dataProvider getStatusCodeFixtures
  520. */
  521. public function testSetStatusCode($code, $text, $expectedText)
  522. {
  523. $response = new Response();
  524. $response->setStatusCode($code, $text);
  525. $statusText = new \ReflectionProperty($response, 'statusText');
  526. $statusText->setAccessible(true);
  527. $this->assertEquals($expectedText, $statusText->getValue($response));
  528. }
  529. public function getStatusCodeFixtures()
  530. {
  531. return array(
  532. array('200', null, 'OK'),
  533. array('200', false, ''),
  534. array('200', 'foo', 'foo'),
  535. array('199', null, ''),
  536. array('199', false, ''),
  537. array('199', 'foo', 'foo'),
  538. );
  539. }
  540. public function testIsInformational()
  541. {
  542. $response = new Response('', 100);
  543. $this->assertTrue($response->isInformational());
  544. $response = new Response('', 200);
  545. $this->assertFalse($response->isInformational());
  546. }
  547. public function testIsRedirectRedirection()
  548. {
  549. foreach (array(301, 302, 303, 307) as $code) {
  550. $response = new Response('', $code);
  551. $this->assertTrue($response->isRedirection());
  552. $this->assertTrue($response->isRedirect());
  553. }
  554. $response = new Response('', 304);
  555. $this->assertTrue($response->isRedirection());
  556. $this->assertFalse($response->isRedirect());
  557. $response = new Response('', 200);
  558. $this->assertFalse($response->isRedirection());
  559. $this->assertFalse($response->isRedirect());
  560. $response = new Response('', 404);
  561. $this->assertFalse($response->isRedirection());
  562. $this->assertFalse($response->isRedirect());
  563. $response = new Response('', 301, array('Location' => '/good-uri'));
  564. $this->assertFalse($response->isRedirect('/bad-uri'));
  565. $this->assertTrue($response->isRedirect('/good-uri'));
  566. }
  567. public function testIsNotFound()
  568. {
  569. $response = new Response('', 404);
  570. $this->assertTrue($response->isNotFound());
  571. $response = new Response('', 200);
  572. $this->assertFalse($response->isNotFound());
  573. }
  574. public function testIsEmpty()
  575. {
  576. foreach (array(204, 304) as $code) {
  577. $response = new Response('', $code);
  578. $this->assertTrue($response->isEmpty());
  579. }
  580. $response = new Response('', 200);
  581. $this->assertFalse($response->isEmpty());
  582. }
  583. public function testIsForbidden()
  584. {
  585. $response = new Response('', 403);
  586. $this->assertTrue($response->isForbidden());
  587. $response = new Response('', 200);
  588. $this->assertFalse($response->isForbidden());
  589. }
  590. public function testIsOk()
  591. {
  592. $response = new Response('', 200);
  593. $this->assertTrue($response->isOk());
  594. $response = new Response('', 404);
  595. $this->assertFalse($response->isOk());
  596. }
  597. public function testIsServerOrClientError()
  598. {
  599. $response = new Response('', 404);
  600. $this->assertTrue($response->isClientError());
  601. $this->assertFalse($response->isServerError());
  602. $response = new Response('', 500);
  603. $this->assertFalse($response->isClientError());
  604. $this->assertTrue($response->isServerError());
  605. }
  606. public function testHasVary()
  607. {
  608. $response = new Response();
  609. $this->assertFalse($response->hasVary());
  610. $response->setVary('User-Agent');
  611. $this->assertTrue($response->hasVary());
  612. }
  613. public function testSetEtag()
  614. {
  615. $response = new Response('', 200, array('ETag' => '"12345"'));
  616. $response->setEtag();
  617. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  618. }
  619. /**
  620. * @dataProvider validContentProvider
  621. */
  622. public function testSetContent($content)
  623. {
  624. $response = new Response();
  625. $response->setContent($content);
  626. $this->assertEquals((string) $content, $response->getContent());
  627. }
  628. /**
  629. * @expectedException \UnexpectedValueException
  630. * @dataProvider invalidContentProvider
  631. */
  632. public function testSetContentInvalid($content)
  633. {
  634. $response = new Response();
  635. $response->setContent($content);
  636. }
  637. public function testSettersAreChainable()
  638. {
  639. $response = new Response();
  640. $setters = array(
  641. 'setProtocolVersion' => '1.0',
  642. 'setCharset' => 'UTF-8',
  643. 'setPublic' => null,
  644. 'setPrivate' => null,
  645. 'setDate' => new \DateTime(),
  646. 'expire' => null,
  647. 'setMaxAge' => 1,
  648. 'setSharedMaxAge' => 1,
  649. 'setTtl' => 1,
  650. 'setClientTtl' => 1,
  651. );
  652. foreach ($setters as $setter => $arg) {
  653. $this->assertEquals($response, $response->{$setter}($arg));
  654. }
  655. }
  656. public function validContentProvider()
  657. {
  658. return array(
  659. 'obj' => array(new StringableObject()),
  660. 'string' => array('Foo'),
  661. 'int' => array(2),
  662. );
  663. }
  664. public function invalidContentProvider()
  665. {
  666. return array(
  667. 'obj' => array(new \stdClass()),
  668. 'array' => array(array()),
  669. 'bool' => array(true, '1'),
  670. );
  671. }
  672. protected function createDateTimeOneHourAgo()
  673. {
  674. $date = new \DateTime();
  675. return $date->sub(new \DateInterval('PT1H'));
  676. }
  677. protected function createDateTimeOneHourLater()
  678. {
  679. $date = new \DateTime();
  680. return $date->add(new \DateInterval('PT1H'));
  681. }
  682. protected function createDateTimeNow()
  683. {
  684. return new \DateTime();
  685. }
  686. protected function provideResponse()
  687. {
  688. return new Response();
  689. }
  690. }
  691. class StringableObject
  692. {
  693. public function __toString()
  694. {
  695. return 'Foo';
  696. }
  697. }