ResponseTest.php 48 KB

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