ResponseTest.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. include_once CORE_TEST_CASES . DS . 'Http' . DS . 'server_mocks.php';
  18. use Cake\Core\Configure;
  19. use Cake\Http\Cookie\Cookie;
  20. use Cake\Http\Cookie\CookieCollection;
  21. use Cake\Http\CorsBuilder;
  22. use Cake\Http\Exception\NotFoundException;
  23. use Cake\Http\Response;
  24. use Cake\Http\ServerRequest;
  25. use Cake\I18n\FrozenTime;
  26. use Cake\TestSuite\TestCase;
  27. use Laminas\Diactoros\Stream;
  28. /**
  29. * ResponseTest
  30. */
  31. class ResponseTest extends TestCase
  32. {
  33. /**
  34. * SERVER variable backup.
  35. *
  36. * @var array
  37. */
  38. protected $server = [];
  39. /**
  40. * setup
  41. *
  42. * @return void
  43. */
  44. public function setUp(): void
  45. {
  46. parent::setUp();
  47. $this->server = $_SERVER;
  48. }
  49. /**
  50. * teardown
  51. *
  52. * @return void
  53. */
  54. public function tearDown(): void
  55. {
  56. parent::tearDown();
  57. $_SERVER = $this->server;
  58. unset($GLOBALS['mockedHeadersSent']);
  59. }
  60. /**
  61. * Tests the request object constructor
  62. *
  63. * @return void
  64. */
  65. public function testConstruct()
  66. {
  67. $response = new Response();
  68. $this->assertSame('', (string)$response->getBody());
  69. $this->assertSame('UTF-8', $response->getCharset());
  70. $this->assertSame('text/html', $response->getType());
  71. $this->assertSame('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  72. $this->assertSame(200, $response->getStatusCode());
  73. $options = [
  74. 'body' => 'This is the body',
  75. 'charset' => 'my-custom-charset',
  76. 'type' => 'mp3',
  77. 'status' => 203,
  78. ];
  79. $response = new Response($options);
  80. $this->assertSame('This is the body', (string)$response->getBody());
  81. $this->assertSame('my-custom-charset', $response->getCharset());
  82. $this->assertSame('audio/mpeg', $response->getType());
  83. $this->assertSame('audio/mpeg', $response->getHeaderLine('Content-Type'));
  84. $this->assertSame(203, $response->getStatusCode());
  85. }
  86. /**
  87. * Tests the getCharset/withCharset methods
  88. *
  89. * @return void
  90. */
  91. public function testWithCharset()
  92. {
  93. $response = new Response();
  94. $this->assertSame('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  95. $new = $response->withCharset('iso-8859-1');
  96. $this->assertStringNotContainsString('iso', $response->getHeaderLine('Content-Type'), 'Old instance not changed');
  97. $this->assertSame('iso-8859-1', $new->getCharset());
  98. $this->assertSame('text/html; charset=iso-8859-1', $new->getHeaderLine('Content-Type'));
  99. }
  100. /**
  101. * Tests the getType method
  102. *
  103. * @return void
  104. */
  105. public function testGetType()
  106. {
  107. $response = new Response();
  108. $this->assertSame('text/html', $response->getType());
  109. $this->assertSame(
  110. 'application/pdf',
  111. $response->withType('pdf')->getType()
  112. );
  113. $this->assertSame(
  114. 'custom/stuff',
  115. $response->withType('custom/stuff')->getType()
  116. );
  117. $this->assertSame(
  118. 'application/json',
  119. $response->withType('json')->getType()
  120. );
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testSetTypeMap()
  126. {
  127. $response = new Response();
  128. $response->setTypeMap('ical', 'text/calendar');
  129. $response = $response->withType('ical')->getType();
  130. $this->assertSame('text/calendar', $response);
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testSetTypeMapAsArray()
  136. {
  137. $response = new Response();
  138. $response->setTypeMap('ical', ['text/calendar']);
  139. $response = $response->withType('ical')->getType();
  140. $this->assertSame('text/calendar', $response);
  141. }
  142. /**
  143. * Tests the withType method
  144. *
  145. * @return void
  146. */
  147. public function testWithTypeAlias()
  148. {
  149. $response = new Response();
  150. $this->assertSame(
  151. 'text/html; charset=UTF-8',
  152. $response->getHeaderLine('Content-Type'),
  153. 'Default content-type should match'
  154. );
  155. $new = $response->withType('pdf');
  156. $this->assertNotSame($new, $response, 'Should be a new instance');
  157. $this->assertSame(
  158. 'text/html; charset=UTF-8',
  159. $response->getHeaderLine('Content-Type'),
  160. 'Original object should not be modified'
  161. );
  162. $this->assertSame('application/pdf', $new->getHeaderLine('Content-Type'));
  163. $json = $new->withType('json');
  164. $this->assertSame('application/json', $json->getHeaderLine('Content-Type'));
  165. $this->assertSame('application/json', $json->getType());
  166. }
  167. /**
  168. * test withType() and full mime-types
  169. *
  170. * @return void
  171. */
  172. public function withTypeFull()
  173. {
  174. $response = new Response();
  175. $this->assertSame(
  176. 'application/json',
  177. $response->withType('application/json')->getHeaderLine('Content-Type'),
  178. 'Should not add charset to explicit type'
  179. );
  180. $this->assertSame(
  181. 'custom/stuff',
  182. $response->withType('custom/stuff')->getHeaderLine('Content-Type'),
  183. 'Should allow arbitrary types'
  184. );
  185. $this->assertSame(
  186. 'text/html; charset=UTF-8',
  187. $response->withType('text/html; charset=UTF-8')->getHeaderLine('Content-Type'),
  188. 'Should allow charset types'
  189. );
  190. }
  191. /**
  192. * Test that an invalid type raises an exception
  193. *
  194. * @return void
  195. */
  196. public function testWithTypeInvalidType()
  197. {
  198. $this->expectException(\InvalidArgumentException::class);
  199. $this->expectExceptionMessage('"beans" is an invalid content type');
  200. $response = new Response();
  201. $response->withType('beans');
  202. }
  203. /**
  204. * Data provider for content type tests.
  205. *
  206. * @return array
  207. */
  208. public static function charsetTypeProvider()
  209. {
  210. return [
  211. ['mp3', 'audio/mpeg'],
  212. ['js', 'application/javascript; charset=UTF-8'],
  213. ['xml', 'application/xml; charset=UTF-8'],
  214. ['txt', 'text/plain; charset=UTF-8'],
  215. ];
  216. }
  217. /**
  218. * Test that setting certain status codes clears the status code.
  219. *
  220. * @return void
  221. */
  222. public function testWithStatusClearsContentType()
  223. {
  224. $response = new Response();
  225. $new = $response->withType('pdf')
  226. ->withStatus(204);
  227. $this->assertFalse($new->hasHeader('Content-Type'));
  228. $this->assertSame('', $new->getType());
  229. $this->assertSame(204, $new->getStatusCode(), 'Status code should clear content-type');
  230. $response = new Response();
  231. $new = $response->withStatus(304)
  232. ->withType('pdf');
  233. $this->assertSame('', $new->getType());
  234. $this->assertFalse(
  235. $new->hasHeader('Content-Type'),
  236. 'Type should not be retained because of status code.'
  237. );
  238. $response = new Response();
  239. $new = $response
  240. ->withHeader('Content-Type', 'application/json')
  241. ->withStatus(204);
  242. $this->assertFalse($new->hasHeader('Content-Type'), 'Should clear direct header');
  243. $this->assertSame('', $new->getType());
  244. }
  245. /**
  246. * Test that setting status codes doesn't overwrite content-type
  247. *
  248. * @return void
  249. */
  250. public function testWithStatusDoesNotChangeContentType()
  251. {
  252. $response = new Response();
  253. $new = $response->withHeader('Content-Type', 'application/json')
  254. ->withStatus(403);
  255. $this->assertSame('application/json', $new->getHeaderLine('Content-Type'));
  256. $this->assertSame(403, $new->getStatusCode());
  257. $response = new Response();
  258. $new = $response->withStatus(403)
  259. ->withHeader('Content-Type', 'application/json');
  260. $this->assertSame('application/json', $new->getHeaderLine('Content-Type'));
  261. $this->assertSame(403, $new->getStatusCode());
  262. $this->assertSame('application/json', $new->getType());
  263. }
  264. /**
  265. * Tests the withDisabledCache method
  266. *
  267. * @return void
  268. */
  269. public function testWithDisabledCache()
  270. {
  271. $response = new Response();
  272. $expected = [
  273. 'Expires' => ['Mon, 26 Jul 1997 05:00:00 GMT'],
  274. 'Last-Modified' => [gmdate('D, d M Y H:i:s') . ' GMT'],
  275. 'Cache-Control' => ['no-store, no-cache, must-revalidate, post-check=0, pre-check=0'],
  276. 'Content-Type' => ['text/html; charset=UTF-8'],
  277. ];
  278. $new = $response->withDisabledCache();
  279. $this->assertFalse($response->hasHeader('Expires'), 'Old instance not mutated.');
  280. $this->assertEquals($expected, $new->getHeaders());
  281. }
  282. /**
  283. * Tests the withCache method
  284. *
  285. * @return void
  286. */
  287. public function testWithCache()
  288. {
  289. $response = new Response();
  290. $since = $time = time();
  291. $new = $response->withCache($since, $time);
  292. $this->assertFalse($response->hasHeader('Date'));
  293. $this->assertFalse($response->hasHeader('Last-Modified'));
  294. $this->assertSame(gmdate('D, j M Y G:i:s ', $since) . 'GMT', $new->getHeaderLine('Date'));
  295. $this->assertSame(gmdate('D, j M Y H:i:s ', $since) . 'GMT', $new->getHeaderLine('Last-Modified'));
  296. $this->assertSame(gmdate('D, j M Y H:i:s', $time) . ' GMT', $new->getHeaderLine('Expires'));
  297. $this->assertSame('public, max-age=0', $new->getHeaderLine('Cache-Control'));
  298. }
  299. /**
  300. * Tests the compress method
  301. *
  302. * @return void
  303. */
  304. public function testCompress()
  305. {
  306. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  307. $response = new Response();
  308. if (ini_get('zlib.output_compression') === '1' || !extension_loaded('zlib')) {
  309. $this->assertFalse($response->compress());
  310. $this->markTestSkipped('Is not possible to test output compression');
  311. }
  312. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  313. $result = $response->compress();
  314. $this->assertFalse($result);
  315. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  316. $result = $response->compress();
  317. $this->assertTrue($result);
  318. $this->assertContains('ob_gzhandler', ob_list_handlers());
  319. ob_get_clean();
  320. }
  321. /**
  322. * Tests the withDownload method
  323. *
  324. * @return void
  325. */
  326. public function testWithDownload()
  327. {
  328. $response = new Response();
  329. $new = $response->withDownload('myfile.mp3');
  330. $this->assertFalse($response->hasHeader('Content-Disposition'), 'No mutation');
  331. $expected = 'attachment; filename="myfile.mp3"';
  332. $this->assertSame($expected, $new->getHeaderLine('Content-Disposition'));
  333. }
  334. /**
  335. * Tests the mapType method
  336. *
  337. * @return void
  338. */
  339. public function testMapType()
  340. {
  341. $response = new Response();
  342. $this->assertSame('wav', $response->mapType('audio/x-wav'));
  343. $this->assertSame('pdf', $response->mapType('application/pdf'));
  344. $this->assertSame('xml', $response->mapType('text/xml'));
  345. $this->assertSame('html', $response->mapType('*/*'));
  346. $this->assertSame('csv', $response->mapType('application/vnd.ms-excel'));
  347. $expected = ['json', 'xhtml', 'css'];
  348. $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
  349. $this->assertEquals($expected, $result);
  350. }
  351. /**
  352. * Tests the outputCompressed method
  353. *
  354. * @return void
  355. */
  356. public function testOutputCompressed()
  357. {
  358. $response = new Response();
  359. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  360. $result = $response->outputCompressed();
  361. $this->assertFalse($result);
  362. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  363. $result = $response->outputCompressed();
  364. $this->assertFalse($result);
  365. if (!extension_loaded('zlib')) {
  366. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  367. }
  368. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  369. if (ini_get('zlib.output_compression') !== '1') {
  370. ob_start('ob_gzhandler');
  371. }
  372. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  373. $result = $response->outputCompressed();
  374. $this->assertTrue($result);
  375. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  376. $result = $response->outputCompressed();
  377. $this->assertFalse($result);
  378. if (ini_get('zlib.output_compression') !== '1') {
  379. ob_get_clean();
  380. }
  381. }
  382. /**
  383. * Tests settings the content length
  384. *
  385. * @return void
  386. */
  387. public function testWithLength()
  388. {
  389. $response = new Response();
  390. $this->assertFalse($response->hasHeader('Content-Length'));
  391. $new = $response->withLength(100);
  392. $this->assertFalse($response->hasHeader('Content-Length'), 'Old instance not modified');
  393. $this->assertSame('100', $new->getHeaderLine('Content-Length'));
  394. }
  395. /**
  396. * Tests settings the link
  397. *
  398. * @return void
  399. */
  400. public function testWithAddedLink()
  401. {
  402. $response = new Response();
  403. $this->assertFalse($response->hasHeader('Link'));
  404. $new = $response->withAddedLink('http://example.com', ['rel' => 'prev']);
  405. $this->assertFalse($response->hasHeader('Link'), 'Old instance not modified');
  406. $this->assertSame('<http://example.com>; rel="prev"', $new->getHeaderLine('Link'));
  407. $new = $response->withAddedLink('http://example.com');
  408. $this->assertSame('<http://example.com>', $new->getHeaderLine('Link'));
  409. $new = $response->withAddedLink('http://example.com?p=1', ['rel' => 'prev'])
  410. ->withAddedLink('http://example.com?p=2', ['rel' => 'next', 'foo' => 'bar']);
  411. $this->assertSame('<http://example.com?p=1>; rel="prev",<http://example.com?p=2>; rel="next"; foo="bar"', $new->getHeaderLine('Link'));
  412. }
  413. /**
  414. * Tests the withExpires method
  415. *
  416. * @return void
  417. */
  418. public function testWithExpires()
  419. {
  420. $format = 'D, j M Y H:i:s';
  421. $response = new Response();
  422. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  423. $new = $response->withExpires($now);
  424. $this->assertFalse($response->hasHeader('Expires'));
  425. $now->setTimeZone(new \DateTimeZone('UTC'));
  426. $this->assertSame($now->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  427. $now = time();
  428. $new = $response->withExpires($now);
  429. $this->assertSame(gmdate($format) . ' GMT', $new->getHeaderLine('Expires'));
  430. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  431. $new = $response->withExpires('+1 day');
  432. $this->assertSame($time->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  433. }
  434. /**
  435. * Tests the withModified method
  436. *
  437. * @return void
  438. */
  439. public function testWithModified()
  440. {
  441. $format = 'D, j M Y H:i:s';
  442. $response = new Response();
  443. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  444. $new = $response->withModified($now);
  445. $this->assertFalse($response->hasHeader('Last-Modified'));
  446. $now->setTimeZone(new \DateTimeZone('UTC'));
  447. $this->assertSame($now->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  448. $now = time();
  449. $new = $response->withModified($now);
  450. $this->assertSame(gmdate($format, $now) . ' GMT', $new->getHeaderLine('Last-Modified'));
  451. $now = new \DateTimeImmutable();
  452. $new = $response->withModified($now);
  453. $this->assertSame(gmdate($format, $now->getTimestamp()) . ' GMT', $new->getHeaderLine('Last-Modified'));
  454. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  455. $new = $response->withModified('+1 day');
  456. $this->assertSame($time->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  457. }
  458. /**
  459. * Tests withSharable()
  460. *
  461. * @return void
  462. */
  463. public function testWithSharable()
  464. {
  465. $response = new Response();
  466. $new = $response->withSharable(true);
  467. $this->assertFalse($response->hasHeader('Cache-Control'), 'old instance unchanged');
  468. $this->assertSame('public', $new->getHeaderLine('Cache-Control'));
  469. $new = $response->withSharable(false);
  470. $this->assertSame('private', $new->getHeaderLine('Cache-Control'));
  471. $new = $response->withSharable(true, 3600);
  472. $this->assertSame('public, max-age=3600', $new->getHeaderLine('Cache-Control'));
  473. $new = $response->withSharable(false, 3600);
  474. $this->assertSame('private, max-age=3600', $new->getHeaderLine('Cache-Control'));
  475. }
  476. /**
  477. * Tests withMaxAge()
  478. *
  479. * @return void
  480. */
  481. public function testWithMaxAge()
  482. {
  483. $response = new Response();
  484. $this->assertFalse($response->hasHeader('Cache-Control'));
  485. $new = $response->withMaxAge(3600);
  486. $this->assertSame('max-age=3600', $new->getHeaderLine('Cache-Control'));
  487. $new = $response->withMaxAge(3600)
  488. ->withSharable(false);
  489. $this->assertSame('max-age=3600, private', $new->getHeaderLine('Cache-Control'));
  490. }
  491. /**
  492. * Tests setting of s-maxage Cache-Control directive
  493. *
  494. * @return void
  495. */
  496. public function testWithSharedMaxAge()
  497. {
  498. $response = new Response();
  499. $new = $response->withSharedMaxAge(3600);
  500. $this->assertFalse($response->hasHeader('Cache-Control'));
  501. $this->assertSame('s-maxage=3600', $new->getHeaderLine('Cache-Control'));
  502. $new = $response->withSharedMaxAge(3600)->withSharable(true);
  503. $this->assertSame('s-maxage=3600, public', $new->getHeaderLine('Cache-Control'));
  504. }
  505. /**
  506. * Tests setting of must-revalidate Cache-Control directive
  507. *
  508. * @return void
  509. */
  510. public function testWithMustRevalidate()
  511. {
  512. $response = new Response();
  513. $this->assertFalse($response->hasHeader('Cache-Control'));
  514. $new = $response->withMustRevalidate(true);
  515. $this->assertFalse($response->hasHeader('Cache-Control'));
  516. $this->assertSame('must-revalidate', $new->getHeaderLine('Cache-Control'));
  517. $new = $new->withMustRevalidate(false);
  518. $this->assertEmpty($new->getHeaderLine('Cache-Control'));
  519. }
  520. /**
  521. * Tests withVary()
  522. *
  523. * @return void
  524. */
  525. public function testWithVary()
  526. {
  527. $response = new Response();
  528. $new = $response->withVary('Accept-encoding');
  529. $this->assertFalse($response->hasHeader('Vary'));
  530. $this->assertSame('Accept-encoding', $new->getHeaderLine('Vary'));
  531. $new = $response->withVary(['Accept-encoding', 'Accept-language']);
  532. $this->assertFalse($response->hasHeader('Vary'));
  533. $this->assertSame('Accept-encoding,Accept-language', $new->getHeaderLine('Vary'));
  534. }
  535. /**
  536. * Tests withEtag()
  537. *
  538. * @return void
  539. */
  540. public function testWithEtag()
  541. {
  542. $response = new Response();
  543. $new = $response->withEtag('something');
  544. $this->assertFalse($response->hasHeader('Etag'));
  545. $this->assertSame('"something"', $new->getHeaderLine('Etag'));
  546. $new = $response->withEtag('something', true);
  547. $this->assertSame('W/"something"', $new->getHeaderLine('Etag'));
  548. }
  549. /**
  550. * Tests that the response is able to be marked as not modified
  551. *
  552. * @return void
  553. */
  554. public function testNotModified()
  555. {
  556. $response = new Response();
  557. $response = $response->withStringBody('something')
  558. ->withStatus(200)
  559. ->withLength(100)
  560. ->withModified('now');
  561. $response->notModified();
  562. $this->assertFalse($response->hasHeader('Content-Length'));
  563. $this->assertFalse($response->hasHeader('Modified'));
  564. $this->assertEmpty((string)$response->getBody());
  565. $this->assertSame(304, $response->getStatusCode());
  566. }
  567. /**
  568. * Tests withNotModified()
  569. *
  570. * @return void
  571. */
  572. public function testWithNotModified()
  573. {
  574. $response = new Response(['body' => 'something']);
  575. $response = $response->withLength(100)
  576. ->withStatus(200)
  577. ->withHeader('Last-Modified', 'value')
  578. ->withHeader('Content-Language', 'en-EN')
  579. ->withHeader('X-things', 'things')
  580. ->withType('application/json');
  581. $new = $response->withNotModified();
  582. $this->assertTrue($response->hasHeader('Content-Language'), 'old instance not changed');
  583. $this->assertTrue($response->hasHeader('Content-Length'), 'old instance not changed');
  584. $this->assertFalse($new->hasHeader('Content-Type'));
  585. $this->assertFalse($new->hasHeader('Content-Length'));
  586. $this->assertFalse($new->hasHeader('Content-Language'));
  587. $this->assertFalse($new->hasHeader('Last-Modified'));
  588. $this->assertSame('things', $new->getHeaderLine('X-things'), 'Other headers are retained');
  589. $this->assertSame(304, $new->getStatusCode());
  590. $this->assertSame('', $new->getBody()->getContents());
  591. }
  592. /**
  593. * Test checkNotModified method
  594. *
  595. * @return void
  596. */
  597. public function testCheckNotModifiedByEtagStar()
  598. {
  599. $request = new ServerRequest();
  600. $request = $request->withHeader('If-None-Match', '*');
  601. $response = new Response();
  602. $response = $response->withEtag('something')
  603. ->withHeader('Content-Length', 99);
  604. $this->assertTrue($response->checkNotModified($request));
  605. $this->assertFalse($response->hasHeader('Content-Type'), 'etags match, should be unmodified');
  606. }
  607. /**
  608. * Test checkNotModified method
  609. *
  610. * @return void
  611. */
  612. public function testCheckNotModifiedByEtagExact()
  613. {
  614. $request = new ServerRequest();
  615. $request = $request->withHeader('If-None-Match', 'W/"something", "other"');
  616. $response = new Response();
  617. $response = $response->withEtag('something', true)
  618. ->withHeader('Content-Length', 99);
  619. $this->assertTrue($response->checkNotModified($request));
  620. $this->assertFalse($response->hasHeader('Content-Type'), 'etags match, should be unmodified');
  621. }
  622. /**
  623. * Test checkNotModified method
  624. *
  625. * @return void
  626. */
  627. public function testCheckNotModifiedByEtagAndTime()
  628. {
  629. $request = new ServerRequest();
  630. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  631. ->withHeader('If-None-Match', 'W/"something", "other"');
  632. $response = new Response();
  633. $response = $response->withModified('2012-01-01 00:00:00')
  634. ->withEtag('something', true)
  635. ->withHeader('Content-Length', 99);
  636. $this->assertTrue($response->checkNotModified($request));
  637. $this->assertFalse($response->hasHeader('Content-Length'), 'etags match, should be unmodified');
  638. }
  639. /**
  640. * Test checkNotModified method
  641. *
  642. * @return void
  643. */
  644. public function testCheckNotModifiedByEtagAndTimeMismatch()
  645. {
  646. $request = new ServerRequest();
  647. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  648. ->withHeader('If-None-Match', 'W/"something", "other"');
  649. $response = new Response();
  650. $response = $response->withModified('2012-01-01 00:00:01')
  651. ->withEtag('something', true)
  652. ->withHeader('Content-Length', 99);
  653. $this->assertFalse($response->checkNotModified($request));
  654. $this->assertTrue($response->hasHeader('Content-Length'), 'timestamp in response is newer');
  655. }
  656. /**
  657. * Test checkNotModified method
  658. *
  659. * @return void
  660. */
  661. public function testCheckNotModifiedByEtagMismatch()
  662. {
  663. $request = new ServerRequest();
  664. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  665. ->withHeader('If-None-Match', 'W/"something-else", "other"');
  666. $response = new Response();
  667. $response = $response->withModified('2012-01-01 00:00:00')
  668. ->withEtag('something', true)
  669. ->withHeader('Content-Length', 99);
  670. $this->assertFalse($response->checkNotModified($request));
  671. $this->assertTrue($response->hasHeader('Content-Length'), 'etags do not match');
  672. }
  673. /**
  674. * Test checkNotModified method
  675. *
  676. * @return void
  677. */
  678. public function testCheckNotModifiedByTime()
  679. {
  680. $request = new ServerRequest();
  681. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
  682. $response = new Response();
  683. $response = $response->withModified('2012-01-01 00:00:00')
  684. ->withHeader('Content-Length', 99);
  685. $this->assertTrue($response->checkNotModified($request));
  686. $this->assertFalse($response->hasHeader('Content-Length'), 'modified time matches');
  687. }
  688. /**
  689. * Test checkNotModified method
  690. *
  691. * @return void
  692. */
  693. public function testCheckNotModifiedNoHints()
  694. {
  695. $request = new ServerRequest();
  696. $request = $request->withHeader('If-None-Match', 'W/"something", "other"')
  697. ->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
  698. $response = new Response();
  699. $this->assertFalse($response->checkNotModified($request));
  700. $this->assertSame(200, $response->getStatusCode());
  701. }
  702. /**
  703. * Test setting cookies with no value
  704. *
  705. * @return void
  706. */
  707. public function testWithCookieEmpty()
  708. {
  709. $response = new Response();
  710. $new = $response->withCookie(new Cookie('testing'));
  711. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  712. $expected = [
  713. 'name' => 'testing',
  714. 'value' => '',
  715. 'expires' => 0,
  716. 'path' => '/',
  717. 'domain' => '',
  718. 'secure' => false,
  719. 'httponly' => false,
  720. ];
  721. $result = $new->getCookie('testing');
  722. $this->assertEquals($expected, $result);
  723. }
  724. /**
  725. * Test setting cookies with scalar values
  726. *
  727. * @return void
  728. */
  729. public function testWithCookieScalar()
  730. {
  731. $response = new Response();
  732. $new = $response->withCookie(new Cookie('testing', 'abc123'));
  733. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  734. $this->assertSame('abc123', $new->getCookie('testing')['value']);
  735. $new = $response->withCookie(new Cookie('testing', 99));
  736. $this->assertSame(99, $new->getCookie('testing')['value']);
  737. $new = $response->withCookie(new Cookie('testing', false));
  738. $this->assertFalse($new->getCookie('testing')['value']);
  739. $new = $response->withCookie(new Cookie('testing', true));
  740. $this->assertTrue($new->getCookie('testing')['value']);
  741. }
  742. /**
  743. * Test withCookie() and duplicate data
  744. *
  745. * @return void
  746. * @throws \Exception
  747. */
  748. public function testWithDuplicateCookie()
  749. {
  750. $expiry = new \DateTimeImmutable('+24 hours');
  751. $response = new Response();
  752. $cookie = new Cookie(
  753. 'testing',
  754. '[a,b,c]',
  755. $expiry,
  756. '/test',
  757. '',
  758. true
  759. );
  760. $new = $response->withCookie($cookie);
  761. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  762. $expected = [
  763. 'name' => 'testing',
  764. 'value' => '[a,b,c]',
  765. 'expires' => $expiry,
  766. 'path' => '/test',
  767. 'domain' => '',
  768. 'secure' => true,
  769. 'httponly' => false,
  770. ];
  771. // Match the date time formatting to Response::convertCookieToArray
  772. $expected['expires'] = $expiry->format('U');
  773. $this->assertEquals($expected, $new->getCookie('testing'));
  774. }
  775. /**
  776. * Test withCookie() and a cookie instance
  777. *
  778. * @return void
  779. */
  780. public function testWithCookieObject()
  781. {
  782. $response = new Response();
  783. $cookie = new Cookie('yay', 'a value');
  784. $new = $response->withCookie($cookie);
  785. $this->assertNull($response->getCookie('yay'), 'withCookie does not mutate');
  786. $this->assertNotEmpty($new->getCookie('yay'));
  787. $this->assertSame($cookie, $new->getCookieCollection()->get('yay'));
  788. }
  789. public function testWithExpiredCookieScalar()
  790. {
  791. $response = new Response();
  792. $response = $response->withCookie(new Cookie('testing', 'abc123'));
  793. $this->assertSame('abc123', $response->getCookie('testing')['value']);
  794. $new = $response->withExpiredCookie(new Cookie('testing'));
  795. $this->assertSame(0, $response->getCookie('testing')['expires']);
  796. $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$new->getCookie('testing')['expires']);
  797. }
  798. /**
  799. * @throws \Exception If DateImmutable emits an error.
  800. */
  801. public function testWithExpiredCookieOptions()
  802. {
  803. $options = [
  804. 'name' => 'testing',
  805. 'value' => 'abc123',
  806. 'options' => [
  807. 'domain' => 'cakephp.org',
  808. 'path' => '/custompath/',
  809. 'secure' => true,
  810. 'httponly' => true,
  811. 'expires' => new \DateTimeImmutable('+14 days'),
  812. ],
  813. ];
  814. $cookie = Cookie::create(
  815. $options['name'],
  816. $options['value'],
  817. $options['options']
  818. );
  819. $response = new Response();
  820. $response = $response->withCookie($cookie);
  821. $options['options']['expires'] = $options['options']['expires']->format('U');
  822. $expected = ['name' => $options['name'], 'value' => $options['value']] + $options['options'];
  823. $this->assertEquals($expected, $response->getCookie('testing'));
  824. $expiredCookie = $response->withExpiredCookie($cookie);
  825. $this->assertSame($expected['expires'], (string)$response->getCookie('testing')['expires']);
  826. $this->assertLessThan(FrozenTime::createFromTimestamp(1), (string)$expiredCookie->getCookie('testing')['expires']);
  827. }
  828. /**
  829. * @return void
  830. */
  831. public function testWithExpiredCookieObject()
  832. {
  833. $response = new Response();
  834. $cookie = new Cookie('yay', 'a value');
  835. $response = $response->withCookie($cookie);
  836. $this->assertSame('a value', $response->getCookie('yay')['value']);
  837. $new = $response->withExpiredCookie($cookie);
  838. $this->assertSame(0, $response->getCookie('yay')['expires']);
  839. $this->assertSame(1, $new->getCookie('yay')['expires']);
  840. }
  841. /**
  842. * Test getCookies() and array data.
  843. *
  844. * @return void
  845. */
  846. public function testGetCookies()
  847. {
  848. $response = new Response();
  849. $new = $response->withCookie(new Cookie('testing', 'a'))
  850. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  851. $expected = [
  852. 'testing' => [
  853. 'name' => 'testing',
  854. 'value' => 'a',
  855. 'expires' => 0,
  856. 'path' => '/',
  857. 'domain' => '',
  858. 'secure' => false,
  859. 'httponly' => false,
  860. ],
  861. 'test2' => [
  862. 'name' => 'test2',
  863. 'value' => 'b',
  864. 'expires' => 0,
  865. 'path' => '/test',
  866. 'domain' => '',
  867. 'secure' => true,
  868. 'httponly' => false,
  869. ],
  870. ];
  871. $this->assertEquals($expected, $new->getCookies());
  872. }
  873. /**
  874. * Test getCookies() and array data.
  875. *
  876. * @return void
  877. */
  878. public function testGetCookiesArrayValue()
  879. {
  880. $response = new Response();
  881. $cookie = (new Cookie('urmc'))
  882. ->withValue(['user_id' => 1, 'token' => 'abc123'])
  883. ->withHttpOnly(true);
  884. $new = $response->withCookie($cookie);
  885. $expected = [
  886. 'urmc' => [
  887. 'name' => 'urmc',
  888. 'value' => '{"user_id":1,"token":"abc123"}',
  889. 'expires' => 0,
  890. 'path' => '/',
  891. 'domain' => '',
  892. 'secure' => false,
  893. 'httponly' => true,
  894. ],
  895. ];
  896. $this->assertEquals($expected, $new->getCookies());
  897. }
  898. /**
  899. * Test getCookieCollection() as array data
  900. *
  901. * @return void
  902. */
  903. public function testGetCookieCollection()
  904. {
  905. $response = new Response();
  906. $new = $response->withCookie(new Cookie('testing', 'a'))
  907. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  908. $cookies = $response->getCookieCollection();
  909. $this->assertInstanceOf(CookieCollection::class, $cookies);
  910. $this->assertCount(0, $cookies, 'Original response not mutated');
  911. $cookies = $new->getCookieCollection();
  912. $this->assertInstanceOf(CookieCollection::class, $cookies);
  913. $this->assertCount(2, $cookies);
  914. $this->assertTrue($cookies->has('testing'));
  915. $this->assertTrue($cookies->has('test2'));
  916. }
  917. /**
  918. * Test withCookieCollection()
  919. *
  920. * @return void
  921. */
  922. public function testWithCookieCollection()
  923. {
  924. $response = new Response();
  925. $collection = new CookieCollection([new Cookie('foo', 'bar')]);
  926. $newResponse = $response->withCookieCollection($collection);
  927. $this->assertNotSame($response, $newResponse);
  928. $this->assertNotSame($response->getCookieCollection(), $newResponse->getCookieCollection());
  929. $this->assertSame($newResponse->getCookie('foo')['value'], 'bar');
  930. }
  931. /**
  932. * Test that cors() returns a builder.
  933. *
  934. * @return void
  935. */
  936. public function testCors()
  937. {
  938. $request = new ServerRequest([
  939. 'environment' => ['HTTP_ORIGIN' => 'http://example.com'],
  940. ]);
  941. $response = new Response();
  942. $builder = $response->cors($request);
  943. $this->assertInstanceOf(CorsBuilder::class, $builder);
  944. $this->assertSame($response, $builder->build(), 'Empty builder returns same object');
  945. }
  946. /**
  947. * test withFile() not found
  948. *
  949. * @return void
  950. */
  951. public function testWithFileNotFound()
  952. {
  953. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  954. $this->expectExceptionMessage('The requested file /some/missing/folder/file.jpg was not found');
  955. $response = new Response();
  956. $response->withFile('/some/missing/folder/file.jpg');
  957. }
  958. /**
  959. * test withFile() not found
  960. *
  961. * @return void
  962. */
  963. public function testWithFileNotFoundNoDebug()
  964. {
  965. Configure::write('debug', 0);
  966. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  967. $this->expectExceptionMessage('The requested file was not found');
  968. $response = new Response();
  969. $response->withFile('/some/missing/folder/file.jpg');
  970. }
  971. /**
  972. * Provider for various kinds of unacceptable files.
  973. *
  974. * @return array
  975. */
  976. public function invalidFileProvider()
  977. {
  978. return [
  979. ['my/../cat.gif', 'The requested file contains `..` and will not be read.'],
  980. ['my\..\cat.gif', 'The requested file contains `..` and will not be read.'],
  981. ['my/ca..t.gif', 'my/ca..t.gif was not found or not readable'],
  982. ['my/ca..t/image.gif', 'my/ca..t/image.gif was not found or not readable'],
  983. ];
  984. }
  985. /**
  986. * test withFile and invalid paths
  987. *
  988. * @dataProvider invalidFileProvider
  989. * @return void
  990. */
  991. public function testWithFileInvalidPath($path, $expectedMessage)
  992. {
  993. $this->expectException(NotFoundException::class);
  994. $this->expectExceptionMessage($expectedMessage);
  995. $response = new Response();
  996. $response->withFile($path);
  997. }
  998. /**
  999. * test withFile() + download & name
  1000. *
  1001. * @return void
  1002. */
  1003. public function testWithFileDownloadAndName()
  1004. {
  1005. $response = new Response();
  1006. $new = $response->withFile(
  1007. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1008. [
  1009. 'name' => 'something_special.css',
  1010. 'download' => true,
  1011. ]
  1012. );
  1013. $this->assertSame(
  1014. 'text/html; charset=UTF-8',
  1015. $response->getHeaderLine('Content-Type'),
  1016. 'No mutation'
  1017. );
  1018. $this->assertSame(
  1019. 'text/css; charset=UTF-8',
  1020. $new->getHeaderLine('Content-Type')
  1021. );
  1022. $this->assertSame(
  1023. 'attachment; filename="something_special.css"',
  1024. $new->getHeaderLine('Content-Disposition')
  1025. );
  1026. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1027. $this->assertSame('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1028. $body = $new->getBody();
  1029. $this->assertInstanceOf('Laminas\Diactoros\Stream', $body);
  1030. $expected = '/* this is the test asset css file */';
  1031. $this->assertSame($expected, trim($body->getContents()));
  1032. $file = $new->getFile()->openFile();
  1033. $this->assertSame($expected, trim($file->fread(100)));
  1034. }
  1035. /**
  1036. * test withFile() + a generic agent
  1037. *
  1038. * @return void
  1039. */
  1040. public function testWithFileUnknownFileTypeGeneric()
  1041. {
  1042. $response = new Response();
  1043. $new = $response->withFile(CONFIG . 'no_section.ini');
  1044. $this->assertSame('text/html; charset=UTF-8', $new->getHeaderLine('Content-Type'));
  1045. $this->assertSame(
  1046. 'attachment; filename="no_section.ini"',
  1047. $new->getHeaderLine('Content-Disposition')
  1048. );
  1049. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1050. $body = $new->getBody();
  1051. $expected = "some_key = some_value\nbool_key = 1\n";
  1052. $this->assertSame($expected, $body->getContents());
  1053. }
  1054. /**
  1055. * test withFile() + opera
  1056. *
  1057. * @return void
  1058. */
  1059. public function testWithFileUnknownFileTypeOpera()
  1060. {
  1061. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1062. $response = new Response();
  1063. $new = $response->withFile(CONFIG . 'no_section.ini');
  1064. $this->assertSame('application/octet-stream', $new->getHeaderLine('Content-Type'));
  1065. $this->assertSame(
  1066. 'attachment; filename="no_section.ini"',
  1067. $new->getHeaderLine('Content-Disposition')
  1068. );
  1069. }
  1070. /**
  1071. * test withFile() + old IE
  1072. *
  1073. * @return void
  1074. */
  1075. public function testWithFileUnknownFileTypeOldIe()
  1076. {
  1077. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)';
  1078. $response = new Response();
  1079. $new = $response->withFile(CONFIG . 'no_section.ini');
  1080. $this->assertSame('application/force-download', $new->getHeaderLine('Content-Type'));
  1081. }
  1082. /**
  1083. * test withFile() + no download
  1084. *
  1085. * @return void
  1086. */
  1087. public function testWithFileNoDownload()
  1088. {
  1089. $response = new Response();
  1090. $new = $response->withFile(CONFIG . 'no_section.ini', [
  1091. 'download' => false,
  1092. ]);
  1093. $this->assertSame(
  1094. 'text/html; charset=UTF-8',
  1095. $new->getHeaderLine('Content-Type')
  1096. );
  1097. $this->assertFalse($new->hasHeader('Content-Disposition'));
  1098. $this->assertFalse($new->hasHeader('Content-Transfer-Encoding'));
  1099. }
  1100. /**
  1101. * Test that uppercase extensions result in correct content-types
  1102. *
  1103. * @return void
  1104. */
  1105. public function testWithFileUpperExtension()
  1106. {
  1107. $response = new Response();
  1108. $new = $response->withFile(TEST_APP . 'vendor/img/test_2.JPG');
  1109. $this->assertSame('image/jpeg', $new->getHeaderLine('Content-Type'));
  1110. }
  1111. /**
  1112. * A data provider for testing various ranges
  1113. *
  1114. * @return array
  1115. */
  1116. public static function rangeProvider()
  1117. {
  1118. return [
  1119. // suffix-byte-range
  1120. [
  1121. 'bytes=-25', 25, 'bytes 13-37/38',
  1122. ],
  1123. [
  1124. 'bytes=0-', 38, 'bytes 0-37/38',
  1125. ],
  1126. [
  1127. 'bytes=10-', 28, 'bytes 10-37/38',
  1128. ],
  1129. [
  1130. 'bytes=10-20', 11, 'bytes 10-20/38',
  1131. ],
  1132. // Spaced out
  1133. [
  1134. 'bytes = 10 - 20', 11, 'bytes 10-20/38',
  1135. ],
  1136. ];
  1137. }
  1138. /**
  1139. * Test withFile() & the various range offset types.
  1140. *
  1141. * @dataProvider rangeProvider
  1142. * @return void
  1143. */
  1144. public function testWithFileRangeOffsets($range, $length, $offsetResponse)
  1145. {
  1146. $_SERVER['HTTP_RANGE'] = $range;
  1147. $response = new Response();
  1148. $new = $response->withFile(
  1149. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1150. ['download' => true]
  1151. );
  1152. $this->assertSame(
  1153. 'attachment; filename="test_asset.css"',
  1154. $new->getHeaderLine('Content-Disposition')
  1155. );
  1156. $this->assertSame('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1157. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1158. $this->assertEquals($length, $new->getHeaderLine('Content-Length'));
  1159. $this->assertEquals($offsetResponse, $new->getHeaderLine('Content-Range'));
  1160. }
  1161. /**
  1162. * Test withFile() fetching ranges from a file.
  1163. *
  1164. * @return void
  1165. */
  1166. public function testWithFileRange()
  1167. {
  1168. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1169. $response = new Response();
  1170. $new = $response->withFile(
  1171. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1172. ['download' => true]
  1173. );
  1174. $this->assertSame(
  1175. 'attachment; filename="test_asset.css"',
  1176. $new->getHeaderLine('Content-Disposition')
  1177. );
  1178. $this->assertSame('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1179. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1180. $this->assertSame('18', $new->getHeaderLine('Content-Length'));
  1181. $this->assertSame('bytes 8-25/38', $new->getHeaderLine('Content-Range'));
  1182. $this->assertSame(206, $new->getStatusCode());
  1183. }
  1184. /**
  1185. * Provider for invalid range header values.
  1186. *
  1187. * @return array
  1188. */
  1189. public function invalidFileRangeProvider()
  1190. {
  1191. return [
  1192. // malformed range
  1193. [
  1194. 'bytes=0,38',
  1195. ],
  1196. // malformed punctuation
  1197. [
  1198. 'bytes: 0 - 38',
  1199. ],
  1200. ];
  1201. }
  1202. /**
  1203. * Test withFile() and invalid ranges
  1204. *
  1205. * @dataProvider invalidFileRangeProvider
  1206. * @return void
  1207. */
  1208. public function testWithFileInvalidRange($range)
  1209. {
  1210. $_SERVER['HTTP_RANGE'] = $range;
  1211. $response = new Response();
  1212. $new = $response->withFile(
  1213. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1214. ['download' => true]
  1215. );
  1216. $this->assertSame(
  1217. 'attachment; filename="test_asset.css"',
  1218. $new->getHeaderLine('Content-Disposition')
  1219. );
  1220. $this->assertSame('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1221. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1222. $this->assertSame('38', $new->getHeaderLine('Content-Length'));
  1223. $this->assertSame('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  1224. $this->assertSame(206, $new->getStatusCode());
  1225. }
  1226. /**
  1227. * Test withFile() and a reversed range
  1228. *
  1229. * @return void
  1230. */
  1231. public function testWithFileReversedRange()
  1232. {
  1233. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1234. $response = new Response();
  1235. $new = $response->withFile(
  1236. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1237. ['download' => true]
  1238. );
  1239. $this->assertSame(
  1240. 'attachment; filename="test_asset.css"',
  1241. $new->getHeaderLine('Content-Disposition')
  1242. );
  1243. $this->assertSame('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1244. $this->assertSame('bytes', $new->getHeaderLine('Accept-Ranges'));
  1245. $this->assertSame('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  1246. $this->assertSame(416, $new->getStatusCode());
  1247. }
  1248. /**
  1249. * Test the withLocation method.
  1250. *
  1251. * @return void
  1252. */
  1253. public function testWithLocation()
  1254. {
  1255. $response = new Response();
  1256. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set.');
  1257. $new = $response->withLocation('http://example.org');
  1258. $this->assertNotSame($new, $response);
  1259. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set');
  1260. $this->assertSame('http://example.org', $new->getHeaderLine('Location'), 'Header should be set');
  1261. $this->assertSame(302, $new->getStatusCode(), 'Status should be updated');
  1262. }
  1263. /**
  1264. * Test get protocol version.
  1265. *
  1266. * @return void
  1267. */
  1268. public function getProtocolVersion()
  1269. {
  1270. $response = new Response();
  1271. $version = $response->getProtocolVersion();
  1272. $this->assertSame('1.1', $version);
  1273. }
  1274. /**
  1275. * Test with protocol.
  1276. *
  1277. * @return void
  1278. */
  1279. public function testWithProtocol()
  1280. {
  1281. $response = new Response();
  1282. $version = $response->getProtocolVersion();
  1283. $this->assertSame('1.1', $version);
  1284. $response2 = $response->withProtocolVersion('1.0');
  1285. $version = $response2->getProtocolVersion();
  1286. $this->assertSame('1.0', $version);
  1287. $version = $response->getProtocolVersion();
  1288. $this->assertSame('1.1', $version);
  1289. $this->assertNotEquals($response, $response2);
  1290. }
  1291. /**
  1292. * Test with status code.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testWithStatusCode()
  1297. {
  1298. $response = new Response();
  1299. $statusCode = $response->getStatusCode();
  1300. $this->assertSame(200, $statusCode);
  1301. $response2 = $response->withStatus(404);
  1302. $statusCode = $response2->getStatusCode();
  1303. $this->assertSame(404, $statusCode);
  1304. $statusCode = $response->getStatusCode();
  1305. $this->assertSame(200, $statusCode);
  1306. $this->assertNotEquals($response, $response2);
  1307. $response3 = $response->withStatus(111);
  1308. $this->assertSame(111, $response3->getStatusCode());
  1309. $this->assertSame('', $response3->getReasonPhrase());
  1310. }
  1311. /**
  1312. * Test invalid status codes
  1313. *
  1314. * @return void
  1315. */
  1316. public function testWithStatusInvalid()
  1317. {
  1318. $this->expectException(\InvalidArgumentException::class);
  1319. $this->expectExceptionMessage('Invalid status code: 1001. Use a valid HTTP status code in range 1xx - 5xx.');
  1320. $response = new Response();
  1321. $response->withStatus(1001);
  1322. }
  1323. /**
  1324. * Test get reason phrase.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testGetReasonPhrase()
  1329. {
  1330. $response = new Response();
  1331. $this->assertSame('OK', $response->getReasonPhrase());
  1332. $response = $response->withStatus(404);
  1333. $reasonPhrase = $response->getReasonPhrase();
  1334. $this->assertSame('Not Found', $reasonPhrase);
  1335. }
  1336. /**
  1337. * Test with body.
  1338. *
  1339. * @return void
  1340. */
  1341. public function testWithBody()
  1342. {
  1343. $response = new Response();
  1344. $body = $response->getBody();
  1345. $body->rewind();
  1346. $result = $body->getContents();
  1347. $this->assertSame('', $result);
  1348. $stream = new Stream('php://memory', 'wb+');
  1349. $stream->write('test1');
  1350. $response2 = $response->withBody($stream);
  1351. $body = $response2->getBody();
  1352. $body->rewind();
  1353. $result = $body->getContents();
  1354. $this->assertSame('test1', $result);
  1355. $body = $response->getBody();
  1356. $body->rewind();
  1357. $result = $body->getContents();
  1358. $this->assertSame('', $result);
  1359. }
  1360. /**
  1361. * Test with string body.
  1362. *
  1363. * @return void
  1364. */
  1365. public function testWithStringBody()
  1366. {
  1367. $response = new Response();
  1368. $newResponse = $response->withStringBody('Foo');
  1369. $body = $newResponse->getBody();
  1370. $this->assertSame('Foo', (string)$body);
  1371. $this->assertNotSame($response, $newResponse);
  1372. $response = new Response();
  1373. $newResponse = $response->withStringBody('');
  1374. $body = $newResponse->getBody();
  1375. $this->assertSame('', (string)$body);
  1376. $this->assertNotSame($response, $newResponse);
  1377. $response = new Response();
  1378. $newResponse = $response->withStringBody(null);
  1379. $body = $newResponse->getBody();
  1380. $this->assertSame('', (string)$body);
  1381. $this->assertNotSame($response, $newResponse);
  1382. $response = new Response();
  1383. $newResponse = $response->withStringBody('1337');
  1384. $body = $newResponse->getBody();
  1385. $this->assertSame('1337', (string)$body);
  1386. $this->assertNotSame($response, $newResponse);
  1387. }
  1388. /**
  1389. * Test get Body.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testGetBody()
  1394. {
  1395. $response = new Response();
  1396. $stream = $response->getBody();
  1397. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $stream);
  1398. }
  1399. /**
  1400. * Test with header.
  1401. *
  1402. * @return void
  1403. */
  1404. public function testWithHeader()
  1405. {
  1406. $response = new Response();
  1407. $response2 = $response->withHeader('Accept', 'application/json');
  1408. $result = $response2->getHeaders();
  1409. $expected = [
  1410. 'Content-Type' => ['text/html; charset=UTF-8'],
  1411. 'Accept' => ['application/json'],
  1412. ];
  1413. $this->assertEquals($expected, $result);
  1414. $this->assertFalse($response->hasHeader('Accept'));
  1415. }
  1416. /**
  1417. * Test get headers.
  1418. *
  1419. * @return void
  1420. */
  1421. public function testGetHeaders()
  1422. {
  1423. $response = new Response();
  1424. $headers = $response->getHeaders();
  1425. $response = $response->withAddedHeader('Location', 'localhost');
  1426. $response = $response->withAddedHeader('Accept', 'application/json');
  1427. $headers = $response->getHeaders();
  1428. $expected = [
  1429. 'Content-Type' => ['text/html; charset=UTF-8'],
  1430. 'Location' => ['localhost'],
  1431. 'Accept' => ['application/json'],
  1432. ];
  1433. $this->assertEquals($expected, $headers);
  1434. }
  1435. /**
  1436. * Test without header.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testWithoutHeader()
  1441. {
  1442. $response = new Response();
  1443. $response = $response->withAddedHeader('Location', 'localhost');
  1444. $response = $response->withAddedHeader('Accept', 'application/json');
  1445. $response2 = $response->withoutHeader('Location');
  1446. $headers = $response2->getHeaders();
  1447. $expected = [
  1448. 'Content-Type' => ['text/html; charset=UTF-8'],
  1449. 'Accept' => ['application/json'],
  1450. ];
  1451. $this->assertEquals($expected, $headers);
  1452. }
  1453. /**
  1454. * Test get header.
  1455. *
  1456. * @return void
  1457. */
  1458. public function testGetHeader()
  1459. {
  1460. $response = new Response();
  1461. $response = $response->withAddedHeader('Location', 'localhost');
  1462. $result = $response->getHeader('Location');
  1463. $this->assertEquals(['localhost'], $result);
  1464. $result = $response->getHeader('location');
  1465. $this->assertEquals(['localhost'], $result);
  1466. $result = $response->getHeader('does-not-exist');
  1467. $this->assertEquals([], $result);
  1468. }
  1469. /**
  1470. * Test get header line.
  1471. *
  1472. * @return void
  1473. */
  1474. public function testGetHeaderLine()
  1475. {
  1476. $response = new Response();
  1477. $headers = $response->getHeaderLine('Accept');
  1478. $this->assertSame('', $headers);
  1479. $response = $response->withAddedHeader('Accept', 'application/json');
  1480. $response = $response->withAddedHeader('Accept', 'application/xml');
  1481. $result = $response->getHeaderLine('Accept');
  1482. $expected = 'application/json,application/xml';
  1483. $this->assertSame($expected, $result);
  1484. $result = $response->getHeaderLine('accept');
  1485. $this->assertSame($expected, $result);
  1486. }
  1487. /**
  1488. * Test has header.
  1489. *
  1490. * @return void
  1491. */
  1492. public function testHasHeader()
  1493. {
  1494. $response = new Response();
  1495. $response = $response->withAddedHeader('Location', 'localhost');
  1496. $this->assertTrue($response->hasHeader('Location'));
  1497. $this->assertTrue($response->hasHeader('location'));
  1498. $this->assertTrue($response->hasHeader('locATIon'));
  1499. $this->assertFalse($response->hasHeader('Accept'));
  1500. $this->assertFalse($response->hasHeader('accept'));
  1501. }
  1502. /**
  1503. * Tests __debugInfo
  1504. *
  1505. * @return void
  1506. */
  1507. public function testDebugInfo()
  1508. {
  1509. $response = new Response();
  1510. $response = $response->withStringBody('Foo');
  1511. $result = $response->__debugInfo();
  1512. $expected = [
  1513. 'status' => 200,
  1514. 'contentType' => 'text/html',
  1515. 'headers' => [
  1516. 'Content-Type' => ['text/html; charset=UTF-8'],
  1517. ],
  1518. 'file' => null,
  1519. 'fileRange' => [],
  1520. 'cookies' => new CookieCollection(),
  1521. 'cacheDirectives' => [],
  1522. 'body' => 'Foo',
  1523. ];
  1524. $this->assertEquals($expected, $result);
  1525. }
  1526. }