ResponseTest.php 61 KB

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