ResponseTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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\Cookie;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class ResponseTest extends ResponseTestCase
  18. {
  19. /**
  20. * @group legacy
  21. */
  22. public function testCreate()
  23. {
  24. $response = Response::create('foo', 301, ['Foo' => 'bar']);
  25. $this->assertInstanceOf(Response::class, $response);
  26. $this->assertEquals(301, $response->getStatusCode());
  27. $this->assertEquals('bar', $response->headers->get('foo'));
  28. }
  29. public function testToString()
  30. {
  31. $response = new Response();
  32. $response = explode("\r\n", $response);
  33. $this->assertEquals('HTTP/1.0 200 OK', $response[0]);
  34. $this->assertEquals('Cache-Control: no-cache, private', $response[1]);
  35. }
  36. public function testClone()
  37. {
  38. $response = new Response();
  39. $responseClone = clone $response;
  40. $this->assertEquals($response, $responseClone);
  41. }
  42. public function testSendHeaders()
  43. {
  44. $response = new Response();
  45. $headers = $response->sendHeaders();
  46. $this->assertSame($response, $headers);
  47. }
  48. public function testSend()
  49. {
  50. $response = new Response();
  51. $responseSend = $response->send();
  52. $this->assertSame($response, $responseSend);
  53. }
  54. public function testGetCharset()
  55. {
  56. $response = new Response();
  57. $charsetOrigin = 'UTF-8';
  58. $response->setCharset($charsetOrigin);
  59. $charset = $response->getCharset();
  60. $this->assertEquals($charsetOrigin, $charset);
  61. }
  62. public function testIsCacheable()
  63. {
  64. $response = new Response();
  65. $this->assertFalse($response->isCacheable());
  66. }
  67. public function testIsCacheableWithErrorCode()
  68. {
  69. $response = new Response('', 500);
  70. $this->assertFalse($response->isCacheable());
  71. }
  72. public function testIsCacheableWithNoStoreDirective()
  73. {
  74. $response = new Response();
  75. $response->headers->set('cache-control', 'private');
  76. $this->assertFalse($response->isCacheable());
  77. }
  78. public function testIsCacheableWithSetTtl()
  79. {
  80. $response = new Response();
  81. $response->setTtl(10);
  82. $this->assertTrue($response->isCacheable());
  83. }
  84. public function testMustRevalidate()
  85. {
  86. $response = new Response();
  87. $this->assertFalse($response->mustRevalidate());
  88. }
  89. public function testMustRevalidateWithMustRevalidateCacheControlHeader()
  90. {
  91. $response = new Response();
  92. $response->headers->set('cache-control', 'must-revalidate');
  93. $this->assertTrue($response->mustRevalidate());
  94. }
  95. public function testMustRevalidateWithProxyRevalidateCacheControlHeader()
  96. {
  97. $response = new Response();
  98. $response->headers->set('cache-control', 'proxy-revalidate');
  99. $this->assertTrue($response->mustRevalidate());
  100. }
  101. public function testSetNotModified()
  102. {
  103. $response = new Response('foo');
  104. $modified = $response->setNotModified();
  105. $this->assertSame($response, $modified);
  106. $this->assertEquals(304, $modified->getStatusCode());
  107. ob_start();
  108. $modified->sendContent();
  109. $string = ob_get_clean();
  110. $this->assertEmpty($string);
  111. }
  112. public function testIsSuccessful()
  113. {
  114. $response = new Response();
  115. $this->assertTrue($response->isSuccessful());
  116. }
  117. public function testIsNotModified()
  118. {
  119. $response = new Response();
  120. $modified = $response->isNotModified(new Request());
  121. $this->assertFalse($modified);
  122. }
  123. public function testIsNotModifiedNotSafe()
  124. {
  125. $request = Request::create('/homepage', 'POST');
  126. $response = new Response();
  127. $this->assertFalse($response->isNotModified($request));
  128. }
  129. public function testIsNotModifiedLastModified()
  130. {
  131. $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
  132. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  133. $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
  134. $request = new Request();
  135. $request->headers->set('If-Modified-Since', $modified);
  136. $response = new Response();
  137. $response->headers->set('Last-Modified', $modified);
  138. $this->assertTrue($response->isNotModified($request));
  139. $response->headers->set('Last-Modified', $before);
  140. $this->assertTrue($response->isNotModified($request));
  141. $response->headers->set('Last-Modified', $after);
  142. $this->assertFalse($response->isNotModified($request));
  143. $response->headers->set('Last-Modified', '');
  144. $this->assertFalse($response->isNotModified($request));
  145. }
  146. public function testIsNotModifiedEtag()
  147. {
  148. $etagOne = 'randomly_generated_etag';
  149. $etagTwo = 'randomly_generated_etag_2';
  150. $request = new Request();
  151. $request->headers->set('If-None-Match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
  152. $response = new Response();
  153. $response->headers->set('ETag', $etagOne);
  154. $this->assertTrue($response->isNotModified($request));
  155. $response->headers->set('ETag', $etagTwo);
  156. $this->assertTrue($response->isNotModified($request));
  157. $response->headers->set('ETag', '');
  158. $this->assertFalse($response->isNotModified($request));
  159. // Test wildcard
  160. $request = new Request();
  161. $request->headers->set('If-None-Match', '*');
  162. $response->headers->set('ETag', $etagOne);
  163. $this->assertTrue($response->isNotModified($request));
  164. }
  165. public function testIsNotModifiedWeakEtag()
  166. {
  167. $etag = 'randomly_generated_etag';
  168. $weakEtag = 'W/randomly_generated_etag';
  169. $request = new Request();
  170. $request->headers->set('If-None-Match', $etag);
  171. $response = new Response();
  172. $response->headers->set('ETag', $etag);
  173. $this->assertTrue($response->isNotModified($request));
  174. $response->headers->set('ETag', $weakEtag);
  175. $this->assertTrue($response->isNotModified($request));
  176. $request->headers->set('If-None-Match', $weakEtag);
  177. $response = new Response();
  178. $response->headers->set('ETag', $etag);
  179. $this->assertTrue($response->isNotModified($request));
  180. $response->headers->set('ETag', $weakEtag);
  181. $this->assertTrue($response->isNotModified($request));
  182. }
  183. public function testIsNotModifiedLastModifiedAndEtag()
  184. {
  185. $before = 'Sun, 25 Aug 2013 18:32:31 GMT';
  186. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  187. $after = 'Sun, 25 Aug 2013 19:33:31 GMT';
  188. $etag = 'randomly_generated_etag';
  189. $request = new Request();
  190. $request->headers->set('If-None-Match', sprintf('%s, %s', $etag, 'etagThree'));
  191. $request->headers->set('If-Modified-Since', $modified);
  192. $response = new Response();
  193. $response->headers->set('ETag', $etag);
  194. $response->headers->set('Last-Modified', $after);
  195. $this->assertTrue($response->isNotModified($request));
  196. $response->headers->set('ETag', 'non-existent-etag');
  197. $response->headers->set('Last-Modified', $before);
  198. $this->assertFalse($response->isNotModified($request));
  199. $response->headers->set('ETag', $etag);
  200. $response->headers->set('Last-Modified', $modified);
  201. $this->assertTrue($response->isNotModified($request));
  202. }
  203. public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
  204. {
  205. $modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
  206. $etag = 'randomly_generated_etag';
  207. $request = new Request();
  208. $request->headers->set('If-None-Match', sprintf('%s, %s', $etag, 'etagThree'));
  209. $request->headers->set('If-Modified-Since', $modified);
  210. $response = new Response();
  211. $response->headers->set('ETag', $etag);
  212. $this->assertTrue($response->isNotModified($request));
  213. $response->headers->set('ETag', 'non-existent-etag');
  214. $this->assertFalse($response->isNotModified($request));
  215. }
  216. public function testIfNoneMatchWithoutETag()
  217. {
  218. $request = new Request();
  219. $request->headers->set('If-None-Match', 'randomly_generated_etag');
  220. $this->assertFalse((new Response())->isNotModified($request));
  221. // Test wildcard
  222. $request = new Request();
  223. $request->headers->set('If-None-Match', '*');
  224. $this->assertFalse((new Response())->isNotModified($request));
  225. }
  226. public function testIsValidateable()
  227. {
  228. $response = new Response('', 200, ['Last-Modified' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);
  229. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if Last-Modified is present');
  230. $response = new Response('', 200, ['ETag' => '"12345"']);
  231. $this->assertTrue($response->isValidateable(), '->isValidateable() returns true if ETag is present');
  232. $response = new Response();
  233. $this->assertFalse($response->isValidateable(), '->isValidateable() returns false when no validator is present');
  234. }
  235. public function testGetDate()
  236. {
  237. $oneHourAgo = $this->createDateTimeOneHourAgo();
  238. $response = new Response('', 200, ['Date' => $oneHourAgo->format(\DATE_RFC2822)]);
  239. $date = $response->getDate();
  240. $this->assertEquals($oneHourAgo->getTimestamp(), $date->getTimestamp(), '->getDate() returns the Date header if present');
  241. $response = new Response();
  242. $date = $response->getDate();
  243. $this->assertEquals(time(), $date->getTimestamp(), '->getDate() returns the current Date if no Date header present');
  244. $response = new Response('', 200, ['Date' => $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822)]);
  245. $now = $this->createDateTimeNow();
  246. $response->headers->set('Date', $now->format(\DATE_RFC2822));
  247. $date = $response->getDate();
  248. $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the date when the header has been modified');
  249. $response = new Response('', 200);
  250. $now = $this->createDateTimeNow();
  251. $response->headers->remove('Date');
  252. $date = $response->getDate();
  253. $this->assertEquals($now->getTimestamp(), $date->getTimestamp(), '->getDate() returns the current Date when the header has previously been removed');
  254. }
  255. public function testGetMaxAge()
  256. {
  257. $response = new Response();
  258. $response->headers->set('Cache-Control', 's-maxage=600, max-age=0');
  259. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() uses s-maxage cache control directive when present');
  260. $response = new Response();
  261. $response->headers->set('Cache-Control', 'max-age=600');
  262. $this->assertEquals(600, $response->getMaxAge(), '->getMaxAge() falls back to max-age when no s-maxage directive present');
  263. $response = new Response();
  264. $response->headers->set('Cache-Control', 'must-revalidate');
  265. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(\DATE_RFC2822));
  266. $this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
  267. $response = new Response();
  268. $response->headers->set('Expires', -1);
  269. $this->assertSame(0, $response->getMaxAge());
  270. $response = new Response();
  271. $this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
  272. }
  273. public function testSetSharedMaxAge()
  274. {
  275. $response = new Response();
  276. $response->setSharedMaxAge(20);
  277. $cacheControl = $response->headers->get('Cache-Control');
  278. $this->assertEquals('public, s-maxage=20', $cacheControl);
  279. }
  280. public function testIsPrivate()
  281. {
  282. $response = new Response();
  283. $response->headers->set('Cache-Control', 'max-age=100');
  284. $response->setPrivate();
  285. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  286. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  287. $response = new Response();
  288. $response->headers->set('Cache-Control', 'public, max-age=100');
  289. $response->setPrivate();
  290. $this->assertEquals(100, $response->headers->getCacheControlDirective('max-age'), '->isPrivate() adds the private Cache-Control directive when set to true');
  291. $this->assertTrue($response->headers->getCacheControlDirective('private'), '->isPrivate() adds the private Cache-Control directive when set to true');
  292. $this->assertFalse($response->headers->hasCacheControlDirective('public'), '->isPrivate() removes the public Cache-Control directive');
  293. }
  294. public function testExpire()
  295. {
  296. $response = new Response();
  297. $response->headers->set('Cache-Control', 'max-age=100');
  298. $response->expire();
  299. $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present');
  300. $response = new Response();
  301. $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500');
  302. $response->expire();
  303. $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present');
  304. $response = new Response();
  305. $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500');
  306. $response->headers->set('Age', '1000');
  307. $response->expire();
  308. $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired');
  309. $response = new Response();
  310. $response->expire();
  311. $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information');
  312. $response = new Response();
  313. $response->headers->set('Expires', -1);
  314. $response->expire();
  315. $this->assertNull($response->headers->get('Age'), '->expire() does not set the Age when the response is expired');
  316. $response = new Response();
  317. $response->headers->set('Expires', date(\DATE_RFC2822, time() + 600));
  318. $response->expire();
  319. $this->assertNull($response->headers->get('Expires'), '->expire() removes the Expires header when the response is fresh');
  320. }
  321. public function testNullExpireHeader()
  322. {
  323. $response = new Response(null, 200, ['Expires' => null]);
  324. $this->assertNull($response->getExpires());
  325. }
  326. public function testGetTtl()
  327. {
  328. $response = new Response();
  329. $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
  330. $response = new Response();
  331. $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(\DATE_RFC2822));
  332. $this->assertEquals(3600, $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
  333. $response = new Response();
  334. $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822));
  335. $this->assertSame(0, $response->getTtl(), '->getTtl() returns zero when Expires is in past');
  336. $response = new Response();
  337. $response->headers->set('Expires', $response->getDate()->format(\DATE_RFC2822));
  338. $response->headers->set('Age', 0);
  339. $this->assertSame(0, $response->getTtl(), '->getTtl() correctly handles zero');
  340. $response = new Response();
  341. $response->headers->set('Cache-Control', 'max-age=60');
  342. $this->assertEquals(60, $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
  343. }
  344. public function testSetClientTtl()
  345. {
  346. $response = new Response();
  347. $response->setClientTtl(10);
  348. $this->assertEquals($response->getMaxAge(), $response->getAge() + 10);
  349. }
  350. public function testGetSetProtocolVersion()
  351. {
  352. $response = new Response();
  353. $this->assertEquals('1.0', $response->getProtocolVersion());
  354. $response->setProtocolVersion('1.1');
  355. $this->assertEquals('1.1', $response->getProtocolVersion());
  356. }
  357. public function testGetVary()
  358. {
  359. $response = new Response();
  360. $this->assertEquals([], $response->getVary(), '->getVary() returns an empty array if no Vary header is present');
  361. $response = new Response();
  362. $response->headers->set('Vary', 'Accept-Language');
  363. $this->assertEquals(['Accept-Language'], $response->getVary(), '->getVary() parses a single header name value');
  364. $response = new Response();
  365. $response->headers->set('Vary', 'Accept-Language User-Agent X-Foo');
  366. $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->getVary() parses multiple header name values separated by spaces');
  367. $response = new Response();
  368. $response->headers->set('Vary', 'Accept-Language,User-Agent, X-Foo');
  369. $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->getVary() parses multiple header name values separated by commas');
  370. $vary = ['Accept-Language', 'User-Agent', 'X-foo'];
  371. $response = new Response();
  372. $response->headers->set('Vary', $vary);
  373. $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
  374. $response = new Response();
  375. $response->headers->set('Vary', 'Accept-Language, User-Agent, X-foo');
  376. $this->assertEquals($vary, $response->getVary(), '->getVary() parses multiple header name values in arrays');
  377. }
  378. public function testSetVary()
  379. {
  380. $response = new Response();
  381. $response->setVary('Accept-Language');
  382. $this->assertEquals(['Accept-Language'], $response->getVary());
  383. $response->setVary('Accept-Language, User-Agent');
  384. $this->assertEquals(['Accept-Language', 'User-Agent'], $response->getVary(), '->setVary() replace the vary header by default');
  385. $response->setVary('X-Foo', false);
  386. $this->assertEquals(['Accept-Language', 'User-Agent', 'X-Foo'], $response->getVary(), '->setVary() doesn\'t wipe out earlier Vary headers if replace is set to false');
  387. }
  388. public function testDefaultContentType()
  389. {
  390. $response = new Response('foo');
  391. $response->prepare(new Request());
  392. $this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
  393. }
  394. public function testContentTypeCharset()
  395. {
  396. $response = new Response();
  397. $response->headers->set('Content-Type', 'text/css');
  398. // force fixContentType() to be called
  399. $response->prepare(new Request());
  400. $this->assertEquals('text/css; charset=UTF-8', $response->headers->get('Content-Type'));
  401. }
  402. public function testContentTypeIsNull()
  403. {
  404. $response = new Response('foo');
  405. $response->headers->set('Content-Type', null);
  406. $response->prepare(new Request());
  407. $this->expectNotToPerformAssertions();
  408. }
  409. public function testPrepareDoesNothingIfContentTypeIsSet()
  410. {
  411. $response = new Response('foo');
  412. $response->headers->set('Content-Type', 'text/plain');
  413. $response->prepare(new Request());
  414. $this->assertEquals('text/plain; charset=UTF-8', $response->headers->get('content-type'));
  415. }
  416. public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
  417. {
  418. $response = new Response('foo');
  419. $response->prepare(new Request());
  420. $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
  421. }
  422. /**
  423. * Same URL cannot produce different Content-Type based on the value of the Accept header,
  424. * unless explicitly stated in the response object.
  425. */
  426. public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader()
  427. {
  428. $response = new Response('foo');
  429. $request = Request::create('/');
  430. $request->headers->set('Accept', 'application/json');
  431. $response->prepare($request);
  432. $this->assertSame('text/html; charset=UTF-8', $response->headers->get('content-type'));
  433. }
  434. public function testPrepareSetContentType()
  435. {
  436. $response = new Response('foo');
  437. $request = Request::create('/');
  438. $request->setRequestFormat('json');
  439. $response->prepare($request);
  440. $this->assertEquals('application/json', $response->headers->get('content-type'));
  441. }
  442. public function testPrepareRemovesContentForHeadRequests()
  443. {
  444. $response = new Response('foo');
  445. $request = Request::create('/', 'HEAD');
  446. $length = 12345;
  447. $response->headers->set('Content-Length', $length);
  448. $response->prepare($request);
  449. $this->assertEquals('', $response->getContent());
  450. $this->assertEquals($length, $response->headers->get('Content-Length'), 'Content-Length should be as if it was GET; see RFC2616 14.13');
  451. }
  452. public function testPrepareRemovesContentForInformationalResponse()
  453. {
  454. $response = new Response('foo');
  455. $request = Request::create('/');
  456. $response->setContent('content');
  457. $response->setStatusCode(101);
  458. $response->prepare($request);
  459. $this->assertEquals('', $response->getContent());
  460. $this->assertFalse($response->headers->has('Content-Type'));
  461. $response->setContent('content');
  462. $response->setStatusCode(304);
  463. $response->prepare($request);
  464. $this->assertEquals('', $response->getContent());
  465. $this->assertFalse($response->headers->has('Content-Type'));
  466. $this->assertFalse($response->headers->has('Content-Length'));
  467. }
  468. public function testPrepareRemovesContentLength()
  469. {
  470. $response = new Response('foo');
  471. $request = Request::create('/');
  472. $response->headers->set('Content-Length', 12345);
  473. $response->prepare($request);
  474. $this->assertEquals(12345, $response->headers->get('Content-Length'));
  475. $response->headers->set('Transfer-Encoding', 'chunked');
  476. $response->prepare($request);
  477. $this->assertFalse($response->headers->has('Content-Length'));
  478. }
  479. public function testPrepareSetsPragmaOnHttp10Only()
  480. {
  481. $request = Request::create('/', 'GET');
  482. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
  483. $response = new Response('foo');
  484. $response->prepare($request);
  485. $this->assertEquals('no-cache', $response->headers->get('pragma'));
  486. $this->assertEquals('-1', $response->headers->get('expires'));
  487. $response = new Response('foo');
  488. $response->headers->remove('cache-control');
  489. $response->prepare($request);
  490. $this->assertFalse($response->headers->has('pragma'));
  491. $this->assertFalse($response->headers->has('expires'));
  492. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
  493. $response = new Response('foo');
  494. $response->prepare($request);
  495. $this->assertFalse($response->headers->has('pragma'));
  496. $this->assertFalse($response->headers->has('expires'));
  497. }
  498. public function testPrepareSetsCookiesSecure()
  499. {
  500. $cookie = Cookie::create('foo', 'bar');
  501. $response = new Response('foo');
  502. $response->headers->setCookie($cookie);
  503. $request = Request::create('/', 'GET');
  504. $response->prepare($request);
  505. $this->assertFalse($cookie->isSecure());
  506. $request = Request::create('https://localhost/', 'GET');
  507. $response->prepare($request);
  508. $this->assertTrue($cookie->isSecure());
  509. }
  510. public function testSetCache()
  511. {
  512. $response = new Response();
  513. // ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public']
  514. try {
  515. $response->setCache(['wrong option' => 'value']);
  516. $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
  517. } catch (\Exception $e) {
  518. $this->assertInstanceOf(\InvalidArgumentException::class, $e, '->setCache() throws an InvalidArgumentException if an option is not supported');
  519. $this->assertStringContainsString('"wrong option"', $e->getMessage());
  520. }
  521. $options = ['etag' => '"whatever"'];
  522. $response->setCache($options);
  523. $this->assertEquals('"whatever"', $response->getEtag());
  524. $now = $this->createDateTimeNow();
  525. $options = ['last_modified' => $now];
  526. $response->setCache($options);
  527. $this->assertEquals($now->getTimestamp(), $response->getLastModified()->getTimestamp());
  528. $options = ['max_age' => 100];
  529. $response->setCache($options);
  530. $this->assertEquals(100, $response->getMaxAge());
  531. $options = ['s_maxage' => 200];
  532. $response->setCache($options);
  533. $this->assertEquals(200, $response->getMaxAge());
  534. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  535. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  536. $response->setCache(['public' => true]);
  537. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  538. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  539. $response->setCache(['public' => false]);
  540. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  541. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  542. $response->setCache(['private' => true]);
  543. $this->assertFalse($response->headers->hasCacheControlDirective('public'));
  544. $this->assertTrue($response->headers->hasCacheControlDirective('private'));
  545. $response->setCache(['private' => false]);
  546. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  547. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  548. $response->setCache(['immutable' => true]);
  549. $this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
  550. $response->setCache(['immutable' => false]);
  551. $this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
  552. $directives = ['proxy_revalidate', 'must_revalidate', 'no_cache', 'no_store', 'no_transform'];
  553. foreach ($directives as $directive) {
  554. $response->setCache([$directive => true]);
  555. $this->assertTrue($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
  556. }
  557. foreach ($directives as $directive) {
  558. $response->setCache([$directive => false]);
  559. $this->assertFalse($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
  560. }
  561. $response = new DefaultResponse();
  562. $options = ['etag' => '"whatever"'];
  563. $response->setCache($options);
  564. $this->assertSame($response->getEtag(), '"whatever"');
  565. }
  566. public function testSendContent()
  567. {
  568. $response = new Response('test response rendering', 200);
  569. ob_start();
  570. $response->sendContent();
  571. $string = ob_get_clean();
  572. $this->assertStringContainsString('test response rendering', $string);
  573. }
  574. public function testSetPublic()
  575. {
  576. $response = new Response();
  577. $response->setPublic();
  578. $this->assertTrue($response->headers->hasCacheControlDirective('public'));
  579. $this->assertFalse($response->headers->hasCacheControlDirective('private'));
  580. }
  581. public function testSetImmutable()
  582. {
  583. $response = new Response();
  584. $response->setImmutable();
  585. $this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
  586. }
  587. public function testIsImmutable()
  588. {
  589. $response = new Response();
  590. $response->setImmutable();
  591. $this->assertTrue($response->isImmutable());
  592. }
  593. public function testSetDate()
  594. {
  595. $response = new Response();
  596. $response->setDate(\DateTime::createFromFormat(\DateTime::ATOM, '2013-01-26T09:21:56+0100', new \DateTimeZone('Europe/Berlin')));
  597. $this->assertEquals('2013-01-26T08:21:56+00:00', $response->getDate()->format(\DateTime::ATOM));
  598. }
  599. public function testSetDateWithImmutable()
  600. {
  601. $response = new Response();
  602. $response->setDate(\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2013-01-26T09:21:56+0100', new \DateTimeZone('Europe/Berlin')));
  603. $this->assertEquals('2013-01-26T08:21:56+00:00', $response->getDate()->format(\DateTime::ATOM));
  604. }
  605. public function testSetExpires()
  606. {
  607. $response = new Response();
  608. $response->setExpires(null);
  609. $this->assertNull($response->getExpires(), '->setExpires() remove the header when passed null');
  610. $now = $this->createDateTimeNow();
  611. $response->setExpires($now);
  612. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  613. }
  614. public function testSetExpiresWithImmutable()
  615. {
  616. $response = new Response();
  617. $now = $this->createDateTimeImmutableNow();
  618. $response->setExpires($now);
  619. $this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
  620. }
  621. public function testSetLastModified()
  622. {
  623. $response = new Response();
  624. $response->setLastModified($this->createDateTimeNow());
  625. $this->assertNotNull($response->getLastModified());
  626. $response->setLastModified(null);
  627. $this->assertNull($response->getLastModified());
  628. }
  629. public function testSetLastModifiedWithImmutable()
  630. {
  631. $response = new Response();
  632. $response->setLastModified($this->createDateTimeImmutableNow());
  633. $this->assertNotNull($response->getLastModified());
  634. $response->setLastModified(null);
  635. $this->assertNull($response->getLastModified());
  636. }
  637. public function testIsInvalid()
  638. {
  639. $response = new Response();
  640. try {
  641. $response->setStatusCode(99);
  642. $this->fail();
  643. } catch (\InvalidArgumentException $e) {
  644. $this->assertTrue($response->isInvalid());
  645. }
  646. try {
  647. $response->setStatusCode(650);
  648. $this->fail();
  649. } catch (\InvalidArgumentException $e) {
  650. $this->assertTrue($response->isInvalid());
  651. }
  652. $response = new Response('', 200);
  653. $this->assertFalse($response->isInvalid());
  654. }
  655. /**
  656. * @dataProvider getStatusCodeFixtures
  657. */
  658. public function testSetStatusCode($code, $text, $expectedText)
  659. {
  660. $response = new Response();
  661. $response->setStatusCode($code, $text);
  662. $statusText = new \ReflectionProperty($response, 'statusText');
  663. $statusText->setAccessible(true);
  664. $this->assertEquals($expectedText, $statusText->getValue($response));
  665. }
  666. public static function getStatusCodeFixtures()
  667. {
  668. return [
  669. ['200', null, 'OK'],
  670. ['200', false, ''],
  671. ['200', 'foo', 'foo'],
  672. ['199', null, 'unknown status'],
  673. ['199', false, ''],
  674. ['199', 'foo', 'foo'],
  675. ];
  676. }
  677. public function testIsInformational()
  678. {
  679. $response = new Response('', 100);
  680. $this->assertTrue($response->isInformational());
  681. $response = new Response('', 200);
  682. $this->assertFalse($response->isInformational());
  683. }
  684. public function testIsRedirectRedirection()
  685. {
  686. foreach ([301, 302, 303, 307] as $code) {
  687. $response = new Response('', $code);
  688. $this->assertTrue($response->isRedirection());
  689. $this->assertTrue($response->isRedirect());
  690. }
  691. $response = new Response('', 304);
  692. $this->assertTrue($response->isRedirection());
  693. $this->assertFalse($response->isRedirect());
  694. $response = new Response('', 200);
  695. $this->assertFalse($response->isRedirection());
  696. $this->assertFalse($response->isRedirect());
  697. $response = new Response('', 404);
  698. $this->assertFalse($response->isRedirection());
  699. $this->assertFalse($response->isRedirect());
  700. $response = new Response('', 301, ['Location' => '/good-uri']);
  701. $this->assertFalse($response->isRedirect('/bad-uri'));
  702. $this->assertTrue($response->isRedirect('/good-uri'));
  703. }
  704. public function testIsNotFound()
  705. {
  706. $response = new Response('', 404);
  707. $this->assertTrue($response->isNotFound());
  708. $response = new Response('', 200);
  709. $this->assertFalse($response->isNotFound());
  710. }
  711. public function testIsEmpty()
  712. {
  713. foreach ([204, 304] as $code) {
  714. $response = new Response('', $code);
  715. $this->assertTrue($response->isEmpty());
  716. }
  717. $response = new Response('', 200);
  718. $this->assertFalse($response->isEmpty());
  719. }
  720. public function testIsForbidden()
  721. {
  722. $response = new Response('', 403);
  723. $this->assertTrue($response->isForbidden());
  724. $response = new Response('', 200);
  725. $this->assertFalse($response->isForbidden());
  726. }
  727. public function testIsOk()
  728. {
  729. $response = new Response('', 200);
  730. $this->assertTrue($response->isOk());
  731. $response = new Response('', 404);
  732. $this->assertFalse($response->isOk());
  733. }
  734. public function testIsServerOrClientError()
  735. {
  736. $response = new Response('', 404);
  737. $this->assertTrue($response->isClientError());
  738. $this->assertFalse($response->isServerError());
  739. $response = new Response('', 500);
  740. $this->assertFalse($response->isClientError());
  741. $this->assertTrue($response->isServerError());
  742. }
  743. public function testHasVary()
  744. {
  745. $response = new Response();
  746. $this->assertFalse($response->hasVary());
  747. $response->setVary('User-Agent');
  748. $this->assertTrue($response->hasVary());
  749. }
  750. public function testSetEtag()
  751. {
  752. $response = new Response('', 200, ['ETag' => '"12345"']);
  753. $response->setEtag();
  754. $this->assertNull($response->headers->get('Etag'), '->setEtag() removes Etags when call with null');
  755. }
  756. /**
  757. * @dataProvider validContentProvider
  758. */
  759. public function testSetContent($content)
  760. {
  761. $response = new Response();
  762. $response->setContent($content);
  763. $this->assertEquals((string) $content, $response->getContent());
  764. }
  765. public function testSettersAreChainable()
  766. {
  767. $response = new Response();
  768. $setters = [
  769. 'setProtocolVersion' => '1.0',
  770. 'setCharset' => 'UTF-8',
  771. 'setPublic' => null,
  772. 'setPrivate' => null,
  773. 'setDate' => $this->createDateTimeNow(),
  774. 'expire' => null,
  775. 'setMaxAge' => 1,
  776. 'setSharedMaxAge' => 1,
  777. 'setTtl' => 1,
  778. 'setClientTtl' => 1,
  779. ];
  780. foreach ($setters as $setter => $arg) {
  781. $this->assertEquals($response, $response->{$setter}($arg));
  782. }
  783. }
  784. public function testNoDeprecationsAreTriggered()
  785. {
  786. new DefaultResponse();
  787. $this->createMock(Response::class);
  788. // we just need to ensure that subclasses of Response can be created without any deprecations
  789. // being triggered if the subclass does not override any final methods
  790. $this->addToAssertionCount(1);
  791. }
  792. public static function validContentProvider()
  793. {
  794. return [
  795. 'obj' => [new StringableObject()],
  796. 'string' => ['Foo'],
  797. 'int' => [2],
  798. ];
  799. }
  800. protected function createDateTimeOneHourAgo()
  801. {
  802. return $this->createDateTimeNow()->sub(new \DateInterval('PT1H'));
  803. }
  804. protected function createDateTimeOneHourLater()
  805. {
  806. return $this->createDateTimeNow()->add(new \DateInterval('PT1H'));
  807. }
  808. protected function createDateTimeNow()
  809. {
  810. $date = new \DateTime();
  811. return $date->setTimestamp(time());
  812. }
  813. protected function createDateTimeImmutableNow()
  814. {
  815. $date = new \DateTimeImmutable();
  816. return $date->setTimestamp(time());
  817. }
  818. protected function provideResponse()
  819. {
  820. return new Response();
  821. }
  822. /**
  823. * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
  824. *
  825. * @author Fábio Pacheco
  826. * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
  827. * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
  828. */
  829. public static function ianaCodesReasonPhrasesProvider()
  830. {
  831. // XML taken from https://www.iana.org/assignments/http-status-codes/http-status-codes.xml
  832. // (might not be up-to-date for older Symfony versions)
  833. $ianaHttpStatusCodes = new \DOMDocument();
  834. $ianaHttpStatusCodes->load(__DIR__.'/Fixtures/xml/http-status-codes.xml');
  835. if (!$ianaHttpStatusCodes->relaxNGValidate(__DIR__.'/schema/http-status-codes.rng')) {
  836. self::fail('Invalid IANA\'s HTTP status code list.');
  837. }
  838. $ianaCodesReasonPhrases = [];
  839. $xpath = new \DOMXPath($ianaHttpStatusCodes);
  840. $xpath->registerNamespace('ns', 'http://www.iana.org/assignments');
  841. $records = $xpath->query('//ns:record');
  842. foreach ($records as $record) {
  843. $value = $xpath->query('.//ns:value', $record)->item(0)->nodeValue;
  844. $description = $xpath->query('.//ns:description', $record)->item(0)->nodeValue;
  845. if (\in_array($description, ['Unassigned', '(Unused)'], true)) {
  846. continue;
  847. }
  848. if (preg_match('/^([0-9]+)\s*\-\s*([0-9]+)$/', $value, $matches)) {
  849. for ($value = $matches[1]; $value <= $matches[2]; ++$value) {
  850. $ianaCodesReasonPhrases[] = [$value, $description];
  851. }
  852. } else {
  853. $ianaCodesReasonPhrases[] = [$value, $description];
  854. }
  855. }
  856. return $ianaCodesReasonPhrases;
  857. }
  858. /**
  859. * @dataProvider ianaCodesReasonPhrasesProvider
  860. */
  861. public function testReasonPhraseDefaultsAgainstIana($code, $reasonPhrase)
  862. {
  863. $this->assertEquals($reasonPhrase, Response::$statusTexts[$code]);
  864. }
  865. public function testSetContentSafe()
  866. {
  867. $response = new Response();
  868. $this->assertFalse($response->headers->has('Preference-Applied'));
  869. $this->assertFalse($response->headers->has('Vary'));
  870. $response->setContentSafe();
  871. $this->assertSame('safe', $response->headers->get('Preference-Applied'));
  872. $this->assertSame('Prefer', $response->headers->get('Vary'));
  873. $response->setContentSafe(false);
  874. $this->assertFalse($response->headers->has('Preference-Applied'));
  875. $this->assertSame('Prefer', $response->headers->get('Vary'));
  876. }
  877. }
  878. class StringableObject
  879. {
  880. public function __toString(): string
  881. {
  882. return 'Foo';
  883. }
  884. }
  885. class DefaultResponse extends Response
  886. {
  887. }