ResponseTest.php 32 KB

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