ResponseTest.php 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260
  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 Zend\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, j M Y G: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, j M Y G: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, j M Y G: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, j M Y G: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. ];
  1409. $result = $response->cookie('CakeTestCookie[Testing]');
  1410. $this->assertEquals($expected, $result);
  1411. $cookie = [
  1412. 'name' => 'CakeTestCookie[Testing2]',
  1413. 'value' => '[a,b,c]',
  1414. 'expire' => 1000,
  1415. 'path' => '/test',
  1416. 'secure' => true,
  1417. ];
  1418. $response->cookie($cookie);
  1419. $expected = [
  1420. 'CakeTestCookie[Testing]' => [
  1421. 'name' => 'CakeTestCookie[Testing]',
  1422. 'value' => '',
  1423. 'expire' => 0,
  1424. 'path' => '/',
  1425. 'domain' => '',
  1426. 'secure' => false,
  1427. 'httpOnly' => false,
  1428. ],
  1429. 'CakeTestCookie[Testing2]' => [
  1430. 'name' => 'CakeTestCookie[Testing2]',
  1431. 'value' => '[a,b,c]',
  1432. 'expire' => 1000,
  1433. 'path' => '/test',
  1434. 'domain' => '',
  1435. 'secure' => true,
  1436. 'httpOnly' => false,
  1437. ],
  1438. ];
  1439. $result = $response->cookie();
  1440. $this->assertEquals($expected, $result);
  1441. $cookie = $expected['CakeTestCookie[Testing]'];
  1442. $cookie['value'] = 'test';
  1443. $response->cookie($cookie);
  1444. $expected = [
  1445. 'CakeTestCookie[Testing]' => [
  1446. 'name' => 'CakeTestCookie[Testing]',
  1447. 'value' => 'test',
  1448. 'expire' => 0,
  1449. 'path' => '/',
  1450. 'domain' => '',
  1451. 'secure' => false,
  1452. 'httpOnly' => false,
  1453. ],
  1454. 'CakeTestCookie[Testing2]' => [
  1455. 'name' => 'CakeTestCookie[Testing2]',
  1456. 'value' => '[a,b,c]',
  1457. 'expire' => 1000,
  1458. 'path' => '/test',
  1459. 'domain' => '',
  1460. 'secure' => true,
  1461. 'httpOnly' => false,
  1462. ],
  1463. ];
  1464. $result = $response->cookie();
  1465. $this->assertEquals($expected, $result);
  1466. });
  1467. }
  1468. /**
  1469. * Test setting cookies with no value
  1470. *
  1471. * @return void
  1472. */
  1473. public function testWithCookieEmpty()
  1474. {
  1475. $response = new Response();
  1476. $new = $response->withCookie(new Cookie('testing'));
  1477. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1478. $expected = [
  1479. 'name' => 'testing',
  1480. 'value' => '',
  1481. 'expire' => 0,
  1482. 'path' => '/',
  1483. 'domain' => '',
  1484. 'secure' => false,
  1485. 'httpOnly' => false];
  1486. $result = $new->getCookie('testing');
  1487. $this->assertEquals($expected, $result);
  1488. }
  1489. /**
  1490. * Test setting cookies with scalar values
  1491. *
  1492. * @return void
  1493. */
  1494. public function testWithCookieScalar()
  1495. {
  1496. $response = new Response();
  1497. $new = $response->withCookie(new Cookie('testing', 'abc123'));
  1498. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1499. $this->assertEquals('abc123', $new->getCookie('testing')['value']);
  1500. $new = $response->withCookie(new Cookie('testing', 99));
  1501. $this->assertEquals(99, $new->getCookie('testing')['value']);
  1502. $new = $response->withCookie(new Cookie('testing', false));
  1503. $this->assertFalse($new->getCookie('testing')['value']);
  1504. $new = $response->withCookie(new Cookie('testing', true));
  1505. $this->assertTrue($new->getCookie('testing')['value']);
  1506. }
  1507. /**
  1508. * Test withCookie() and duplicate data
  1509. *
  1510. * @return void
  1511. * @throws \Exception
  1512. */
  1513. public function testWithDuplicateCookie()
  1514. {
  1515. $expiry = new \DateTimeImmutable('+24 hours');
  1516. $response = new Response();
  1517. $cookie = new Cookie(
  1518. 'testing',
  1519. '[a,b,c]',
  1520. $expiry,
  1521. '/test',
  1522. '',
  1523. true
  1524. );
  1525. $new = $response->withCookie($cookie);
  1526. $this->assertNull($response->getCookie('testing'), 'withCookie does not mutate');
  1527. $expected = [
  1528. 'name' => 'testing',
  1529. 'value' => '[a,b,c]',
  1530. 'expire' => $expiry,
  1531. 'path' => '/test',
  1532. 'domain' => '',
  1533. 'secure' => true,
  1534. 'httpOnly' => false,
  1535. ];
  1536. // Match the date time formatting to Response::convertCookieToArray
  1537. $expected['expire'] = $expiry->format('U');
  1538. $this->assertEquals($expected, $new->getCookie('testing'));
  1539. }
  1540. /**
  1541. * Test withCookie() and a cookie instance
  1542. *
  1543. * @return void
  1544. */
  1545. public function testWithCookieObject()
  1546. {
  1547. $response = new Response();
  1548. $cookie = new Cookie('yay', 'a value');
  1549. $new = $response->withCookie($cookie);
  1550. $this->assertNull($response->getCookie('yay'), 'withCookie does not mutate');
  1551. $this->assertNotEmpty($new->getCookie('yay'));
  1552. $this->assertSame($cookie, $new->getCookieCollection()->get('yay'));
  1553. }
  1554. public function testWithExpiredCookieScalar()
  1555. {
  1556. $response = new Response();
  1557. $response = $response->withCookie(new Cookie('testing', 'abc123'));
  1558. $this->assertEquals('abc123', $response->getCookie('testing')['value']);
  1559. $new = $response->withExpiredCookie(new Cookie('testing'));
  1560. $this->assertNull($response->getCookie('testing')['expire']);
  1561. $this->assertLessThan(FrozenTime::createFromTimestamp(1), $new->getCookie('testing')['expire']);
  1562. }
  1563. /**
  1564. * @throws \Exception If DateImmutable emits an error.
  1565. */
  1566. public function testWithExpiredCookieOptions()
  1567. {
  1568. $options = [
  1569. 'name' => 'testing',
  1570. 'value' => 'abc123',
  1571. 'domain' => 'cakephp.org',
  1572. 'path' => '/custompath/',
  1573. 'secure' => true,
  1574. 'httpOnly' => true,
  1575. 'expire' => new \DateTimeImmutable('+14 days'),
  1576. ];
  1577. $cookie = new Cookie(
  1578. $options['name'],
  1579. $options['value'],
  1580. $options['expire'],
  1581. $options['path'],
  1582. $options['domain'],
  1583. $options['secure'],
  1584. $options['httpOnly']
  1585. );
  1586. $response = new Response();
  1587. $response = $response->withCookie($cookie);
  1588. // Change the timestamp format to match the Response::convertCookieToArray
  1589. $options['expire'] = $options['expire']->format('U');
  1590. $this->assertEquals($options, $response->getCookie('testing'));
  1591. $expiredCookie = $response->withExpiredCookie($cookie);
  1592. $this->assertEquals($options['expire'], $response->getCookie('testing')['expire']);
  1593. $this->assertLessThan(Chronos::createFromTimestamp(1), $expiredCookie->getCookie('testing')['expire']);
  1594. }
  1595. public function testWithExpiredCookieObject()
  1596. {
  1597. $response = new Response();
  1598. $cookie = new Cookie('yay', 'a value');
  1599. $response = $response->withCookie($cookie);
  1600. $this->assertEquals('a value', $response->getCookie('yay')['value']);
  1601. $new = $response->withExpiredCookie($cookie);
  1602. $this->assertNull($response->getCookie('yay')['expire']);
  1603. $this->assertEquals('1', $new->getCookie('yay')['expire']);
  1604. }
  1605. /**
  1606. * Test getCookies() and array data.
  1607. *
  1608. * @return void
  1609. */
  1610. public function testGetCookies()
  1611. {
  1612. $response = new Response();
  1613. $new = $response->withCookie(new Cookie('testing', 'a'))
  1614. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  1615. $expected = [
  1616. 'testing' => [
  1617. 'name' => 'testing',
  1618. 'value' => 'a',
  1619. 'expire' => null,
  1620. 'path' => '/',
  1621. 'domain' => '',
  1622. 'secure' => false,
  1623. 'httpOnly' => false,
  1624. ],
  1625. 'test2' => [
  1626. 'name' => 'test2',
  1627. 'value' => 'b',
  1628. 'expire' => null,
  1629. 'path' => '/test',
  1630. 'domain' => '',
  1631. 'secure' => true,
  1632. 'httpOnly' => false,
  1633. ],
  1634. ];
  1635. $this->assertEquals($expected, $new->getCookies());
  1636. }
  1637. /**
  1638. * Test getCookies() and array data.
  1639. *
  1640. * @return void
  1641. */
  1642. public function testGetCookiesArrayValue()
  1643. {
  1644. $response = new Response();
  1645. $cookie = (new Cookie('urmc'))
  1646. ->withValue(['user_id' => 1, 'token' => 'abc123'])
  1647. ->withHttpOnly(true);
  1648. $new = $response->withCookie($cookie);
  1649. $expected = [
  1650. 'urmc' => [
  1651. 'name' => 'urmc',
  1652. 'value' => '{"user_id":1,"token":"abc123"}',
  1653. 'expire' => null,
  1654. 'path' => '/',
  1655. 'domain' => '',
  1656. 'secure' => false,
  1657. 'httpOnly' => true,
  1658. ],
  1659. ];
  1660. $this->assertEquals($expected, $new->getCookies());
  1661. }
  1662. /**
  1663. * Test getCookieCollection() as array data
  1664. *
  1665. * @return void
  1666. */
  1667. public function testGetCookieCollection()
  1668. {
  1669. $response = new Response();
  1670. $new = $response->withCookie(new Cookie('testing', 'a'))
  1671. ->withCookie(new Cookie('test2', 'b', null, '/test', '', true));
  1672. $cookies = $response->getCookieCollection();
  1673. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1674. $this->assertCount(0, $cookies, 'Original response not mutated');
  1675. $cookies = $new->getCookieCollection();
  1676. $this->assertInstanceOf(CookieCollection::class, $cookies);
  1677. $this->assertCount(2, $cookies);
  1678. $this->assertTrue($cookies->has('testing'));
  1679. $this->assertTrue($cookies->has('test2'));
  1680. }
  1681. /**
  1682. * Test withCookieCollection()
  1683. *
  1684. * @return void
  1685. */
  1686. public function testWithCookieCollection()
  1687. {
  1688. $response = new Response();
  1689. $collection = new CookieCollection([new Cookie('foo', 'bar')]);
  1690. $newResponse = $response->withCookieCollection($collection);
  1691. $this->assertNotSame($response, $newResponse);
  1692. $this->assertNotSame($response->getCookieCollection(), $newResponse->getCookieCollection());
  1693. $this->assertSame($newResponse->getCookie('foo')['value'], 'bar');
  1694. }
  1695. /**
  1696. * Test that cors() returns a builder.
  1697. *
  1698. * @return void
  1699. */
  1700. public function testCors()
  1701. {
  1702. $request = new ServerRequest([
  1703. 'environment' => ['HTTP_ORIGIN' => 'http://example.com'],
  1704. ]);
  1705. $response = new Response();
  1706. $builder = $response->cors($request);
  1707. $this->assertInstanceOf(CorsBuilder::class, $builder);
  1708. $this->assertSame($response, $builder->build(), 'Empty builder returns same object');
  1709. }
  1710. /**
  1711. * Test CORS with additional parameters
  1712. *
  1713. * @group deprecated
  1714. * @dataProvider corsData
  1715. * @param Request $request
  1716. * @param string $origin
  1717. * @param string|array $domains
  1718. * @param string|array $methods
  1719. * @param string|array $headers
  1720. * @param string|bool $expectedOrigin
  1721. * @param string|bool $expectedMethods
  1722. * @param string|bool $expectedHeaders
  1723. * @return void
  1724. */
  1725. public function testCorsParameters($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false)
  1726. {
  1727. $this->deprecated(function () use ($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods, $expectedHeaders) {
  1728. $request = $request->withEnv('HTTP_ORIGIN', $origin);
  1729. $response = new Response();
  1730. $result = $response->cors($request, $domains, $methods, $headers);
  1731. $this->assertInstanceOf('Cake\Network\CorsBuilder', $result);
  1732. if ($expectedOrigin) {
  1733. $this->assertTrue($response->hasHeader('Access-Control-Allow-Origin'));
  1734. $this->assertEquals($expectedOrigin, $response->getHeaderLine('Access-Control-Allow-Origin'));
  1735. }
  1736. if ($expectedMethods) {
  1737. $this->assertTrue($response->hasHeader('Access-Control-Allow-Methods'));
  1738. $this->assertEquals($expectedMethods, $response->getHeaderLine('Access-Control-Allow-Methods'));
  1739. }
  1740. if ($expectedHeaders) {
  1741. $this->assertTrue($response->hasHeader('Access-Control-Allow-Headers'));
  1742. $this->assertEquals($expectedHeaders, $response->getHeaderLine('Access-Control-Allow-Headers'));
  1743. }
  1744. });
  1745. }
  1746. /**
  1747. * Feed for testCors
  1748. *
  1749. * @return array
  1750. */
  1751. public function corsData()
  1752. {
  1753. $fooRequest = new ServerRequest();
  1754. $secureRequest = function () {
  1755. $secureRequest = $this->getMockBuilder('Cake\Http\ServerRequest')
  1756. ->setMethods(['is'])
  1757. ->getMock();
  1758. $secureRequest->expects($this->any())
  1759. ->method('is')
  1760. ->with('ssl')
  1761. ->will($this->returnValue(true));
  1762. return $secureRequest;
  1763. };
  1764. return [
  1765. [$fooRequest, null, '*', '', '', false, false],
  1766. [$fooRequest, 'http://www.foo.com', '*', '', '', '*', false],
  1767. [$fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false],
  1768. [$fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false],
  1769. [$fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false],
  1770. [$fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false],
  1771. [$fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false],
  1772. [$fooRequest, 'http://www.foo.com', ['*.bar.com', '*.foo.com'], '', '', 'http://www.foo.com', false],
  1773. [$fooRequest, 'http://not-foo.com', '*.foo.com', '', '', false, false],
  1774. [$fooRequest, 'http://bad.academy', '*.acad.my', '', '', false, false],
  1775. [$fooRequest, 'http://www.foo.com.at.bad.com', '*.foo.com', '', '', false, false],
  1776. [$fooRequest, 'https://www.foo.com', '*.foo.com', '', '', false, false],
  1777. [$secureRequest(), 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false],
  1778. [$secureRequest(), 'https://www.bar.com', 'http://www.bar.com', '', '', false, false],
  1779. [$secureRequest(), 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false],
  1780. [$secureRequest(), 'http://www.bar.com', '*.bar.com', '', '', false, false],
  1781. [$fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'],
  1782. [$fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'],
  1783. [$fooRequest, 'http://www.foo.com', '*.foo.com', ['GET', 'POST'], '', 'http://www.foo.com', 'GET, POST'],
  1784. [$fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'],
  1785. [$fooRequest, 'http://www.foo.com', '*', '', ['X-CakePHP', 'X-MyApp'], '*', false, 'X-CakePHP, X-MyApp'],
  1786. [$fooRequest, 'http://www.foo.com', '*', ['GET', 'OPTIONS'], ['X-CakePHP', 'X-MyApp'], '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'],
  1787. ];
  1788. }
  1789. /**
  1790. * testFileNotFound
  1791. *
  1792. * @group deprecated
  1793. * @return void
  1794. */
  1795. public function testFileNotFound()
  1796. {
  1797. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  1798. $this->deprecated(function () {
  1799. $response = new Response();
  1800. $response->file('/some/missing/folder/file.jpg');
  1801. });
  1802. }
  1803. /**
  1804. * test withFile() not found
  1805. *
  1806. * Don't remove this test when cleaning up deprecation warnings.
  1807. * Just remove the deprecated wrapper.
  1808. *
  1809. * @return void
  1810. */
  1811. public function testWithFileNotFound()
  1812. {
  1813. $this->expectException(\Cake\Http\Exception\NotFoundException::class);
  1814. $this->deprecated(function () {
  1815. $response = new Response();
  1816. $response->withFile('/some/missing/folder/file.jpg');
  1817. });
  1818. }
  1819. /**
  1820. * Provider for various kinds of unacceptable files.
  1821. *
  1822. * @return array
  1823. */
  1824. public function invalidFileProvider()
  1825. {
  1826. return [
  1827. ['my/../cat.gif', 'The requested file contains `..` and will not be read.'],
  1828. ['my\..\cat.gif', 'The requested file contains `..` and will not be read.'],
  1829. ['my/ca..t.gif', 'my/ca..t.gif was not found or not readable'],
  1830. ['my/ca..t/image.gif', 'my/ca..t/image.gif was not found or not readable'],
  1831. ];
  1832. }
  1833. /**
  1834. * test invalid file paths.
  1835. *
  1836. * @group deprecated
  1837. * @dataProvider invalidFileProvider
  1838. * @return void
  1839. */
  1840. public function testFileInvalidPath($path, $expectedMessage)
  1841. {
  1842. $this->deprecated(function () use ($path, $expectedMessage) {
  1843. $response = new Response();
  1844. try {
  1845. $response->file($path);
  1846. } catch (NotFoundException $e) {
  1847. $this->assertContains($expectedMessage, $e->getMessage());
  1848. }
  1849. });
  1850. }
  1851. /**
  1852. * test withFile and invalid paths
  1853. *
  1854. * This test should not be removed when deprecation warnings are removed.
  1855. * Just remove the deprecated wrapper.
  1856. *
  1857. * @dataProvider invalidFileProvider
  1858. * @return void
  1859. */
  1860. public function testWithFileInvalidPath($path, $expectedMessage)
  1861. {
  1862. $this->deprecated(function () use ($path, $expectedMessage) {
  1863. $response = new Response();
  1864. try {
  1865. $response->withFile($path);
  1866. } catch (NotFoundException $e) {
  1867. $this->assertContains($expectedMessage, $e->getMessage());
  1868. }
  1869. });
  1870. }
  1871. /**
  1872. * testFile method
  1873. *
  1874. * @group deprecated
  1875. * @return void
  1876. */
  1877. public function testFile()
  1878. {
  1879. $this->deprecated(function () {
  1880. $response = $this->getMockBuilder('Cake\Http\Response')
  1881. ->setMethods(['_sendHeader', '_isActive'])
  1882. ->getMock();
  1883. $response->expects($this->exactly(1))
  1884. ->method('_isActive')
  1885. ->will($this->returnValue(true));
  1886. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1887. ob_start();
  1888. $result = $response->send();
  1889. $this->assertTrue($result !== false);
  1890. $output = ob_get_clean();
  1891. $expected = '/* this is the test asset css file */';
  1892. $this->assertEquals($expected, trim($output));
  1893. $this->assertEquals($expected, trim($response->getBody()->getContents()));
  1894. $this->assertEquals('text/css', $response->getType());
  1895. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  1896. });
  1897. }
  1898. /**
  1899. * test withFile() + download & name
  1900. *
  1901. * @return void
  1902. */
  1903. public function testWithFileDownloadAndName()
  1904. {
  1905. $response = new Response();
  1906. $new = $response->withFile(
  1907. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1908. [
  1909. 'name' => 'something_special.css',
  1910. 'download' => true,
  1911. ]
  1912. );
  1913. $this->assertEquals(
  1914. 'text/html; charset=UTF-8',
  1915. $response->getHeaderLine('Content-Type'),
  1916. 'No mutation'
  1917. );
  1918. $this->assertEquals(
  1919. 'text/css; charset=UTF-8',
  1920. $new->getHeaderLine('Content-Type')
  1921. );
  1922. $this->assertEquals(
  1923. 'attachment; filename="something_special.css"',
  1924. $new->getHeaderLine('Content-Disposition')
  1925. );
  1926. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  1927. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  1928. $body = $new->getBody();
  1929. $this->assertInstanceOf('Zend\Diactoros\Stream', $body);
  1930. $expected = '/* this is the test asset css file */';
  1931. $this->assertEquals($expected, trim($body->getContents()));
  1932. $this->assertEquals($expected, trim($new->getFile()->read()));
  1933. }
  1934. /**
  1935. * testFileWithDownloadAndName
  1936. *
  1937. * @group deprecated
  1938. * @return void
  1939. */
  1940. public function testFileWithDownloadAndName()
  1941. {
  1942. $this->deprecated(function () {
  1943. $response = $this->getMockBuilder('Cake\Http\Response')
  1944. ->setMethods(['download', '_sendHeader', '_isActive'])
  1945. ->getMock();
  1946. $response->expects($this->once())
  1947. ->method('download')
  1948. ->with('something_special.css');
  1949. $response->expects($this->exactly(1))
  1950. ->method('_isActive')
  1951. ->will($this->returnValue(true));
  1952. $response->file(
  1953. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1954. [
  1955. 'name' => 'something_special.css',
  1956. 'download' => true,
  1957. ]
  1958. );
  1959. ob_start();
  1960. $result = $response->send();
  1961. $output = ob_get_clean();
  1962. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1963. $this->assertNotFalse($result);
  1964. $this->assertEquals('text/css', $response->getType());
  1965. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  1966. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  1967. });
  1968. }
  1969. /**
  1970. * testFileWithUnknownFileTypeGeneric method
  1971. *
  1972. * @group deprecated
  1973. * @return void
  1974. */
  1975. public function testFileWithUnknownFileTypeGeneric()
  1976. {
  1977. $this->deprecated(function () {
  1978. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1979. $response = $this->getMockBuilder('Cake\Http\Response')
  1980. ->setMethods(['download', '_sendHeader', '_isActive'])
  1981. ->getMock();
  1982. $response->expects($this->once())
  1983. ->method('download')
  1984. ->with('no_section.ini');
  1985. $response->expects($this->exactly(1))
  1986. ->method('_isActive')
  1987. ->will($this->returnValue(true));
  1988. $response->file(CONFIG . 'no_section.ini');
  1989. ob_start();
  1990. $result = $response->send();
  1991. $output = ob_get_clean();
  1992. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1993. $this->assertNotFalse($result);
  1994. $this->assertEquals('text/html', $response->getType());
  1995. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  1996. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  1997. });
  1998. }
  1999. /**
  2000. * test withFile() + a generic agent
  2001. *
  2002. * @return void
  2003. */
  2004. public function testWithFileUnknownFileTypeGeneric()
  2005. {
  2006. $response = new Response();
  2007. $new = $response->withFile(CONFIG . 'no_section.ini');
  2008. $this->assertEquals('text/html; charset=UTF-8', $new->getHeaderLine('Content-Type'));
  2009. $this->assertEquals(
  2010. 'attachment; filename="no_section.ini"',
  2011. $new->getHeaderLine('Content-Disposition')
  2012. );
  2013. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2014. $body = $new->getBody();
  2015. $expected = "some_key = some_value\nbool_key = 1\n";
  2016. $this->assertEquals($expected, $body->getContents());
  2017. }
  2018. /**
  2019. * testFileWithUnknownFileTypeOpera method
  2020. *
  2021. * @group deprecated
  2022. * @return void
  2023. */
  2024. public function testFileWithUnknownFileTypeOpera()
  2025. {
  2026. $this->deprecated(function () {
  2027. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  2028. $response = $this->getMockBuilder('Cake\Http\Response')
  2029. ->setMethods(['download', '_sendHeader', '_isActive'])
  2030. ->getMock();
  2031. $response->expects($this->once())
  2032. ->method('download')
  2033. ->with('no_section.ini');
  2034. $response->expects($this->exactly(1))
  2035. ->method('_isActive')
  2036. ->will($this->returnValue(true));
  2037. $response->file(CONFIG . 'no_section.ini');
  2038. ob_start();
  2039. $result = $response->send();
  2040. $output = ob_get_clean();
  2041. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  2042. $this->assertNotFalse($result);
  2043. $this->assertEquals('application/octet-stream', $response->getType());
  2044. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2045. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2046. });
  2047. }
  2048. /**
  2049. * test withFile() + opera
  2050. *
  2051. * @return void
  2052. */
  2053. public function testWithFileUnknownFileTypeOpera()
  2054. {
  2055. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  2056. $response = new Response();
  2057. $new = $response->withFile(CONFIG . 'no_section.ini');
  2058. $this->assertEquals('application/octet-stream', $new->getHeaderLine('Content-Type'));
  2059. $this->assertEquals(
  2060. 'attachment; filename="no_section.ini"',
  2061. $new->getHeaderLine('Content-Disposition')
  2062. );
  2063. }
  2064. /**
  2065. * testFileWithUnknownFileTypeIE method
  2066. *
  2067. * @group deprecated
  2068. * @return void
  2069. */
  2070. public function testFileWithUnknownFileTypeIE()
  2071. {
  2072. $this->deprecated(function () {
  2073. $_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)';
  2074. $response = $this->getMockBuilder('Cake\Http\Response')
  2075. ->setMethods(['download', '_isActive', '_sendHeade'])
  2076. ->getMock();
  2077. $response->expects($this->once())
  2078. ->method('download')
  2079. ->with('config.ini');
  2080. $response->expects($this->exactly(1))
  2081. ->method('_isActive')
  2082. ->will($this->returnValue(true));
  2083. $response->file(CONFIG . 'no_section.ini', [
  2084. 'name' => 'config.ini',
  2085. ]);
  2086. ob_start();
  2087. $result = $response->send();
  2088. $output = ob_get_clean();
  2089. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  2090. $this->assertNotFalse($result);
  2091. $this->assertEquals('application/force-download', $response->getType());
  2092. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2093. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2094. });
  2095. }
  2096. /**
  2097. * test withFile() + old IE
  2098. *
  2099. * @return void
  2100. */
  2101. public function testWithFileUnknownFileTypeOldIe()
  2102. {
  2103. $_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)';
  2104. $response = new Response();
  2105. $new = $response->withFile(CONFIG . 'no_section.ini');
  2106. $this->assertEquals('application/force-download', $new->getHeaderLine('Content-Type'));
  2107. }
  2108. /**
  2109. * testFileWithUnknownFileNoDownload method
  2110. *
  2111. * @group deprecated
  2112. * @return void
  2113. */
  2114. public function testFileWithUnknownFileNoDownload()
  2115. {
  2116. $this->deprecated(function () {
  2117. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  2118. $response = $this->getMockBuilder('Cake\Http\Response')
  2119. ->setMethods(['download'])
  2120. ->getMock();
  2121. $response->expects($this->never())
  2122. ->method('download');
  2123. $response->file(CONFIG . 'no_section.ini', [
  2124. 'download' => false,
  2125. ]);
  2126. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2127. $this->assertEquals('text/html', $response->getType());
  2128. });
  2129. }
  2130. /**
  2131. * test withFile() + no download
  2132. *
  2133. * @return void
  2134. */
  2135. public function testWithFileNoDownload()
  2136. {
  2137. $response = new Response();
  2138. $new = $response->withFile(CONFIG . 'no_section.ini', [
  2139. 'download' => false,
  2140. ]);
  2141. $this->assertEquals(
  2142. 'text/html; charset=UTF-8',
  2143. $new->getHeaderLine('Content-Type')
  2144. );
  2145. $this->assertFalse($new->hasHeader('Content-Disposition'));
  2146. $this->assertFalse($new->hasHeader('Content-Transfer-Encoding'));
  2147. }
  2148. /**
  2149. * test getFile method
  2150. *
  2151. * @group deprecated
  2152. * @return void
  2153. */
  2154. public function testGetFile()
  2155. {
  2156. $this->deprecated(function () {
  2157. $response = new Response();
  2158. $this->assertNull($response->getFile(), 'No file to get');
  2159. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  2160. $file = $response->getFile();
  2161. $this->assertInstanceOf('Cake\Filesystem\File', $file, 'Should get a file');
  2162. $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path);
  2163. });
  2164. }
  2165. /**
  2166. * testConnectionAbortedOnBuffering method
  2167. *
  2168. * @group deprecated
  2169. * @return void
  2170. */
  2171. public function testConnectionAbortedOnBuffering()
  2172. {
  2173. $this->deprecated(function () {
  2174. $response = $this->getMockBuilder('Cake\Http\Response')
  2175. ->setMethods([
  2176. 'download',
  2177. '_sendHeader',
  2178. '_isActive',
  2179. ])
  2180. ->getMock();
  2181. $response->expects($this->at(0))
  2182. ->method('_isActive')
  2183. ->will($this->returnValue(false));
  2184. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  2185. ob_start();
  2186. $result = $response->send();
  2187. ob_end_clean();
  2188. $this->assertNull($result);
  2189. $this->assertEquals('text/css', $response->getType());
  2190. $this->assertFalse($response->hasHeader('Content-Range'));
  2191. });
  2192. }
  2193. /**
  2194. * Test downloading files with UPPERCASE extensions.
  2195. *
  2196. * @group deprecated
  2197. * @return void
  2198. */
  2199. public function testFileUpperExtension()
  2200. {
  2201. $this->deprecated(function () {
  2202. $response = new Response();
  2203. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  2204. $this->assertSame('image/jpeg', $response->getType());
  2205. });
  2206. }
  2207. /**
  2208. * Test that uppercase extensions result in correct content-types
  2209. *
  2210. * @return void
  2211. */
  2212. public function testWithFileUpperExtension()
  2213. {
  2214. $response = new Response();
  2215. $new = $response->withFile(TEST_APP . 'vendor/img/test_2.JPG');
  2216. $this->assertEquals('image/jpeg', $new->getHeaderLine('Content-Type'));
  2217. }
  2218. /**
  2219. * A data provider for testing various ranges
  2220. *
  2221. * @return array
  2222. */
  2223. public static function rangeProvider()
  2224. {
  2225. return [
  2226. // suffix-byte-range
  2227. [
  2228. 'bytes=-25', 25, 'bytes 13-37/38',
  2229. ],
  2230. [
  2231. 'bytes=0-', 38, 'bytes 0-37/38',
  2232. ],
  2233. [
  2234. 'bytes=10-', 28, 'bytes 10-37/38',
  2235. ],
  2236. [
  2237. 'bytes=10-20', 11, 'bytes 10-20/38',
  2238. ],
  2239. // Spaced out
  2240. [
  2241. 'bytes = 10 - 20', 11, 'bytes 10-20/38',
  2242. ],
  2243. ];
  2244. }
  2245. /**
  2246. * Test the various range offset types.
  2247. *
  2248. * @group deprecated
  2249. * @dataProvider rangeProvider
  2250. * @return void
  2251. */
  2252. public function testFileRangeOffsets($range, $length, $offsetResponse)
  2253. {
  2254. $this->deprecated(function () use ($range, $length, $offsetResponse) {
  2255. $_SERVER['HTTP_RANGE'] = $range;
  2256. $response = $this->getMockBuilder('Cake\Http\Response')
  2257. ->setMethods([
  2258. '_sendHeader',
  2259. '_isActive',
  2260. ])
  2261. ->getMock();
  2262. $response->expects($this->any())
  2263. ->method('_isActive')
  2264. ->will($this->returnValue(true));
  2265. $response->file(
  2266. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2267. ['download' => true]
  2268. );
  2269. ob_start();
  2270. $result = $response->send();
  2271. ob_get_clean();
  2272. $this->assertEquals(
  2273. 'attachment; filename="test_asset.css"',
  2274. $response->getHeaderLine('Content-Disposition')
  2275. );
  2276. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2277. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2278. $this->assertEquals($length, $response->getHeaderLine('Content-Length'));
  2279. $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range'));
  2280. });
  2281. }
  2282. /**
  2283. * Test withFile() & the various range offset types.
  2284. *
  2285. * @dataProvider rangeProvider
  2286. * @return void
  2287. */
  2288. public function testWithFileRangeOffsets($range, $length, $offsetResponse)
  2289. {
  2290. $_SERVER['HTTP_RANGE'] = $range;
  2291. $response = new Response();
  2292. $new = $response->withFile(
  2293. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2294. ['download' => true]
  2295. );
  2296. $this->assertEquals(
  2297. 'attachment; filename="test_asset.css"',
  2298. $new->getHeaderLine('Content-Disposition')
  2299. );
  2300. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2301. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2302. $this->assertEquals($length, $new->getHeaderLine('Content-Length'));
  2303. $this->assertEquals($offsetResponse, $new->getHeaderLine('Content-Range'));
  2304. }
  2305. /**
  2306. * Test fetching ranges from a file.
  2307. *
  2308. * @group deprecated
  2309. * @return void
  2310. */
  2311. public function testFileRange()
  2312. {
  2313. $this->deprecated(function () {
  2314. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2315. $response = $this->getMockBuilder('Cake\Http\Response')
  2316. ->setMethods([
  2317. '_sendHeader',
  2318. '_isActive',
  2319. ])
  2320. ->getMock();
  2321. $response->expects($this->any())
  2322. ->method('_isActive')
  2323. ->will($this->returnValue(true));
  2324. $response->file(
  2325. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2326. ['download' => true]
  2327. );
  2328. ob_start();
  2329. $result = $response->send();
  2330. $output = ob_get_clean();
  2331. $this->assertEquals('is the test asset ', $output);
  2332. $this->assertNotFalse($result);
  2333. $this->assertEquals(
  2334. 'attachment; filename="test_asset.css"',
  2335. $response->getHeaderLine('Content-Disposition')
  2336. );
  2337. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2338. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2339. $this->assertEquals('18', $response->getHeaderLine('Content-Length'));
  2340. $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range'));
  2341. $this->assertEquals(206, $response->getStatusCode());
  2342. });
  2343. }
  2344. /**
  2345. * Test withFile() fetching ranges from a file.
  2346. *
  2347. * @return void
  2348. */
  2349. public function testWithFileRange()
  2350. {
  2351. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2352. $response = new Response();
  2353. $new = $response->withFile(
  2354. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2355. ['download' => true]
  2356. );
  2357. $this->assertEquals(
  2358. 'attachment; filename="test_asset.css"',
  2359. $new->getHeaderLine('Content-Disposition')
  2360. );
  2361. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2362. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2363. $this->assertEquals('18', $new->getHeaderLine('Content-Length'));
  2364. $this->assertEquals('bytes 8-25/38', $new->getHeaderLine('Content-Range'));
  2365. $this->assertEquals(206, $new->getStatusCode());
  2366. }
  2367. /**
  2368. * Provider for invalid range header values.
  2369. *
  2370. * @return array
  2371. */
  2372. public function invalidFileRangeProvider()
  2373. {
  2374. return [
  2375. // malformed range
  2376. [
  2377. 'bytes=0,38',
  2378. ],
  2379. // malformed punctuation
  2380. [
  2381. 'bytes: 0 - 38',
  2382. ],
  2383. ];
  2384. }
  2385. /**
  2386. * Test invalid file ranges.
  2387. *
  2388. * @group deprecated
  2389. * @dataProvider invalidFileRangeProvider
  2390. * @return void
  2391. */
  2392. public function testFileRangeInvalid($range)
  2393. {
  2394. $this->deprecated(function () use ($range) {
  2395. $_SERVER['HTTP_RANGE'] = $range;
  2396. $response = $this->getMockBuilder('Cake\Http\Response')
  2397. ->setMethods([
  2398. '_sendHeader',
  2399. '_isActive',
  2400. ])
  2401. ->getMock();
  2402. $response->file(
  2403. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2404. ['download' => true]
  2405. );
  2406. $this->assertEquals('text/css', $response->getType());
  2407. $this->assertEquals(
  2408. 'attachment; filename="test_asset.css"',
  2409. $response->getHeaderLine('Content-Disposition')
  2410. );
  2411. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2412. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2413. $this->assertEquals('38', $response->getHeaderLine('Content-Length'));
  2414. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2415. });
  2416. }
  2417. /**
  2418. * Test withFile() and invalid ranges
  2419. *
  2420. * @dataProvider invalidFileRangeProvider
  2421. * @return void
  2422. */
  2423. public function testWithFileInvalidRange($range)
  2424. {
  2425. $_SERVER['HTTP_RANGE'] = $range;
  2426. $response = new Response();
  2427. $new = $response->withFile(
  2428. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2429. ['download' => true]
  2430. );
  2431. $this->assertEquals(
  2432. 'attachment; filename="test_asset.css"',
  2433. $new->getHeaderLine('Content-Disposition')
  2434. );
  2435. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2436. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2437. $this->assertEquals('38', $new->getHeaderLine('Content-Length'));
  2438. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2439. $this->assertEquals(206, $new->getStatusCode());
  2440. }
  2441. /**
  2442. * Test reversed file ranges.
  2443. *
  2444. * @group deprecated
  2445. * @return void
  2446. */
  2447. public function testFileRangeReversed()
  2448. {
  2449. $this->deprecated(function () {
  2450. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2451. $response = $this->getMockBuilder('Cake\Http\Response')
  2452. ->setMethods([
  2453. '_sendHeader',
  2454. '_isActive',
  2455. ])
  2456. ->getMock();
  2457. $response->file(
  2458. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2459. ['download' => true]
  2460. );
  2461. $this->assertEquals(416, $response->getStatusCode());
  2462. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2463. $this->assertEquals(
  2464. 'attachment; filename="test_asset.css"',
  2465. $response->getHeaderLine('Content-Disposition')
  2466. );
  2467. $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding'));
  2468. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2469. });
  2470. }
  2471. /**
  2472. * Test withFile() and a reversed range
  2473. *
  2474. * @return void
  2475. */
  2476. public function testWithFileReversedRange()
  2477. {
  2478. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2479. $response = new Response();
  2480. $new = $response->withFile(
  2481. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2482. ['download' => true]
  2483. );
  2484. $this->assertEquals(
  2485. 'attachment; filename="test_asset.css"',
  2486. $new->getHeaderLine('Content-Disposition')
  2487. );
  2488. $this->assertEquals('binary', $new->getHeaderLine('Content-Transfer-Encoding'));
  2489. $this->assertEquals('bytes', $new->getHeaderLine('Accept-Ranges'));
  2490. $this->assertEquals('bytes 0-37/38', $new->getHeaderLine('Content-Range'));
  2491. $this->assertEquals(416, $new->getStatusCode());
  2492. }
  2493. /**
  2494. * testFileRangeOffsetsNoDownload method
  2495. *
  2496. * @group deprecated
  2497. * @dataProvider rangeProvider
  2498. * @return void
  2499. */
  2500. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  2501. {
  2502. $this->deprecated(function () use ($range, $length, $offsetResponse) {
  2503. $_SERVER['HTTP_RANGE'] = $range;
  2504. $response = $this->getMockBuilder('Cake\Http\Response')
  2505. ->setMethods([
  2506. '_sendHeader',
  2507. '_isActive',
  2508. ])
  2509. ->getMock();
  2510. $response->expects($this->any())
  2511. ->method('_isActive')
  2512. ->will($this->returnValue(true));
  2513. $response->file(
  2514. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2515. ['download' => false]
  2516. );
  2517. ob_start();
  2518. $result = $response->send();
  2519. ob_get_clean();
  2520. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2521. $this->assertEquals($length, $response->getHeaderLine('Content-Length'));
  2522. $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range'));
  2523. });
  2524. }
  2525. /**
  2526. * testFileRangeNoDownload method
  2527. *
  2528. * @group deprecated
  2529. * @return void
  2530. */
  2531. public function testFileRangeNoDownload()
  2532. {
  2533. $this->deprecated(function () {
  2534. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  2535. $response = $this->getMockBuilder('Cake\Http\Response')
  2536. ->setMethods([
  2537. '_sendHeader',
  2538. '_isActive',
  2539. ])
  2540. ->getMock();
  2541. $response->expects($this->any())
  2542. ->method('_isActive')
  2543. ->will($this->returnValue(true));
  2544. $response->file(
  2545. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2546. ['download' => false]
  2547. );
  2548. ob_start();
  2549. $result = $response->send();
  2550. $output = ob_get_clean();
  2551. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2552. $this->assertEquals('18', $response->getHeaderLine('Content-Length'));
  2553. $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range'));
  2554. $this->assertEquals('text/css', $response->getType());
  2555. $this->assertEquals(206, $response->getStatusCode());
  2556. $this->assertEquals('is the test asset ', $output);
  2557. $this->assertNotFalse($result);
  2558. });
  2559. }
  2560. /**
  2561. * testFileRangeInvalidNoDownload method
  2562. *
  2563. * @group deprecated
  2564. * @return void
  2565. */
  2566. public function testFileRangeInvalidNoDownload()
  2567. {
  2568. $this->deprecated(function () {
  2569. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  2570. $response = new Response();
  2571. $response->file(
  2572. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  2573. ['download' => false]
  2574. );
  2575. $this->assertEquals(416, $response->statusCode());
  2576. $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
  2577. $this->assertEquals('text/css', $response->getType());
  2578. $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
  2579. });
  2580. }
  2581. /**
  2582. * Test the location method.
  2583. *
  2584. * @group deprecated
  2585. * @return void
  2586. */
  2587. public function testLocation()
  2588. {
  2589. $this->deprecated(function () {
  2590. $response = new Response();
  2591. $this->assertNull($response->location(), 'No header should be set.');
  2592. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  2593. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  2594. });
  2595. }
  2596. /**
  2597. * Test the withLocation method.
  2598. *
  2599. * @return void
  2600. */
  2601. public function testWithLocation()
  2602. {
  2603. $response = new Response();
  2604. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set.');
  2605. $new = $response->withLocation('http://example.org');
  2606. $this->assertNotSame($new, $response);
  2607. $this->assertSame('', $response->getHeaderLine('Location'), 'No header should be set');
  2608. $this->assertSame('http://example.org', $new->getHeaderLine('Location'), 'Header should be set');
  2609. $this->assertSame(302, $new->getStatusCode(), 'Status should be updated');
  2610. }
  2611. /**
  2612. * Test get protocol version.
  2613. *
  2614. * @return void
  2615. */
  2616. public function getProtocolVersion()
  2617. {
  2618. $response = new Response();
  2619. $version = $response->getProtocolVersion();
  2620. $this->assertEquals('1.1', $version);
  2621. }
  2622. /**
  2623. * Test with protocol.
  2624. *
  2625. * @return void
  2626. */
  2627. public function testWithProtocol()
  2628. {
  2629. $response = new Response();
  2630. $version = $response->getProtocolVersion();
  2631. $this->assertEquals('1.1', $version);
  2632. $response2 = $response->withProtocolVersion('1.0');
  2633. $version = $response2->getProtocolVersion();
  2634. $this->assertEquals('1.0', $version);
  2635. $version = $response->getProtocolVersion();
  2636. $this->assertEquals('1.1', $version);
  2637. $this->assertNotEquals($response, $response2);
  2638. }
  2639. /**
  2640. * Test with status code.
  2641. *
  2642. * @return void
  2643. */
  2644. public function testWithStatusCode()
  2645. {
  2646. $response = new Response();
  2647. $statusCode = $response->getStatusCode();
  2648. $this->assertEquals(200, $statusCode);
  2649. $response2 = $response->withStatus(404);
  2650. $statusCode = $response2->getStatusCode();
  2651. $this->assertEquals(404, $statusCode);
  2652. $statusCode = $response->getStatusCode();
  2653. $this->assertEquals(200, $statusCode);
  2654. $this->assertNotEquals($response, $response2);
  2655. $response3 = $response->withStatus(111);
  2656. $this->assertEquals(111, $response3->getStatusCode());
  2657. $this->assertSame('', $response3->getReasonPhrase());
  2658. }
  2659. /**
  2660. * Test invalid status codes
  2661. *
  2662. * @return void
  2663. */
  2664. public function testWithStatusInvalid()
  2665. {
  2666. $this->expectException(\InvalidArgumentException::class);
  2667. $this->expectExceptionMessage('Invalid status code: 1001. Use a valid HTTP status code in range 1xx - 5xx.');
  2668. $response = new Response();
  2669. $response->withStatus(1001);
  2670. }
  2671. /**
  2672. * Test get reason phrase.
  2673. *
  2674. * @return void
  2675. */
  2676. public function testGetReasonPhrase()
  2677. {
  2678. $response = new Response();
  2679. $this->assertSame('OK', $response->getReasonPhrase());
  2680. $response = $response->withStatus(404);
  2681. $reasonPhrase = $response->getReasonPhrase();
  2682. $this->assertEquals('Not Found', $reasonPhrase);
  2683. }
  2684. /**
  2685. * Test with body.
  2686. *
  2687. * @return void
  2688. */
  2689. public function testWithBody()
  2690. {
  2691. $response = new Response();
  2692. $body = $response->getBody();
  2693. $body->rewind();
  2694. $result = $body->getContents();
  2695. $this->assertEquals('', $result);
  2696. $stream = new Stream('php://memory', 'wb+');
  2697. $stream->write('test1');
  2698. $response2 = $response->withBody($stream);
  2699. $body = $response2->getBody();
  2700. $body->rewind();
  2701. $result = $body->getContents();
  2702. $this->assertEquals('test1', $result);
  2703. $body = $response->getBody();
  2704. $body->rewind();
  2705. $result = $body->getContents();
  2706. $this->assertEquals('', $result);
  2707. }
  2708. /**
  2709. * Test with string body.
  2710. *
  2711. * @return void
  2712. */
  2713. public function testWithStringBody()
  2714. {
  2715. $response = new Response();
  2716. $newResponse = $response->withStringBody('Foo');
  2717. $body = $newResponse->getBody();
  2718. $this->assertSame('Foo', (string)$body);
  2719. $this->assertNotSame($response, $newResponse);
  2720. $response = new Response();
  2721. $newResponse = $response->withStringBody('');
  2722. $body = $newResponse->getBody();
  2723. $this->assertSame('', (string)$body);
  2724. $this->assertNotSame($response, $newResponse);
  2725. $response = new Response();
  2726. $newResponse = $response->withStringBody(null);
  2727. $body = $newResponse->getBody();
  2728. $this->assertSame('', (string)$body);
  2729. $this->assertNotSame($response, $newResponse);
  2730. $response = new Response();
  2731. $newResponse = $response->withStringBody(1337);
  2732. $body = $newResponse->getBody();
  2733. $this->assertSame('1337', (string)$body);
  2734. $this->assertNotSame($response, $newResponse);
  2735. }
  2736. /**
  2737. * Test with string body with passed array.
  2738. *
  2739. * This should produce an "Array to string conversion" error
  2740. * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
  2741. *
  2742. * @return void
  2743. */
  2744. public function testWithStringBodyArray()
  2745. {
  2746. $this->expectException(\PHPUnit\Framework\Error\Error::class);
  2747. $this->expectExceptionMessage('Array to string conversion');
  2748. $response = new Response();
  2749. $newResponse = $response->withStringBody(['foo' => 'bar']);
  2750. $body = $newResponse->getBody();
  2751. $this->assertSame('', (string)$body);
  2752. $this->assertNotSame($response, $newResponse);
  2753. }
  2754. /**
  2755. * Test get Body.
  2756. *
  2757. * @return void
  2758. */
  2759. public function testGetBody()
  2760. {
  2761. $response = new Response();
  2762. $stream = $response->getBody();
  2763. $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $stream);
  2764. }
  2765. /**
  2766. * Test with header.
  2767. *
  2768. * @return void
  2769. */
  2770. public function testWithHeader()
  2771. {
  2772. $response = new Response();
  2773. $response2 = $response->withHeader('Accept', 'application/json');
  2774. $result = $response2->getHeaders();
  2775. $expected = [
  2776. 'Content-Type' => ['text/html; charset=UTF-8'],
  2777. 'Accept' => ['application/json'],
  2778. ];
  2779. $this->assertEquals($expected, $result);
  2780. $this->assertFalse($response->hasHeader('Accept'));
  2781. }
  2782. /**
  2783. * Test get headers.
  2784. *
  2785. * @return void
  2786. */
  2787. public function testGetHeaders()
  2788. {
  2789. $response = new Response();
  2790. $headers = $response->getHeaders();
  2791. $response = $response->withAddedHeader('Location', 'localhost');
  2792. $response = $response->withAddedHeader('Accept', 'application/json');
  2793. $headers = $response->getHeaders();
  2794. $expected = [
  2795. 'Content-Type' => ['text/html; charset=UTF-8'],
  2796. 'Location' => ['localhost'],
  2797. 'Accept' => ['application/json'],
  2798. ];
  2799. $this->assertEquals($expected, $headers);
  2800. }
  2801. /**
  2802. * Test without header.
  2803. *
  2804. * @return void
  2805. */
  2806. public function testWithoutHeader()
  2807. {
  2808. $response = new Response();
  2809. $response = $response->withAddedHeader('Location', 'localhost');
  2810. $response = $response->withAddedHeader('Accept', 'application/json');
  2811. $response2 = $response->withoutHeader('Location');
  2812. $headers = $response2->getHeaders();
  2813. $expected = [
  2814. 'Content-Type' => ['text/html; charset=UTF-8'],
  2815. 'Accept' => ['application/json'],
  2816. ];
  2817. $this->assertEquals($expected, $headers);
  2818. }
  2819. /**
  2820. * Test get header.
  2821. *
  2822. * @return void
  2823. */
  2824. public function testGetHeader()
  2825. {
  2826. $response = new Response();
  2827. $response = $response->withAddedHeader('Location', 'localhost');
  2828. $result = $response->getHeader('Location');
  2829. $this->assertEquals(['localhost'], $result);
  2830. $result = $response->getHeader('location');
  2831. $this->assertEquals(['localhost'], $result);
  2832. $result = $response->getHeader('does-not-exist');
  2833. $this->assertEquals([], $result);
  2834. }
  2835. /**
  2836. * Test get header line.
  2837. *
  2838. * @return void
  2839. */
  2840. public function testGetHeaderLine()
  2841. {
  2842. $response = new Response();
  2843. $headers = $response->getHeaderLine('Accept');
  2844. $this->assertEquals('', $headers);
  2845. $response = $response->withAddedHeader('Accept', 'application/json');
  2846. $response = $response->withAddedHeader('Accept', 'application/xml');
  2847. $result = $response->getHeaderLine('Accept');
  2848. $expected = 'application/json,application/xml';
  2849. $this->assertEquals($expected, $result);
  2850. $result = $response->getHeaderLine('accept');
  2851. $this->assertEquals($expected, $result);
  2852. }
  2853. /**
  2854. * Test has header.
  2855. *
  2856. * @return void
  2857. */
  2858. public function testHasHeader()
  2859. {
  2860. $response = new Response();
  2861. $response = $response->withAddedHeader('Location', 'localhost');
  2862. $this->assertTrue($response->hasHeader('Location'));
  2863. $this->assertTrue($response->hasHeader('location'));
  2864. $this->assertTrue($response->hasHeader('locATIon'));
  2865. $this->assertFalse($response->hasHeader('Accept'));
  2866. $this->assertFalse($response->hasHeader('accept'));
  2867. }
  2868. /**
  2869. * Tests __debugInfo
  2870. *
  2871. * @return void
  2872. */
  2873. public function testDebugInfo()
  2874. {
  2875. $response = new Response();
  2876. $response = $response->withStringBody('Foo');
  2877. $result = $response->__debugInfo();
  2878. $expected = [
  2879. 'status' => 200,
  2880. 'contentType' => 'text/html',
  2881. 'headers' => [
  2882. 'Content-Type' => ['text/html; charset=UTF-8'],
  2883. ],
  2884. 'file' => null,
  2885. 'fileRange' => [],
  2886. 'cookies' => new CookieCollection(),
  2887. 'cacheDirectives' => [],
  2888. 'body' => 'Foo',
  2889. ];
  2890. $this->assertEquals($expected, $result);
  2891. }
  2892. }