ResponseTest.php 90 KB

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