ResponseTest.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  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. use Cake\Network\Request;
  17. use Cake\Network\Response;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Class ResponseTest
  21. *
  22. */
  23. class ResponseTest extends TestCase
  24. {
  25. /**
  26. * Setup for tests
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. include_once __DIR__ . DS . 'mocks.php';
  34. }
  35. /**
  36. * Cleanup after tests
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. }
  44. /**
  45. * Tests the request object constructor
  46. *
  47. * @return void
  48. */
  49. public function testConstruct()
  50. {
  51. $response = new Response();
  52. $this->assertNull($response->body());
  53. $this->assertEquals('UTF-8', $response->charset());
  54. $this->assertEquals('text/html', $response->type());
  55. $this->assertEquals(200, $response->statusCode());
  56. $options = [
  57. 'body' => 'This is the body',
  58. 'charset' => 'my-custom-charset',
  59. 'type' => 'mp3',
  60. 'status' => '203'
  61. ];
  62. $response = new Response($options);
  63. $this->assertEquals('This is the body', $response->body());
  64. $this->assertEquals('my-custom-charset', $response->charset());
  65. $this->assertEquals('audio/mpeg', $response->type());
  66. $this->assertEquals(203, $response->statusCode());
  67. $options = [
  68. 'body' => 'This is the body',
  69. 'charset' => 'my-custom-charset',
  70. 'type' => 'mp3',
  71. 'status' => '422',
  72. 'statusCodes' => [
  73. 422 => 'Unprocessable Entity'
  74. ]
  75. ];
  76. $response = new Response($options);
  77. $this->assertEquals($options['body'], $response->body());
  78. $this->assertEquals($options['charset'], $response->charset());
  79. $this->assertEquals($response->getMimeType($options['type']), $response->type());
  80. $this->assertEquals($options['status'], $response->statusCode());
  81. }
  82. /**
  83. * Tests the body method
  84. *
  85. * @return void
  86. */
  87. public function testBody()
  88. {
  89. $response = new Response();
  90. $this->assertNull($response->body());
  91. $response->body('Response body');
  92. $this->assertEquals('Response body', $response->body());
  93. $this->assertEquals('Changed Body', $response->body('Changed Body'));
  94. }
  95. /**
  96. * Tests the charset method
  97. *
  98. * @return void
  99. */
  100. public function testCharset()
  101. {
  102. $response = new Response();
  103. $this->assertEquals('UTF-8', $response->charset());
  104. $response->charset('iso-8859-1');
  105. $this->assertEquals('iso-8859-1', $response->charset());
  106. $this->assertEquals('UTF-16', $response->charset('UTF-16'));
  107. }
  108. /**
  109. * Tests the statusCode method
  110. *
  111. * @expectedException \InvalidArgumentException
  112. * @return void
  113. */
  114. public function testStatusCode()
  115. {
  116. $response = new Response();
  117. $this->assertEquals(200, $response->statusCode());
  118. $response->statusCode(404);
  119. $this->assertEquals(404, $response->statusCode());
  120. $this->assertEquals(500, $response->statusCode(500));
  121. //Throws exception
  122. $response->statusCode(1001);
  123. }
  124. /**
  125. * Tests the type method
  126. *
  127. * @return void
  128. */
  129. public function testType()
  130. {
  131. $response = new Response();
  132. $this->assertEquals('text/html', $response->type());
  133. $response->type('pdf');
  134. $this->assertEquals('application/pdf', $response->type());
  135. $this->assertEquals('application/crazy-mime', $response->type('application/crazy-mime'));
  136. $this->assertEquals('application/json', $response->type('json'));
  137. $this->assertEquals('text/vnd.wap.wml', $response->type('wap'));
  138. $this->assertEquals('application/vnd.wap.xhtml+xml', $response->type('xhtml-mobile'));
  139. $this->assertEquals('text/csv', $response->type('csv'));
  140. $response->type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);
  141. $this->assertEquals('application/keynote', $response->type('keynote'));
  142. $this->assertEquals('application/bat', $response->type('bat'));
  143. $this->assertFalse($response->type('wackytype'));
  144. }
  145. /**
  146. * Tests the header method
  147. *
  148. * @return void
  149. */
  150. public function testHeader()
  151. {
  152. $response = new Response();
  153. $headers = [];
  154. $this->assertEquals($headers, $response->header());
  155. $response->header('Location', 'http://example.com');
  156. $headers += ['Location' => 'http://example.com'];
  157. $this->assertEquals($headers, $response->header());
  158. //Headers with the same name are overwritten
  159. $response->header('Location', 'http://example2.com');
  160. $headers = ['Location' => 'http://example2.com'];
  161. $this->assertEquals($headers, $response->header());
  162. $response->header(['WWW-Authenticate' => 'Negotiate']);
  163. $headers += ['WWW-Authenticate' => 'Negotiate'];
  164. $this->assertEquals($headers, $response->header());
  165. $response->header(['WWW-Authenticate' => 'Not-Negotiate']);
  166. $headers['WWW-Authenticate'] = 'Not-Negotiate';
  167. $this->assertEquals($headers, $response->header());
  168. $response->header(['Age' => 12, 'Allow' => 'GET, HEAD']);
  169. $headers += ['Age' => 12, 'Allow' => 'GET, HEAD'];
  170. $this->assertEquals($headers, $response->header());
  171. // String headers are allowed
  172. $response->header('Content-Language: da');
  173. $headers += ['Content-Language' => 'da'];
  174. $this->assertEquals($headers, $response->header());
  175. $response->header('Content-Language: da');
  176. $headers += ['Content-Language' => 'da'];
  177. $this->assertEquals($headers, $response->header());
  178. $response->header(['Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache']);
  179. $headers += ['Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'];
  180. $this->assertEquals($headers, $response->header());
  181. $response->header('Access-Control-Allow-Origin', ['domain1', 'domain2']);
  182. $headers += ['Access-Control-Allow-Origin' => ['domain1', 'domain2']];
  183. $this->assertEquals($headers, $response->header());
  184. }
  185. /**
  186. * Tests the send method
  187. *
  188. * @return void
  189. */
  190. public function testSend()
  191. {
  192. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent', '_setCookies']);
  193. $response->header([
  194. 'Content-Language' => 'es',
  195. 'WWW-Authenticate' => 'Negotiate',
  196. 'Access-Control-Allow-Origin' => ['domain1', 'domain2'],
  197. ]);
  198. $response->body('the response body');
  199. $response->expects($this->once())->method('_sendContent')->with('the response body');
  200. $response->expects($this->at(0))->method('_setCookies');
  201. $response->expects($this->at(1))
  202. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  203. $response->expects($this->at(2))
  204. ->method('_sendHeader')->with('Content-Language', 'es');
  205. $response->expects($this->at(3))
  206. ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
  207. $response->expects($this->at(4))
  208. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
  209. $response->expects($this->at(5))
  210. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
  211. $response->expects($this->at(6))
  212. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  213. $response->send();
  214. }
  215. /**
  216. * Data provider for content type tests.
  217. *
  218. * @return array
  219. */
  220. public static function charsetTypeProvider()
  221. {
  222. return [
  223. ['mp3', 'audio/mpeg'],
  224. ['js', 'application/javascript; charset=UTF-8'],
  225. ['json', 'application/json; charset=UTF-8'],
  226. ['xml', 'application/xml; charset=UTF-8'],
  227. ['txt', 'text/plain; charset=UTF-8'],
  228. ];
  229. }
  230. /**
  231. * Tests the send method and changing the content type
  232. *
  233. * @dataProvider charsetTypeProvider
  234. * @return void
  235. */
  236. public function testSendChangingContentType($original, $expected)
  237. {
  238. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent', '_setCookies']);
  239. $response->type($original);
  240. $response->body('the response body');
  241. $response->expects($this->once())->method('_sendContent')->with('the response body');
  242. $response->expects($this->at(0))->method('_setCookies');
  243. $response->expects($this->at(1))
  244. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  245. $response->expects($this->at(2))
  246. ->method('_sendHeader')->with('Content-Type', $expected);
  247. $response->send();
  248. }
  249. /**
  250. * Tests the send method and changing the content type to JS without adding the charset
  251. *
  252. * @return void
  253. */
  254. public function testSendChangingContentTypeWithoutCharset()
  255. {
  256. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent', '_setCookies']);
  257. $response->type('js');
  258. $response->charset('');
  259. $response->body('var $foo = "bar";');
  260. $response->expects($this->once())->method('_sendContent')->with('var $foo = "bar";');
  261. $response->expects($this->at(0))->method('_setCookies');
  262. $response->expects($this->at(1))
  263. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  264. $response->expects($this->at(2))
  265. ->method('_sendHeader')->with('Content-Type', 'application/javascript');
  266. $response->send();
  267. }
  268. /**
  269. * Tests the send method and changing the content type
  270. *
  271. * @return void
  272. */
  273. public function testSendWithLocation()
  274. {
  275. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent', '_setCookies']);
  276. $response->header('Location', 'http://www.example.com');
  277. $response->expects($this->at(0))->method('_setCookies');
  278. $response->expects($this->at(1))
  279. ->method('_sendHeader')->with('HTTP/1.1 302 Found');
  280. $response->expects($this->at(2))
  281. ->method('_sendHeader')->with('Location', 'http://www.example.com');
  282. $response->expects($this->at(3))
  283. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  284. $response->send();
  285. }
  286. /**
  287. * Tests the send method and changing the content type
  288. *
  289. * @return void
  290. */
  291. public function testSendWithCallableBody()
  292. {
  293. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  294. $response->body(function () {
  295. echo 'the response body';
  296. });
  297. ob_start();
  298. $response->send();
  299. $this->assertEquals('the response body', ob_get_clean());
  300. }
  301. /**
  302. * Tests that the returned a string from a body callable is also sent
  303. * as the response body
  304. *
  305. * @return void
  306. */
  307. public function testSendWithCallableBodyWithReturn()
  308. {
  309. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  310. $response->body(function () {
  311. return 'the response body';
  312. });
  313. ob_start();
  314. $response->send();
  315. $this->assertEquals('the response body', ob_get_clean());
  316. }
  317. /**
  318. * Tests the disableCache method
  319. *
  320. * @return void
  321. */
  322. public function testDisableCache()
  323. {
  324. $response = new Response();
  325. $expected = [
  326. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  327. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  328. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  329. ];
  330. $response->disableCache();
  331. $this->assertEquals($expected, $response->header());
  332. }
  333. /**
  334. * Tests the cache method
  335. *
  336. * @return void
  337. */
  338. public function testCache()
  339. {
  340. $response = new Response();
  341. $since = time();
  342. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  343. $response->expires('+1 day');
  344. $expected = [
  345. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  346. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  347. 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
  348. 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time())
  349. ];
  350. $response->cache($since);
  351. $this->assertEquals($expected, $response->header());
  352. $response = new Response();
  353. $since = time();
  354. $time = '+5 day';
  355. $expected = [
  356. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  357. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  358. 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
  359. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time())
  360. ];
  361. $response->cache($since, $time);
  362. $this->assertEquals($expected, $response->header());
  363. $response = new Response();
  364. $since = time();
  365. $time = time();
  366. $expected = [
  367. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  368. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  369. 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
  370. 'Cache-Control' => 'public, max-age=0'
  371. ];
  372. $response->cache($since, $time);
  373. $this->assertEquals($expected, $response->header());
  374. }
  375. /**
  376. * Tests the compress method
  377. *
  378. * @return void
  379. */
  380. public function testCompress()
  381. {
  382. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  383. $response = new Response();
  384. if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
  385. $this->assertFalse($response->compress());
  386. $this->markTestSkipped('Is not possible to test output compression');
  387. }
  388. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  389. $result = $response->compress();
  390. $this->assertFalse($result);
  391. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  392. $result = $response->compress();
  393. $this->assertTrue($result);
  394. $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
  395. ob_get_clean();
  396. }
  397. /**
  398. * Tests the httpCodes method
  399. *
  400. * @expectedException \InvalidArgumentException
  401. * @return void
  402. */
  403. public function testHttpCodes()
  404. {
  405. $response = new Response();
  406. $result = $response->httpCodes();
  407. $this->assertEquals(41, count($result));
  408. $result = $response->httpCodes(100);
  409. $expected = [100 => 'Continue'];
  410. $this->assertEquals($expected, $result);
  411. $codes = [
  412. 381 => 'Unicorn Moved',
  413. 555 => 'Unexpected Minotaur'
  414. ];
  415. $result = $response->httpCodes($codes);
  416. $this->assertTrue($result);
  417. $this->assertEquals(43, count($response->httpCodes()));
  418. $result = $response->httpCodes(381);
  419. $expected = [381 => 'Unicorn Moved'];
  420. $this->assertEquals($expected, $result);
  421. $codes = [404 => 'Sorry Bro'];
  422. $result = $response->httpCodes($codes);
  423. $this->assertTrue($result);
  424. $this->assertEquals(43, count($response->httpCodes()));
  425. $result = $response->httpCodes(404);
  426. $expected = [404 => 'Sorry Bro'];
  427. $this->assertEquals($expected, $result);
  428. //Throws exception
  429. $response->httpCodes([
  430. 0 => 'Nothing Here',
  431. -1 => 'Reverse Infinity',
  432. 12345 => 'Universal Password',
  433. 'Hello' => 'World'
  434. ]);
  435. }
  436. /**
  437. * Tests the download method
  438. *
  439. * @return void
  440. */
  441. public function testDownload()
  442. {
  443. $response = new Response();
  444. $expected = [
  445. 'Content-Disposition' => 'attachment; filename="myfile.mp3"'
  446. ];
  447. $response->download('myfile.mp3');
  448. $this->assertEquals($expected, $response->header());
  449. }
  450. /**
  451. * Tests the mapType method
  452. *
  453. * @return void
  454. */
  455. public function testMapType()
  456. {
  457. $response = new Response();
  458. $this->assertEquals('wav', $response->mapType('audio/x-wav'));
  459. $this->assertEquals('pdf', $response->mapType('application/pdf'));
  460. $this->assertEquals('xml', $response->mapType('text/xml'));
  461. $this->assertEquals('html', $response->mapType('*/*'));
  462. $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
  463. $expected = ['json', 'xhtml', 'css'];
  464. $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
  465. $this->assertEquals($expected, $result);
  466. }
  467. /**
  468. * Tests the outputCompressed method
  469. *
  470. * @return void
  471. */
  472. public function testOutputCompressed()
  473. {
  474. $response = new Response();
  475. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  476. $result = $response->outputCompressed();
  477. $this->assertFalse($result);
  478. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  479. $result = $response->outputCompressed();
  480. $this->assertFalse($result);
  481. if (!extension_loaded("zlib")) {
  482. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  483. }
  484. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  485. if (ini_get("zlib.output_compression") !== '1') {
  486. ob_start('ob_gzhandler');
  487. }
  488. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  489. $result = $response->outputCompressed();
  490. $this->assertTrue($result);
  491. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  492. $result = $response->outputCompressed();
  493. $this->assertFalse($result);
  494. if (ini_get("zlib.output_compression") !== '1') {
  495. ob_get_clean();
  496. }
  497. }
  498. /**
  499. * Tests getting/setting the protocol
  500. *
  501. * @return void
  502. */
  503. public function testProtocol()
  504. {
  505. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  506. $response->protocol('HTTP/1.0');
  507. $this->assertEquals('HTTP/1.0', $response->protocol());
  508. $response->expects($this->at(0))
  509. ->method('_sendHeader')->with('HTTP/1.0 200 OK');
  510. $response->send();
  511. }
  512. /**
  513. * Tests getting/setting the Content-Length
  514. *
  515. * @return void
  516. */
  517. public function testLength()
  518. {
  519. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  520. $response->length(100);
  521. $this->assertEquals(100, $response->length());
  522. $response->expects($this->at(1))
  523. ->method('_sendHeader')->with('Content-Length', 100);
  524. $response->send();
  525. }
  526. /**
  527. * Tests that the response body is unset if the status code is 304 or 204
  528. *
  529. * @return void
  530. */
  531. public function testUnmodifiedContent()
  532. {
  533. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  534. $response->body('This is a body');
  535. $response->statusCode(204);
  536. $response->expects($this->once())
  537. ->method('_sendContent')->with('');
  538. $response->send();
  539. $this->assertFalse(array_key_exists('Content-Type', $response->header()));
  540. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  541. $response->body('This is a body');
  542. $response->statusCode(304);
  543. $response->expects($this->once())
  544. ->method('_sendContent')->with('');
  545. $response->send();
  546. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  547. $response->body('This is a body');
  548. $response->statusCode(200);
  549. $response->expects($this->once())
  550. ->method('_sendContent')->with('This is a body');
  551. $response->send();
  552. }
  553. /**
  554. * Tests setting the expiration date
  555. *
  556. * @return void
  557. */
  558. public function testExpires()
  559. {
  560. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  561. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  562. $response->expires($now);
  563. $now->setTimeZone(new \DateTimeZone('UTC'));
  564. $this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->expires());
  565. $response->expects($this->at(1))
  566. ->method('_sendHeader')->with('Expires', $now->format('D, j M Y H:i:s') . ' GMT');
  567. $response->send();
  568. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  569. $now = time();
  570. $response->expires($now);
  571. $this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->expires());
  572. $response->expects($this->at(1))
  573. ->method('_sendHeader')->with('Expires', gmdate('D, j M Y H:i:s', $now) . ' GMT');
  574. $response->send();
  575. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  576. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  577. $response->expires('+1 day');
  578. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->expires());
  579. $response->expects($this->at(1))
  580. ->method('_sendHeader')->with('Expires', $time->format('D, j M Y H:i:s') . ' GMT');
  581. $response->send();
  582. }
  583. /**
  584. * Tests setting the modification date
  585. *
  586. * @return void
  587. */
  588. public function testModified()
  589. {
  590. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  591. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  592. $response->modified($now);
  593. $now->setTimeZone(new \DateTimeZone('UTC'));
  594. $this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  595. $response->expects($this->at(1))
  596. ->method('_sendHeader')->with('Last-Modified', $now->format('D, j M Y H:i:s') . ' GMT');
  597. $response->send();
  598. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  599. $now = time();
  600. $response->modified($now);
  601. $this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->modified());
  602. $response->expects($this->at(1))
  603. ->method('_sendHeader')->with('Last-Modified', gmdate('D, j M Y H:i:s', $now) . ' GMT');
  604. $response->send();
  605. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  606. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  607. $response->modified('+1 day');
  608. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  609. $response->expects($this->at(1))
  610. ->method('_sendHeader')->with('Last-Modified', $time->format('D, j M Y H:i:s') . ' GMT');
  611. $response->send();
  612. }
  613. /**
  614. * Tests setting of public/private Cache-Control directives
  615. *
  616. * @return void
  617. */
  618. public function testSharable()
  619. {
  620. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  621. $this->assertNull($response->sharable());
  622. $response->sharable(true);
  623. $headers = $response->header();
  624. $this->assertEquals('public', $headers['Cache-Control']);
  625. $response->expects($this->at(1))
  626. ->method('_sendHeader')->with('Cache-Control', 'public');
  627. $response->send();
  628. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  629. $response->sharable(false);
  630. $headers = $response->header();
  631. $this->assertEquals('private', $headers['Cache-Control']);
  632. $response->expects($this->at(1))
  633. ->method('_sendHeader')->with('Cache-Control', 'private');
  634. $response->send();
  635. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  636. $response->sharable(true);
  637. $headers = $response->header();
  638. $this->assertEquals('public', $headers['Cache-Control']);
  639. $response->sharable(false);
  640. $headers = $response->header();
  641. $this->assertEquals('private', $headers['Cache-Control']);
  642. $response->expects($this->at(1))
  643. ->method('_sendHeader')->with('Cache-Control', 'private');
  644. $response->send();
  645. $this->assertFalse($response->sharable());
  646. $response->sharable(true);
  647. $this->assertTrue($response->sharable());
  648. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  649. $response->sharable(true, 3600);
  650. $headers = $response->header();
  651. $this->assertEquals('public, max-age=3600', $headers['Cache-Control']);
  652. $response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
  653. $response->sharable(false, 3600);
  654. $headers = $response->header();
  655. $this->assertEquals('private, max-age=3600', $headers['Cache-Control']);
  656. $response->send();
  657. }
  658. /**
  659. * Tests setting of max-age Cache-Control directive
  660. *
  661. * @return void
  662. */
  663. public function testMaxAge()
  664. {
  665. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  666. $this->assertNull($response->maxAge());
  667. $response->maxAge(3600);
  668. $this->assertEquals(3600, $response->maxAge());
  669. $headers = $response->header();
  670. $this->assertEquals('max-age=3600', $headers['Cache-Control']);
  671. $response->expects($this->at(1))
  672. ->method('_sendHeader')->with('Cache-Control', 'max-age=3600');
  673. $response->send();
  674. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  675. $response->maxAge(3600);
  676. $response->sharable(false);
  677. $headers = $response->header();
  678. $this->assertEquals('max-age=3600, private', $headers['Cache-Control']);
  679. $response->expects($this->at(1))
  680. ->method('_sendHeader')->with('Cache-Control', 'max-age=3600, private');
  681. $response->send();
  682. }
  683. /**
  684. * Tests setting of s-maxage Cache-Control directive
  685. *
  686. * @return void
  687. */
  688. public function testSharedMaxAge()
  689. {
  690. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  691. $this->assertNull($response->maxAge());
  692. $response->sharedMaxAge(3600);
  693. $this->assertEquals(3600, $response->sharedMaxAge());
  694. $headers = $response->header();
  695. $this->assertEquals('s-maxage=3600', $headers['Cache-Control']);
  696. $response->expects($this->at(1))
  697. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600');
  698. $response->send();
  699. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  700. $response->sharedMaxAge(3600);
  701. $response->sharable(true);
  702. $headers = $response->header();
  703. $this->assertEquals('s-maxage=3600, public', $headers['Cache-Control']);
  704. $response->expects($this->at(1))
  705. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, public');
  706. $response->send();
  707. }
  708. /**
  709. * Tests setting of must-revalidate Cache-Control directive
  710. *
  711. * @return void
  712. */
  713. public function testMustRevalidate()
  714. {
  715. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  716. $this->assertFalse($response->mustRevalidate());
  717. $response->mustRevalidate(true);
  718. $this->assertTrue($response->mustRevalidate());
  719. $headers = $response->header();
  720. $this->assertEquals('must-revalidate', $headers['Cache-Control']);
  721. $response->expects($this->at(1))
  722. ->method('_sendHeader')->with('Cache-Control', 'must-revalidate');
  723. $response->send();
  724. $response->mustRevalidate(false);
  725. $this->assertFalse($response->mustRevalidate());
  726. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  727. $response->sharedMaxAge(3600);
  728. $response->mustRevalidate(true);
  729. $headers = $response->header();
  730. $this->assertEquals('s-maxage=3600, must-revalidate', $headers['Cache-Control']);
  731. $response->expects($this->at(1))
  732. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, must-revalidate');
  733. $response->send();
  734. }
  735. /**
  736. * Tests getting/setting the Vary header
  737. *
  738. * @return void
  739. */
  740. public function testVary()
  741. {
  742. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  743. $response->vary('Accept-encoding');
  744. $this->assertEquals(['Accept-encoding'], $response->vary());
  745. $response->expects($this->at(1))
  746. ->method('_sendHeader')->with('Vary', 'Accept-encoding');
  747. $response->send();
  748. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  749. $response->vary(['Accept-language', 'Accept-encoding']);
  750. $response->expects($this->at(1))
  751. ->method('_sendHeader')->with('Vary', 'Accept-language, Accept-encoding');
  752. $response->send();
  753. $this->assertEquals(['Accept-language', 'Accept-encoding'], $response->vary());
  754. }
  755. /**
  756. * Tests getting/setting the Etag header
  757. *
  758. * @return void
  759. */
  760. public function testEtag()
  761. {
  762. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  763. $response->etag('something');
  764. $this->assertEquals('"something"', $response->etag());
  765. $response->expects($this->at(1))
  766. ->method('_sendHeader')->with('Etag', '"something"');
  767. $response->send();
  768. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  769. $response->etag('something', true);
  770. $this->assertEquals('W/"something"', $response->etag());
  771. $response->expects($this->at(1))
  772. ->method('_sendHeader')->with('Etag', 'W/"something"');
  773. $response->send();
  774. }
  775. /**
  776. * Tests that the response is able to be marked as not modified
  777. *
  778. * @return void
  779. */
  780. public function testNotModified()
  781. {
  782. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', '_sendContent']);
  783. $response->body('something');
  784. $response->statusCode(200);
  785. $response->length(100);
  786. $response->modified('now');
  787. $response->notModified();
  788. $this->assertEmpty($response->header());
  789. $this->assertEmpty($response->body());
  790. $this->assertEquals(304, $response->statusCode());
  791. }
  792. /**
  793. * Test checkNotModified method
  794. *
  795. * @return void
  796. */
  797. public function testCheckNotModifiedByEtagStar()
  798. {
  799. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  800. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  801. $response->etag('something');
  802. $response->expects($this->once())->method('notModified');
  803. $response->checkNotModified(new Request);
  804. }
  805. /**
  806. * Test checkNotModified method
  807. *
  808. * @return void
  809. */
  810. public function testCheckNotModifiedByEtagExact()
  811. {
  812. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  813. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  814. $response->etag('something', true);
  815. $response->expects($this->once())->method('notModified');
  816. $this->assertTrue($response->checkNotModified(new Request));
  817. }
  818. /**
  819. * Test checkNotModified method
  820. *
  821. * @return void
  822. */
  823. public function testCheckNotModifiedByEtagAndTime()
  824. {
  825. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  826. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  827. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  828. $response->etag('something', true);
  829. $response->modified('2012-01-01 00:00:00');
  830. $response->expects($this->once())->method('notModified');
  831. $this->assertTrue($response->checkNotModified(new Request));
  832. }
  833. /**
  834. * Test checkNotModified method
  835. *
  836. * @return void
  837. */
  838. public function testCheckNotModifiedByEtagAndTimeMismatch()
  839. {
  840. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  841. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  842. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  843. $response->etag('something', true);
  844. $response->modified('2012-01-01 00:00:01');
  845. $response->expects($this->never())->method('notModified');
  846. $this->assertFalse($response->checkNotModified(new Request));
  847. }
  848. /**
  849. * Test checkNotModified method
  850. *
  851. * @return void
  852. */
  853. public function testCheckNotModifiedByEtagMismatch()
  854. {
  855. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something-else", "other"';
  856. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  857. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  858. $response->etag('something', true);
  859. $response->modified('2012-01-01 00:00:00');
  860. $response->expects($this->never())->method('notModified');
  861. $this->assertFalse($response->checkNotModified(new Request));
  862. }
  863. /**
  864. * Test checkNotModified method
  865. *
  866. * @return void
  867. */
  868. public function testCheckNotModifiedByTime()
  869. {
  870. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  871. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  872. $response->modified('2012-01-01 00:00:00');
  873. $response->expects($this->once())->method('notModified');
  874. $this->assertTrue($response->checkNotModified(new Request));
  875. }
  876. /**
  877. * Test checkNotModified method
  878. *
  879. * @return void
  880. */
  881. public function testCheckNotModifiedNoHints()
  882. {
  883. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  884. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  885. $response = $this->getMock('Cake\Network\Response', ['notModified']);
  886. $response->expects($this->never())->method('notModified');
  887. $this->assertFalse($response->checkNotModified(new Request));
  888. }
  889. /**
  890. * Test cookie setting
  891. *
  892. * @return void
  893. */
  894. public function testCookieSettings()
  895. {
  896. $response = new Response();
  897. $cookie = [
  898. 'name' => 'CakeTestCookie[Testing]'
  899. ];
  900. $response->cookie($cookie);
  901. $expected = [
  902. 'name' => 'CakeTestCookie[Testing]',
  903. 'value' => '',
  904. 'expire' => 0,
  905. 'path' => '/',
  906. 'domain' => '',
  907. 'secure' => false,
  908. 'httpOnly' => false];
  909. $result = $response->cookie('CakeTestCookie[Testing]');
  910. $this->assertEquals($expected, $result);
  911. $cookie = [
  912. 'name' => 'CakeTestCookie[Testing2]',
  913. 'value' => '[a,b,c]',
  914. 'expire' => 1000,
  915. 'path' => '/test',
  916. 'secure' => true
  917. ];
  918. $response->cookie($cookie);
  919. $expected = [
  920. 'CakeTestCookie[Testing]' => [
  921. 'name' => 'CakeTestCookie[Testing]',
  922. 'value' => '',
  923. 'expire' => 0,
  924. 'path' => '/',
  925. 'domain' => '',
  926. 'secure' => false,
  927. 'httpOnly' => false
  928. ],
  929. 'CakeTestCookie[Testing2]' => [
  930. 'name' => 'CakeTestCookie[Testing2]',
  931. 'value' => '[a,b,c]',
  932. 'expire' => 1000,
  933. 'path' => '/test',
  934. 'domain' => '',
  935. 'secure' => true,
  936. 'httpOnly' => false
  937. ]
  938. ];
  939. $result = $response->cookie();
  940. $this->assertEquals($expected, $result);
  941. $cookie = $expected['CakeTestCookie[Testing]'];
  942. $cookie['value'] = 'test';
  943. $response->cookie($cookie);
  944. $expected = [
  945. 'CakeTestCookie[Testing]' => [
  946. 'name' => 'CakeTestCookie[Testing]',
  947. 'value' => 'test',
  948. 'expire' => 0,
  949. 'path' => '/',
  950. 'domain' => '',
  951. 'secure' => false,
  952. 'httpOnly' => false
  953. ],
  954. 'CakeTestCookie[Testing2]' => [
  955. 'name' => 'CakeTestCookie[Testing2]',
  956. 'value' => '[a,b,c]',
  957. 'expire' => 1000,
  958. 'path' => '/test',
  959. 'domain' => '',
  960. 'secure' => true,
  961. 'httpOnly' => false
  962. ]
  963. ];
  964. $result = $response->cookie();
  965. $this->assertEquals($expected, $result);
  966. }
  967. /**
  968. * Test CORS
  969. *
  970. * @dataProvider corsData
  971. * @param Request $request
  972. * @param string $origin
  973. * @param string|array $domains
  974. * @param string|array $methods
  975. * @param string|array $headers
  976. * @param string|bool $expectedOrigin
  977. * @param string|bool $expectedMethods
  978. * @param string|bool $expectedHeaders
  979. * @return void
  980. */
  981. public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false)
  982. {
  983. $request->env('HTTP_ORIGIN', $origin);
  984. $response = $this->getMock('Cake\Network\Response', ['header']);
  985. $method = $response->expects(!$expectedOrigin ? $this->never() : $this->at(0))->method('header');
  986. $expectedOrigin && $method->with('Access-Control-Allow-Origin', $expectedOrigin ? $expectedOrigin : $this->anything());
  987. $i = 1;
  988. if ($expectedMethods) {
  989. $response->expects($this->at($i++))
  990. ->method('header')
  991. ->with('Access-Control-Allow-Methods', $expectedMethods ? $expectedMethods : $this->anything());
  992. }
  993. if ($expectedHeaders) {
  994. $response->expects($this->at($i++))
  995. ->method('header')
  996. ->with('Access-Control-Allow-Headers', $expectedHeaders ? $expectedHeaders : $this->anything());
  997. }
  998. $response->cors($request, $domains, $methods, $headers);
  999. unset($_SERVER['HTTP_ORIGIN']);
  1000. }
  1001. /**
  1002. * Feed for testCors
  1003. *
  1004. * @return array
  1005. */
  1006. public function corsData()
  1007. {
  1008. $fooRequest = new Request();
  1009. $secureRequest = $this->getMock('Cake\Network\Request', ['is']);
  1010. $secureRequest->expects($this->any())
  1011. ->method('is')
  1012. ->with('ssl')
  1013. ->will($this->returnValue(true));
  1014. return [
  1015. [$fooRequest, null, '*', '', '', false, false],
  1016. [$fooRequest, 'http://www.foo.com', '*', '', '', '*', false],
  1017. [$fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false],
  1018. [$fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false],
  1019. [$fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false],
  1020. [$fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false],
  1021. [$fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false],
  1022. [$fooRequest, 'http://www.foo.com', ['*.bar.com', '*.foo.com'], '', '', 'http://www.foo.com', false],
  1023. [$secureRequest, 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false],
  1024. [$secureRequest, 'https://www.bar.com', 'http://www.bar.com', '', '', false, false],
  1025. [$secureRequest, 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false],
  1026. [$fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'],
  1027. [$fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'],
  1028. [$fooRequest, 'http://www.foo.com', '*.foo.com', ['GET', 'POST'], '', 'http://www.foo.com', 'GET, POST'],
  1029. [$fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'],
  1030. [$fooRequest, 'http://www.foo.com', '*', '', ['X-CakePHP', 'X-MyApp'], '*', false, 'X-CakePHP, X-MyApp'],
  1031. [$fooRequest, 'http://www.foo.com', '*', ['GET', 'OPTIONS'], ['X-CakePHP', 'X-MyApp'], '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'],
  1032. ];
  1033. }
  1034. /**
  1035. * testFileNotFound
  1036. *
  1037. * @expectedException \Cake\Network\Exception\NotFoundException
  1038. * @return void
  1039. */
  1040. public function testFileNotFound()
  1041. {
  1042. $response = new Response();
  1043. $response->file('/some/missing/folder/file.jpg');
  1044. }
  1045. /**
  1046. * test file with ..
  1047. *
  1048. * @expectedException \Cake\Network\Exception\NotFoundException
  1049. * @expectedExceptionMessage The requested file contains `..` and will not be read.
  1050. * @return void
  1051. */
  1052. public function testFileWithPathTraversal()
  1053. {
  1054. $response = new Response();
  1055. $response->file('my/../cat.gif');
  1056. }
  1057. /**
  1058. * test file with ..
  1059. *
  1060. * @expectedException \Cake\Network\Exception\NotFoundException
  1061. * @expectedExceptionMessage my/ca..t.gif was not found or not readable
  1062. * @return void
  1063. */
  1064. public function testFileWithDotIntheName()
  1065. {
  1066. $response = new Response();
  1067. $response->file('my/ca..t.gif');
  1068. }
  1069. /**
  1070. * testFile method
  1071. *
  1072. * @return void
  1073. */
  1074. public function testFile()
  1075. {
  1076. $response = $this->getMock('Cake\Network\Response', [
  1077. 'header',
  1078. 'type',
  1079. '_sendHeader',
  1080. '_setContentType',
  1081. '_isActive',
  1082. '_clearBuffer',
  1083. '_flushBuffer'
  1084. ]);
  1085. $response->expects($this->exactly(1))
  1086. ->method('type')
  1087. ->with('css')
  1088. ->will($this->returnArgument(0));
  1089. $response->expects($this->at(1))
  1090. ->method('header')
  1091. ->with('Accept-Ranges', 'bytes');
  1092. $response->expects($this->once())->method('_clearBuffer');
  1093. $response->expects($this->once())->method('_flushBuffer');
  1094. $response->expects($this->exactly(1))
  1095. ->method('_isActive')
  1096. ->will($this->returnValue(true));
  1097. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1098. ob_start();
  1099. $result = $response->send();
  1100. $output = ob_get_clean();
  1101. $this->assertEquals("/* this is the test asset css file */", trim($output));
  1102. $this->assertTrue($result !== false);
  1103. }
  1104. /**
  1105. * testFileWithDownloadAndName
  1106. *
  1107. * @return void
  1108. */
  1109. public function testFileWithDownloadAndName()
  1110. {
  1111. $response = $this->getMock('Cake\Network\Response', [
  1112. 'header',
  1113. 'type',
  1114. 'download',
  1115. '_sendHeader',
  1116. '_setContentType',
  1117. '_isActive',
  1118. '_clearBuffer',
  1119. '_flushBuffer'
  1120. ]);
  1121. $response->expects($this->exactly(1))
  1122. ->method('type')
  1123. ->with('css')
  1124. ->will($this->returnArgument(0));
  1125. $response->expects($this->once())
  1126. ->method('download')
  1127. ->with('something_special.css');
  1128. $response->expects($this->at(2))
  1129. ->method('header')
  1130. ->with('Content-Transfer-Encoding', 'binary');
  1131. $response->expects($this->at(3))
  1132. ->method('header')
  1133. ->with('Accept-Ranges', 'bytes');
  1134. $response->expects($this->once())->method('_clearBuffer');
  1135. $response->expects($this->once())->method('_flushBuffer');
  1136. $response->expects($this->exactly(1))
  1137. ->method('_isActive')
  1138. ->will($this->returnValue(true));
  1139. $response->file(
  1140. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1141. [
  1142. 'name' => 'something_special.css',
  1143. 'download' => true,
  1144. ]
  1145. );
  1146. ob_start();
  1147. $result = $response->send();
  1148. $output = ob_get_clean();
  1149. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1150. $this->assertNotSame(false, $result);
  1151. }
  1152. /**
  1153. * testFileWithUnknownFileTypeGeneric method
  1154. *
  1155. * @return void
  1156. */
  1157. public function testFileWithUnknownFileTypeGeneric()
  1158. {
  1159. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1160. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1161. $response = $this->getMock('Cake\Network\Response', [
  1162. 'header',
  1163. 'type',
  1164. 'download',
  1165. '_sendHeader',
  1166. '_setContentType',
  1167. '_isActive',
  1168. '_clearBuffer',
  1169. '_flushBuffer'
  1170. ]);
  1171. $response->expects($this->exactly(1))
  1172. ->method('type')
  1173. ->with('ini')
  1174. ->will($this->returnValue(false));
  1175. $response->expects($this->once())
  1176. ->method('download')
  1177. ->with('no_section.ini');
  1178. $response->expects($this->at(2))
  1179. ->method('header')
  1180. ->with('Content-Transfer-Encoding', 'binary');
  1181. $response->expects($this->at(3))
  1182. ->method('header')
  1183. ->with('Accept-Ranges', 'bytes');
  1184. $response->expects($this->once())->method('_clearBuffer');
  1185. $response->expects($this->once())->method('_flushBuffer');
  1186. $response->expects($this->exactly(1))
  1187. ->method('_isActive')
  1188. ->will($this->returnValue(true));
  1189. $response->file(CONFIG . 'no_section.ini');
  1190. ob_start();
  1191. $result = $response->send();
  1192. $output = ob_get_clean();
  1193. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1194. $this->assertNotSame(false, $result);
  1195. if ($currentUserAgent !== null) {
  1196. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1197. }
  1198. }
  1199. /**
  1200. * testFileWithUnknownFileTypeOpera method
  1201. *
  1202. * @return void
  1203. */
  1204. public function testFileWithUnknownFileTypeOpera()
  1205. {
  1206. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1207. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1208. $response = $this->getMock('Cake\Network\Response', [
  1209. 'header',
  1210. 'type',
  1211. 'download',
  1212. '_sendHeader',
  1213. '_setContentType',
  1214. '_isActive',
  1215. '_clearBuffer',
  1216. '_flushBuffer'
  1217. ]);
  1218. $response->expects($this->at(0))
  1219. ->method('type')
  1220. ->with('ini')
  1221. ->will($this->returnValue(false));
  1222. $response->expects($this->at(1))
  1223. ->method('type')
  1224. ->with('application/octet-stream')
  1225. ->will($this->returnValue(false));
  1226. $response->expects($this->once())
  1227. ->method('download')
  1228. ->with('no_section.ini');
  1229. $response->expects($this->at(3))
  1230. ->method('header')
  1231. ->with('Content-Transfer-Encoding', 'binary');
  1232. $response->expects($this->at(4))
  1233. ->method('header')
  1234. ->with('Accept-Ranges', 'bytes');
  1235. $response->expects($this->once())->method('_clearBuffer');
  1236. $response->expects($this->once())->method('_flushBuffer');
  1237. $response->expects($this->exactly(1))
  1238. ->method('_isActive')
  1239. ->will($this->returnValue(true));
  1240. $response->file(CONFIG . 'no_section.ini');
  1241. ob_start();
  1242. $result = $response->send();
  1243. $output = ob_get_clean();
  1244. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1245. $this->assertNotSame(false, $result);
  1246. if ($currentUserAgent !== null) {
  1247. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1248. }
  1249. }
  1250. /**
  1251. * testFileWithUnknownFileTypeIE method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testFileWithUnknownFileTypeIE()
  1256. {
  1257. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1258. $_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)';
  1259. $response = $this->getMock('Cake\Network\Response', [
  1260. 'header',
  1261. 'type',
  1262. 'download',
  1263. '_sendHeader',
  1264. '_setContentType',
  1265. '_isActive',
  1266. '_clearBuffer',
  1267. '_flushBuffer'
  1268. ]);
  1269. $response->expects($this->at(0))
  1270. ->method('type')
  1271. ->with('ini')
  1272. ->will($this->returnValue(false));
  1273. $response->expects($this->at(1))
  1274. ->method('type')
  1275. ->with('application/force-download')
  1276. ->will($this->returnValue(false));
  1277. $response->expects($this->once())
  1278. ->method('download')
  1279. ->with('config.ini');
  1280. $response->expects($this->at(3))
  1281. ->method('header')
  1282. ->with('Content-Transfer-Encoding', 'binary');
  1283. $response->expects($this->at(4))
  1284. ->method('header')
  1285. ->with('Accept-Ranges', 'bytes');
  1286. $response->expects($this->once())->method('_clearBuffer');
  1287. $response->expects($this->once())->method('_flushBuffer');
  1288. $response->expects($this->exactly(1))
  1289. ->method('_isActive')
  1290. ->will($this->returnValue(true));
  1291. $response->file(CONFIG . 'no_section.ini', [
  1292. 'name' => 'config.ini'
  1293. ]);
  1294. ob_start();
  1295. $result = $response->send();
  1296. $output = ob_get_clean();
  1297. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1298. $this->assertNotSame(false, $result);
  1299. if ($currentUserAgent !== null) {
  1300. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1301. }
  1302. }
  1303. /**
  1304. * testFileWithUnknownFileNoDownload method
  1305. *
  1306. * @return void
  1307. */
  1308. public function testFileWithUnknownFileNoDownload()
  1309. {
  1310. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1311. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1312. $response = $this->getMock('Cake\Network\Response', [
  1313. 'header',
  1314. 'type',
  1315. 'download',
  1316. '_sendHeader',
  1317. '_setContentType',
  1318. '_isActive',
  1319. '_clearBuffer',
  1320. '_flushBuffer'
  1321. ]);
  1322. $response->expects($this->exactly(1))
  1323. ->method('type')
  1324. ->with('ini')
  1325. ->will($this->returnValue(false));
  1326. $response->expects($this->at(1))
  1327. ->method('header')
  1328. ->with('Accept-Ranges', 'bytes');
  1329. $response->expects($this->never())
  1330. ->method('download');
  1331. $response->file(CONFIG . 'no_section.ini', [
  1332. 'download' => false
  1333. ]);
  1334. if ($currentUserAgent !== null) {
  1335. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1336. }
  1337. }
  1338. /**
  1339. * testConnectionAbortedOnBuffering method
  1340. *
  1341. * @return void
  1342. */
  1343. public function testConnectionAbortedOnBuffering()
  1344. {
  1345. $response = $this->getMock('Cake\Network\Response', [
  1346. 'header',
  1347. 'type',
  1348. 'download',
  1349. '_sendHeader',
  1350. '_setContentType',
  1351. '_isActive',
  1352. '_clearBuffer',
  1353. '_flushBuffer'
  1354. ]);
  1355. $response->expects($this->any())
  1356. ->method('type')
  1357. ->with('css')
  1358. ->will($this->returnArgument(0));
  1359. $response->expects($this->at(0))
  1360. ->method('_isActive')
  1361. ->will($this->returnValue(false));
  1362. $response->expects($this->once())->method('_clearBuffer');
  1363. $response->expects($this->never())->method('_flushBuffer');
  1364. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1365. $result = $response->send();
  1366. $this->assertNull($result);
  1367. }
  1368. /**
  1369. * Test downloading files with UPPERCASE extensions.
  1370. *
  1371. * @return void
  1372. */
  1373. public function testFileUpperExtension()
  1374. {
  1375. $response = $this->getMock('Cake\Network\Response', [
  1376. 'header',
  1377. 'type',
  1378. 'download',
  1379. '_sendHeader',
  1380. '_setContentType',
  1381. '_isActive',
  1382. '_clearBuffer',
  1383. '_flushBuffer'
  1384. ]);
  1385. $response->expects($this->any())
  1386. ->method('type')
  1387. ->with('jpg')
  1388. ->will($this->returnArgument(0));
  1389. $response->expects($this->at(0))
  1390. ->method('_isActive')
  1391. ->will($this->returnValue(true));
  1392. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1393. }
  1394. /**
  1395. * Test downloading files with extension not explicitly set.
  1396. *
  1397. * @return void
  1398. */
  1399. public function testFileExtensionNotSet()
  1400. {
  1401. $response = $this->getMock('Cake\Network\Response', [
  1402. 'header',
  1403. 'type',
  1404. 'download',
  1405. '_sendHeader',
  1406. '_setContentType',
  1407. '_isActive',
  1408. '_clearBuffer',
  1409. '_flushBuffer'
  1410. ]);
  1411. $response->expects($this->any())
  1412. ->method('type')
  1413. ->with('jpg')
  1414. ->will($this->returnArgument(0));
  1415. $response->expects($this->at(0))
  1416. ->method('_isActive')
  1417. ->will($this->returnValue(true));
  1418. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1419. }
  1420. /**
  1421. * A data provider for testing various ranges
  1422. *
  1423. * @return array
  1424. */
  1425. public static function rangeProvider()
  1426. {
  1427. return [
  1428. // suffix-byte-range
  1429. [
  1430. 'bytes=-25', 25, 'bytes 13-37/38'
  1431. ],
  1432. [
  1433. 'bytes=0-', 38, 'bytes 0-37/38'
  1434. ],
  1435. [
  1436. 'bytes=10-', 28, 'bytes 10-37/38'
  1437. ],
  1438. [
  1439. 'bytes=10-20', 11, 'bytes 10-20/38'
  1440. ],
  1441. ];
  1442. }
  1443. /**
  1444. * Test the various range offset types.
  1445. *
  1446. * @dataProvider rangeProvider
  1447. * @return void
  1448. */
  1449. public function testFileRangeOffsets($range, $length, $offsetResponse)
  1450. {
  1451. $_SERVER['HTTP_RANGE'] = $range;
  1452. $response = $this->getMock('Cake\Network\Response', [
  1453. 'header',
  1454. 'type',
  1455. '_sendHeader',
  1456. '_isActive',
  1457. '_clearBuffer',
  1458. '_flushBuffer'
  1459. ]);
  1460. $response->expects($this->at(1))
  1461. ->method('header')
  1462. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1463. $response->expects($this->at(2))
  1464. ->method('header')
  1465. ->with('Content-Transfer-Encoding', 'binary');
  1466. $response->expects($this->at(3))
  1467. ->method('header')
  1468. ->with('Accept-Ranges', 'bytes');
  1469. $response->expects($this->at(4))
  1470. ->method('header')
  1471. ->with([
  1472. 'Content-Length' => $length,
  1473. 'Content-Range' => $offsetResponse,
  1474. ]);
  1475. $response->expects($this->any())
  1476. ->method('_isActive')
  1477. ->will($this->returnValue(true));
  1478. $response->file(
  1479. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1480. ['download' => true]
  1481. );
  1482. ob_start();
  1483. $result = $response->send();
  1484. ob_get_clean();
  1485. }
  1486. /**
  1487. * Test fetching ranges from a file.
  1488. *
  1489. * @return void
  1490. */
  1491. public function testFileRange()
  1492. {
  1493. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1494. $response = $this->getMock('Cake\Network\Response', [
  1495. 'header',
  1496. 'type',
  1497. '_sendHeader',
  1498. '_setContentType',
  1499. '_isActive',
  1500. '_clearBuffer',
  1501. '_flushBuffer'
  1502. ]);
  1503. $response->expects($this->exactly(1))
  1504. ->method('type')
  1505. ->with('css')
  1506. ->will($this->returnArgument(0));
  1507. $response->expects($this->at(1))
  1508. ->method('header')
  1509. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1510. $response->expects($this->at(2))
  1511. ->method('header')
  1512. ->with('Content-Transfer-Encoding', 'binary');
  1513. $response->expects($this->at(3))
  1514. ->method('header')
  1515. ->with('Accept-Ranges', 'bytes');
  1516. $response->expects($this->at(4))
  1517. ->method('header')
  1518. ->with([
  1519. 'Content-Length' => 18,
  1520. 'Content-Range' => 'bytes 8-25/38',
  1521. ]);
  1522. $response->expects($this->once())->method('_clearBuffer');
  1523. $response->expects($this->any())
  1524. ->method('_isActive')
  1525. ->will($this->returnValue(true));
  1526. $response->file(
  1527. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1528. ['download' => true]
  1529. );
  1530. ob_start();
  1531. $result = $response->send();
  1532. $output = ob_get_clean();
  1533. $this->assertEquals(206, $response->statusCode());
  1534. $this->assertEquals("is the test asset ", $output);
  1535. $this->assertNotSame(false, $result);
  1536. }
  1537. /**
  1538. * Test invalid file ranges.
  1539. *
  1540. * @return void
  1541. */
  1542. public function testFileRangeInvalid()
  1543. {
  1544. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1545. $response = $this->getMock('Cake\Network\Response', [
  1546. 'header',
  1547. 'type',
  1548. '_sendHeader',
  1549. '_setContentType',
  1550. '_isActive',
  1551. '_clearBuffer',
  1552. '_flushBuffer'
  1553. ]);
  1554. $response->expects($this->at(1))
  1555. ->method('header')
  1556. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  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->at(4))
  1564. ->method('header')
  1565. ->with([
  1566. 'Content-Range' => 'bytes 0-37/38',
  1567. ]);
  1568. $response->file(
  1569. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1570. ['download' => true]
  1571. );
  1572. $this->assertEquals(416, $response->statusCode());
  1573. $result = $response->send();
  1574. }
  1575. /**
  1576. * testFileRangeOffsetsNoDownload method
  1577. *
  1578. * @dataProvider rangeProvider
  1579. * @return void
  1580. */
  1581. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  1582. {
  1583. $_SERVER['HTTP_RANGE'] = $range;
  1584. $response = $this->getMock('Cake\Network\Response', [
  1585. 'header',
  1586. 'type',
  1587. '_sendHeader',
  1588. '_isActive',
  1589. '_clearBuffer',
  1590. '_flushBuffer'
  1591. ]);
  1592. $response->expects($this->at(1))
  1593. ->method('header')
  1594. ->with('Accept-Ranges', 'bytes');
  1595. $response->expects($this->at(2))
  1596. ->method('header')
  1597. ->with([
  1598. 'Content-Length' => $length,
  1599. 'Content-Range' => $offsetResponse,
  1600. ]);
  1601. $response->expects($this->any())
  1602. ->method('_isActive')
  1603. ->will($this->returnValue(true));
  1604. $response->file(
  1605. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1606. ['download' => false]
  1607. );
  1608. ob_start();
  1609. $result = $response->send();
  1610. ob_get_clean();
  1611. }
  1612. /**
  1613. * testFileRangeNoDownload method
  1614. *
  1615. * @return void
  1616. */
  1617. public function testFileRangeNoDownload()
  1618. {
  1619. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1620. $response = $this->getMock('Cake\Network\Response', [
  1621. 'header',
  1622. 'type',
  1623. '_sendHeader',
  1624. '_setContentType',
  1625. '_isActive',
  1626. '_clearBuffer',
  1627. '_flushBuffer'
  1628. ]);
  1629. $response->expects($this->exactly(1))
  1630. ->method('type')
  1631. ->with('css')
  1632. ->will($this->returnArgument(0));
  1633. $response->expects($this->at(1))
  1634. ->method('header')
  1635. ->with('Accept-Ranges', 'bytes');
  1636. $response->expects($this->at(2))
  1637. ->method('header')
  1638. ->with([
  1639. 'Content-Length' => 18,
  1640. 'Content-Range' => 'bytes 8-25/38',
  1641. ]);
  1642. $response->expects($this->once())->method('_clearBuffer');
  1643. $response->expects($this->any())
  1644. ->method('_isActive')
  1645. ->will($this->returnValue(true));
  1646. $response->file(
  1647. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1648. ['download' => false]
  1649. );
  1650. ob_start();
  1651. $result = $response->send();
  1652. $output = ob_get_clean();
  1653. $this->assertEquals(206, $response->statusCode());
  1654. $this->assertEquals("is the test asset ", $output);
  1655. $this->assertNotSame(false, $result);
  1656. }
  1657. /**
  1658. * testFileRangeInvalidNoDownload method
  1659. *
  1660. * @return void
  1661. */
  1662. public function testFileRangeInvalidNoDownload()
  1663. {
  1664. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1665. $response = $this->getMock('Cake\Network\Response', [
  1666. 'header',
  1667. 'type',
  1668. '_sendHeader',
  1669. '_setContentType',
  1670. '_isActive',
  1671. '_clearBuffer',
  1672. '_flushBuffer'
  1673. ]);
  1674. $response->expects($this->at(1))
  1675. ->method('header')
  1676. ->with('Accept-Ranges', 'bytes');
  1677. $response->expects($this->at(2))
  1678. ->method('header')
  1679. ->with([
  1680. 'Content-Range' => 'bytes 0-37/38',
  1681. ]);
  1682. $response->file(
  1683. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1684. ['download' => false]
  1685. );
  1686. $this->assertEquals(416, $response->statusCode());
  1687. $result = $response->send();
  1688. }
  1689. /**
  1690. * Test the location method.
  1691. *
  1692. * @return void
  1693. */
  1694. public function testLocation()
  1695. {
  1696. $response = new Response();
  1697. $this->assertNull($response->location(), 'No header should be set.');
  1698. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  1699. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  1700. }
  1701. }