ResponseTest.php 50 KB

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