ResponseTest.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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 2.0.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. * @return void
  46. */
  47. public function testConstruct() {
  48. $response = new Response();
  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 Response($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 Response($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 Response();
  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 Response();
  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 InvalidArgumentException
  107. * @return void
  108. */
  109. public function testStatusCode() {
  110. $response = new Response();
  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 Response();
  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 Response();
  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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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 Response();
  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 Response();
  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 Response();
  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 Response();
  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 Response();
  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 InvalidArgumentException
  362. * @return void
  363. */
  364. public function testHttpCodes() {
  365. $response = new Response();
  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 Response();
  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 Response();
  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 Response();
  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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', array('_sendHeader', '_sendContent'));
  509. $goofyOutput = 'I am goofily sending output in the controller';
  510. echo $goofyOutput;
  511. $response = $this->getMock('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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 = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  675. $response->sharable(true, 3600);
  676. $headers = $response->header();
  677. $this->assertEquals('public, max-age=3600', $headers['Cache-Control']);
  678. $response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', 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('Cake\Network\Response', array('notModified'));
  820. $response->etag('something');
  821. $response->expects($this->once())->method('notModified');
  822. $response->checkNotModified(new Request);
  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('Cake\Network\Response', array('notModified'));
  832. $response->etag('something', true);
  833. $response->expects($this->once())->method('notModified');
  834. $this->assertTrue($response->checkNotModified(new Request));
  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('Cake\Network\Response', 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 Request));
  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('Cake\Network\Response', 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 Request));
  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('Cake\Network\Response', 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 Request));
  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('Cake\Network\Response', array('notModified'));
  886. $response->modified('2012-01-01 00:00:00');
  887. $response->expects($this->once())->method('notModified');
  888. $this->assertTrue($response->checkNotModified(new Request));
  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('Cake\Network\Response', array('notModified'));
  899. $response->expects($this->never())->method('notModified');
  900. $this->assertFalse($response->checkNotModified(new Request));
  901. }
  902. /**
  903. * Test cookie setting
  904. *
  905. * @return void
  906. */
  907. public function testCookieSettings() {
  908. $response = new Response();
  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 Request $request
  984. * @param string $origin
  985. * @param string|array $domains
  986. * @param string|array $methods
  987. * @param string|array $headers
  988. * @param string|bool $expectedOrigin
  989. * @param string|bool $expectedMethods
  990. * @param string|bool $expectedHeaders
  991. * @return void
  992. */
  993. public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false) {
  994. $request->env('HTTP_ORIGIN', $origin);
  995. $response = $this->getMock('Cake\Network\Response', 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 Request();
  1019. $secureRequest = $this->getMock('Cake\Network\Request', 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 Cake\Network\Exception\NotFoundException
  1048. * @return void
  1049. */
  1050. public function testFileNotFound() {
  1051. $response = new Response();
  1052. $response->file('/some/missing/folder/file.jpg');
  1053. }
  1054. /**
  1055. * test file with ..
  1056. *
  1057. * @expectedException Cake\Network\Exception\NotFoundException
  1058. * @return void
  1059. */
  1060. public function testFileWithPathTraversal() {
  1061. $response = new Response();
  1062. $response->file('my/../cat.gif');
  1063. }
  1064. /**
  1065. * testFile method
  1066. *
  1067. * @return void
  1068. */
  1069. public function testFile() {
  1070. $response = $this->getMock('Cake\Network\Response', 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(TEST_APP . 'vendor/css/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 */", trim($output));
  1096. $this->assertTrue($result !== false);
  1097. }
  1098. /**
  1099. * testFileWithDownloadAndName
  1100. *
  1101. * @return void
  1102. */
  1103. public function testFileWithDownloadAndName() {
  1104. $response = $this->getMock('Cake\Network\Response', array(
  1105. 'header',
  1106. 'type',
  1107. 'download',
  1108. '_sendHeader',
  1109. '_setContentType',
  1110. '_isActive',
  1111. '_clearBuffer',
  1112. '_flushBuffer'
  1113. ));
  1114. $response->expects($this->exactly(1))
  1115. ->method('type')
  1116. ->with('css')
  1117. ->will($this->returnArgument(0));
  1118. $response->expects($this->once())
  1119. ->method('download')
  1120. ->with('something_special.css');
  1121. $response->expects($this->at(2))
  1122. ->method('header')
  1123. ->with('Accept-Ranges', 'bytes');
  1124. $response->expects($this->at(3))
  1125. ->method('header')
  1126. ->with('Content-Transfer-Encoding', 'binary');
  1127. $response->expects($this->at(4))
  1128. ->method('header')
  1129. ->with('Content-Length', 38);
  1130. $response->expects($this->once())->method('_clearBuffer');
  1131. $response->expects($this->once())->method('_flushBuffer');
  1132. $response->expects($this->exactly(1))
  1133. ->method('_isActive')
  1134. ->will($this->returnValue(true));
  1135. $response->file(
  1136. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1137. array(
  1138. 'name' => 'something_special.css',
  1139. 'download' => true,
  1140. )
  1141. );
  1142. ob_start();
  1143. $result = $response->send();
  1144. $output = ob_get_clean();
  1145. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1146. $this->assertTrue($result !== false);
  1147. }
  1148. /**
  1149. * testFileWithUnknownFileTypeGeneric method
  1150. *
  1151. * @return void
  1152. */
  1153. public function testFileWithUnknownFileTypeGeneric() {
  1154. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1155. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1156. $response = $this->getMock('Cake\Network\Response', array(
  1157. 'header',
  1158. 'type',
  1159. 'download',
  1160. '_sendHeader',
  1161. '_setContentType',
  1162. '_isActive',
  1163. '_clearBuffer',
  1164. '_flushBuffer'
  1165. ));
  1166. $response->expects($this->exactly(1))
  1167. ->method('type')
  1168. ->with('ini')
  1169. ->will($this->returnValue(false));
  1170. $response->expects($this->once())
  1171. ->method('download')
  1172. ->with('no_section.ini');
  1173. $response->expects($this->at(2))
  1174. ->method('header')
  1175. ->with('Accept-Ranges', 'bytes');
  1176. $response->expects($this->at(3))
  1177. ->method('header')
  1178. ->with('Content-Transfer-Encoding', 'binary');
  1179. $response->expects($this->at(4))
  1180. ->method('header')
  1181. ->with('Content-Length', 35);
  1182. $response->expects($this->once())->method('_clearBuffer');
  1183. $response->expects($this->once())->method('_flushBuffer');
  1184. $response->expects($this->exactly(1))
  1185. ->method('_isActive')
  1186. ->will($this->returnValue(true));
  1187. $response->file(CONFIG . 'no_section.ini');
  1188. ob_start();
  1189. $result = $response->send();
  1190. $output = ob_get_clean();
  1191. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1192. $this->assertTrue($result !== false);
  1193. if ($currentUserAgent !== null) {
  1194. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1195. }
  1196. }
  1197. /**
  1198. * testFileWithUnknownFileTypeOpera method
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFileWithUnknownFileTypeOpera() {
  1203. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1204. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1205. $response = $this->getMock('Cake\Network\Response', array(
  1206. 'header',
  1207. 'type',
  1208. 'download',
  1209. '_sendHeader',
  1210. '_setContentType',
  1211. '_isActive',
  1212. '_clearBuffer',
  1213. '_flushBuffer'
  1214. ));
  1215. $response->expects($this->at(0))
  1216. ->method('type')
  1217. ->with('ini')
  1218. ->will($this->returnValue(false));
  1219. $response->expects($this->at(1))
  1220. ->method('type')
  1221. ->with('application/octet-stream')
  1222. ->will($this->returnValue(false));
  1223. $response->expects($this->once())
  1224. ->method('download')
  1225. ->with('no_section.ini');
  1226. $response->expects($this->at(3))
  1227. ->method('header')
  1228. ->with('Accept-Ranges', 'bytes');
  1229. $response->expects($this->at(4))
  1230. ->method('header')
  1231. ->with('Content-Transfer-Encoding', 'binary');
  1232. $response->expects($this->at(5))
  1233. ->method('header')
  1234. ->with('Content-Length', 35);
  1235. $response->expects($this->once())->method('_clearBuffer');
  1236. $response->expects($this->once())->method('_flushBuffer');
  1237. $response->expects($this->exactly(1))
  1238. ->method('_isActive')
  1239. ->will($this->returnValue(true));
  1240. $response->file(CONFIG . 'no_section.ini');
  1241. ob_start();
  1242. $result = $response->send();
  1243. $output = ob_get_clean();
  1244. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1245. $this->assertTrue($result !== false);
  1246. if ($currentUserAgent !== null) {
  1247. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1248. }
  1249. }
  1250. /**
  1251. * testFileWithUnknownFileTypeIE method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testFileWithUnknownFileTypeIE() {
  1256. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1257. $_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)';
  1258. $response = $this->getMock('Cake\Network\Response', array(
  1259. 'header',
  1260. 'type',
  1261. 'download',
  1262. '_sendHeader',
  1263. '_setContentType',
  1264. '_isActive',
  1265. '_clearBuffer',
  1266. '_flushBuffer'
  1267. ));
  1268. $response->expects($this->at(0))
  1269. ->method('type')
  1270. ->with('ini')
  1271. ->will($this->returnValue(false));
  1272. $response->expects($this->at(1))
  1273. ->method('type')
  1274. ->with('application/force-download')
  1275. ->will($this->returnValue(false));
  1276. $response->expects($this->once())
  1277. ->method('download')
  1278. ->with('config.ini');
  1279. $response->expects($this->at(3))
  1280. ->method('header')
  1281. ->with('Accept-Ranges', 'bytes');
  1282. $response->expects($this->at(4))
  1283. ->method('header')
  1284. ->with('Content-Transfer-Encoding', 'binary');
  1285. $response->expects($this->at(5))
  1286. ->method('header')
  1287. ->with('Content-Length', 35);
  1288. $response->expects($this->once())->method('_clearBuffer');
  1289. $response->expects($this->once())->method('_flushBuffer');
  1290. $response->expects($this->exactly(1))
  1291. ->method('_isActive')
  1292. ->will($this->returnValue(true));
  1293. $response->file(CONFIG . 'no_section.ini', array(
  1294. 'name' => 'config.ini'
  1295. ));
  1296. ob_start();
  1297. $result = $response->send();
  1298. $output = ob_get_clean();
  1299. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1300. $this->assertTrue($result !== false);
  1301. if ($currentUserAgent !== null) {
  1302. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1303. }
  1304. }
  1305. /**
  1306. * testFileWithUnknownFileNoDownload method
  1307. *
  1308. * @return void
  1309. */
  1310. public function testFileWithUnknownFileNoDownload() {
  1311. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1312. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1313. $response = $this->getMock('Cake\Network\Response', array(
  1314. 'header',
  1315. 'type',
  1316. 'download',
  1317. '_sendHeader',
  1318. '_setContentType',
  1319. '_isActive',
  1320. '_clearBuffer',
  1321. '_flushBuffer'
  1322. ));
  1323. $response->expects($this->exactly(1))
  1324. ->method('type')
  1325. ->with('ini')
  1326. ->will($this->returnValue(false));
  1327. $response->expects($this->never())
  1328. ->method('download');
  1329. $response->file(CONFIG . 'no_section.ini', array(
  1330. 'download' => false
  1331. ));
  1332. if ($currentUserAgent !== null) {
  1333. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1334. }
  1335. }
  1336. /**
  1337. * testConnectionAbortedOnBuffering method
  1338. *
  1339. * @return void
  1340. */
  1341. public function testConnectionAbortedOnBuffering() {
  1342. $response = $this->getMock('Cake\Network\Response', array(
  1343. 'header',
  1344. 'type',
  1345. 'download',
  1346. '_sendHeader',
  1347. '_setContentType',
  1348. '_isActive',
  1349. '_clearBuffer',
  1350. '_flushBuffer'
  1351. ));
  1352. $response->expects($this->any())
  1353. ->method('type')
  1354. ->with('css')
  1355. ->will($this->returnArgument(0));
  1356. $response->expects($this->at(0))
  1357. ->method('_isActive')
  1358. ->will($this->returnValue(false));
  1359. $response->expects($this->once())->method('_clearBuffer');
  1360. $response->expects($this->never())->method('_flushBuffer');
  1361. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1362. $result = $response->send();
  1363. $this->assertNull($result);
  1364. }
  1365. /**
  1366. * Test downloading files with UPPERCASE extensions.
  1367. *
  1368. * @return void
  1369. */
  1370. public function testFileUpperExtension() {
  1371. $response = $this->getMock('Cake\Network\Response', array(
  1372. 'header',
  1373. 'type',
  1374. 'download',
  1375. '_sendHeader',
  1376. '_setContentType',
  1377. '_isActive',
  1378. '_clearBuffer',
  1379. '_flushBuffer'
  1380. ));
  1381. $response->expects($this->any())
  1382. ->method('type')
  1383. ->with('jpg')
  1384. ->will($this->returnArgument(0));
  1385. $response->expects($this->at(0))
  1386. ->method('_isActive')
  1387. ->will($this->returnValue(true));
  1388. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1389. }
  1390. /**
  1391. * Test downloading files with extension not explicitly set.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testFileExtensionNotSet() {
  1396. $response = $this->getMock('Cake\Network\Response', array(
  1397. 'header',
  1398. 'type',
  1399. 'download',
  1400. '_sendHeader',
  1401. '_setContentType',
  1402. '_isActive',
  1403. '_clearBuffer',
  1404. '_flushBuffer'
  1405. ));
  1406. $response->expects($this->any())
  1407. ->method('type')
  1408. ->with('jpg')
  1409. ->will($this->returnArgument(0));
  1410. $response->expects($this->at(0))
  1411. ->method('_isActive')
  1412. ->will($this->returnValue(true));
  1413. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1414. }
  1415. /**
  1416. * A data provider for testing various ranges
  1417. *
  1418. * @return array
  1419. */
  1420. public static function rangeProvider() {
  1421. return array(
  1422. // suffix-byte-range
  1423. array(
  1424. 'bytes=-25', 25, 'bytes 13-37/38'
  1425. ),
  1426. array(
  1427. 'bytes=0-', 38, 'bytes 0-37/38'
  1428. ),
  1429. array(
  1430. 'bytes=10-', 28, 'bytes 10-37/38'
  1431. ),
  1432. array(
  1433. 'bytes=10-20', 11, 'bytes 10-20/38'
  1434. ),
  1435. );
  1436. }
  1437. /**
  1438. * Test the various range offset types.
  1439. *
  1440. * @dataProvider rangeProvider
  1441. * @return void
  1442. */
  1443. public function testFileRangeOffsets($range, $length, $offsetResponse) {
  1444. $_SERVER['HTTP_RANGE'] = $range;
  1445. $response = $this->getMock('Cake\Network\Response', array(
  1446. 'header',
  1447. 'type',
  1448. '_sendHeader',
  1449. '_isActive',
  1450. '_clearBuffer',
  1451. '_flushBuffer'
  1452. ));
  1453. $response->expects($this->at(1))
  1454. ->method('header')
  1455. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1456. $response->expects($this->at(2))
  1457. ->method('header')
  1458. ->with('Accept-Ranges', 'bytes');
  1459. $response->expects($this->at(3))
  1460. ->method('header')
  1461. ->with('Content-Transfer-Encoding', 'binary');
  1462. $response->expects($this->at(4))
  1463. ->method('header')
  1464. ->with(array(
  1465. 'Content-Length' => $length,
  1466. 'Content-Range' => $offsetResponse,
  1467. ));
  1468. $response->expects($this->any())
  1469. ->method('_isActive')
  1470. ->will($this->returnValue(true));
  1471. $response->file(
  1472. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1473. array('download' => true)
  1474. );
  1475. ob_start();
  1476. $result = $response->send();
  1477. ob_get_clean();
  1478. }
  1479. /**
  1480. * Test fetching ranges from a file.
  1481. *
  1482. * @return void
  1483. */
  1484. public function testFileRange() {
  1485. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1486. $response = $this->getMock('Cake\Network\Response', array(
  1487. 'header',
  1488. 'type',
  1489. '_sendHeader',
  1490. '_setContentType',
  1491. '_isActive',
  1492. '_clearBuffer',
  1493. '_flushBuffer'
  1494. ));
  1495. $response->expects($this->exactly(1))
  1496. ->method('type')
  1497. ->with('css')
  1498. ->will($this->returnArgument(0));
  1499. $response->expects($this->at(1))
  1500. ->method('header')
  1501. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1502. $response->expects($this->at(2))
  1503. ->method('header')
  1504. ->with('Accept-Ranges', 'bytes');
  1505. $response->expects($this->at(3))
  1506. ->method('header')
  1507. ->with('Content-Transfer-Encoding', 'binary');
  1508. $response->expects($this->at(4))
  1509. ->method('header')
  1510. ->with(array(
  1511. 'Content-Length' => 18,
  1512. 'Content-Range' => 'bytes 8-25/38',
  1513. ));
  1514. $response->expects($this->once())->method('_clearBuffer');
  1515. $response->expects($this->any())
  1516. ->method('_isActive')
  1517. ->will($this->returnValue(true));
  1518. $response->file(
  1519. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1520. array('download' => true)
  1521. );
  1522. ob_start();
  1523. $result = $response->send();
  1524. $output = ob_get_clean();
  1525. $this->assertEquals(206, $response->statusCode());
  1526. $this->assertEquals("is the test asset ", $output);
  1527. $this->assertTrue($result !== false);
  1528. }
  1529. /**
  1530. * Test invalid file ranges.
  1531. *
  1532. * @return void
  1533. */
  1534. public function testFileRangeInvalid() {
  1535. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1536. $response = $this->getMock('Cake\Network\Response', array(
  1537. 'header',
  1538. 'type',
  1539. '_sendHeader',
  1540. '_setContentType',
  1541. '_isActive',
  1542. '_clearBuffer',
  1543. '_flushBuffer'
  1544. ));
  1545. $response->expects($this->at(1))
  1546. ->method('header')
  1547. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1548. $response->expects($this->at(2))
  1549. ->method('header')
  1550. ->with('Accept-Ranges', 'bytes');
  1551. $response->expects($this->at(3))
  1552. ->method('header')
  1553. ->with('Content-Transfer-Encoding', 'binary');
  1554. $response->expects($this->at(4))
  1555. ->method('header')
  1556. ->with(array(
  1557. 'Content-Range' => 'bytes 0-37/38',
  1558. ));
  1559. $response->file(
  1560. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1561. array('download' => true)
  1562. );
  1563. $this->assertEquals(416, $response->statusCode());
  1564. $result = $response->send();
  1565. }
  1566. /**
  1567. * Test the location method.
  1568. *
  1569. * @return void
  1570. */
  1571. public function testLocation() {
  1572. $response = new Response();
  1573. $this->assertNull($response->location(), 'No header should be set.');
  1574. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  1575. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  1576. }
  1577. }