CakeResponseTest.php 42 KB

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