CakeResponseTest.php 52 KB

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