ResponseTest.php 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. include_once CORE_TEST_CASES . DS . 'Http' . DS . 'server_mocks.php';
  17. use Cake\Chronos\Chronos;
  18. use Cake\Http\Cookie\Cookie;
  19. use Cake\Http\Cookie\CookieCollection;
  20. use Cake\Http\CorsBuilder;
  21. use Cake\Http\Exception\NotFoundException;
  22. use Cake\Http\Response;
  23. use Cake\Http\ServerRequest;
  24. use Cake\I18n\FrozenTime;
  25. use Cake\TestSuite\TestCase;
  26. use Laminas\Diactoros\Stream;
  27. /**
  28. * ResponseTest
  29. */
  30. class ResponseTest extends TestCase
  31. {
  32. /**
  33. * SERVER variable backup.
  34. *
  35. * @var array
  36. */
  37. protected $server = [];
  38. /**
  39. * setup
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. $this->server = $_SERVER;
  47. }
  48. /**
  49. * teardown
  50. *
  51. * @return void
  52. */
  53. public function tearDown()
  54. {
  55. parent::tearDown();
  56. $_SERVER = $this->server;
  57. unset($GLOBALS['mockedHeadersSent']);
  58. }
  59. /**
  60. * Tests the request object constructor
  61. *
  62. * @return void
  63. */
  64. public function testConstruct()
  65. {
  66. $response = new Response();
  67. $this->assertSame('', (string)$response->getBody());
  68. $this->assertEquals('UTF-8', $response->getCharset());
  69. $this->assertEquals('text/html', $response->getType());
  70. $this->assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $options = [
  73. 'body' => 'This is the body',
  74. 'charset' => 'my-custom-charset',
  75. 'type' => 'mp3',
  76. 'status' => '203',
  77. ];
  78. $response = new Response($options);
  79. $this->assertEquals('This is the body', (string)$response->getBody());
  80. $this->assertEquals('my-custom-charset', $response->getCharset());
  81. $this->assertEquals('audio/mpeg', $response->getType());
  82. $this->assertEquals('audio/mpeg', $response->getHeaderLine('Content-Type'));
  83. $this->assertEquals(203, $response->getStatusCode());
  84. }
  85. /**
  86. * Test statusCodes constructor argument.
  87. *
  88. * @group deprecated
  89. * @return void
  90. */
  91. public function testConstructCustomCodes()
  92. {
  93. $this->deprecated(function () {
  94. $options = [
  95. 'body' => 'This is the body',
  96. 'charset' => 'ISO-8859-1',
  97. 'type' => 'txt',
  98. 'status' => '422',
  99. 'statusCodes' => [
  100. 422 => 'Unprocessable Entity',
  101. ],
  102. ];
  103. $response = new Response($options);
  104. $this->assertEquals($options['body'], (string)$response->getBody());
  105. $this->assertEquals($options['charset'], $response->getCharset());
  106. $this->assertEquals($response->getMimeType($options['type']), $response->getType());
  107. $this->assertEquals($options['status'], $response->getStatusCode());
  108. $this->assertEquals('text/plain; charset=ISO-8859-1', $response->getHeaderLine('Content-Type'));
  109. });
  110. }
  111. /**
  112. * Tests the body method
  113. *
  114. * @group deprecated
  115. * @return void
  116. */
  117. public function testBody()
  118. {
  119. $this->deprecated(function () {
  120. $response = new Response();
  121. $this->assertNull($response->body());
  122. $response->body('Response body');
  123. $this->assertEquals('Response body', $response->body());
  124. $this->assertEquals('Changed Body', $response->body('Changed Body'));
  125. $response = new Response();
  126. $response->body(0);
  127. $this->assertEquals(0, $response->body());
  128. $response = new Response();
  129. $response->body('0');
  130. $this->assertEquals('0', $response->body());
  131. $response = new Response();
  132. $response->body(null);
  133. $this->assertNull($response->body());
  134. });
  135. }
  136. /**
  137. * Tests the charset method
  138. *
  139. * @group deprecated
  140. * @return void
  141. */
  142. public function testCharset()
  143. {
  144. $this->deprecated(function () {
  145. $response = new Response();
  146. $this->assertEquals('UTF-8', $response->charset());
  147. $response->charset('iso-8859-1');
  148. $this->assertEquals('iso-8859-1', $response->charset());
  149. $this->assertEquals('UTF-16', $response->charset('UTF-16'));
  150. });
  151. }
  152. /**
  153. * Tests the getCharset/withCharset methods
  154. *
  155. * @return void
  156. */
  157. public function testWithCharset()
  158. {
  159. $response = new Response();
  160. $this->assertEquals('text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'));
  161. $new = $response->withCharset('iso-8859-1');
  162. $this->assertNotContains('iso', $response->getHeaderLine('Content-Type'), 'Old instance not changed');
  163. $this->assertSame('iso-8859-1', $new->getCharset());
  164. $this->assertEquals('text/html; charset=iso-8859-1', $new->getHeaderLine('Content-Type'));
  165. }
  166. /**
  167. * Tests the statusCode method
  168. *
  169. * @group deprecated
  170. * @return void
  171. */
  172. public function testStatusCode()
  173. {
  174. $this->deprecated(function () {
  175. $response = new Response();
  176. $this->assertEquals(200, $response->statusCode());
  177. $response->statusCode(404);
  178. $this->assertEquals(404, $response->getStatusCode(), 'Sets shared state.');
  179. $this->assertEquals(404, $response->statusCode());
  180. $this->assertEquals('Not Found', $response->getReasonPhrase());
  181. $this->assertEquals(500, $response->statusCode(500));
  182. });
  183. }
  184. /**
  185. * Test invalid status codes
  186. *
  187. * @group deprecated
  188. * @return void
  189. */
  190. public function testStatusCodeInvalid()
  191. {
  192. $this->expectException(\InvalidArgumentException::class);
  193. $this->deprecated(function () {
  194. $response = new Response();
  195. $response->statusCode(1001);
  196. });
  197. }
  198. /**
  199. * Tests the type method
  200. *
  201. * @group deprecated
  202. * @return void
  203. */
  204. public function testType()
  205. {
  206. $this->deprecated(function () {
  207. $response = new Response();
  208. $this->assertEquals('text/html', $response->type());
  209. $response->type('pdf');
  210. $this->assertEquals('application/pdf', $response->type());
  211. $this->assertEquals('application/crazy-mime', $response->type('application/crazy-mime'));
  212. $this->assertEquals('application/json', $response->type('json'));
  213. $this->assertEquals('text/vnd.wap.wml', $response->type('wap'));
  214. $this->assertEquals('application/vnd.wap.xhtml+xml', $response->type('xhtml-mobile'));
  215. $this->assertEquals('text/csv', $response->type('csv'));
  216. $response->type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);
  217. $this->assertEquals('application/keynote', $response->type('keynote'));
  218. $this->assertEquals('application/bat', $response->type('bat'));
  219. $this->assertFalse($response->type('wackytype'));
  220. });
  221. }
  222. /**
  223. * Tests the getType method
  224. *
  225. * @return void
  226. */
  227. public function testGetType()
  228. {
  229. $response = new Response();
  230. $this->assertEquals('text/html', $response->getType());
  231. $this->assertEquals(
  232. 'application/pdf',
  233. $response->withType('pdf')->getType()
  234. );
  235. $this->assertEquals(
  236. 'custom/stuff',
  237. $response->withType('custom/stuff')->getType()
  238. );
  239. $this->assertEquals(
  240. 'application/json',
  241. $response->withType('json')->getType()
  242. );
  243. }
  244. /**
  245. * @return void
  246. */
  247. public function testSetTypeMap()
  248. {
  249. $response = new Response();
  250. $response->setTypeMap('ical', 'text/calendar');
  251. $response = $response->withType('ical')->getType();
  252. $this->assertEquals('text/calendar', $response);
  253. }
  254. /**
  255. * @return void
  256. */
  257. public function testSetTypeMapAsArray()
  258. {
  259. $response = new Response();
  260. $response->setTypeMap('ical', ['text/calendar']);
  261. $response = $response->withType('ical')->getType();
  262. $this->assertEquals('text/calendar', $response);
  263. }
  264. /**
  265. * Tests the withType method
  266. *
  267. * @return void
  268. */
  269. public function testWithTypeAlias()
  270. {
  271. $response = new Response();
  272. $this->assertEquals(
  273. 'text/html; charset=UTF-8',
  274. $response->getHeaderLine('Content-Type'),
  275. 'Default content-type should match'
  276. );
  277. $new = $response->withType('pdf');
  278. $this->assertNotSame($new, $response, 'Should be a new instance');
  279. $this->assertSame(
  280. 'text/html; charset=UTF-8',
  281. $response->getHeaderLine('Content-Type'),
  282. 'Original object should not be modified'
  283. );
  284. $this->assertSame('application/pdf', $new->getHeaderLine('Content-Type'));
  285. $json = $new->withType('json');
  286. $this->assertSame('application/json', $json->getHeaderLine('Content-Type'));
  287. $this->assertSame('application/json', $json->getType());
  288. }
  289. /**
  290. * test withType() and full mime-types
  291. *
  292. * @return void
  293. */
  294. public function withTypeFull()
  295. {
  296. $response = new Response();
  297. $this->assertEquals(
  298. 'application/json',
  299. $response->withType('application/json')->getHeaderLine('Content-Type'),
  300. 'Should not add charset to explicit type'
  301. );
  302. $this->assertEquals(
  303. 'custom/stuff',
  304. $response->withType('custom/stuff')->getHeaderLine('Content-Type'),
  305. 'Should allow arbitrary types'
  306. );
  307. $this->assertEquals(
  308. 'text/html; charset=UTF-8',
  309. $response->withType('text/html; charset=UTF-8')->getHeaderLine('Content-Type'),
  310. 'Should allow charset types'
  311. );
  312. }
  313. /**
  314. * Test that an invalid type raises an exception
  315. *
  316. * @return void
  317. */
  318. public function testWithTypeInvalidType()
  319. {
  320. $this->expectException(\InvalidArgumentException::class);
  321. $this->expectExceptionMessage('"beans" is an invalid content type');
  322. $response = new Response();
  323. $response->withType('beans');
  324. }
  325. /**
  326. * Tests the header method
  327. *
  328. * @group deprecated
  329. * @return void
  330. */
  331. public function testHeader()
  332. {
  333. $this->deprecated(function () {
  334. $response = new Response();
  335. $headers = [
  336. 'Content-Type' => 'text/html; charset=UTF-8',
  337. ];
  338. $this->assertEquals($headers, $response->header());
  339. $response->header('Location', 'http://example.com');
  340. $headers += ['Location' => 'http://example.com'];
  341. $this->assertEquals($headers, $response->header());
  342. // Headers with the same name are overwritten
  343. $response->header('Location', 'http://example2.com');
  344. $headers = [
  345. 'Content-Type' => 'text/html; charset=UTF-8',
  346. 'Location' => 'http://example2.com',
  347. ];
  348. $this->assertEquals($headers, $response->header());
  349. $response->header(['WWW-Authenticate' => 'Negotiate']);
  350. $headers += ['WWW-Authenticate' => 'Negotiate'];
  351. $this->assertEquals($headers, $response->header());
  352. $response->header(['WWW-Authenticate' => 'Not-Negotiate']);
  353. $headers['WWW-Authenticate'] = 'Not-Negotiate';
  354. $this->assertEquals($headers, $response->header());
  355. $response->header(['Age' => 12, 'Allow' => 'GET, HEAD']);
  356. $headers += ['Age' => 12, 'Allow' => 'GET, HEAD'];
  357. $this->assertEquals($headers, $response->header());
  358. // String headers are allowed
  359. $response->header('Content-Language: da');
  360. $headers += ['Content-Language' => 'da'];
  361. $this->assertEquals($headers, $response->header());
  362. $response->header('Content-Language: da');
  363. $headers += ['Content-Language' => 'da'];
  364. $this->assertEquals($headers, $response->header());
  365. $response->header(['Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache']);
  366. $headers += ['Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'];
  367. $this->assertEquals($headers, $response->header());
  368. $response->header('Access-Control-Allow-Origin', ['domain1', 'domain2']);
  369. $headers += ['Access-Control-Allow-Origin' => ['domain1', 'domain2']];
  370. $this->assertEquals($headers, $response->header());
  371. });
  372. }
  373. /**
  374. * Tests the send method
  375. *
  376. * @group deprecated
  377. * @return void
  378. */
  379. public function testSend()
  380. {
  381. $GLOBALS['mockedHeadersSent'] = false;
  382. $this->deprecated(function () {
  383. $response = $this->getMockBuilder('Cake\Http\Response')
  384. ->setMethods(['_sendHeader', '_sendContent'])
  385. ->getMock();
  386. $response->header([
  387. 'Content-Language' => 'es',
  388. 'WWW-Authenticate' => 'Negotiate',
  389. 'Access-Control-Allow-Origin' => ['domain1', 'domain2'],
  390. ]);
  391. $response->cookie(['name' => 'thing', 'value' => 'value']);
  392. $response->body('the response body');
  393. $response->expects($this->once())->method('_sendContent')->with('the response body');
  394. $response->expects($this->at(0))
  395. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  396. $response->expects($this->at(1))
  397. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  398. $response->expects($this->at(2))
  399. ->method('_sendHeader')->with('Content-Language', 'es');
  400. $response->expects($this->at(3))
  401. ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
  402. $response->expects($this->at(4))
  403. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
  404. $response->expects($this->at(5))
  405. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
  406. $response->send();
  407. $this->assertCount(1, $GLOBALS['mockedCookies']);
  408. $this->assertSame('value', $GLOBALS['mockedCookies'][0]['value']);
  409. $this->assertSame('thing', $GLOBALS['mockedCookies'][0]['name']);
  410. });
  411. }
  412. /**
  413. * Data provider for content type tests.
  414. *
  415. * @return array
  416. */
  417. public static function charsetTypeProvider()
  418. {
  419. return [
  420. ['mp3', 'audio/mpeg'],
  421. ['js', 'application/javascript; charset=UTF-8'],
  422. ['xml', 'application/xml; charset=UTF-8'],
  423. ['txt', 'text/plain; charset=UTF-8'],
  424. ];
  425. }
  426. /**
  427. * Tests the send method and changing the content type
  428. *
  429. * @group deprecated
  430. * @dataProvider charsetTypeProvider
  431. * @return void
  432. */
  433. public function testSendChangingContentType($original, $expected)
  434. {
  435. $this->deprecated(function () use ($original, $expected) {
  436. $response = new Response();
  437. $response->type($original);
  438. $response->body('the response body');
  439. $this->assertEquals($expected, $response->getHeaderLine('Content-Type'));
  440. });
  441. }
  442. /**
  443. * Tests the send method and changing the content type to JS without adding the charset
  444. *
  445. * @group deprecated
  446. * @return void
  447. */
  448. public function testCharsetSetContentTypeWithoutCharset()
  449. {
  450. $this->deprecated(function () {
  451. $response = new Response();
  452. $response->type('js');
  453. $response->charset('');
  454. $this->assertEquals('application/javascript', $response->getHeaderLine('Content-Type'));
  455. });
  456. }
  457. /**
  458. * Tests the send method and changing the content type
  459. *
  460. * @group deprecated
  461. * @return void
  462. */
  463. public function testLocationSetsStatus()
  464. {
  465. $this->deprecated(function () {
  466. $response = new Response();
  467. $response->location('http://www.example.com');
  468. $this->assertEquals(302, $response->getStatusCode());
  469. });
  470. }
  471. /**
  472. * Test that setting certain status codes clears the status code.
  473. *
  474. * @group deprecated
  475. * @return void
  476. */
  477. public function testStatusClearsContentType()
  478. {
  479. $this->deprecated(function () {
  480. $response = new Response();
  481. $response->statusCode(204);
  482. $response->statusCode(304);
  483. $this->assertFalse($response->hasHeader('Content-Type'));
  484. $this->assertSame(304, $response->getStatusCode());
  485. $response = new Response();
  486. $response->type('pdf');
  487. $response->statusCode(204);
  488. $this->assertFalse($response->hasHeader('Content-Type'));
  489. $this->assertSame(204, $response->getStatusCode());
  490. $response = new Response();
  491. $response->statusCode(204);
  492. $response->type('pdf');
  493. $this->assertFalse($response->hasHeader('Content-Type'));
  494. });
  495. }
  496. /**
  497. * Test that setting certain status codes clears the status code.
  498. *
  499. * @return void
  500. */
  501. public function testWithStatusClearsContentType()
  502. {
  503. $response = new Response();
  504. $new = $response->withType('pdf')
  505. ->withStatus(204);
  506. $this->assertFalse($new->hasHeader('Content-Type'));
  507. $this->assertSame('', $new->getType());
  508. $this->assertSame(204, $new->getStatusCode(), 'Status code should clear content-type');
  509. $response = new Response();
  510. $new = $response->withStatus(304)
  511. ->withType('pdf');
  512. $this->assertSame('', $new->getType());
  513. $this->assertFalse(
  514. $new->hasHeader('Content-Type'),
  515. 'Type should not be retained because of status code.'
  516. );
  517. $response = new Response();
  518. $new = $response
  519. ->withHeader('Content-Type', 'application/json')
  520. ->withStatus(204);
  521. $this->assertFalse($new->hasHeader('Content-Type'), 'Should clear direct header');
  522. $this->assertSame('', $new->getType());
  523. }
  524. /**
  525. * Test that setting status codes doesn't overwrite content-type
  526. *
  527. * @return void
  528. */
  529. public function testWithStatusDoesNotChangeContentType()
  530. {
  531. $response = new Response();
  532. $new = $response->withHeader('Content-Type', 'application/json')
  533. ->withStatus(403);
  534. $this->assertSame('application/json', $new->getHeaderLine('Content-Type'));
  535. $this->assertSame(403, $new->getStatusCode());
  536. $response = new Response();
  537. $new = $response->withStatus(403)
  538. ->withHeader('Content-Type', 'application/json');
  539. $this->assertSame('application/json', $new->getHeaderLine('Content-Type'));
  540. $this->assertSame(403, $new->getStatusCode());
  541. $this->assertSame('application/json', $new->getType());
  542. }
  543. /**
  544. * Tests the send method and changing the content type
  545. *
  546. * @group deprecated
  547. * @return void
  548. */
  549. public function testSendWithCallableBody()
  550. {
  551. $this->deprecated(function () {
  552. $response = $this->getMockBuilder('Cake\Http\Response')
  553. ->setMethods(['_sendHeader'])
  554. ->getMock();
  555. $response->body(function () {
  556. echo 'the response body';
  557. });
  558. ob_start();
  559. $response->send();
  560. $this->assertEquals('the response body', ob_get_clean());
  561. });
  562. }
  563. /**
  564. * Tests that the returned a string from a body callable is also sent
  565. * as the response body
  566. *
  567. * @group deprecated
  568. * @return void
  569. */
  570. public function testSendWithCallableBodyWithReturn()
  571. {
  572. $this->deprecated(function () {
  573. $response = $this->getMockBuilder('Cake\Http\Response')
  574. ->setMethods(['_sendHeader'])
  575. ->getMock();
  576. $response->body(function () {
  577. return 'the response body';
  578. });
  579. ob_start();
  580. $response->send();
  581. $this->assertEquals('the response body', ob_get_clean());
  582. });
  583. }
  584. /**
  585. * Tests that callable strings are not triggered
  586. *
  587. * @group deprecated
  588. * @return void
  589. */
  590. public function testSendWithCallableStringBody()
  591. {
  592. $this->deprecated(function () {
  593. $response = $this->getMockBuilder('Cake\Http\Response')
  594. ->setMethods(['_sendHeader'])
  595. ->getMock();
  596. $response->body('phpversion');
  597. ob_start();
  598. $response->send();
  599. $this->assertEquals('phpversion', ob_get_clean());
  600. });
  601. }
  602. /**
  603. * Tests the disableCache method
  604. *
  605. * @group deprecated
  606. * @return void
  607. */
  608. public function testDisableCache()
  609. {
  610. $this->deprecated(function () {
  611. $response = new Response();
  612. $expected = [
  613. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  614. 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
  615. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  616. 'Content-Type' => 'text/html; charset=UTF-8',
  617. ];
  618. $response->disableCache();
  619. $this->assertEquals($expected, $response->header());
  620. });
  621. }
  622. /**
  623. * Tests the withDisabledCache method
  624. *
  625. * @return void
  626. */
  627. public function testWithDisabledCache()
  628. {
  629. $response = new Response();
  630. $expected = [
  631. 'Expires' => ['Mon, 26 Jul 1997 05:00:00 GMT'],
  632. 'Last-Modified' => [gmdate('D, d M Y H:i:s') . ' GMT'],
  633. 'Cache-Control' => ['no-store, no-cache, must-revalidate, post-check=0, pre-check=0'],
  634. 'Content-Type' => ['text/html; charset=UTF-8'],
  635. ];
  636. $new = $response->withDisabledCache();
  637. $this->assertFalse($response->hasHeader('Expires'), 'Old instance not mutated.');
  638. $this->assertEquals($expected, $new->getHeaders());
  639. }
  640. /**
  641. * Tests the cache method
  642. *
  643. * @group deprecated
  644. * @return void
  645. */
  646. public function testCache()
  647. {
  648. $this->deprecated(function () {
  649. $response = new Response();
  650. $since = time();
  651. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  652. $response->expires('+1 day');
  653. $expected = [
  654. 'Date' => gmdate('D, d M Y H:i:s ', $since) . 'GMT',
  655. 'Last-Modified' => gmdate('D, j M Y H:i:s ', $since) . 'GMT',
  656. 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
  657. 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
  658. 'Content-Type' => 'text/html; charset=UTF-8',
  659. ];
  660. $response->cache($since);
  661. $this->assertEquals($expected, $response->header());
  662. $response = new Response();
  663. $since = time();
  664. $time = '+5 day';
  665. $expected = [
  666. 'Date' => gmdate('D, d M Y H:i:s ', $since) . 'GMT',
  667. 'Last-Modified' => gmdate('D, j M Y H:i:s ', $since) . 'GMT',
  668. 'Expires' => gmdate('D, j M Y H:i:s', strtotime($time)) . ' GMT',
  669. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
  670. 'Content-Type' => 'text/html; charset=UTF-8',
  671. ];
  672. $response->cache($since, $time);
  673. $this->assertEquals($expected, $response->header());
  674. $response = new Response();
  675. $since = time();
  676. $time = time();
  677. $expected = [
  678. 'Date' => gmdate('D, d M Y H:i:s ', $since) . 'GMT',
  679. 'Last-Modified' => gmdate('D, j M Y H:i:s ', $since) . 'GMT',
  680. 'Expires' => gmdate('D, j M Y H:i:s', $time) . ' GMT',
  681. 'Cache-Control' => 'public, max-age=0',
  682. 'Content-Type' => 'text/html; charset=UTF-8',
  683. ];
  684. $response->cache($since, $time);
  685. $this->assertEquals($expected, $response->header());
  686. });
  687. }
  688. /**
  689. * Tests the withCache method
  690. *
  691. * @return void
  692. */
  693. public function testWithCache()
  694. {
  695. $response = new Response();
  696. $since = $time = time();
  697. $new = $response->withCache($since, $time);
  698. $this->assertFalse($response->hasHeader('Date'));
  699. $this->assertFalse($response->hasHeader('Last-Modified'));
  700. $this->assertEquals(gmdate('D, d M Y H:i:s ', $since) . 'GMT', $new->getHeaderLine('Date'));
  701. $this->assertEquals(gmdate('D, j M Y H:i:s ', $since) . 'GMT', $new->getHeaderLine('Last-Modified'));
  702. $this->assertEquals(gmdate('D, j M Y H:i:s', $time) . ' GMT', $new->getHeaderLine('Expires'));
  703. $this->assertEquals('public, max-age=0', $new->getHeaderLine('Cache-Control'));
  704. }
  705. /**
  706. * Tests the compress method
  707. *
  708. * @return void
  709. */
  710. public function testCompress()
  711. {
  712. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  713. $response = new Response();
  714. if (ini_get('zlib.output_compression') === '1' || !extension_loaded('zlib')) {
  715. $this->assertFalse($response->compress());
  716. $this->markTestSkipped('Is not possible to test output compression');
  717. }
  718. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  719. $result = $response->compress();
  720. $this->assertFalse($result);
  721. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  722. $result = $response->compress();
  723. $this->assertTrue($result);
  724. $this->assertContains('ob_gzhandler', ob_list_handlers());
  725. ob_get_clean();
  726. }
  727. /**
  728. * Tests the httpCodes method
  729. *
  730. * @group deprecated
  731. * @return void
  732. */
  733. public function testHttpCodes()
  734. {
  735. $this->expectException(\InvalidArgumentException::class);
  736. $this->deprecated(function () {
  737. $response = new Response();
  738. $result = $response->httpCodes();
  739. $this->assertCount(65, $result);
  740. $result = $response->httpCodes(100);
  741. $expected = [100 => 'Continue'];
  742. $this->assertEquals($expected, $result);
  743. $codes = [
  744. 381 => 'Unicorn Moved',
  745. 555 => 'Unexpected Minotaur',
  746. ];
  747. $result = $response->httpCodes($codes);
  748. $this->assertTrue($result);
  749. $this->assertCount(67, $response->httpCodes());
  750. $result = $response->httpCodes(381);
  751. $expected = [381 => 'Unicorn Moved'];
  752. $this->assertEquals($expected, $result);
  753. $codes = [404 => 'Sorry Bro'];
  754. $result = $response->httpCodes($codes);
  755. $this->assertTrue($result);
  756. $this->assertCount(67, $response->httpCodes());
  757. $result = $response->httpCodes(404);
  758. $expected = [404 => 'Sorry Bro'];
  759. $this->assertEquals($expected, $result);
  760. //Throws exception
  761. $response->httpCodes([
  762. 0 => 'Nothing Here',
  763. -1 => 'Reverse Infinity',
  764. 12345 => 'Universal Password',
  765. 'Hello' => 'World',
  766. ]);
  767. });
  768. }
  769. /**
  770. * Tests the download method
  771. *
  772. * @group deprecated
  773. * @return void
  774. */
  775. public function testDownload()
  776. {
  777. $this->deprecated(function () {
  778. $response = new Response();
  779. $expected = [
  780. 'Content-Type' => 'text/html; charset=UTF-8',
  781. 'Content-Disposition' => 'attachment; filename="myfile.mp3"',
  782. ];
  783. $response->download('myfile.mp3');
  784. $this->assertEquals($expected, $response->header());
  785. });
  786. }
  787. /**
  788. * Tests the withDownload method
  789. *
  790. * @return void
  791. */
  792. public function testWithDownload()
  793. {
  794. $response = new Response();
  795. $new = $response->withDownload('myfile.mp3');
  796. $this->assertFalse($response->hasHeader('Content-Disposition'), 'No mutation');
  797. $expected = 'attachment; filename="myfile.mp3"';
  798. $this->assertEquals($expected, $new->getHeaderLine('Content-Disposition'));
  799. }
  800. /**
  801. * Tests the mapType method
  802. *
  803. * @return void
  804. */
  805. public function testMapType()
  806. {
  807. $response = new Response();
  808. $this->assertEquals('wav', $response->mapType('audio/x-wav'));
  809. $this->assertEquals('pdf', $response->mapType('application/pdf'));
  810. $this->assertEquals('xml', $response->mapType('text/xml'));
  811. $this->assertEquals('html', $response->mapType('*/*'));
  812. $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
  813. $expected = ['json', 'xhtml', 'css'];
  814. $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
  815. $this->assertEquals($expected, $result);
  816. }
  817. /**
  818. * Tests the outputCompressed method
  819. *
  820. * @return void
  821. */
  822. public function testOutputCompressed()
  823. {
  824. $response = new Response();
  825. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  826. $result = $response->outputCompressed();
  827. $this->assertFalse($result);
  828. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  829. $result = $response->outputCompressed();
  830. $this->assertFalse($result);
  831. if (!extension_loaded('zlib')) {
  832. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  833. }
  834. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  835. if (ini_get('zlib.output_compression') !== '1') {
  836. ob_start('ob_gzhandler');
  837. }
  838. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  839. $result = $response->outputCompressed();
  840. $this->assertTrue($result);
  841. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  842. $result = $response->outputCompressed();
  843. $this->assertFalse($result);
  844. if (ini_get('zlib.output_compression') !== '1') {
  845. ob_get_clean();
  846. }
  847. }
  848. /**
  849. * Tests getting/setting the protocol
  850. *
  851. * @group deprecated
  852. * @return void
  853. */
  854. public function testProtocol()
  855. {
  856. $GLOBALS['mockedHeadersSent'] = false;
  857. $this->deprecated(function () {
  858. $response = $this->getMockBuilder('Cake\Http\Response')
  859. ->setMethods(['_sendHeader', '_sendContent'])
  860. ->getMock();
  861. $response->protocol('HTTP/1.0');
  862. $this->assertEquals('HTTP/1.0', $response->protocol());
  863. $response->expects($this->at(0))
  864. ->method('_sendHeader')->with('HTTP/1.0 200 OK');
  865. $response->send();
  866. });
  867. }
  868. /**
  869. * Tests getting/setting the Content-Length
  870. *
  871. * @group deprecated
  872. * @return void
  873. */
  874. public function testLength()
  875. {
  876. $this->deprecated(function () {
  877. $response = new Response();
  878. $response->length(100);
  879. $this->assertEquals(100, $response->length());
  880. $this->assertEquals('100', $response->getHeaderLine('Content-Length'));
  881. });
  882. }
  883. /**
  884. * Tests settings the content length
  885. *
  886. * @return void
  887. */
  888. public function testWithLength()
  889. {
  890. $response = new Response();
  891. $this->assertFalse($response->hasHeader('Content-Length'));
  892. $new = $response->withLength(100);
  893. $this->assertFalse($response->hasHeader('Content-Length'), 'Old instance not modified');
  894. $this->assertSame('100', $new->getHeaderLine('Content-Length'));
  895. }
  896. /**
  897. * Tests settings the link
  898. *
  899. * @return void
  900. */
  901. public function testWithAddedLink()
  902. {
  903. $response = new Response();
  904. $this->assertFalse($response->hasHeader('Link'));
  905. $new = $response->withAddedLink('http://example.com', ['rel' => 'prev']);
  906. $this->assertFalse($response->hasHeader('Link'), 'Old instance not modified');
  907. $this->assertEquals('<http://example.com>; rel="prev"', $new->getHeaderLine('Link'));
  908. $new = $response->withAddedLink('http://example.com');
  909. $this->assertEquals('<http://example.com>', $new->getHeaderLine('Link'));
  910. $new = $response->withAddedLink('http://example.com?p=1', ['rel' => 'prev'])
  911. ->withAddedLink('http://example.com?p=2', ['rel' => 'next', 'foo' => 'bar']);
  912. $this->assertEquals('<http://example.com?p=1>; rel="prev",<http://example.com?p=2>; rel="next"; foo="bar"', $new->getHeaderLine('Link'));
  913. }
  914. /**
  915. * Tests setting the expiration date
  916. *
  917. * @group deprecated
  918. * @return void
  919. */
  920. public function testExpires()
  921. {
  922. $this->deprecated(function () {
  923. $format = 'D, j M Y H:i:s';
  924. $response = new Response();
  925. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  926. $response->expires($now);
  927. $now->setTimeZone(new \DateTimeZone('UTC'));
  928. $this->assertEquals($now->format($format) . ' GMT', $response->expires());
  929. $this->assertEquals($now->format($format) . ' GMT', $response->getHeaderLine('Expires'));
  930. $now = time();
  931. $response = new Response();
  932. $response->expires($now);
  933. $this->assertEquals(gmdate($format) . ' GMT', $response->expires());
  934. $this->assertEquals(gmdate($format) . ' GMT', $response->getHeaderLine('Expires'));
  935. $response = new Response();
  936. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  937. $response->expires('+1 day');
  938. $this->assertEquals($time->format($format) . ' GMT', $response->expires());
  939. $this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Expires'));
  940. });
  941. }
  942. /**
  943. * Tests the withExpires method
  944. *
  945. * @return void
  946. */
  947. public function testWithExpires()
  948. {
  949. $format = 'D, j M Y H:i:s';
  950. $response = new Response();
  951. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  952. $new = $response->withExpires($now);
  953. $this->assertFalse($response->hasHeader('Expires'));
  954. $now->setTimeZone(new \DateTimeZone('UTC'));
  955. $this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  956. $now = time();
  957. $new = $response->withExpires($now);
  958. $this->assertEquals(gmdate($format) . ' GMT', $new->getHeaderLine('Expires'));
  959. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  960. $new = $response->withExpires('+1 day');
  961. $this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Expires'));
  962. }
  963. /**
  964. * Tests setting the modification date
  965. *
  966. * @group deprecated
  967. * @return void
  968. */
  969. public function testModified()
  970. {
  971. $this->deprecated(function () {
  972. $format = 'D, j M Y H:i:s';
  973. $response = new Response();
  974. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  975. $response->modified($now);
  976. $now->setTimeZone(new \DateTimeZone('UTC'));
  977. $this->assertEquals($now->format($format) . ' GMT', $response->modified());
  978. $this->assertEquals($now->format($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  979. $response = new Response();
  980. $now = time();
  981. $response->modified($now);
  982. $this->assertEquals(gmdate($format) . ' GMT', $response->modified());
  983. $this->assertEquals(gmdate($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  984. $response = new Response();
  985. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  986. $response->modified('+1 day');
  987. $this->assertEquals($time->format($format) . ' GMT', $response->modified());
  988. $this->assertEquals($time->format($format) . ' GMT', $response->getHeaderLine('Last-Modified'));
  989. });
  990. }
  991. /**
  992. * Tests the withModified method
  993. *
  994. * @return void
  995. */
  996. public function testWithModified()
  997. {
  998. $format = 'D, j M Y H:i:s';
  999. $response = new Response();
  1000. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  1001. $new = $response->withModified($now);
  1002. $this->assertFalse($response->hasHeader('Last-Modified'));
  1003. $now->setTimeZone(new \DateTimeZone('UTC'));
  1004. $this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  1005. $now = time();
  1006. $new = $response->withModified($now);
  1007. $this->assertEquals(gmdate($format, $now) . ' GMT', $new->getHeaderLine('Last-Modified'));
  1008. $now = new \DateTimeImmutable();
  1009. $new = $response->withModified($now);
  1010. $this->assertEquals(gmdate($format, $now->getTimestamp()) . ' GMT', $new->getHeaderLine('Last-Modified'));
  1011. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  1012. $new = $response->withModified('+1 day');
  1013. $this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
  1014. }
  1015. /**
  1016. * Tests setting of public/private Cache-Control directives
  1017. *
  1018. * @deprecated
  1019. * @return void
  1020. */
  1021. public function testSharable()
  1022. {
  1023. $this->deprecated(function () {
  1024. $response = new Response();
  1025. $this->assertNull($response->sharable());
  1026. $response->sharable(true);
  1027. $this->assertTrue($response->sharable());
  1028. $this->assertEquals('public', $response->getHeaderLine('Cache-Control'));
  1029. $response = new Response();
  1030. $response->sharable(false);
  1031. $this->assertFalse($response->sharable());
  1032. $this->assertEquals('private', $response->getHeaderLine('Cache-Control'));
  1033. $response = new Response();
  1034. $response->sharable(true, 3600);
  1035. $this->assertEquals('public, max-age=3600', $response->getHeaderLine('Cache-Control'));
  1036. $response = new Response();
  1037. $response->sharable(false, 3600);
  1038. $this->assertEquals('private, max-age=3600', $response->getHeaderLine('Cache-Control'));
  1039. });
  1040. }
  1041. /**
  1042. * Tests withSharable()
  1043. *
  1044. * @return void
  1045. */
  1046. public function testWithSharable()
  1047. {
  1048. $response = new Response();
  1049. $new = $response->withSharable(true);
  1050. $this->assertFalse($response->hasHeader('Cache-Control'), 'old instance unchanged');
  1051. $this->assertEquals('public', $new->getHeaderLine('Cache-Control'));
  1052. $new = $response->withSharable(false);
  1053. $this->assertEquals('private', $new->getHeaderLine('Cache-Control'));
  1054. $new = $response->withSharable(true, 3600);
  1055. $this->assertEquals('public, max-age=3600', $new->getHeaderLine('Cache-Control'));
  1056. $new = $response->withSharable(false, 3600);
  1057. $this->assertEquals('private, max-age=3600', $new->getHeaderLine('Cache-Control'));
  1058. }
  1059. /**
  1060. * Tests setting of max-age Cache-Control directive
  1061. *
  1062. * @deprecated
  1063. * @return void
  1064. */
  1065. public function testMaxAge()
  1066. {
  1067. $this->deprecated(function () {
  1068. $response = new Response();
  1069. $this->assertNull($response->maxAge());
  1070. $response->maxAge(3600);
  1071. $this->assertEquals(3600, $response->maxAge());
  1072. $this->assertEquals('max-age=3600', $response->getHeaderLine('Cache-Control'));
  1073. $response = new Response();
  1074. $response->maxAge(3600);
  1075. $response->sharable(false);
  1076. $this->assertEquals('max-age=3600, private', $response->getHeaderLine('Cache-Control'));
  1077. });
  1078. }
  1079. /**
  1080. * Tests withMaxAge()
  1081. *
  1082. * @return void
  1083. */
  1084. public function testWithMaxAge()
  1085. {
  1086. $response = new Response();
  1087. $this->assertFalse($response->hasHeader('Cache-Control'));
  1088. $new = $response->withMaxAge(3600);
  1089. $this->assertEquals('max-age=3600', $new->getHeaderLine('Cache-Control'));
  1090. $new = $response->withMaxAge(3600)
  1091. ->withSharable(false);
  1092. $this->assertEquals('max-age=3600, private', $new->getHeaderLine('Cache-Control'));
  1093. }
  1094. /**
  1095. * Tests setting of s-maxage Cache-Control directive
  1096. *
  1097. * @deprecated
  1098. * @return void
  1099. */
  1100. public function testSharedMaxAge()
  1101. {
  1102. $this->deprecated(function () {
  1103. $response = new Response();
  1104. $this->assertNull($response->maxAge());
  1105. $response->sharedMaxAge(3600);
  1106. $this->assertEquals(3600, $response->sharedMaxAge());
  1107. $this->assertEquals('s-maxage=3600', $response->getHeaderLine('Cache-Control'));
  1108. $response = new Response();
  1109. $response->sharedMaxAge(3600);
  1110. $response->sharable(true);
  1111. $this->assertEquals('s-maxage=3600, public', $response->getHeaderLine('Cache-Control'));
  1112. });
  1113. }
  1114. /**
  1115. * Tests setting of s-maxage Cache-Control directive
  1116. *
  1117. * @return void
  1118. */
  1119. public function testWithSharedMaxAge()
  1120. {
  1121. $response = new Response();
  1122. $new = $response->withSharedMaxAge(3600);
  1123. $this->assertFalse($response->hasHeader('Cache-Control'));
  1124. $this->assertEquals('s-maxage=3600', $new->getHeaderLine('Cache-Control'));
  1125. $new = $response->withSharedMaxAge(3600)->withSharable(true);
  1126. $this->assertEquals('s-maxage=3600, public', $new->getHeaderLine('Cache-Control'));
  1127. }
  1128. /**
  1129. * Tests setting of must-revalidate Cache-Control directive
  1130. *
  1131. * @group deprecated
  1132. * @return void
  1133. */
  1134. public function testMustRevalidate()
  1135. {
  1136. $this->deprecated(function () {
  1137. $response = new Response();
  1138. $this->assertFalse($response->mustRevalidate());
  1139. $response->mustRevalidate(true);
  1140. $this->assertTrue($response->mustRevalidate());
  1141. $this->assertEquals('must-revalidate', $response->getHeaderLine('Cache-Control'));
  1142. $response->mustRevalidate(false);
  1143. $this->assertFalse($response->mustRevalidate());
  1144. $response = new Response();
  1145. $response->sharedMaxAge(3600);
  1146. $response->mustRevalidate(true);
  1147. $this->assertEquals('s-maxage=3600, must-revalidate', $response->getHeaderLine('Cache-Control'));
  1148. });
  1149. }
  1150. /**
  1151. * Tests setting of must-revalidate Cache-Control directive
  1152. *
  1153. * @return void
  1154. */
  1155. public function testWithMustRevalidate()
  1156. {
  1157. $response = new Response();
  1158. $this->assertFalse($response->hasHeader('Cache-Control'));
  1159. $new = $response->withMustRevalidate(true);
  1160. $this->assertFalse($response->hasHeader('Cache-Control'));
  1161. $this->assertEquals('must-revalidate', $new->getHeaderLine('Cache-Control'));
  1162. $new = $new->withMustRevalidate(false);
  1163. $this->assertEmpty($new->getHeaderLine('Cache-Control'));
  1164. }
  1165. /**
  1166. * Tests getting/setting the Vary header
  1167. *
  1168. * @group deprecated
  1169. * @return void
  1170. */
  1171. public function testVary()
  1172. {
  1173. $this->deprecated(function () {
  1174. $response = new Response();
  1175. $response->vary('Accept-encoding');
  1176. $this->assertEquals('Accept-encoding', $response->getHeaderLine('vary'));
  1177. $response = new Response();
  1178. $response->vary(['Accept-language', 'Accept-encoding']);
  1179. $this->assertEquals(['Accept-language', 'Accept-encoding'], $response->vary());
  1180. $this->assertEquals('Accept-language, Accept-encoding', $response->getHeaderLine('vary'));
  1181. });
  1182. }
  1183. /**
  1184. * Tests withVary()
  1185. *
  1186. * @return void
  1187. */
  1188. public function testWithVary()
  1189. {
  1190. $response = new Response();
  1191. $new = $response->withVary('Accept-encoding');
  1192. $this->assertFalse($response->hasHeader('Vary'));
  1193. $this->assertEquals('Accept-encoding', $new->getHeaderLine('Vary'));
  1194. $new = $response->withVary(['Accept-encoding', 'Accept-language']);
  1195. $this->assertFalse($response->hasHeader('Vary'));
  1196. $this->assertEquals('Accept-encoding,Accept-language', $new->getHeaderLine('Vary'));
  1197. }
  1198. /**
  1199. * Tests getting/setting the Etag header
  1200. *
  1201. * @group deprecated
  1202. * @return void
  1203. */
  1204. public function testEtag()
  1205. {
  1206. $this->deprecated(function () {
  1207. $response = new Response();
  1208. $response->etag('something');
  1209. $this->assertEquals('"something"', $response->etag());
  1210. $this->assertEquals('"something"', $response->getHeaderLine('Etag'));
  1211. $response = new Response();
  1212. $response->etag('something', true);
  1213. $this->assertEquals('W/"something"', $response->etag());
  1214. $this->assertEquals('W/"something"', $response->getHeaderLine('Etag'));
  1215. });
  1216. }
  1217. /**
  1218. * Tests withEtag()
  1219. *
  1220. * @return void
  1221. */
  1222. public function testWithEtag()
  1223. {
  1224. $response = new Response();
  1225. $new = $response->withEtag('something');
  1226. $this->assertFalse($response->hasHeader('Etag'));
  1227. $this->assertEquals('"something"', $new->getHeaderLine('Etag'));
  1228. $new = $response->withEtag('something', true);
  1229. $this->assertEquals('W/"something"', $new->getHeaderLine('Etag'));
  1230. }
  1231. /**
  1232. * Tests that the response is able to be marked as not modified
  1233. *
  1234. * @return void
  1235. */
  1236. public function testNotModified()
  1237. {
  1238. $response = new Response();
  1239. $response = $response->withStringBody('something')
  1240. ->withStatus(200)
  1241. ->withLength(100)
  1242. ->withModified('now');
  1243. $response->notModified();
  1244. $this->assertFalse($response->hasHeader('Content-Length'));
  1245. $this->assertFalse($response->hasHeader('Modified'));
  1246. $this->assertEmpty((string)$response->getBody());
  1247. $this->assertEquals(304, $response->getStatusCode());
  1248. }
  1249. /**
  1250. * Tests withNotModified()
  1251. *
  1252. * @return void
  1253. */
  1254. public function testWithNotModified()
  1255. {
  1256. $response = new Response(['body' => 'something']);
  1257. $response = $response->withLength(100)
  1258. ->withStatus(200)
  1259. ->withHeader('Last-Modified', 'value')
  1260. ->withHeader('Content-Language', 'en-EN')
  1261. ->withHeader('X-things', 'things')
  1262. ->withType('application/json');
  1263. $new = $response->withNotModified();
  1264. $this->assertTrue($response->hasHeader('Content-Language'), 'old instance not changed');
  1265. $this->assertTrue($response->hasHeader('Content-Length'), 'old instance not changed');
  1266. $this->assertFalse($new->hasHeader('Content-Type'));
  1267. $this->assertFalse($new->hasHeader('Content-Length'));
  1268. $this->assertFalse($new->hasHeader('Content-Language'));
  1269. $this->assertFalse($new->hasHeader('Last-Modified'));
  1270. $this->assertSame('things', $new->getHeaderLine('X-things'), 'Other headers are retained');
  1271. $this->assertSame(304, $new->getStatusCode());
  1272. $this->assertSame('', $new->getBody()->getContents());
  1273. }
  1274. /**
  1275. * Test checkNotModified method
  1276. *
  1277. * @return void
  1278. */
  1279. public function testCheckNotModifiedByEtagStar()
  1280. {
  1281. $request = new ServerRequest();
  1282. $request = $request->withHeader('If-None-Match', '*');
  1283. $response = new Response();
  1284. $response = $response->withEtag('something')
  1285. ->withHeader('Content-Length', 99);
  1286. $this->assertTrue($response->checkNotModified($request));
  1287. $this->assertFalse($response->hasHeader('Content-Type'), 'etags match, should be unmodified');
  1288. }
  1289. /**
  1290. * Test checkNotModified method
  1291. *
  1292. * @return void
  1293. */
  1294. public function testCheckNotModifiedByEtagExact()
  1295. {
  1296. $request = new ServerRequest();
  1297. $request = $request->withHeader('If-None-Match', 'W/"something", "other"');
  1298. $response = new Response();
  1299. $response = $response->withEtag('something', true)
  1300. ->withHeader('Content-Length', 99);
  1301. $this->assertTrue($response->checkNotModified($request));
  1302. $this->assertFalse($response->hasHeader('Content-Type'), 'etags match, should be unmodified');
  1303. }
  1304. /**
  1305. * Test checkNotModified method
  1306. *
  1307. * @return void
  1308. */
  1309. public function testCheckNotModifiedByEtagAndTime()
  1310. {
  1311. $request = new ServerRequest();
  1312. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  1313. ->withHeader('If-None-Match', 'W/"something", "other"');
  1314. $response = new Response();
  1315. $response = $response->withModified('2012-01-01 00:00:00')
  1316. ->withEtag('something', true)
  1317. ->withHeader('Content-Length', 99);
  1318. $this->assertTrue($response->checkNotModified($request));
  1319. $this->assertFalse($response->hasHeader('Content-Length'), 'etags match, should be unmodified');
  1320. }
  1321. /**
  1322. * Test checkNotModified method
  1323. *
  1324. * @return void
  1325. */
  1326. public function testCheckNotModifiedByEtagAndTimeMismatch()
  1327. {
  1328. $request = new ServerRequest();
  1329. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  1330. ->withHeader('If-None-Match', 'W/"something", "other"');
  1331. $response = new Response();
  1332. $response = $response->withModified('2012-01-01 00:00:01')
  1333. ->withEtag('something', true)
  1334. ->withHeader('Content-Length', 99);
  1335. $this->assertFalse($response->checkNotModified($request));
  1336. $this->assertTrue($response->hasHeader('Content-Length'), 'timestamp in response is newer');
  1337. }
  1338. /**
  1339. * Test checkNotModified method
  1340. *
  1341. * @return void
  1342. */
  1343. public function testCheckNotModifiedByEtagMismatch()
  1344. {
  1345. $request = new ServerRequest();
  1346. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00')
  1347. ->withHeader('If-None-Match', 'W/"something-else", "other"');
  1348. $response = new Response();
  1349. $response = $response->withModified('2012-01-01 00:00:00')
  1350. ->withEtag('something', true)
  1351. ->withHeader('Content-Length', 99);
  1352. $this->assertFalse($response->checkNotModified($request));
  1353. $this->assertTrue($response->hasHeader('Content-Length'), 'etags do not match');
  1354. }
  1355. /**
  1356. * Test checkNotModified method
  1357. *
  1358. * @return void
  1359. */
  1360. public function testCheckNotModifiedByTime()
  1361. {
  1362. $request = new ServerRequest();
  1363. $request = $request->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
  1364. $response = new Response();
  1365. $response = $response->withModified('2012-01-01 00:00:00')
  1366. ->withHeader('Content-Length', 99);
  1367. $this->assertTrue($response->checkNotModified($request));
  1368. $this->assertFalse($response->hasHeader('Content-Length'), 'modified time matches');
  1369. }
  1370. /**
  1371. * Test checkNotModified method
  1372. *
  1373. * @return void
  1374. */
  1375. public function testCheckNotModifiedNoHints()
  1376. {
  1377. $request = new ServerRequest();
  1378. $request = $request->withHeader('If-None-Match', 'W/"something", "other"')
  1379. ->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
  1380. $response = $this->getMockBuilder('Cake\Http\Response')
  1381. ->setMethods(['notModified'])
  1382. ->getMock();
  1383. $response->expects($this->never())->method('notModified');
  1384. $this->assertFalse($response->checkNotModified($request));
  1385. }
  1386. /**
  1387. * Test cookie setting
  1388. *
  1389. * @group deprecated
  1390. * @return void
  1391. */
  1392. public function testCookieSettings()
  1393. {
  1394. $this->deprecated(function () {
  1395. $response = new Response();
  1396. $cookie = [
  1397. 'name' => 'CakeTestCookie[Testing]',
  1398. ];
  1399. $response->cookie($cookie);
  1400. $expected = [
  1401. 'name' => 'CakeTestCookie[Testing]',
  1402. 'value' => '',
  1403. 'expire' => 0,
  1404. 'path' => '/',
  1405. 'domain' => '',
  1406. 'secure' => false,
  1407. 'httpOnly' => false,
  1408. 'samesite' => null,
  1409. ];
  1410. $result = $response->cookie('CakeTestCookie[Testing]');
  1411. $this->assertEquals($expected, $result);
  1412. $cookie = [
  1413. 'name' => 'CakeTestCookie[Testing2]',
  1414. 'value' => '[a,b,c]',
  1415. 'expire' => 1000,
  1416. 'path' => '/test',
  1417. 'secure' => true,
  1418. ];
  1419. $response->cookie($cookie);
  1420. $expected = [
  1421. 'CakeTestCookie[Testing]' => [
  1422. 'name' => 'CakeTestCookie[Testing]',
  1423. 'value' => '',
  1424. 'expire' => 0,
  1425. 'path' => '/',
  1426. 'domain' => '',
  1427. 'secure' => false,
  1428. 'httpOnly' => false,
  1429. 'samesite' => null,
  1430. ],
  1431. 'CakeTestCookie[Testing2]' => [
  1432. 'name' => 'CakeTestCookie[Testing2]',
  1433. 'value' => '[a,b,c]',
  1434. 'expire' => 1000,
  1435. 'path' => '/test',
  1436. 'domain' => '',
  1437. 'secure' => true,
  1438. 'httpOnly' => false,
  1439. 'samesite' => null,
  1440. ],
  1441. ];
  1442. $result = $response->cookie();
  1443. $this->assertEquals($expected, $result);
  1444. $cookie = $expected['CakeTestCookie[Testing]'];
  1445. $cookie['value'] = 'test';
  1446. $response->cookie($cookie);
  1447. $expected = [
  1448. 'CakeTestCookie[Testing]' => [
  1449. 'name' => 'CakeTestCookie[Testing]',
  1450. 'value' => 'test',
  1451. 'expire' => 0,
  1452. 'path' => '/',
  1453. 'domain' => '',
  1454. 'secure' => false,
  1455. 'httpOnly' => false,
  1456. 'samesite' => null,
  1457. ],
  1458. 'CakeTestCookie[Testing2]' => [
  1459. 'name' => 'CakeTestCookie[Testing2]',
  1460. 'value' => '[a,b,c]',
  1461. 'expire' => 1000,
  1462. 'path' => '/test',
  1463. 'domain' => '',
  1464. 'secure' => true,
  1465. 'httpOnly' => false,
  1466. 'samesite' => null,
  1467. ],
  1468. ];
  1469. $result = $response->cookie();
  1470. $this->assertEquals($expected, $result);
  1471. });
  1472. }
  1473. /**
  1474. * Test setting cookies with no value
  1475. *
  1476. * @return void
  1477. */
  1478. public function testWithCookieEmpty()
  1479. {
  1480. $response = new Response();
  1481. $new = $response->withCookie(new Cookie('testing'));
  1482. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1483. $expected = [
  1484. 'name' => 'testing',
  1485. 'value' => '',
  1486. 'expire' => 0,
  1487. 'path' => '/',
  1488. 'domain' => '',
  1489. 'secure' => false,
  1490. 'httpOnly' => false,
  1491. 'samesite' => null,
  1492. ];
  1493. $result = $new->getCookie('testing');
  1494. $this->assertEquals($expected, $result);
  1495. }
  1496. /**
  1497. * Test setting cookies with scalar values
  1498. *
  1499. * @return void
  1500. */
  1501. public function testWithCookieScalar()
  1502. {
  1503. $response = new Response();
  1504. $new = $response->withCookie(new Cookie('testing', 'abc123'));
  1505. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1506. $this->assertEquals('abc123', $new->getCookie('testing')['value']);
  1507. $new = $response->withCookie(new Cookie('testing', 99));
  1508. $this->assertEquals(99, $new->getCookie('testing')['value']);
  1509. $new = $response->withCookie(new Cookie('testing', false));
  1510. $this->assertFalse($new->getCookie('testing')['value']);
  1511. $new = $response->withCookie(new Cookie('testing', true));
  1512. $this->assertTrue($new->getCookie('testing')['value']);
  1513. }
  1514. /**
  1515. * Test withCookie() and duplicate data
  1516. *
  1517. * @return void
  1518. * @throws \Exception
  1519. */
  1520. public function testWithDuplicateCookie()
  1521. {
  1522. $expiry = new \DateTimeImmutable('+24 hours');
  1523. $response = new Response();
  1524. $cookie = new Cookie(
  1525. 'testing',
  1526. '[a,b,c]',
  1527. $expiry,
  1528. '/test',
  1529. '',
  1530. true
  1531. );
  1532. $new = $response->withCookie($cookie);
  1533. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1534. $expected = [
  1535. 'name' => 'testing',
  1536. 'value' => '[a,b,c]',
  1537. 'expire' => $expiry,
  1538. 'path' => '/test',
  1539. 'domain' => '',
  1540. 'secure' => true,
  1541. 'httpOnly' => false,
  1542. 'samesite' => null,
  1543. ];
  1544. // Match the date time formatting to Response::convertCookieToArray
  1545. $expected['expire'] = $expiry->format('U');
  1546. $this->assertEquals($expected, $new->getCookie('testing'));
  1547. }
  1548. /**
  1549. * Test withCookie() and a cookie instance
  1550. *
  1551. * @return void
  1552. */
  1553. public function testWithCookieObject()
  1554. {
  1555. $response = new Response();
  1556. $cookie = new Cookie('yay', 'a value');
  1557. $new = $response->withCookie($cookie);
  1558. $this->assertNull($response->getCookie('yay'), 'withCookie does not mutate');
  1559. $this->assertNotEmpty($new->getCookie('yay'));
  1560. $this->assertSame($cookie, $new->getCookieCollection()->get('yay'));
  1561. }
  1562. public function testWithExpiredCookieScalar()
  1563. {
  1564. $response = new Response();
  1565. $response = $response->withCookie(new Cookie('testing', 'abc123'));
  1566. $this->assertEquals('abc123', $response->getCookie('testing')['value']);
  1567. $new = $response->withExpiredCookie(new Cookie('testing'));
  1568. $this->assertNull($response->getCookie('testing')['expire']);
  1569. $this->assertLessThan(FrozenTime::createFromTimestamp(1), $new->getCookie('testing')['expire']);
  1570. }
  1571. /**
  1572. * @throws \Exception If DateImmutable emits an error.
  1573. */
  1574. public function testWithExpiredCookieOptions()
  1575. {
  1576. $options = [
  1577. 'name' => 'testing',
  1578. 'value' => 'abc123',
  1579. 'domain' => 'cakephp.org',
  1580. 'path' => '/custompath/',
  1581. 'secure' => true,
  1582. 'httpOnly' => true,
  1583. 'expire' => new \DateTimeImmutable('+14 days'),
  1584. 'samesite' => null,
  1585. ];
  1586. $cookie = new Cookie(
  1587. $options['name'],
  1588. $options['value'],
  1589. $options['expire'],
  1590. $options['path'],
  1591. $options['domain'],
  1592. $options['secure'],
  1593. $options['httpOnly']
  1594. );
  1595. $response = new Response();
  1596. $response = $response->withCookie($cookie);
  1597. // Change the timestamp format to match the Response::convertCookieToArray
  1598. $options['expire'] = $options['expire']->format('U');
  1599. $this->assertEquals($options, $response->getCookie('testing'));
  1600. $expiredCookie = $response->withExpiredCookie($cookie);
  1601. $this->assertEquals($options['expire'], $response->getCookie('testing')['expire']);
  1602. $this->assertLessThan(Chronos::createFromTimestamp(1), $expiredCookie->getCookie('testing')['expire']);
  1603. }
  1604. public function testWithExpiredCookieObject()
  1605. {
  1606. $response = new Response();
  1607. $cookie = new Cookie('yay', 'a value');
  1608. $response = $response->withCookie($cookie);
  1609. $this->assertEquals('a value', $response->getCookie('yay')['value']);
  1610. $new = $response->withExpiredCookie($cookie);
  1611. $this->assertNull($response->getCookie('yay')['expire']);
  1612. $this->assertEquals('1', $new->getCookie('yay')['expire']);
  1613. }
  1614. /**
  1615. * Test getCookies() and array data.
  1616. *
  1617. * @return void
  1618. */
  1619. public function testGetCookies()
  1620. {
  1621. $response = new Response();
  1622. $new = $response->withCookie(new Cookie('testing', 'a'))
  1623. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  1624. $expected = [
  1625. 'testing' => [
  1626. 'name' => 'testing',
  1627. 'value' => 'a',
  1628. 'expire' => null,
  1629. 'path' => '/',
  1630. 'domain' => '',
  1631. 'secure' => false,
  1632. 'httpOnly' => false,
  1633. 'samesite' => null,
  1634. ],
  1635. 'test2' => [
  1636. 'name' => 'test2',
  1637. 'value' => 'b',
  1638. 'expire' => null,
  1639. 'path' => '/test',
  1640. 'domain' => '',
  1641. 'secure' => true,
  1642. 'httpOnly' => false,
  1643. 'samesite' => null,
  1644. ],
  1645. ];
  1646. $this->assertEquals($expected, $new->getCookies());
  1647. }
  1648. /**
  1649. * Test getCookies() and array data.
  1650. *
  1651. * @return void
  1652. */
  1653. public function testGetCookiesArrayValue()
  1654. {
  1655. $response = new Response();
  1656. $cookie = (new Cookie('urmc'))
  1657. ->withValue(['user_id' => 1, 'token' => 'abc123'])
  1658. ->withHttpOnly(true);
  1659. $new = $response->withCookie($cookie);
  1660. $expected = [
  1661. 'urmc' => [
  1662. 'name' => 'urmc',
  1663. 'value' => '{"user_id":1,"token":"abc123"}',
  1664. 'expire' => null,
  1665. 'path' => '/',
  1666. 'domain' => '',
  1667. 'secure' => false,
  1668. 'httpOnly' => true,
  1669. 'samesite' => null,
  1670. ],
  1671. ];
  1672. $this->assertEquals($expected, $new->getCookies());
  1673. }
  1674. /**
  1675. * Test getCookieCollection() as array data
  1676. *
  1677. * @return void
  1678. */
  1679. public function testGetCookieCollection()
  1680. {
  1681. $response = new Response();
  1682. $new = $response->withCookie(new Cookie('testing', 'a'))
  1683. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  1684. $cookies = $response->getCookieCollection();
  1685. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1686. $this->assertCount(0, $cookies, 'Original response not mutated');
  1687. $cookies = $new->getCookieCollection();
  1688. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1689. $this->assertCount(2, $cookies);
  1690. $this->assertTrue($cookies->has('testing'));
  1691. $this->assertTrue($cookies->has('test2'));
  1692. }
  1693. /**
  1694. * Test withCookieCollection()
  1695. *
  1696. * @return void
  1697. */
  1698. public function testWithCookieCollection()
  1699. {
  1700. $response = new Response();
  1701. $collection = new CookieCollection([new Cookie('foo', 'bar')]);
  1702. $newResponse = $response->withCookieCollection($collection);
  1703. $this->assertNotSame($response, $newResponse);
  1704. $this->assertNotSame($response->getCookieCollection(), $newResponse->getCookieCollection());
  1705. $this->assertSame($newResponse->getCookie('foo')['value'], 'bar');
  1706. }
  1707. /**
  1708. * Test that cors() returns a builder.
  1709. *
  1710. * @return void
  1711. */
  1712. public function testCors()
  1713. {
  1714. $request = new ServerRequest([
  1715. 'environment' => ['HTTP_ORIGIN' => 'http://example.com'],
  1716. ]);
  1717. $response = new Response();
  1718. $builder = $response->cors($request);
  1719. $this->assertInstanceOf(CorsBuilder::class, $builder);
  1720. $this->assertSame($response, $builder->build(), 'Empty builder returns same object');
  1721. }
  1722. /**
  1723. * Test CORS with additional parameters
  1724. *
  1725. * @group deprecated
  1726. * @dataProvider corsData
  1727. * @param Request $request
  1728. * @param string $origin
  1729. * @param string|array $domains
  1730. * @param string|array $methods
  1731. * @param string|array $headers
  1732. * @param string|bool $expectedOrigin
  1733. * @param string|bool $expectedMethods
  1734. * @param string|bool $expectedHeaders
  1735. * @return void
  1736. */
  1737. public function testCorsParameters($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false)
  1738. {
  1739. $this->deprecated(function () use ($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods, $expectedHeaders) {
  1740. $request = $request->withEnv('HTTP_ORIGIN', $origin);
  1741. $response = new Response();
  1742. $result = $response->cors($request, $domains, $methods, $headers);
  1743. $this->assertInstanceOf('Cake\Network\CorsBuilder', $result);
  1744. if ($expectedOrigin) {
  1745. $this->assertTrue($response->hasHeader('Access-Control-Allow-Origin'));
  1746. $this->assertEquals($expectedOrigin, $response->getHeaderLine('Access-Control-Allow-Origin'));
  1747. }
  1748. if ($expectedMethods) {
  1749. $this->assertTrue($response->hasHeader('Access-Control-Allow-Methods'));
  1750. $this->assertEquals($expectedMethods, $response->getHeaderLine('Access-Control-Allow-Methods'));
  1751. }
  1752. if ($expectedHeaders) {
  1753. $this->assertTrue($response->hasHeader('Access-Control-Allow-Headers'));
  1754. $this->assertEquals($expectedHeaders, $response->getHeaderLine('Access-Control-Allow-Headers'));
  1755. }
  1756. });
  1757. }
  1758. /**
  1759. * Feed for testCors
  1760. *
  1761. * @return array
  1762. */
  1763. public function corsData()
  1764. {
  1765. $fooRequest = new ServerRequest();
  1766. $secureRequest = function () {
  1767. $secureRequest = $this->getMockBuilder('Cake\Http\ServerRequest')
  1768. ->setMethods(['is'])
  1769. ->getMock();
  1770. $secureRequest->expects($this->any())
  1771. ->method('is')
  1772. ->with('ssl')
  1773. ->will($this->returnValue(true));
  1774. return $secureRequest;
  1775. };
  1776. return [
  1777. [$fooRequest, null, '*', '', '', false, false],
  1778. [$fooRequest, 'http://www.foo.com', '*', '', '', '*', false],
  1779. [$fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false],
  1780. [$fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false],
  1781. [$fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false],
  1782. [$fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false],
  1783. [$fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false],
  1784. [$fooRequest, 'http://www.foo.com', ['*.bar.com', '*.foo.com'], '', '', 'http://www.foo.com', false],
  1785. [$fooRequest, 'http://not-foo.com', '*.foo.com', '', '', false, false],
  1786. [$fooRequest, 'http://bad.academy', '*.acad.my', '', '', false, false],
  1787. [$fooRequest, 'http://www.foo.com.at.bad.com', '*.foo.com', '', '', false, false],
  1788. [$fooRequest, 'https://www.foo.com', '*.foo.com', '', '', false, false],
  1789. [$secureRequest(), 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false],
  1790. [$secureRequest(), 'https://www.bar.com', 'http://www.bar.com', '', '', false, false],
  1791. [$secureRequest(), 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false],
  1792. [$secureRequest(), 'http://www.bar.com', '*.bar.com', '', '', false, false],
  1793. [$fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'],
  1794. [$fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'],
  1795. [$fooRequest, 'http://www.foo.com', '*.foo.com', ['GET', 'POST'], '', 'http://www.foo.com', 'GET, POST'],
  1796. [$fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'],
  1797. [$fooRequest, 'http://www.foo.com', '*', '', ['X-CakePHP', 'X-MyApp'], '*', false, 'X-CakePHP, X-MyApp'],
  1798. [$fooRequest, 'http://www.foo.com', '*', ['GET', 'OPTIONS'], ['X-CakePHP', 'X-MyApp'], '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'],
  1799. ];
  1800. }
  1801. /**
  1802. * testFileNotFound
  1803. *
  1804. * @group deprecated
  1805. * @return void
  1806. */
  1807. public function testFileNotFound()
  1808. {
  1809. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  1810. $this->deprecated(function () {
  1811. $response = new Response();
  1812. $response->file('/some/missing/folder/file.jpg');
  1813. });
  1814. }
  1815. /**
  1816. * test withFile() not found
  1817. *
  1818. * Don't remove this test when cleaning up deprecation warnings.
  1819. * Just remove the deprecated wrapper.
  1820. *
  1821. * @return void
  1822. */
  1823. public function testWithFileNotFound()
  1824. {
  1825. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  1826. $this->deprecated(function () {
  1827. $response = new Response();
  1828. $response->withFile('/some/missing/folder/file.jpg');
  1829. });
  1830. }
  1831. /**
  1832. * Provider for various kinds of unacceptable files.
  1833. *
  1834. * @return array
  1835. */
  1836. public function invalidFileProvider()
  1837. {
  1838. return [
  1839. ['my/../cat.gif', 'The requested file contains `..` and will not be read.'],
  1840. ['my\..\cat.gif', 'The requested file contains `..` and will not be read.'],
  1841. ['my/ca..t.gif', 'my/ca..t.gif was not found or not readable'],
  1842. ['my/ca..t/image.gif', 'my/ca..t/image.gif was not found or not readable'],
  1843. ];
  1844. }
  1845. /**
  1846. * test invalid file paths.
  1847. *
  1848. * @group deprecated
  1849. * @dataProvider invalidFileProvider
  1850. * @return void
  1851. */
  1852. public function testFileInvalidPath($path, $expectedMessage)
  1853. {
  1854. $this->deprecated(function () use ($path, $expectedMessage) {
  1855. $response = new Response();
  1856. try {
  1857. $response->file($path);
  1858. } catch (NotFoundException $e) {
  1859. $this->assertContains($expectedMessage, $e->getMessage());
  1860. }
  1861. });
  1862. }
  1863. /**
  1864. * test withFile and invalid paths
  1865. *
  1866. * This test should not be removed when deprecation warnings are removed.
  1867. * Just remove the deprecated wrapper.
  1868. *
  1869. * @dataProvider invalidFileProvider
  1870. * @return void
  1871. */
  1872. public function testWithFileInvalidPath($path, $expectedMessage)
  1873. {
  1874. $this->deprecated(function () use ($path, $expectedMessage) {
  1875. $response = new Response();
  1876. try {
  1877. $response->withFile($path);
  1878. } catch (NotFoundException $e) {
  1879. $this->assertContains($expectedMessage, $e->getMessage());
  1880. }
  1881. });
  1882. }
  1883. /**
  1884. * testFile method
  1885. *
  1886. * @group deprecated
  1887. * @return void
  1888. */
  1889. public function testFile()
  1890. {
  1891. $this->deprecated(function () {
  1892. $response = $this->getMockBuilder('Cake\Http\Response')
  1893. ->setMethods(['_sendHeader', '_isActive'])
  1894. ->getMock();
  1895. $response->expects($this->exactly(1))
  1896. ->method('_isActive')
  1897. ->will($this->returnValue(true));
  1898. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1899. ob_start();
  1900. $result = $response->send();
  1901. $this->assertTrue($result !== false);
  1902. $output = ob_get_clean();
  1903. $expected = '/* this is the test asset css file */';
  1904. $this->assertEquals($expected, trim($output));
  1905. $this->assertEquals($expected, trim($response->getBody()->getContents()));
  1906. $this->assertEquals('text/css', $response->getType());
  1907. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  1908. });
  1909. }
  1910. /**
  1911. * test withFile() + download & name
  1912. *
  1913. * @return void
  1914. */
  1915. public function testWithFileDownloadAndName()
  1916. {
  1917. $response = new Response();
  1918. $new = $response->withFile(
  1919. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1920. [
  1921. 'name' => 'something_special.css',
  1922. 'download' => true,
  1923. ]
  1924. );
  1925. $this->assertEquals(
  1926. 'text/html; charset=UTF-8',
  1927. $response->getHeaderLine('Content-Type'),
  1928. 'No mutation'
  1929. );
  1930. $this->assertEquals(
  1931. 'text/css; charset=UTF-8',
  1932. $new->getHeaderLine('Content-Type')
  1933. );
  1934. $this->assertEquals(
  1935. 'attachment; filename="something_special.css"',
  1936. $new->getHeaderLine('Content-Disposition')
  1937. );
  1938. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  1939. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1940. $body = $new->getBody();
  1941. $this->assertInstanceOf('Laminas\Diactoros\Stream', $body);
  1942. $expected = '/* this is the test asset css file */';
  1943. $this->assertEquals($expected, trim($body->getContents()));
  1944. $this->assertEquals($expected, trim($new->getFile()->read()));
  1945. }
  1946. /**
  1947. * testFileWithDownloadAndName
  1948. *
  1949. * @group deprecated
  1950. * @return void
  1951. */
  1952. public function testFileWithDownloadAndName()
  1953. {
  1954. $this->deprecated(function () {
  1955. $response = $this->getMockBuilder('Cake\Http\Response')
  1956. ->setMethods(['download', '_sendHeader', '_isActive'])
  1957. ->getMock();
  1958. $response->expects($this->once())
  1959. ->method('download')
  1960. ->with('something_special.css');
  1961. $response->expects($this->exactly(1))
  1962. ->method('_isActive')
  1963. ->will($this->returnValue(true));
  1964. $response->file(
  1965. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1966. [
  1967. 'name' => 'something_special.css',
  1968. 'download' => true,
  1969. ]
  1970. );
  1971. ob_start();
  1972. $result = $response->send();
  1973. $output = ob_get_clean();
  1974. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1975. $this->assertNotFalse($result);
  1976. $this->assertEquals('text/css', $response->getType());
  1977. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  1978. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  1979. });
  1980. }
  1981. /**
  1982. * testFileWithUnknownFileTypeGeneric method
  1983. *
  1984. * @group deprecated
  1985. * @return void
  1986. */
  1987. public function testFileWithUnknownFileTypeGeneric()
  1988. {
  1989. $this->deprecated(function () {
  1990. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1991. $response = $this->getMockBuilder('Cake\Http\Response')
  1992. ->setMethods(['download', '_sendHeader', '_isActive'])
  1993. ->getMock();
  1994. $response->expects($this->once())
  1995. ->method('download')
  1996. ->with('no_section.ini');
  1997. $response->expects($this->exactly(1))
  1998. ->method('_isActive')
  1999. ->will($this->returnValue(true));
  2000. $response->file(CONFIG . 'no_section.ini');
  2001. ob_start();
  2002. $result = $response->send();
  2003. $output = ob_get_clean();
  2004. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  2005. $this->assertNotFalse($result);
  2006. $this->assertEquals('text/html', $response->getType());
  2007. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2008. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2009. });
  2010. }
  2011. /**
  2012. * test withFile() + a generic agent
  2013. *
  2014. * @return void
  2015. */
  2016. public function testWithFileUnknownFileTypeGeneric()
  2017. {
  2018. $response = new Response();
  2019. $new = $response->withFile(CONFIG . 'no_section.ini');
  2020. $this->assertEquals('text/html; charset=UTF-8', $new->getHeaderLine('Content-Type'));
  2021. $this->assertEquals(
  2022. 'attachment; filename="no_section.ini"',
  2023. $new->getHeaderLine('Content-Disposition')
  2024. );
  2025. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2026. $body = $new->getBody();
  2027. $expected = "some_key = some_value\nbool_key = 1\n";
  2028. $this->assertEquals($expected, $body->getContents());
  2029. }
  2030. /**
  2031. * testFileWithUnknownFileTypeOpera method
  2032. *
  2033. * @group deprecated
  2034. * @return void
  2035. */
  2036. public function testFileWithUnknownFileTypeOpera()
  2037. {
  2038. $this->deprecated(function () {
  2039. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  2040. $response = $this->getMockBuilder('Cake\Http\Response')
  2041. ->setMethods(['download', '_sendHeader', '_isActive'])
  2042. ->getMock();
  2043. $response->expects($this->once())
  2044. ->method('download')
  2045. ->with('no_section.ini');
  2046. $response->expects($this->exactly(1))
  2047. ->method('_isActive')
  2048. ->will($this->returnValue(true));
  2049. $response->file(CONFIG . 'no_section.ini');
  2050. ob_start();
  2051. $result = $response->send();
  2052. $output = ob_get_clean();
  2053. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  2054. $this->assertNotFalse($result);
  2055. $this->assertEquals('application/octet-stream', $response->getType());
  2056. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2057. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2058. });
  2059. }
  2060. /**
  2061. * test withFile() + opera
  2062. *
  2063. * @return void
  2064. */
  2065. public function testWithFileUnknownFileTypeOpera()
  2066. {
  2067. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  2068. $response = new Response();
  2069. $new = $response->withFile(CONFIG . 'no_section.ini');
  2070. $this->assertEquals('application/octet-stream', $new->getHeaderLine('Content-Type'));
  2071. $this->assertEquals(
  2072. 'attachment; filename="no_section.ini"',
  2073. $new->getHeaderLine('Content-Disposition')
  2074. );
  2075. }
  2076. /**
  2077. * testFileWithUnknownFileTypeIE method
  2078. *
  2079. * @group deprecated
  2080. * @return void
  2081. */
  2082. public function testFileWithUnknownFileTypeIE()
  2083. {
  2084. $this->deprecated(function () {
  2085. $_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)';
  2086. $response = $this->getMockBuilder('Cake\Http\Response')
  2087. ->setMethods(['download', '_isActive', '_sendHeade'])
  2088. ->getMock();
  2089. $response->expects($this->once())
  2090. ->method('download')
  2091. ->with('config.ini');
  2092. $response->expects($this->exactly(1))
  2093. ->method('_isActive')
  2094. ->will($this->returnValue(true));
  2095. $response->file(CONFIG . 'no_section.ini', [
  2096. 'name' => 'config.ini',
  2097. ]);
  2098. ob_start();
  2099. $result = $response->send();
  2100. $output = ob_get_clean();
  2101. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  2102. $this->assertNotFalse($result);
  2103. $this->assertEquals('application/force-download', $response->getType());
  2104. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2105. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2106. });
  2107. }
  2108. /**
  2109. * test withFile() + old IE
  2110. *
  2111. * @return void
  2112. */
  2113. public function testWithFileUnknownFileTypeOldIe()
  2114. {
  2115. $_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)';
  2116. $response = new Response();
  2117. $new = $response->withFile(CONFIG . 'no_section.ini');
  2118. $this->assertEquals('application/force-download', $new->getHeaderLine('Content-Type'));
  2119. }
  2120. /**
  2121. * testFileWithUnknownFileNoDownload method
  2122. *
  2123. * @group deprecated
  2124. * @return void
  2125. */
  2126. public function testFileWithUnknownFileNoDownload()
  2127. {
  2128. $this->deprecated(function () {
  2129. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  2130. $response = $this->getMockBuilder('Cake\Http\Response')
  2131. ->setMethods(['download'])
  2132. ->getMock();
  2133. $response->expects($this->never())
  2134. ->method('download');
  2135. $response->file(CONFIG . 'no_section.ini', [
  2136. 'download' => false,
  2137. ]);
  2138. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2139. $this->assertEquals('text/html', $response->getType());
  2140. });
  2141. }
  2142. /**
  2143. * test withFile() + no download
  2144. *
  2145. * @return void
  2146. */
  2147. public function testWithFileNoDownload()
  2148. {
  2149. $response = new Response();
  2150. $new = $response->withFile(CONFIG . 'no_section.ini', [
  2151. 'download' => false,
  2152. ]);
  2153. $this->assertEquals(
  2154. 'text/html; charset=UTF-8',
  2155. $new->getHeaderLine('Content-Type')
  2156. );
  2157. $this->assertFalse($new->hasHeader('Content-Disposition'));
  2158. $this->assertFalse($new->hasHeader('Content-Transfer-Encoding'));
  2159. }
  2160. /**
  2161. * test getFile method
  2162. *
  2163. * @group deprecated
  2164. * @return void
  2165. */
  2166. public function testGetFile()
  2167. {
  2168. $this->deprecated(function () {
  2169. $response = new Response();
  2170. $this->assertNull($response->getFile(), 'No file to get');
  2171. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  2172. $file = $response->getFile();
  2173. $this->assertInstanceOf('Cake\Filesystem\File', $file, 'Should get a file');
  2174. $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path);
  2175. });
  2176. }
  2177. /**
  2178. * testConnectionAbortedOnBuffering method
  2179. *
  2180. * @group deprecated
  2181. * @return void
  2182. */
  2183. public function testConnectionAbortedOnBuffering()
  2184. {
  2185. $this->deprecated(function () {
  2186. $response = $this->getMockBuilder('Cake\Http\Response')
  2187. ->setMethods([
  2188. 'download',
  2189. '_sendHeader',
  2190. '_isActive',
  2191. ])
  2192. ->getMock();
  2193. $response->expects($this->at(0))
  2194. ->method('_isActive')
  2195. ->will($this->returnValue(false));
  2196. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  2197. ob_start();
  2198. $result = $response->send();
  2199. ob_end_clean();
  2200. $this->assertNull($result);
  2201. $this->assertEquals('text/css', $response->getType());
  2202. $this->assertFalse($response->hasHeader('Content-Range'));
  2203. });
  2204. }
  2205. /**
  2206. * Test downloading files with UPPERCASE extensions.
  2207. *
  2208. * @group deprecated
  2209. * @return void
  2210. */
  2211. public function testFileUpperExtension()
  2212. {
  2213. $this->deprecated(function () {
  2214. $response = new Response();
  2215. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  2216. $this->assertSame('image/jpeg', $response->getType());
  2217. });
  2218. }
  2219. /**
  2220. * Test that uppercase extensions result in correct content-types
  2221. *
  2222. * @return void
  2223. */
  2224. public function testWithFileUpperExtension()
  2225. {
  2226. $response = new Response();
  2227. $new = $response->withFile(TEST_APP . 'vendor/img/test_2.JPG');
  2228. $this->assertEquals('image/jpeg', $new->getHeaderLine('Content-Type'));
  2229. }
  2230. /**
  2231. * A data provider for testing various ranges
  2232. *
  2233. * @return array
  2234. */
  2235. public static function rangeProvider()
  2236. {
  2237. return [
  2238. // suffix-byte-range
  2239. [
  2240. 'bytes=-25', 25, 'bytes 13-37/38',
  2241. ],
  2242. [
  2243. 'bytes=0-', 38, 'bytes 0-37/38',
  2244. ],
  2245. [
  2246. 'bytes=10-', 28, 'bytes 10-37/38',
  2247. ],
  2248. [
  2249. 'bytes=10-20', 11, 'bytes 10-20/38',
  2250. ],
  2251. // Spaced out
  2252. [
  2253. 'bytes = 10 - 20', 11, 'bytes 10-20/38',
  2254. ],
  2255. ];
  2256. }
  2257. /**
  2258. * Test the various range offset types.
  2259. *
  2260. * @group deprecated
  2261. * @dataProvider rangeProvider
  2262. * @return void
  2263. */
  2264. public function testFileRangeOffsets($range, $length, $offsetResponse)
  2265. {
  2266. $this->deprecated(function () use ($range, $length, $offsetResponse) {
  2267. $_SERVER['HTTP_RANGE'] = $range;
  2268. $response = $this->getMockBuilder('Cake\Http\Response')
  2269. ->setMethods([
  2270. '_sendHeader',
  2271. '_isActive',
  2272. ])
  2273. ->getMock();
  2274. $response->expects($this->any())
  2275. ->method('_isActive')
  2276. ->will($this->returnValue(true));
  2277. $response->file(
  2278. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2279. ['download' => true]
  2280. );
  2281. ob_start();
  2282. $result = $response->send();
  2283. ob_get_clean();
  2284. $this->assertEquals(
  2285. 'attachment; filename="test_asset.css"',
  2286. $response->getHeaderLine('Content-Disposition')
  2287. );
  2288. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2289. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2290. $this->assertEquals($length, $response->getHeaderLine('Content-Length'));
  2291. $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range'));
  2292. });
  2293. }
  2294. /**
  2295. * Test withFile() & the various range offset types.
  2296. *
  2297. * @dataProvider rangeProvider
  2298. * @return void
  2299. */
  2300. public function testWithFileRangeOffsets($range, $length, $offsetResponse)
  2301. {
  2302. $_SERVER['HTTP_RANGE'] = $range;
  2303. $response = new Response();
  2304. $new = $response->withFile(
  2305. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2306. ['download' => true]
  2307. );
  2308. $this->assertEquals(
  2309. 'attachment; filename="test_asset.css"',
  2310. $new->getHeaderLine('Content-Disposition')
  2311. );
  2312. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2313. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2314. $this->assertEquals($length, $new->getHeaderLine('Content-Length'));
  2315. $this->assertEquals($offsetResponse, $new->getHeaderLine('Content-Range'));
  2316. }
  2317. /**
  2318. * Test fetching ranges from a file.
  2319. *
  2320. * @group deprecated
  2321. * @return void
  2322. */
  2323. public function testFileRange()
  2324. {
  2325. $this->deprecated(function () {
  2326. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2327. $response = $this->getMockBuilder('Cake\Http\Response')
  2328. ->setMethods([
  2329. '_sendHeader',
  2330. '_isActive',
  2331. ])
  2332. ->getMock();
  2333. $response->expects($this->any())
  2334. ->method('_isActive')
  2335. ->will($this->returnValue(true));
  2336. $response->file(
  2337. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2338. ['download' => true]
  2339. );
  2340. ob_start();
  2341. $result = $response->send();
  2342. $output = ob_get_clean();
  2343. $this->assertEquals('is the test asset ', $output);
  2344. $this->assertNotFalse($result);
  2345. $this->assertEquals(
  2346. 'attachment; filename="test_asset.css"',
  2347. $response->getHeaderLine('Content-Disposition')
  2348. );
  2349. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2350. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2351. $this->assertEquals('18', $response->getHeaderLine('Content-Length'));
  2352. $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range'));
  2353. $this->assertEquals(206, $response->getStatusCode());
  2354. });
  2355. }
  2356. /**
  2357. * Test withFile() fetching ranges from a file.
  2358. *
  2359. * @return void
  2360. */
  2361. public function testWithFileRange()
  2362. {
  2363. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2364. $response = new Response();
  2365. $new = $response->withFile(
  2366. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2367. ['download' => true]
  2368. );
  2369. $this->assertEquals(
  2370. 'attachment; filename="test_asset.css"',
  2371. $new->getHeaderLine('Content-Disposition')
  2372. );
  2373. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2374. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2375. $this->assertEquals('18', $new->getHeaderLine('Content-Length'));
  2376. $this->assertEquals('bytes 8-25/38', $new->getHeaderLine('Content-Range'));
  2377. $this->assertEquals(206, $new->getStatusCode());
  2378. }
  2379. /**
  2380. * Provider for invalid range header values.
  2381. *
  2382. * @return array
  2383. */
  2384. public function invalidFileRangeProvider()
  2385. {
  2386. return [
  2387. // malformed range
  2388. [
  2389. 'bytes=0,38',
  2390. ],
  2391. // malformed punctuation
  2392. [
  2393. 'bytes: 0 - 38',
  2394. ],
  2395. ];
  2396. }
  2397. /**
  2398. * Test invalid file ranges.
  2399. *
  2400. * @group deprecated
  2401. * @dataProvider invalidFileRangeProvider
  2402. * @return void
  2403. */
  2404. public function testFileRangeInvalid($range)
  2405. {
  2406. $this->deprecated(function () use ($range) {
  2407. $_SERVER['HTTP_RANGE'] = $range;
  2408. $response = $this->getMockBuilder('Cake\Http\Response')
  2409. ->setMethods([
  2410. '_sendHeader',
  2411. '_isActive',
  2412. ])
  2413. ->getMock();
  2414. $response->file(
  2415. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2416. ['download' => true]
  2417. );
  2418. $this->assertEquals('text/css', $response->getType());
  2419. $this->assertEquals(
  2420. 'attachment; filename="test_asset.css"',
  2421. $response->getHeaderLine('Content-Disposition')
  2422. );
  2423. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2424. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2425. $this->assertEquals('38', $response->getHeaderLine('Content-Length'));
  2426. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2427. });
  2428. }
  2429. /**
  2430. * Test withFile() and invalid ranges
  2431. *
  2432. * @dataProvider invalidFileRangeProvider
  2433. * @return void
  2434. */
  2435. public function testWithFileInvalidRange($range)
  2436. {
  2437. $_SERVER['HTTP_RANGE'] = $range;
  2438. $response = new Response();
  2439. $new = $response->withFile(
  2440. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2441. ['download' => true]
  2442. );
  2443. $this->assertEquals(
  2444. 'attachment; filename="test_asset.css"',
  2445. $new->getHeaderLine('Content-Disposition')
  2446. );
  2447. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2448. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2449. $this->assertEquals('38', $new->getHeaderLine('Content-Length'));
  2450. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2451. $this->assertEquals(206, $new->getStatusCode());
  2452. }
  2453. /**
  2454. * Test reversed file ranges.
  2455. *
  2456. * @group deprecated
  2457. * @return void
  2458. */
  2459. public function testFileRangeReversed()
  2460. {
  2461. $this->deprecated(function () {
  2462. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2463. $response = $this->getMockBuilder('Cake\Http\Response')
  2464. ->setMethods([
  2465. '_sendHeader',
  2466. '_isActive',
  2467. ])
  2468. ->getMock();
  2469. $response->file(
  2470. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2471. ['download' => true]
  2472. );
  2473. $this->assertEquals(416, $response->getStatusCode());
  2474. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2475. $this->assertEquals(
  2476. 'attachment; filename="test_asset.css"',
  2477. $response->getHeaderLine('Content-Disposition')
  2478. );
  2479. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2480. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2481. });
  2482. }
  2483. /**
  2484. * Test withFile() and a reversed range
  2485. *
  2486. * @return void
  2487. */
  2488. public function testWithFileReversedRange()
  2489. {
  2490. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2491. $response = new Response();
  2492. $new = $response->withFile(
  2493. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2494. ['download' => true]
  2495. );
  2496. $this->assertEquals(
  2497. 'attachment; filename="test_asset.css"',
  2498. $new->getHeaderLine('Content-Disposition')
  2499. );
  2500. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2501. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2502. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2503. $this->assertEquals(416, $new->getStatusCode());
  2504. }
  2505. /**
  2506. * testFileRangeOffsetsNoDownload method
  2507. *
  2508. * @group deprecated
  2509. * @dataProvider rangeProvider
  2510. * @return void
  2511. */
  2512. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  2513. {
  2514. $this->deprecated(function () use ($range, $length, $offsetResponse) {
  2515. $_SERVER['HTTP_RANGE'] = $range;
  2516. $response = $this->getMockBuilder('Cake\Http\Response')
  2517. ->setMethods([
  2518. '_sendHeader',
  2519. '_isActive',
  2520. ])
  2521. ->getMock();
  2522. $response->expects($this->any())
  2523. ->method('_isActive')
  2524. ->will($this->returnValue(true));
  2525. $response->file(
  2526. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2527. ['download' => false]
  2528. );
  2529. ob_start();
  2530. $result = $response->send();
  2531. ob_get_clean();
  2532. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2533. $this->assertEquals($length, $response->getHeaderLine('Content-Length'));
  2534. $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range'));
  2535. });
  2536. }
  2537. /**
  2538. * testFileRangeNoDownload method
  2539. *
  2540. * @group deprecated
  2541. * @return void
  2542. */
  2543. public function testFileRangeNoDownload()
  2544. {
  2545. $this->deprecated(function () {
  2546. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2547. $response = $this->getMockBuilder('Cake\Http\Response')
  2548. ->setMethods([
  2549. '_sendHeader',
  2550. '_isActive',
  2551. ])
  2552. ->getMock();
  2553. $response->expects($this->any())
  2554. ->method('_isActive')
  2555. ->will($this->returnValue(true));
  2556. $response->file(
  2557. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2558. ['download' => false]
  2559. );
  2560. ob_start();
  2561. $result = $response->send();
  2562. $output = ob_get_clean();
  2563. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2564. $this->assertEquals('18', $response->getHeaderLine('Content-Length'));
  2565. $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range'));
  2566. $this->assertEquals('text/css', $response->getType());
  2567. $this->assertEquals(206, $response->getStatusCode());
  2568. $this->assertEquals('is the test asset ', $output);
  2569. $this->assertNotFalse($result);
  2570. });
  2571. }
  2572. /**
  2573. * testFileRangeInvalidNoDownload method
  2574. *
  2575. * @group deprecated
  2576. * @return void
  2577. */
  2578. public function testFileRangeInvalidNoDownload()
  2579. {
  2580. $this->deprecated(function () {
  2581. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2582. $response = new Response();
  2583. $response->file(
  2584. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2585. ['download' => false]
  2586. );
  2587. $this->assertEquals(416, $response->statusCode());
  2588. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2589. $this->assertEquals('text/css', $response->getType());
  2590. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2591. });
  2592. }
  2593. /**
  2594. * Test the location method.
  2595. *
  2596. * @group deprecated
  2597. * @return void
  2598. */
  2599. public function testLocation()
  2600. {
  2601. $this->deprecated(function () {
  2602. $response = new Response();
  2603. $this->assertNull($response->location(), 'No header should be set.');
  2604. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  2605. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  2606. });
  2607. }
  2608. /**
  2609. * Test the withLocation method.
  2610. *
  2611. * @return void
  2612. */
  2613. public function testWithLocation()
  2614. {
  2615. $response = new Response();
  2616. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set.');
  2617. $new = $response->withLocation('http://example.org');
  2618. $this->assertNotSame($new, $response);
  2619. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set');
  2620. $this->assertSame('http://example.org', $new->getHeaderLine('Location'), 'Header should be set');
  2621. $this->assertSame(302, $new->getStatusCode(), 'Status should be updated');
  2622. }
  2623. /**
  2624. * Test get protocol version.
  2625. *
  2626. * @return void
  2627. */
  2628. public function getProtocolVersion()
  2629. {
  2630. $response = new Response();
  2631. $version = $response->getProtocolVersion();
  2632. $this->assertEquals('1.1', $version);
  2633. }
  2634. /**
  2635. * Test with protocol.
  2636. *
  2637. * @return void
  2638. */
  2639. public function testWithProtocol()
  2640. {
  2641. $response = new Response();
  2642. $version = $response->getProtocolVersion();
  2643. $this->assertEquals('1.1', $version);
  2644. $response2 = $response->withProtocolVersion('1.0');
  2645. $version = $response2->getProtocolVersion();
  2646. $this->assertEquals('1.0', $version);
  2647. $version = $response->getProtocolVersion();
  2648. $this->assertEquals('1.1', $version);
  2649. $this->assertNotEquals($response, $response2);
  2650. }
  2651. /**
  2652. * Test with status code.
  2653. *
  2654. * @return void
  2655. */
  2656. public function testWithStatusCode()
  2657. {
  2658. $response = new Response();
  2659. $statusCode = $response->getStatusCode();
  2660. $this->assertEquals(200, $statusCode);
  2661. $response2 = $response->withStatus(404);
  2662. $statusCode = $response2->getStatusCode();
  2663. $this->assertEquals(404, $statusCode);
  2664. $statusCode = $response->getStatusCode();
  2665. $this->assertEquals(200, $statusCode);
  2666. $this->assertNotEquals($response, $response2);
  2667. $response3 = $response->withStatus(111);
  2668. $this->assertEquals(111, $response3->getStatusCode());
  2669. $this->assertSame('', $response3->getReasonPhrase());
  2670. }
  2671. /**
  2672. * Test invalid status codes
  2673. *
  2674. * @return void
  2675. */
  2676. public function testWithStatusInvalid()
  2677. {
  2678. $this->expectException(\InvalidArgumentException::class);
  2679. $this->expectExceptionMessage('Invalid status code: 1001. Use a valid HTTP status code in range 1xx - 5xx.');
  2680. $response = new Response();
  2681. $response->withStatus(1001);
  2682. }
  2683. /**
  2684. * Test get reason phrase.
  2685. *
  2686. * @return void
  2687. */
  2688. public function testGetReasonPhrase()
  2689. {
  2690. $response = new Response();
  2691. $this->assertSame('OK', $response->getReasonPhrase());
  2692. $response = $response->withStatus(404);
  2693. $reasonPhrase = $response->getReasonPhrase();
  2694. $this->assertEquals('Not Found', $reasonPhrase);
  2695. }
  2696. /**
  2697. * Test with body.
  2698. *
  2699. * @return void
  2700. */
  2701. public function testWithBody()
  2702. {
  2703. $response = new Response();
  2704. $body = $response->getBody();
  2705. $body->rewind();
  2706. $result = $body->getContents();
  2707. $this->assertEquals('', $result);
  2708. $stream = new Stream('php://memory', 'wb+');
  2709. $stream->write('test1');
  2710. $response2 = $response->withBody($stream);
  2711. $body = $response2->getBody();
  2712. $body->rewind();
  2713. $result = $body->getContents();
  2714. $this->assertEquals('test1', $result);
  2715. $body = $response->getBody();
  2716. $body->rewind();
  2717. $result = $body->getContents();
  2718. $this->assertEquals('', $result);
  2719. }
  2720. /**
  2721. * Test with string body.
  2722. *
  2723. * @return void
  2724. */
  2725. public function testWithStringBody()
  2726. {
  2727. $response = new Response();
  2728. $newResponse = $response->withStringBody('Foo');
  2729. $body = $newResponse->getBody();
  2730. $this->assertSame('Foo', (string)$body);
  2731. $this->assertNotSame($response, $newResponse);
  2732. $response = new Response();
  2733. $newResponse = $response->withStringBody('');
  2734. $body = $newResponse->getBody();
  2735. $this->assertSame('', (string)$body);
  2736. $this->assertNotSame($response, $newResponse);
  2737. $response = new Response();
  2738. $newResponse = $response->withStringBody(null);
  2739. $body = $newResponse->getBody();
  2740. $this->assertSame('', (string)$body);
  2741. $this->assertNotSame($response, $newResponse);
  2742. $response = new Response();
  2743. $newResponse = $response->withStringBody(1337);
  2744. $body = $newResponse->getBody();
  2745. $this->assertSame('1337', (string)$body);
  2746. $this->assertNotSame($response, $newResponse);
  2747. }
  2748. /**
  2749. * Test with string body with passed array.
  2750. *
  2751. * This should produce an "Array to string conversion" error
  2752. * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
  2753. *
  2754. * @return void
  2755. */
  2756. public function testWithStringBodyArray()
  2757. {
  2758. $this->expectException(\PHPUnit\Framework\Error\Error::class);
  2759. $this->expectExceptionMessage('Array to string conversion');
  2760. $response = new Response();
  2761. $newResponse = $response->withStringBody(['foo' => 'bar']);
  2762. $body = $newResponse->getBody();
  2763. $this->assertSame('', (string)$body);
  2764. $this->assertNotSame($response, $newResponse);
  2765. }
  2766. /**
  2767. * Test get Body.
  2768. *
  2769. * @return void
  2770. */
  2771. public function testGetBody()
  2772. {
  2773. $response = new Response();
  2774. $stream = $response->getBody();
  2775. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $stream);
  2776. }
  2777. /**
  2778. * Test with header.
  2779. *
  2780. * @return void
  2781. */
  2782. public function testWithHeader()
  2783. {
  2784. $response = new Response();
  2785. $response2 = $response->withHeader('Accept', 'application/json');
  2786. $result = $response2->getHeaders();
  2787. $expected = [
  2788. 'Content-Type' => ['text/html; charset=UTF-8'],
  2789. 'Accept' => ['application/json'],
  2790. ];
  2791. $this->assertEquals($expected, $result);
  2792. $this->assertFalse($response->hasHeader('Accept'));
  2793. }
  2794. /**
  2795. * Test get headers.
  2796. *
  2797. * @return void
  2798. */
  2799. public function testGetHeaders()
  2800. {
  2801. $response = new Response();
  2802. $headers = $response->getHeaders();
  2803. $response = $response->withAddedHeader('Location', 'localhost');
  2804. $response = $response->withAddedHeader('Accept', 'application/json');
  2805. $headers = $response->getHeaders();
  2806. $expected = [
  2807. 'Content-Type' => ['text/html; charset=UTF-8'],
  2808. 'Location' => ['localhost'],
  2809. 'Accept' => ['application/json'],
  2810. ];
  2811. $this->assertEquals($expected, $headers);
  2812. }
  2813. /**
  2814. * Test without header.
  2815. *
  2816. * @return void
  2817. */
  2818. public function testWithoutHeader()
  2819. {
  2820. $response = new Response();
  2821. $response = $response->withAddedHeader('Location', 'localhost');
  2822. $response = $response->withAddedHeader('Accept', 'application/json');
  2823. $response2 = $response->withoutHeader('Location');
  2824. $headers = $response2->getHeaders();
  2825. $expected = [
  2826. 'Content-Type' => ['text/html; charset=UTF-8'],
  2827. 'Accept' => ['application/json'],
  2828. ];
  2829. $this->assertEquals($expected, $headers);
  2830. }
  2831. /**
  2832. * Test get header.
  2833. *
  2834. * @return void
  2835. */
  2836. public function testGetHeader()
  2837. {
  2838. $response = new Response();
  2839. $response = $response->withAddedHeader('Location', 'localhost');
  2840. $result = $response->getHeader('Location');
  2841. $this->assertEquals(['localhost'], $result);
  2842. $result = $response->getHeader('location');
  2843. $this->assertEquals(['localhost'], $result);
  2844. $result = $response->getHeader('does-not-exist');
  2845. $this->assertEquals([], $result);
  2846. }
  2847. /**
  2848. * Test get header line.
  2849. *
  2850. * @return void
  2851. */
  2852. public function testGetHeaderLine()
  2853. {
  2854. $response = new Response();
  2855. $headers = $response->getHeaderLine('Accept');
  2856. $this->assertEquals('', $headers);
  2857. $response = $response->withAddedHeader('Accept', 'application/json');
  2858. $response = $response->withAddedHeader('Accept', 'application/xml');
  2859. $result = $response->getHeaderLine('Accept');
  2860. $expected = 'application/json,application/xml';
  2861. $this->assertEquals($expected, $result);
  2862. $result = $response->getHeaderLine('accept');
  2863. $this->assertEquals($expected, $result);
  2864. }
  2865. /**
  2866. * Test has header.
  2867. *
  2868. * @return void
  2869. */
  2870. public function testHasHeader()
  2871. {
  2872. $response = new Response();
  2873. $response = $response->withAddedHeader('Location', 'localhost');
  2874. $this->assertTrue($response->hasHeader('Location'));
  2875. $this->assertTrue($response->hasHeader('location'));
  2876. $this->assertTrue($response->hasHeader('locATIon'));
  2877. $this->assertFalse($response->hasHeader('Accept'));
  2878. $this->assertFalse($response->hasHeader('accept'));
  2879. }
  2880. /**
  2881. * Tests __debugInfo
  2882. *
  2883. * @return void
  2884. */
  2885. public function testDebugInfo()
  2886. {
  2887. $response = new Response();
  2888. $response = $response->withStringBody('Foo');
  2889. $result = $response->__debugInfo();
  2890. $expected = [
  2891. 'status' => 200,
  2892. 'contentType' => 'text/html',
  2893. 'headers' => [
  2894. 'Content-Type' => ['text/html; charset=UTF-8'],
  2895. ],
  2896. 'file' => null,
  2897. 'fileRange' => [],
  2898. 'cookies' => new CookieCollection(),
  2899. 'cacheDirectives' => [],
  2900. 'body' => 'Foo',
  2901. ];
  2902. $this->assertEquals($expected, $result);
  2903. }
  2904. }