ResponseTest.php 61 KB

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