ResponseTest.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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(TEST_APP . '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-Transfer-Encoding', 'binary');
  1020. $response->expects($this->at(4))
  1021. ->method('header')
  1022. ->with('Content-Length', 38);
  1023. $response->expects($this->once())->method('_clearBuffer');
  1024. $response->expects($this->once())->method('_flushBuffer');
  1025. $response->expects($this->exactly(1))
  1026. ->method('_isActive')
  1027. ->will($this->returnValue(true));
  1028. $response->file(
  1029. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1030. array(
  1031. 'name' => 'something_special.css',
  1032. 'download' => true,
  1033. )
  1034. );
  1035. ob_start();
  1036. $result = $response->send();
  1037. $output = ob_get_clean();
  1038. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1039. $this->assertTrue($result !== false);
  1040. }
  1041. /**
  1042. * testFileWithUnknownFileTypeGeneric method
  1043. *
  1044. * @return void
  1045. */
  1046. public function testFileWithUnknownFileTypeGeneric() {
  1047. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1048. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1049. $response = $this->getMock('Cake\Network\Response', array(
  1050. 'header',
  1051. 'type',
  1052. 'download',
  1053. '_sendHeader',
  1054. '_setContentType',
  1055. '_isActive',
  1056. '_clearBuffer',
  1057. '_flushBuffer'
  1058. ));
  1059. $response->expects($this->exactly(1))
  1060. ->method('type')
  1061. ->with('ini')
  1062. ->will($this->returnValue(false));
  1063. $response->expects($this->once())
  1064. ->method('download')
  1065. ->with('no_section.ini');
  1066. $response->expects($this->at(2))
  1067. ->method('header')
  1068. ->with('Accept-Ranges', 'bytes');
  1069. $response->expects($this->at(3))
  1070. ->method('header')
  1071. ->with('Content-Transfer-Encoding', 'binary');
  1072. $response->expects($this->at(4))
  1073. ->method('header')
  1074. ->with('Content-Length', 35);
  1075. $response->expects($this->once())->method('_clearBuffer');
  1076. $response->expects($this->once())->method('_flushBuffer');
  1077. $response->expects($this->exactly(1))
  1078. ->method('_isActive')
  1079. ->will($this->returnValue(true));
  1080. $response->file(TEST_APP . 'TestApp/Config/no_section.ini');
  1081. ob_start();
  1082. $result = $response->send();
  1083. $output = ob_get_clean();
  1084. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1085. $this->assertTrue($result !== false);
  1086. if ($currentUserAgent !== null) {
  1087. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1088. }
  1089. }
  1090. /**
  1091. * testFileWithUnknownFileTypeOpera method
  1092. *
  1093. * @return void
  1094. */
  1095. public function testFileWithUnknownFileTypeOpera() {
  1096. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1097. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1098. $response = $this->getMock('Cake\Network\Response', array(
  1099. 'header',
  1100. 'type',
  1101. 'download',
  1102. '_sendHeader',
  1103. '_setContentType',
  1104. '_isActive',
  1105. '_clearBuffer',
  1106. '_flushBuffer'
  1107. ));
  1108. $response->expects($this->at(0))
  1109. ->method('type')
  1110. ->with('ini')
  1111. ->will($this->returnValue(false));
  1112. $response->expects($this->at(1))
  1113. ->method('type')
  1114. ->with('application/octet-stream')
  1115. ->will($this->returnValue(false));
  1116. $response->expects($this->once())
  1117. ->method('download')
  1118. ->with('no_section.ini');
  1119. $response->expects($this->at(3))
  1120. ->method('header')
  1121. ->with('Accept-Ranges', 'bytes');
  1122. $response->expects($this->at(4))
  1123. ->method('header')
  1124. ->with('Content-Transfer-Encoding', 'binary');
  1125. $response->expects($this->at(5))
  1126. ->method('header')
  1127. ->with('Content-Length', 35);
  1128. $response->expects($this->once())->method('_clearBuffer');
  1129. $response->expects($this->once())->method('_flushBuffer');
  1130. $response->expects($this->exactly(1))
  1131. ->method('_isActive')
  1132. ->will($this->returnValue(true));
  1133. $response->file(TEST_APP . 'TestApp/Config/no_section.ini');
  1134. ob_start();
  1135. $result = $response->send();
  1136. $output = ob_get_clean();
  1137. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1138. $this->assertTrue($result !== false);
  1139. if ($currentUserAgent !== null) {
  1140. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1141. }
  1142. }
  1143. /**
  1144. * testFileWithUnknownFileTypeIE method
  1145. *
  1146. * @return void
  1147. */
  1148. public function testFileWithUnknownFileTypeIE() {
  1149. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1150. $_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)';
  1151. $response = $this->getMock('Cake\Network\Response', array(
  1152. 'header',
  1153. 'type',
  1154. 'download',
  1155. '_sendHeader',
  1156. '_setContentType',
  1157. '_isActive',
  1158. '_clearBuffer',
  1159. '_flushBuffer'
  1160. ));
  1161. $response->expects($this->at(0))
  1162. ->method('type')
  1163. ->with('ini')
  1164. ->will($this->returnValue(false));
  1165. $response->expects($this->at(1))
  1166. ->method('type')
  1167. ->with('application/force-download')
  1168. ->will($this->returnValue(false));
  1169. $response->expects($this->once())
  1170. ->method('download')
  1171. ->with('config.ini');
  1172. $response->expects($this->at(3))
  1173. ->method('header')
  1174. ->with('Accept-Ranges', 'bytes');
  1175. $response->expects($this->at(4))
  1176. ->method('header')
  1177. ->with('Content-Transfer-Encoding', 'binary');
  1178. $response->expects($this->at(5))
  1179. ->method('header')
  1180. ->with('Content-Length', 35);
  1181. $response->expects($this->once())->method('_clearBuffer');
  1182. $response->expects($this->once())->method('_flushBuffer');
  1183. $response->expects($this->exactly(1))
  1184. ->method('_isActive')
  1185. ->will($this->returnValue(true));
  1186. $response->file(TEST_APP . 'TestApp/Config/no_section.ini', array(
  1187. 'name' => 'config.ini'
  1188. ));
  1189. ob_start();
  1190. $result = $response->send();
  1191. $output = ob_get_clean();
  1192. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1193. $this->assertTrue($result !== false);
  1194. if ($currentUserAgent !== null) {
  1195. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1196. }
  1197. }
  1198. /**
  1199. * testFileWithUnknownFileNoDownload method
  1200. *
  1201. * @return void
  1202. */
  1203. public function testFileWithUnknownFileNoDownload() {
  1204. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1205. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1206. $response = $this->getMock('Cake\Network\Response', array(
  1207. 'header',
  1208. 'type',
  1209. 'download',
  1210. '_sendHeader',
  1211. '_setContentType',
  1212. '_isActive',
  1213. '_clearBuffer',
  1214. '_flushBuffer'
  1215. ));
  1216. $response->expects($this->exactly(1))
  1217. ->method('type')
  1218. ->with('ini')
  1219. ->will($this->returnValue(false));
  1220. $response->expects($this->never())
  1221. ->method('download');
  1222. $response->file(TEST_APP . 'TestApp/Config/no_section.ini', array(
  1223. 'download' => false
  1224. ));
  1225. if ($currentUserAgent !== null) {
  1226. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1227. }
  1228. }
  1229. /**
  1230. * testConnectionAbortedOnBuffering method
  1231. *
  1232. * @return void
  1233. */
  1234. public function testConnectionAbortedOnBuffering() {
  1235. $response = $this->getMock('Cake\Network\Response', array(
  1236. 'header',
  1237. 'type',
  1238. 'download',
  1239. '_sendHeader',
  1240. '_setContentType',
  1241. '_isActive',
  1242. '_clearBuffer',
  1243. '_flushBuffer'
  1244. ));
  1245. $response->expects($this->any())
  1246. ->method('type')
  1247. ->with('css')
  1248. ->will($this->returnArgument(0));
  1249. $response->expects($this->at(0))
  1250. ->method('_isActive')
  1251. ->will($this->returnValue(false));
  1252. $response->expects($this->once())->method('_clearBuffer');
  1253. $response->expects($this->never())->method('_flushBuffer');
  1254. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1255. $result = $response->send();
  1256. $this->assertNull($result);
  1257. }
  1258. /**
  1259. * Test downloading files with UPPERCASE extensions.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testFileUpperExtension() {
  1264. $response = $this->getMock('Cake\Network\Response', array(
  1265. 'header',
  1266. 'type',
  1267. 'download',
  1268. '_sendHeader',
  1269. '_setContentType',
  1270. '_isActive',
  1271. '_clearBuffer',
  1272. '_flushBuffer'
  1273. ));
  1274. $response->expects($this->any())
  1275. ->method('type')
  1276. ->with('jpg')
  1277. ->will($this->returnArgument(0));
  1278. $response->expects($this->at(0))
  1279. ->method('_isActive')
  1280. ->will($this->returnValue(true));
  1281. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1282. }
  1283. /**
  1284. * Test downloading files with extension not explicitly set.
  1285. *
  1286. * @return void
  1287. */
  1288. public function testFileExtensionNotSet() {
  1289. $response = $this->getMock('Cake\Network\Response', array(
  1290. 'header',
  1291. 'type',
  1292. 'download',
  1293. '_sendHeader',
  1294. '_setContentType',
  1295. '_isActive',
  1296. '_clearBuffer',
  1297. '_flushBuffer'
  1298. ));
  1299. $response->expects($this->any())
  1300. ->method('type')
  1301. ->with('jpg')
  1302. ->will($this->returnArgument(0));
  1303. $response->expects($this->at(0))
  1304. ->method('_isActive')
  1305. ->will($this->returnValue(true));
  1306. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1307. }
  1308. /**
  1309. * A data provider for testing various ranges
  1310. *
  1311. * @return array
  1312. */
  1313. public static function rangeProvider() {
  1314. return array(
  1315. // suffix-byte-range
  1316. array(
  1317. 'bytes=-25', 25, 'bytes 13-37/38'
  1318. ),
  1319. array(
  1320. 'bytes=0-', 38, 'bytes 0-37/38'
  1321. ),
  1322. array(
  1323. 'bytes=10-', 28, 'bytes 10-37/38'
  1324. ),
  1325. array(
  1326. 'bytes=10-20', 11, 'bytes 10-20/38'
  1327. ),
  1328. );
  1329. }
  1330. /**
  1331. * Test the various range offset types.
  1332. *
  1333. * @dataProvider rangeProvider
  1334. * @return void
  1335. */
  1336. public function testFileRangeOffsets($range, $length, $offsetResponse) {
  1337. $_SERVER['HTTP_RANGE'] = $range;
  1338. $response = $this->getMock('Cake\Network\Response', array(
  1339. 'header',
  1340. 'type',
  1341. '_sendHeader',
  1342. '_isActive',
  1343. '_clearBuffer',
  1344. '_flushBuffer'
  1345. ));
  1346. $response->expects($this->at(1))
  1347. ->method('header')
  1348. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1349. $response->expects($this->at(2))
  1350. ->method('header')
  1351. ->with('Accept-Ranges', 'bytes');
  1352. $response->expects($this->at(3))
  1353. ->method('header')
  1354. ->with('Content-Transfer-Encoding', 'binary');
  1355. $response->expects($this->at(4))
  1356. ->method('header')
  1357. ->with(array(
  1358. 'Content-Length' => $length,
  1359. 'Content-Range' => $offsetResponse,
  1360. ));
  1361. $response->expects($this->any())
  1362. ->method('_isActive')
  1363. ->will($this->returnValue(true));
  1364. $response->file(
  1365. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1366. array('download' => true)
  1367. );
  1368. ob_start();
  1369. $result = $response->send();
  1370. ob_get_clean();
  1371. }
  1372. /**
  1373. * Test fetching ranges from a file.
  1374. *
  1375. * @return void
  1376. */
  1377. public function testFileRange() {
  1378. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1379. $response = $this->getMock('Cake\Network\Response', array(
  1380. 'header',
  1381. 'type',
  1382. '_sendHeader',
  1383. '_setContentType',
  1384. '_isActive',
  1385. '_clearBuffer',
  1386. '_flushBuffer'
  1387. ));
  1388. $response->expects($this->exactly(1))
  1389. ->method('type')
  1390. ->with('css')
  1391. ->will($this->returnArgument(0));
  1392. $response->expects($this->at(1))
  1393. ->method('header')
  1394. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1395. $response->expects($this->at(2))
  1396. ->method('header')
  1397. ->with('Accept-Ranges', 'bytes');
  1398. $response->expects($this->at(3))
  1399. ->method('header')
  1400. ->with('Content-Transfer-Encoding', 'binary');
  1401. $response->expects($this->at(4))
  1402. ->method('header')
  1403. ->with(array(
  1404. 'Content-Length' => 18,
  1405. 'Content-Range' => 'bytes 8-25/38',
  1406. ));
  1407. $response->expects($this->once())->method('_clearBuffer');
  1408. $response->expects($this->any())
  1409. ->method('_isActive')
  1410. ->will($this->returnValue(true));
  1411. $response->file(
  1412. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1413. array('download' => true)
  1414. );
  1415. ob_start();
  1416. $result = $response->send();
  1417. $output = ob_get_clean();
  1418. $this->assertEquals(206, $response->statusCode());
  1419. $this->assertEquals("is the test asset ", $output);
  1420. $this->assertTrue($result !== false);
  1421. }
  1422. /**
  1423. * Test invalid file ranges.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFileRangeInvalid() {
  1428. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1429. $response = $this->getMock('Cake\Network\Response', array(
  1430. 'header',
  1431. 'type',
  1432. '_sendHeader',
  1433. '_setContentType',
  1434. '_isActive',
  1435. '_clearBuffer',
  1436. '_flushBuffer'
  1437. ));
  1438. $response->expects($this->at(1))
  1439. ->method('header')
  1440. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1441. $response->expects($this->at(2))
  1442. ->method('header')
  1443. ->with('Accept-Ranges', 'bytes');
  1444. $response->expects($this->at(3))
  1445. ->method('header')
  1446. ->with('Content-Transfer-Encoding', 'binary');
  1447. $response->expects($this->at(4))
  1448. ->method('header')
  1449. ->with(array(
  1450. 'Content-Range' => 'bytes 0-37/38',
  1451. ));
  1452. $response->file(
  1453. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1454. array('download' => true)
  1455. );
  1456. $this->assertEquals(416, $response->statusCode());
  1457. $result = $response->send();
  1458. }
  1459. /**
  1460. * Test the location method.
  1461. *
  1462. * @return void
  1463. */
  1464. public function testLocation() {
  1465. $response = new Response();
  1466. $this->assertNull($response->location(), 'No header should be set.');
  1467. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  1468. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  1469. }
  1470. }