ResponseTest.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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. /**
  26. * Setup for tests
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. include_once __DIR__ . DS . 'mocks.php';
  34. }
  35. /**
  36. * Cleanup after tests
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. }
  44. /**
  45. * Tests the request object constructor
  46. *
  47. * @return void
  48. */
  49. public function testConstruct()
  50. {
  51. $response = new Response();
  52. $this->assertNull($response->body());
  53. $this->assertEquals('UTF-8', $response->charset());
  54. $this->assertEquals('text/html', $response->type());
  55. $this->assertEquals(200, $response->statusCode());
  56. $options = [
  57. 'body' => 'This is the body',
  58. 'charset' => 'my-custom-charset',
  59. 'type' => 'mp3',
  60. 'status' => '203'
  61. ];
  62. $response = new Response($options);
  63. $this->assertEquals('This is the body', $response->body());
  64. $this->assertEquals('my-custom-charset', $response->charset());
  65. $this->assertEquals('audio/mpeg', $response->type());
  66. $this->assertEquals(203, $response->statusCode());
  67. $options = [
  68. 'body' => 'This is the body',
  69. 'charset' => 'my-custom-charset',
  70. 'type' => 'mp3',
  71. 'status' => '422',
  72. 'statusCodes' => [
  73. 422 => 'Unprocessable Entity'
  74. ]
  75. ];
  76. $response = new Response($options);
  77. $this->assertEquals($options['body'], $response->body());
  78. $this->assertEquals($options['charset'], $response->charset());
  79. $this->assertEquals($response->getMimeType($options['type']), $response->type());
  80. $this->assertEquals($options['status'], $response->statusCode());
  81. }
  82. /**
  83. * Tests the body method
  84. *
  85. * @return void
  86. */
  87. public function testBody()
  88. {
  89. $response = new Response();
  90. $this->assertNull($response->body());
  91. $response->body('Response body');
  92. $this->assertEquals('Response body', $response->body());
  93. $this->assertEquals('Changed Body', $response->body('Changed Body'));
  94. }
  95. /**
  96. * Tests the charset method
  97. *
  98. * @return void
  99. */
  100. public function testCharset()
  101. {
  102. $response = new Response();
  103. $this->assertEquals('UTF-8', $response->charset());
  104. $response->charset('iso-8859-1');
  105. $this->assertEquals('iso-8859-1', $response->charset());
  106. $this->assertEquals('UTF-16', $response->charset('UTF-16'));
  107. }
  108. /**
  109. * Tests the statusCode method
  110. *
  111. * @expectedException \InvalidArgumentException
  112. * @return void
  113. */
  114. public function testStatusCode()
  115. {
  116. $response = new Response();
  117. $this->assertEquals(200, $response->statusCode());
  118. $response->statusCode(404);
  119. $this->assertEquals(404, $response->statusCode());
  120. $this->assertEquals(500, $response->statusCode(500));
  121. //Throws exception
  122. $response->statusCode(1001);
  123. }
  124. /**
  125. * Tests the type method
  126. *
  127. * @return void
  128. */
  129. public function testType()
  130. {
  131. $response = new Response();
  132. $this->assertEquals('text/html', $response->type());
  133. $response->type('pdf');
  134. $this->assertEquals('application/pdf', $response->type());
  135. $this->assertEquals('application/crazy-mime', $response->type('application/crazy-mime'));
  136. $this->assertEquals('application/json', $response->type('json'));
  137. $this->assertEquals('text/vnd.wap.wml', $response->type('wap'));
  138. $this->assertEquals('application/vnd.wap.xhtml+xml', $response->type('xhtml-mobile'));
  139. $this->assertEquals('text/csv', $response->type('csv'));
  140. $response->type(['keynote' => 'application/keynote', 'bat' => 'application/bat']);
  141. $this->assertEquals('application/keynote', $response->type('keynote'));
  142. $this->assertEquals('application/bat', $response->type('bat'));
  143. $this->assertFalse($response->type('wackytype'));
  144. }
  145. /**
  146. * Tests the header method
  147. *
  148. * @return void
  149. */
  150. public function testHeader()
  151. {
  152. $response = new Response();
  153. $headers = [];
  154. $this->assertEquals($headers, $response->header());
  155. $response->header('Location', 'http://example.com');
  156. $headers += ['Location' => 'http://example.com'];
  157. $this->assertEquals($headers, $response->header());
  158. //Headers with the same name are overwritten
  159. $response->header('Location', 'http://example2.com');
  160. $headers = ['Location' => 'http://example2.com'];
  161. $this->assertEquals($headers, $response->header());
  162. $response->header(['WWW-Authenticate' => 'Negotiate']);
  163. $headers += ['WWW-Authenticate' => 'Negotiate'];
  164. $this->assertEquals($headers, $response->header());
  165. $response->header(['WWW-Authenticate' => 'Not-Negotiate']);
  166. $headers['WWW-Authenticate'] = 'Not-Negotiate';
  167. $this->assertEquals($headers, $response->header());
  168. $response->header(['Age' => 12, 'Allow' => 'GET, HEAD']);
  169. $headers += ['Age' => 12, 'Allow' => 'GET, HEAD'];
  170. $this->assertEquals($headers, $response->header());
  171. // String headers are allowed
  172. $response->header('Content-Language: da');
  173. $headers += ['Content-Language' => 'da'];
  174. $this->assertEquals($headers, $response->header());
  175. $response->header('Content-Language: da');
  176. $headers += ['Content-Language' => 'da'];
  177. $this->assertEquals($headers, $response->header());
  178. $response->header(['Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache']);
  179. $headers += ['Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'];
  180. $this->assertEquals($headers, $response->header());
  181. $response->header('Access-Control-Allow-Origin', ['domain1', 'domain2']);
  182. $headers += ['Access-Control-Allow-Origin' => ['domain1', 'domain2']];
  183. $this->assertEquals($headers, $response->header());
  184. }
  185. /**
  186. * Tests the send method
  187. *
  188. * @return void
  189. */
  190. public function testSend()
  191. {
  192. $response = $this->getMockBuilder('Cake\Network\Response')
  193. ->setMethods(['_sendHeader', '_sendContent', '_setCookies'])
  194. ->getMock();
  195. $response->header([
  196. 'Content-Language' => 'es',
  197. 'WWW-Authenticate' => 'Negotiate',
  198. 'Access-Control-Allow-Origin' => ['domain1', 'domain2'],
  199. ]);
  200. $response->body('the response body');
  201. $response->expects($this->once())->method('_sendContent')->with('the response body');
  202. $response->expects($this->at(0))->method('_setCookies');
  203. $response->expects($this->at(1))
  204. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  205. $response->expects($this->at(2))
  206. ->method('_sendHeader')->with('Content-Language', 'es');
  207. $response->expects($this->at(3))
  208. ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
  209. $response->expects($this->at(4))
  210. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain1');
  211. $response->expects($this->at(5))
  212. ->method('_sendHeader')->with('Access-Control-Allow-Origin', 'domain2');
  213. $response->expects($this->at(6))
  214. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  215. $response->send();
  216. }
  217. /**
  218. * Data provider for content type tests.
  219. *
  220. * @return array
  221. */
  222. public static function charsetTypeProvider()
  223. {
  224. return [
  225. ['mp3', 'audio/mpeg'],
  226. ['js', 'application/javascript; charset=UTF-8'],
  227. ['json', 'application/json; charset=UTF-8'],
  228. ['xml', 'application/xml; charset=UTF-8'],
  229. ['txt', 'text/plain; charset=UTF-8'],
  230. ];
  231. }
  232. /**
  233. * Tests the send method and changing the content type
  234. *
  235. * @dataProvider charsetTypeProvider
  236. * @return void
  237. */
  238. public function testSendChangingContentType($original, $expected)
  239. {
  240. $response = $this->getMockBuilder('Cake\Network\Response')
  241. ->setMethods(['_sendHeader', '_sendContent', '_setCookies'])
  242. ->getMock();
  243. $response->type($original);
  244. $response->body('the response body');
  245. $response->expects($this->once())->method('_sendContent')->with('the response body');
  246. $response->expects($this->at(0))->method('_setCookies');
  247. $response->expects($this->at(1))
  248. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  249. $response->expects($this->at(2))
  250. ->method('_sendHeader')->with('Content-Type', $expected);
  251. $response->send();
  252. }
  253. /**
  254. * Tests the send method and changing the content type to JS without adding the charset
  255. *
  256. * @return void
  257. */
  258. public function testSendChangingContentTypeWithoutCharset()
  259. {
  260. $response = $this->getMockBuilder('Cake\Network\Response')
  261. ->setMethods(['_sendHeader', '_sendContent', '_setCookies'])
  262. ->getMock();
  263. $response->type('js');
  264. $response->charset('');
  265. $response->body('var $foo = "bar";');
  266. $response->expects($this->once())->method('_sendContent')->with('var $foo = "bar";');
  267. $response->expects($this->at(0))->method('_setCookies');
  268. $response->expects($this->at(1))
  269. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  270. $response->expects($this->at(2))
  271. ->method('_sendHeader')->with('Content-Type', 'application/javascript');
  272. $response->send();
  273. }
  274. /**
  275. * Tests the send method and changing the content type
  276. *
  277. * @return void
  278. */
  279. public function testSendWithLocation()
  280. {
  281. $response = $this->getMockBuilder('Cake\Network\Response')
  282. ->setMethods(['_sendHeader', '_sendContent', '_setCookies'])
  283. ->getMock();
  284. $response->header('Location', 'http://www.example.com');
  285. $response->expects($this->at(0))->method('_setCookies');
  286. $response->expects($this->at(1))
  287. ->method('_sendHeader')->with('HTTP/1.1 302 Found');
  288. $response->expects($this->at(2))
  289. ->method('_sendHeader')->with('Location', 'http://www.example.com');
  290. $response->expects($this->at(3))
  291. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  292. $response->send();
  293. }
  294. /**
  295. * Tests the send method and changing the content type
  296. *
  297. * @return void
  298. */
  299. public function testSendWithCallableBody()
  300. {
  301. $response = $this->getMockBuilder('Cake\Network\Response')
  302. ->setMethods(['_sendHeader'])
  303. ->getMock();
  304. $response->body(function () {
  305. echo 'the response body';
  306. });
  307. ob_start();
  308. $response->send();
  309. $this->assertEquals('the response body', ob_get_clean());
  310. }
  311. /**
  312. * Tests that the returned a string from a body callable is also sent
  313. * as the response body
  314. *
  315. * @return void
  316. */
  317. public function testSendWithCallableBodyWithReturn()
  318. {
  319. $response = $this->getMockBuilder('Cake\Network\Response')
  320. ->setMethods(['_sendHeader'])
  321. ->getMock();
  322. $response->body(function () {
  323. return 'the response body';
  324. });
  325. ob_start();
  326. $response->send();
  327. $this->assertEquals('the response body', ob_get_clean());
  328. }
  329. /**
  330. * Tests the disableCache method
  331. *
  332. * @return void
  333. */
  334. public function testDisableCache()
  335. {
  336. $response = new Response();
  337. $expected = [
  338. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  339. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  340. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  341. ];
  342. $response->disableCache();
  343. $this->assertEquals($expected, $response->header());
  344. }
  345. /**
  346. * Tests the cache method
  347. *
  348. * @return void
  349. */
  350. public function testCache()
  351. {
  352. $response = new Response();
  353. $since = time();
  354. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  355. $response->expires('+1 day');
  356. $expected = [
  357. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  358. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  359. 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
  360. 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time())
  361. ];
  362. $response->cache($since);
  363. $this->assertEquals($expected, $response->header());
  364. $response = new Response();
  365. $since = time();
  366. $time = '+5 day';
  367. $expected = [
  368. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  369. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  370. 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
  371. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time())
  372. ];
  373. $response->cache($since, $time);
  374. $this->assertEquals($expected, $response->header());
  375. $response = new Response();
  376. $since = time();
  377. $time = time();
  378. $expected = [
  379. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  380. 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
  381. 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
  382. 'Cache-Control' => 'public, max-age=0'
  383. ];
  384. $response->cache($since, $time);
  385. $this->assertEquals($expected, $response->header());
  386. }
  387. /**
  388. * Tests the compress method
  389. *
  390. * @return void
  391. */
  392. public function testCompress()
  393. {
  394. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  395. $response = new Response();
  396. if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
  397. $this->assertFalse($response->compress());
  398. $this->markTestSkipped('Is not possible to test output compression');
  399. }
  400. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  401. $result = $response->compress();
  402. $this->assertFalse($result);
  403. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  404. $result = $response->compress();
  405. $this->assertTrue($result);
  406. $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
  407. ob_get_clean();
  408. }
  409. /**
  410. * Tests the httpCodes method
  411. *
  412. * @expectedException \InvalidArgumentException
  413. * @return void
  414. */
  415. public function testHttpCodes()
  416. {
  417. $response = new Response();
  418. $result = $response->httpCodes();
  419. $this->assertEquals(42, count($result));
  420. $result = $response->httpCodes(100);
  421. $expected = [100 => 'Continue'];
  422. $this->assertEquals($expected, $result);
  423. $codes = [
  424. 381 => 'Unicorn Moved',
  425. 555 => 'Unexpected Minotaur'
  426. ];
  427. $result = $response->httpCodes($codes);
  428. $this->assertTrue($result);
  429. $this->assertEquals(44, count($response->httpCodes()));
  430. $result = $response->httpCodes(381);
  431. $expected = [381 => 'Unicorn Moved'];
  432. $this->assertEquals($expected, $result);
  433. $codes = [404 => 'Sorry Bro'];
  434. $result = $response->httpCodes($codes);
  435. $this->assertTrue($result);
  436. $this->assertEquals(44, count($response->httpCodes()));
  437. $result = $response->httpCodes(404);
  438. $expected = [404 => 'Sorry Bro'];
  439. $this->assertEquals($expected, $result);
  440. //Throws exception
  441. $response->httpCodes([
  442. 0 => 'Nothing Here',
  443. -1 => 'Reverse Infinity',
  444. 12345 => 'Universal Password',
  445. 'Hello' => 'World'
  446. ]);
  447. }
  448. /**
  449. * Tests the download method
  450. *
  451. * @return void
  452. */
  453. public function testDownload()
  454. {
  455. $response = new Response();
  456. $expected = [
  457. 'Content-Disposition' => 'attachment; filename="myfile.mp3"'
  458. ];
  459. $response->download('myfile.mp3');
  460. $this->assertEquals($expected, $response->header());
  461. }
  462. /**
  463. * Tests the mapType method
  464. *
  465. * @return void
  466. */
  467. public function testMapType()
  468. {
  469. $response = new Response();
  470. $this->assertEquals('wav', $response->mapType('audio/x-wav'));
  471. $this->assertEquals('pdf', $response->mapType('application/pdf'));
  472. $this->assertEquals('xml', $response->mapType('text/xml'));
  473. $this->assertEquals('html', $response->mapType('*/*'));
  474. $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
  475. $expected = ['json', 'xhtml', 'css'];
  476. $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
  477. $this->assertEquals($expected, $result);
  478. }
  479. /**
  480. * Tests the outputCompressed method
  481. *
  482. * @return void
  483. */
  484. public function testOutputCompressed()
  485. {
  486. $response = new Response();
  487. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  488. $result = $response->outputCompressed();
  489. $this->assertFalse($result);
  490. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  491. $result = $response->outputCompressed();
  492. $this->assertFalse($result);
  493. if (!extension_loaded("zlib")) {
  494. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  495. }
  496. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
  497. if (ini_get("zlib.output_compression") !== '1') {
  498. ob_start('ob_gzhandler');
  499. }
  500. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  501. $result = $response->outputCompressed();
  502. $this->assertTrue($result);
  503. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  504. $result = $response->outputCompressed();
  505. $this->assertFalse($result);
  506. if (ini_get("zlib.output_compression") !== '1') {
  507. ob_get_clean();
  508. }
  509. }
  510. /**
  511. * Tests getting/setting the protocol
  512. *
  513. * @return void
  514. */
  515. public function testProtocol()
  516. {
  517. $response = $this->getMockBuilder('Cake\Network\Response')
  518. ->setMethods(['_sendHeader', '_sendContent'])
  519. ->getMock();
  520. $response->protocol('HTTP/1.0');
  521. $this->assertEquals('HTTP/1.0', $response->protocol());
  522. $response->expects($this->at(0))
  523. ->method('_sendHeader')->with('HTTP/1.0 200 OK');
  524. $response->send();
  525. }
  526. /**
  527. * Tests getting/setting the Content-Length
  528. *
  529. * @return void
  530. */
  531. public function testLength()
  532. {
  533. $response = $this->getMockBuilder('Cake\Network\Response')
  534. ->setMethods(['_sendHeader', '_sendContent'])
  535. ->getMock();
  536. $response->length(100);
  537. $this->assertEquals(100, $response->length());
  538. $response->expects($this->at(1))
  539. ->method('_sendHeader')->with('Content-Length', 100);
  540. $response->send();
  541. }
  542. /**
  543. * Tests that the response body is unset if the status code is 304 or 204
  544. *
  545. * @return void
  546. */
  547. public function testUnmodifiedContent()
  548. {
  549. $response = $this->getMockBuilder('Cake\Network\Response')
  550. ->setMethods(['_sendHeader', '_sendContent'])
  551. ->getMock();
  552. $response->body('This is a body');
  553. $response->statusCode(204);
  554. $response->expects($this->once())
  555. ->method('_sendContent')->with('');
  556. $response->send();
  557. $this->assertFalse(array_key_exists('Content-Type', $response->header()));
  558. $response = $this->getMockBuilder('Cake\Network\Response')
  559. ->setMethods(['_sendHeader', '_sendContent'])
  560. ->getMock();
  561. $response->body('This is a body');
  562. $response->statusCode(304);
  563. $response->expects($this->once())
  564. ->method('_sendContent')->with('');
  565. $response->send();
  566. $response = $this->getMockBuilder('Cake\Network\Response')
  567. ->setMethods(['_sendHeader', '_sendContent'])
  568. ->getMock();
  569. $response->body('This is a body');
  570. $response->statusCode(200);
  571. $response->expects($this->once())
  572. ->method('_sendContent')->with('This is a body');
  573. $response->send();
  574. }
  575. /**
  576. * Tests setting the expiration date
  577. *
  578. * @return void
  579. */
  580. public function testExpires()
  581. {
  582. $response = $this->getMockBuilder('Cake\Network\Response')
  583. ->setMethods(['_sendHeader', '_sendContent'])
  584. ->getMock();
  585. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  586. $response->expires($now);
  587. $now->setTimeZone(new \DateTimeZone('UTC'));
  588. $this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->expires());
  589. $response->expects($this->at(1))
  590. ->method('_sendHeader')->with('Expires', $now->format('D, j M Y H:i:s') . ' GMT');
  591. $response->send();
  592. $response = $this->getMockBuilder('Cake\Network\Response')
  593. ->setMethods(['_sendHeader', '_sendContent'])
  594. ->getMock();
  595. $now = time();
  596. $response->expires($now);
  597. $this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->expires());
  598. $response->expects($this->at(1))
  599. ->method('_sendHeader')->with('Expires', gmdate('D, j M Y H:i:s', $now) . ' GMT');
  600. $response->send();
  601. $response = $this->getMockBuilder('Cake\Network\Response')
  602. ->setMethods(['_sendHeader', '_sendContent'])
  603. ->getMock();
  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. {
  618. $response = $this->getMockBuilder('Cake\Network\Response')
  619. ->setMethods(['_sendHeader', '_sendContent'])
  620. ->getMock();
  621. $now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
  622. $response->modified($now);
  623. $now->setTimeZone(new \DateTimeZone('UTC'));
  624. $this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  625. $response->expects($this->at(1))
  626. ->method('_sendHeader')->with('Last-Modified', $now->format('D, j M Y H:i:s') . ' GMT');
  627. $response->send();
  628. $response = $this->getMockBuilder('Cake\Network\Response')
  629. ->setMethods(['_sendHeader', '_sendContent'])
  630. ->getMock();
  631. $now = time();
  632. $response->modified($now);
  633. $this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->modified());
  634. $response->expects($this->at(1))
  635. ->method('_sendHeader')->with('Last-Modified', gmdate('D, j M Y H:i:s', $now) . ' GMT');
  636. $response->send();
  637. $response = $this->getMockBuilder('Cake\Network\Response')
  638. ->setMethods(['_sendHeader', '_sendContent'])
  639. ->getMock();
  640. $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
  641. $response->modified('+1 day');
  642. $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
  643. $response->expects($this->at(1))
  644. ->method('_sendHeader')->with('Last-Modified', $time->format('D, j M Y H:i:s') . ' GMT');
  645. $response->send();
  646. }
  647. /**
  648. * Tests setting of public/private Cache-Control directives
  649. *
  650. * @return void
  651. */
  652. public function testSharable()
  653. {
  654. $response = $this->getMockBuilder('Cake\Network\Response')
  655. ->setMethods(['_sendHeader', '_sendContent'])
  656. ->getMock();
  657. $this->assertNull($response->sharable());
  658. $response->sharable(true);
  659. $headers = $response->header();
  660. $this->assertEquals('public', $headers['Cache-Control']);
  661. $response->expects($this->at(1))
  662. ->method('_sendHeader')->with('Cache-Control', 'public');
  663. $response->send();
  664. $response = $this->getMockBuilder('Cake\Network\Response')
  665. ->setMethods(['_sendHeader', '_sendContent'])
  666. ->getMock();
  667. $response->sharable(false);
  668. $headers = $response->header();
  669. $this->assertEquals('private', $headers['Cache-Control']);
  670. $response->expects($this->at(1))
  671. ->method('_sendHeader')->with('Cache-Control', 'private');
  672. $response->send();
  673. $response = $this->getMockBuilder('Cake\Network\Response')
  674. ->setMethods(['_sendHeader', '_sendContent'])
  675. ->getMock();
  676. $response->sharable(true);
  677. $headers = $response->header();
  678. $this->assertEquals('public', $headers['Cache-Control']);
  679. $response->sharable(false);
  680. $headers = $response->header();
  681. $this->assertEquals('private', $headers['Cache-Control']);
  682. $response->expects($this->at(1))
  683. ->method('_sendHeader')->with('Cache-Control', 'private');
  684. $response->send();
  685. $this->assertFalse($response->sharable());
  686. $response->sharable(true);
  687. $this->assertTrue($response->sharable());
  688. $response = $this->getMockBuilder('Cake\Network\Response')
  689. ->setMethods(['_sendHeader'])
  690. ->getMock();
  691. $response->sharable(true, 3600);
  692. $headers = $response->header();
  693. $this->assertEquals('public, max-age=3600', $headers['Cache-Control']);
  694. $response = $this->getMockBuilder('Cake\Network\Response')
  695. ->setMethods(['_sendHeader'])
  696. ->getMock();
  697. $response->sharable(false, 3600);
  698. $headers = $response->header();
  699. $this->assertEquals('private, max-age=3600', $headers['Cache-Control']);
  700. $response->send();
  701. }
  702. /**
  703. * Tests setting of max-age Cache-Control directive
  704. *
  705. * @return void
  706. */
  707. public function testMaxAge()
  708. {
  709. $response = $this->getMockBuilder('Cake\Network\Response')
  710. ->setMethods(['_sendHeader', '_sendContent'])
  711. ->getMock();
  712. $this->assertNull($response->maxAge());
  713. $response->maxAge(3600);
  714. $this->assertEquals(3600, $response->maxAge());
  715. $headers = $response->header();
  716. $this->assertEquals('max-age=3600', $headers['Cache-Control']);
  717. $response->expects($this->at(1))
  718. ->method('_sendHeader')->with('Cache-Control', 'max-age=3600');
  719. $response->send();
  720. $response = $this->getMockBuilder('Cake\Network\Response')
  721. ->setMethods(['_sendHeader', '_sendContent'])
  722. ->getMock();
  723. $response->maxAge(3600);
  724. $response->sharable(false);
  725. $headers = $response->header();
  726. $this->assertEquals('max-age=3600, private', $headers['Cache-Control']);
  727. $response->expects($this->at(1))
  728. ->method('_sendHeader')->with('Cache-Control', 'max-age=3600, private');
  729. $response->send();
  730. }
  731. /**
  732. * Tests setting of s-maxage Cache-Control directive
  733. *
  734. * @return void
  735. */
  736. public function testSharedMaxAge()
  737. {
  738. $response = $this->getMockBuilder('Cake\Network\Response')
  739. ->setMethods(['_sendHeader', '_sendContent'])
  740. ->getMock();
  741. $this->assertNull($response->maxAge());
  742. $response->sharedMaxAge(3600);
  743. $this->assertEquals(3600, $response->sharedMaxAge());
  744. $headers = $response->header();
  745. $this->assertEquals('s-maxage=3600', $headers['Cache-Control']);
  746. $response->expects($this->at(1))
  747. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600');
  748. $response->send();
  749. $response = $this->getMockBuilder('Cake\Network\Response')
  750. ->setMethods(['_sendHeader', '_sendContent'])
  751. ->getMock();
  752. $response->sharedMaxAge(3600);
  753. $response->sharable(true);
  754. $headers = $response->header();
  755. $this->assertEquals('s-maxage=3600, public', $headers['Cache-Control']);
  756. $response->expects($this->at(1))
  757. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, public');
  758. $response->send();
  759. }
  760. /**
  761. * Tests setting of must-revalidate Cache-Control directive
  762. *
  763. * @return void
  764. */
  765. public function testMustRevalidate()
  766. {
  767. $response = $this->getMockBuilder('Cake\Network\Response')
  768. ->setMethods(['_sendHeader', '_sendContent'])
  769. ->getMock();
  770. $this->assertFalse($response->mustRevalidate());
  771. $response->mustRevalidate(true);
  772. $this->assertTrue($response->mustRevalidate());
  773. $headers = $response->header();
  774. $this->assertEquals('must-revalidate', $headers['Cache-Control']);
  775. $response->expects($this->at(1))
  776. ->method('_sendHeader')->with('Cache-Control', 'must-revalidate');
  777. $response->send();
  778. $response->mustRevalidate(false);
  779. $this->assertFalse($response->mustRevalidate());
  780. $response = $this->getMockBuilder('Cake\Network\Response')
  781. ->setMethods(['_sendHeader', '_sendContent'])
  782. ->getMock();
  783. $response->sharedMaxAge(3600);
  784. $response->mustRevalidate(true);
  785. $headers = $response->header();
  786. $this->assertEquals('s-maxage=3600, must-revalidate', $headers['Cache-Control']);
  787. $response->expects($this->at(1))
  788. ->method('_sendHeader')->with('Cache-Control', 's-maxage=3600, must-revalidate');
  789. $response->send();
  790. }
  791. /**
  792. * Tests getting/setting the Vary header
  793. *
  794. * @return void
  795. */
  796. public function testVary()
  797. {
  798. $response = $this->getMockBuilder('Cake\Network\Response')
  799. ->setMethods(['_sendHeader', '_sendContent'])
  800. ->getMock();
  801. $response->vary('Accept-encoding');
  802. $this->assertEquals(['Accept-encoding'], $response->vary());
  803. $response->expects($this->at(1))
  804. ->method('_sendHeader')->with('Vary', 'Accept-encoding');
  805. $response->send();
  806. $response = $this->getMockBuilder('Cake\Network\Response')
  807. ->setMethods(['_sendHeader', '_sendContent'])
  808. ->getMock();
  809. $response->vary(['Accept-language', 'Accept-encoding']);
  810. $response->expects($this->at(1))
  811. ->method('_sendHeader')->with('Vary', 'Accept-language, Accept-encoding');
  812. $response->send();
  813. $this->assertEquals(['Accept-language', 'Accept-encoding'], $response->vary());
  814. }
  815. /**
  816. * Tests getting/setting the Etag header
  817. *
  818. * @return void
  819. */
  820. public function testEtag()
  821. {
  822. $response = $this->getMockBuilder('Cake\Network\Response')
  823. ->setMethods(['_sendHeader', '_sendContent'])
  824. ->getMock();
  825. $response->etag('something');
  826. $this->assertEquals('"something"', $response->etag());
  827. $response->expects($this->at(1))
  828. ->method('_sendHeader')->with('Etag', '"something"');
  829. $response->send();
  830. $response = $this->getMockBuilder('Cake\Network\Response')
  831. ->setMethods(['_sendHeader', '_sendContent'])
  832. ->getMock();
  833. $response->etag('something', true);
  834. $this->assertEquals('W/"something"', $response->etag());
  835. $response->expects($this->at(1))
  836. ->method('_sendHeader')->with('Etag', 'W/"something"');
  837. $response->send();
  838. }
  839. /**
  840. * Tests that the response is able to be marked as not modified
  841. *
  842. * @return void
  843. */
  844. public function testNotModified()
  845. {
  846. $response = $this->getMockBuilder('Cake\Network\Response')
  847. ->setMethods(['_sendHeader', '_sendContent'])
  848. ->getMock();
  849. $response->body('something');
  850. $response->statusCode(200);
  851. $response->length(100);
  852. $response->modified('now');
  853. $response->notModified();
  854. $this->assertEmpty($response->header());
  855. $this->assertEmpty($response->body());
  856. $this->assertEquals(304, $response->statusCode());
  857. }
  858. /**
  859. * Test checkNotModified method
  860. *
  861. * @return void
  862. */
  863. public function testCheckNotModifiedByEtagStar()
  864. {
  865. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  866. $response = $this->getMockBuilder('Cake\Network\Response')
  867. ->setMethods(['notModified'])
  868. ->getMock();
  869. $response->etag('something');
  870. $response->expects($this->once())->method('notModified');
  871. $response->checkNotModified(new Request);
  872. }
  873. /**
  874. * Test checkNotModified method
  875. *
  876. * @return void
  877. */
  878. public function testCheckNotModifiedByEtagExact()
  879. {
  880. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  881. $response = $this->getMockBuilder('Cake\Network\Response')
  882. ->setMethods(['notModified'])
  883. ->getMock();
  884. $response->etag('something', true);
  885. $response->expects($this->once())->method('notModified');
  886. $this->assertTrue($response->checkNotModified(new Request));
  887. }
  888. /**
  889. * Test checkNotModified method
  890. *
  891. * @return void
  892. */
  893. public function testCheckNotModifiedByEtagAndTime()
  894. {
  895. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  896. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  897. $response = $this->getMockBuilder('Cake\Network\Response')
  898. ->setMethods(['notModified'])
  899. ->getMock();
  900. $response->etag('something', true);
  901. $response->modified('2012-01-01 00:00:00');
  902. $response->expects($this->once())->method('notModified');
  903. $this->assertTrue($response->checkNotModified(new Request));
  904. }
  905. /**
  906. * Test checkNotModified method
  907. *
  908. * @return void
  909. */
  910. public function testCheckNotModifiedByEtagAndTimeMismatch()
  911. {
  912. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  913. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  914. $response = $this->getMockBuilder('Cake\Network\Response')
  915. ->setMethods(['notModified'])
  916. ->getMock();
  917. $response->etag('something', true);
  918. $response->modified('2012-01-01 00:00:01');
  919. $response->expects($this->never())->method('notModified');
  920. $this->assertFalse($response->checkNotModified(new Request));
  921. }
  922. /**
  923. * Test checkNotModified method
  924. *
  925. * @return void
  926. */
  927. public function testCheckNotModifiedByEtagMismatch()
  928. {
  929. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something-else", "other"';
  930. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  931. $response = $this->getMockBuilder('Cake\Network\Response')
  932. ->setMethods(['notModified'])
  933. ->getMock();
  934. $response->etag('something', true);
  935. $response->modified('2012-01-01 00:00:00');
  936. $response->expects($this->never())->method('notModified');
  937. $this->assertFalse($response->checkNotModified(new Request));
  938. }
  939. /**
  940. * Test checkNotModified method
  941. *
  942. * @return void
  943. */
  944. public function testCheckNotModifiedByTime()
  945. {
  946. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  947. $response = $this->getMockBuilder('Cake\Network\Response')
  948. ->setMethods(['notModified'])
  949. ->getMock();
  950. $response->modified('2012-01-01 00:00:00');
  951. $response->expects($this->once())->method('notModified');
  952. $this->assertTrue($response->checkNotModified(new Request));
  953. }
  954. /**
  955. * Test checkNotModified method
  956. *
  957. * @return void
  958. */
  959. public function testCheckNotModifiedNoHints()
  960. {
  961. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  962. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  963. $response = $this->getMockBuilder('Cake\Network\Response')
  964. ->setMethods(['notModified'])
  965. ->getMock();
  966. $response->expects($this->never())->method('notModified');
  967. $this->assertFalse($response->checkNotModified(new Request));
  968. }
  969. /**
  970. * Test cookie setting
  971. *
  972. * @return void
  973. */
  974. public function testCookieSettings()
  975. {
  976. $response = new Response();
  977. $cookie = [
  978. 'name' => 'CakeTestCookie[Testing]'
  979. ];
  980. $response->cookie($cookie);
  981. $expected = [
  982. 'name' => 'CakeTestCookie[Testing]',
  983. 'value' => '',
  984. 'expire' => 0,
  985. 'path' => '/',
  986. 'domain' => '',
  987. 'secure' => false,
  988. 'httpOnly' => false];
  989. $result = $response->cookie('CakeTestCookie[Testing]');
  990. $this->assertEquals($expected, $result);
  991. $cookie = [
  992. 'name' => 'CakeTestCookie[Testing2]',
  993. 'value' => '[a,b,c]',
  994. 'expire' => 1000,
  995. 'path' => '/test',
  996. 'secure' => true
  997. ];
  998. $response->cookie($cookie);
  999. $expected = [
  1000. 'CakeTestCookie[Testing]' => [
  1001. 'name' => 'CakeTestCookie[Testing]',
  1002. 'value' => '',
  1003. 'expire' => 0,
  1004. 'path' => '/',
  1005. 'domain' => '',
  1006. 'secure' => false,
  1007. 'httpOnly' => false
  1008. ],
  1009. 'CakeTestCookie[Testing2]' => [
  1010. 'name' => 'CakeTestCookie[Testing2]',
  1011. 'value' => '[a,b,c]',
  1012. 'expire' => 1000,
  1013. 'path' => '/test',
  1014. 'domain' => '',
  1015. 'secure' => true,
  1016. 'httpOnly' => false
  1017. ]
  1018. ];
  1019. $result = $response->cookie();
  1020. $this->assertEquals($expected, $result);
  1021. $cookie = $expected['CakeTestCookie[Testing]'];
  1022. $cookie['value'] = 'test';
  1023. $response->cookie($cookie);
  1024. $expected = [
  1025. 'CakeTestCookie[Testing]' => [
  1026. 'name' => 'CakeTestCookie[Testing]',
  1027. 'value' => 'test',
  1028. 'expire' => 0,
  1029. 'path' => '/',
  1030. 'domain' => '',
  1031. 'secure' => false,
  1032. 'httpOnly' => false
  1033. ],
  1034. 'CakeTestCookie[Testing2]' => [
  1035. 'name' => 'CakeTestCookie[Testing2]',
  1036. 'value' => '[a,b,c]',
  1037. 'expire' => 1000,
  1038. 'path' => '/test',
  1039. 'domain' => '',
  1040. 'secure' => true,
  1041. 'httpOnly' => false
  1042. ]
  1043. ];
  1044. $result = $response->cookie();
  1045. $this->assertEquals($expected, $result);
  1046. }
  1047. /**
  1048. * Test CORS
  1049. *
  1050. * @dataProvider corsData
  1051. * @param Request $request
  1052. * @param string $origin
  1053. * @param string|array $domains
  1054. * @param string|array $methods
  1055. * @param string|array $headers
  1056. * @param string|bool $expectedOrigin
  1057. * @param string|bool $expectedMethods
  1058. * @param string|bool $expectedHeaders
  1059. * @return void
  1060. */
  1061. public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false)
  1062. {
  1063. $request->env('HTTP_ORIGIN', $origin);
  1064. $response = new Response();
  1065. $result = $response->cors($request, $domains, $methods, $headers);
  1066. $this->assertInstanceOf('Cake\Network\CorsBuilder', $result);
  1067. $headers = $response->header();
  1068. if ($expectedOrigin) {
  1069. $this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
  1070. $this->assertEquals($expectedOrigin, $headers['Access-Control-Allow-Origin']);
  1071. }
  1072. if ($expectedMethods) {
  1073. $this->assertArrayHasKey('Access-Control-Allow-Methods', $headers);
  1074. $this->assertEquals($expectedMethods, $headers['Access-Control-Allow-Methods']);
  1075. }
  1076. if ($expectedHeaders) {
  1077. $this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
  1078. $this->assertEquals($expectedHeaders, $headers['Access-Control-Allow-Headers']);
  1079. }
  1080. unset($_SERVER['HTTP_ORIGIN']);
  1081. }
  1082. /**
  1083. * Feed for testCors
  1084. *
  1085. * @return array
  1086. */
  1087. public function corsData()
  1088. {
  1089. $fooRequest = new Request();
  1090. $secureRequest = function () {
  1091. $secureRequest = $this->getMockBuilder('Cake\Network\Request')
  1092. ->setMethods(['is'])
  1093. ->getMock();
  1094. $secureRequest->expects($this->any())
  1095. ->method('is')
  1096. ->with('ssl')
  1097. ->will($this->returnValue(true));
  1098. return $secureRequest;
  1099. };
  1100. return [
  1101. // [$fooRequest, null, '*', '', '', false, false],
  1102. // [$fooRequest, 'http://www.foo.com', '*', '', '', '*', false],
  1103. // [$fooRequest, 'http://www.foo.com', 'www.foo.com', '', '', 'http://www.foo.com', false],
  1104. // [$fooRequest, 'http://www.foo.com', '*.foo.com', '', '', 'http://www.foo.com', false],
  1105. // [$fooRequest, 'http://www.foo.com', 'http://*.foo.com', '', '', 'http://www.foo.com', false],
  1106. // [$fooRequest, 'http://www.foo.com', 'https://www.foo.com', '', '', false, false],
  1107. // [$fooRequest, 'http://www.foo.com', 'https://*.foo.com', '', '', false, false],
  1108. // [$fooRequest, 'http://www.foo.com', ['*.bar.com', '*.foo.com'], '', '', 'http://www.foo.com', false],
  1109. // [$fooRequest, 'http://not-foo.com', '*.foo.com', '', '', false, false],
  1110. // [$fooRequest, 'http://bad.academy', '*.acad.my', '', '', false, false],
  1111. // [$fooRequest, 'http://www.foo.com.at.bad.com', '*.foo.com', '', '', false, false],
  1112. // [$fooRequest, 'https://www.foo.com', '*.foo.com', '', '', false, false],
  1113. [$secureRequest(), 'https://www.bar.com', 'www.bar.com', '', '', 'https://www.bar.com', false],
  1114. [$secureRequest(), 'https://www.bar.com', 'http://www.bar.com', '', '', false, false],
  1115. [$secureRequest(), 'https://www.bar.com', '*.bar.com', '', '', 'https://www.bar.com', false],
  1116. [$secureRequest(), 'http://www.bar.com', '*.bar.com', '', '', false, false],
  1117. [$fooRequest, 'http://www.foo.com', '*', 'GET', '', '*', 'GET'],
  1118. [$fooRequest, 'http://www.foo.com', '*.foo.com', 'GET', '', 'http://www.foo.com', 'GET'],
  1119. [$fooRequest, 'http://www.foo.com', '*.foo.com', ['GET', 'POST'], '', 'http://www.foo.com', 'GET, POST'],
  1120. [$fooRequest, 'http://www.foo.com', '*', '', 'X-CakePHP', '*', false, 'X-CakePHP'],
  1121. [$fooRequest, 'http://www.foo.com', '*', '', ['X-CakePHP', 'X-MyApp'], '*', false, 'X-CakePHP, X-MyApp'],
  1122. [$fooRequest, 'http://www.foo.com', '*', ['GET', 'OPTIONS'], ['X-CakePHP', 'X-MyApp'], '*', 'GET, OPTIONS', 'X-CakePHP, X-MyApp'],
  1123. ];
  1124. }
  1125. /**
  1126. * testFileNotFound
  1127. *
  1128. * @expectedException \Cake\Network\Exception\NotFoundException
  1129. * @return void
  1130. */
  1131. public function testFileNotFound()
  1132. {
  1133. $response = new Response();
  1134. $response->file('/some/missing/folder/file.jpg');
  1135. }
  1136. /**
  1137. * test file with ../
  1138. *
  1139. * @expectedException \Cake\Network\Exception\NotFoundException
  1140. * @expectedExceptionMessage The requested file contains `..` and will not be read.
  1141. * @return void
  1142. */
  1143. public function testFileWithForwardSlashPathTraversal()
  1144. {
  1145. $response = new Response();
  1146. $response->file('my/../cat.gif');
  1147. }
  1148. /**
  1149. * test file with ..\
  1150. *
  1151. * @expectedException \Cake\Network\Exception\NotFoundException
  1152. * @expectedExceptionMessage The requested file contains `..` and will not be read.
  1153. * @return void
  1154. */
  1155. public function testFileWithBackwardSlashPathTraversal()
  1156. {
  1157. $response = new Response();
  1158. $response->file('my\..\cat.gif');
  1159. }
  1160. /**
  1161. * test file with ..
  1162. *
  1163. * @expectedException \Cake\Network\Exception\NotFoundException
  1164. * @expectedExceptionMessage my/ca..t.gif was not found or not readable
  1165. * @return void
  1166. */
  1167. public function testFileWithDotsInTheFilename()
  1168. {
  1169. $response = new Response();
  1170. $response->file('my/ca..t.gif');
  1171. }
  1172. /**
  1173. * test file with .. in a path fragment
  1174. *
  1175. * @expectedException \Cake\Network\Exception\NotFoundException
  1176. * @expectedExceptionMessage my/ca..t/image.gif was not found or not readable
  1177. * @return void
  1178. */
  1179. public function testFileWithDotsInAPathFragment()
  1180. {
  1181. $response = new Response();
  1182. $response->file('my/ca..t/image.gif');
  1183. }
  1184. /**
  1185. * testFile method
  1186. *
  1187. * @return void
  1188. */
  1189. public function testFile()
  1190. {
  1191. $response = $this->getMockBuilder('Cake\Network\Response')
  1192. ->setMethods([
  1193. 'header',
  1194. 'type',
  1195. '_sendHeader',
  1196. '_setContentType',
  1197. '_isActive',
  1198. ])
  1199. ->getMock();
  1200. $response->expects($this->exactly(1))
  1201. ->method('type')
  1202. ->with('css')
  1203. ->will($this->returnArgument(0));
  1204. $response->expects($this->at(1))
  1205. ->method('header')
  1206. ->with('Accept-Ranges', 'bytes');
  1207. $response->expects($this->exactly(1))
  1208. ->method('_isActive')
  1209. ->will($this->returnValue(true));
  1210. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1211. ob_start();
  1212. $result = $response->send();
  1213. $output = ob_get_clean();
  1214. $this->assertEquals("/* this is the test asset css file */", trim($output));
  1215. $this->assertTrue($result !== false);
  1216. }
  1217. /**
  1218. * testFileWithDownloadAndName
  1219. *
  1220. * @return void
  1221. */
  1222. public function testFileWithDownloadAndName()
  1223. {
  1224. $response = $this->getMockBuilder('Cake\Network\Response')
  1225. ->setMethods([
  1226. 'header',
  1227. 'type',
  1228. 'download',
  1229. '_sendHeader',
  1230. '_setContentType',
  1231. '_isActive',
  1232. ])
  1233. ->getMock();
  1234. $response->expects($this->exactly(1))
  1235. ->method('type')
  1236. ->with('css')
  1237. ->will($this->returnArgument(0));
  1238. $response->expects($this->once())
  1239. ->method('download')
  1240. ->with('something_special.css');
  1241. $response->expects($this->at(2))
  1242. ->method('header')
  1243. ->with('Content-Transfer-Encoding', 'binary');
  1244. $response->expects($this->at(3))
  1245. ->method('header')
  1246. ->with('Accept-Ranges', 'bytes');
  1247. $response->expects($this->exactly(1))
  1248. ->method('_isActive')
  1249. ->will($this->returnValue(true));
  1250. $response->file(
  1251. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1252. [
  1253. 'name' => 'something_special.css',
  1254. 'download' => true,
  1255. ]
  1256. );
  1257. ob_start();
  1258. $result = $response->send();
  1259. $output = ob_get_clean();
  1260. $this->assertEquals("/* this is the test asset css file */\n", $output);
  1261. $this->assertNotSame(false, $result);
  1262. }
  1263. /**
  1264. * testFileWithUnknownFileTypeGeneric method
  1265. *
  1266. * @return void
  1267. */
  1268. public function testFileWithUnknownFileTypeGeneric()
  1269. {
  1270. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1271. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1272. $response = $this->getMockBuilder('Cake\Network\Response')
  1273. ->setMethods([
  1274. 'header',
  1275. 'type',
  1276. 'download',
  1277. '_sendHeader',
  1278. '_setContentType',
  1279. '_isActive',
  1280. ])
  1281. ->getMock();
  1282. $response->expects($this->exactly(1))
  1283. ->method('type')
  1284. ->with('ini')
  1285. ->will($this->returnValue(false));
  1286. $response->expects($this->once())
  1287. ->method('download')
  1288. ->with('no_section.ini');
  1289. $response->expects($this->at(2))
  1290. ->method('header')
  1291. ->with('Content-Transfer-Encoding', 'binary');
  1292. $response->expects($this->at(3))
  1293. ->method('header')
  1294. ->with('Accept-Ranges', 'bytes');
  1295. $response->expects($this->exactly(1))
  1296. ->method('_isActive')
  1297. ->will($this->returnValue(true));
  1298. $response->file(CONFIG . 'no_section.ini');
  1299. ob_start();
  1300. $result = $response->send();
  1301. $output = ob_get_clean();
  1302. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1303. $this->assertNotSame(false, $result);
  1304. if ($currentUserAgent !== null) {
  1305. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1306. }
  1307. }
  1308. /**
  1309. * testFileWithUnknownFileTypeOpera method
  1310. *
  1311. * @return void
  1312. */
  1313. public function testFileWithUnknownFileTypeOpera()
  1314. {
  1315. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1316. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  1317. $response = $this->getMockBuilder('Cake\Network\Response')
  1318. ->setMethods([
  1319. 'header',
  1320. 'type',
  1321. 'download',
  1322. '_sendHeader',
  1323. '_setContentType',
  1324. '_isActive',
  1325. ])
  1326. ->getMock();
  1327. $response->expects($this->at(0))
  1328. ->method('type')
  1329. ->with('ini')
  1330. ->will($this->returnValue(false));
  1331. $response->expects($this->at(1))
  1332. ->method('type')
  1333. ->with('application/octet-stream')
  1334. ->will($this->returnValue(false));
  1335. $response->expects($this->once())
  1336. ->method('download')
  1337. ->with('no_section.ini');
  1338. $response->expects($this->at(3))
  1339. ->method('header')
  1340. ->with('Content-Transfer-Encoding', 'binary');
  1341. $response->expects($this->at(4))
  1342. ->method('header')
  1343. ->with('Accept-Ranges', 'bytes');
  1344. $response->expects($this->exactly(1))
  1345. ->method('_isActive')
  1346. ->will($this->returnValue(true));
  1347. $response->file(CONFIG . 'no_section.ini');
  1348. ob_start();
  1349. $result = $response->send();
  1350. $output = ob_get_clean();
  1351. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1352. $this->assertNotSame(false, $result);
  1353. if ($currentUserAgent !== null) {
  1354. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1355. }
  1356. }
  1357. /**
  1358. * testFileWithUnknownFileTypeIE method
  1359. *
  1360. * @return void
  1361. */
  1362. public function testFileWithUnknownFileTypeIE()
  1363. {
  1364. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1365. $_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)';
  1366. $response = $this->getMockBuilder('Cake\Network\Response')
  1367. ->setMethods([
  1368. 'header',
  1369. 'type',
  1370. 'download',
  1371. '_sendHeader',
  1372. '_setContentType',
  1373. '_isActive',
  1374. ])
  1375. ->getMock();
  1376. $response->expects($this->at(0))
  1377. ->method('type')
  1378. ->with('ini')
  1379. ->will($this->returnValue(false));
  1380. $response->expects($this->at(1))
  1381. ->method('type')
  1382. ->with('application/force-download')
  1383. ->will($this->returnValue(false));
  1384. $response->expects($this->once())
  1385. ->method('download')
  1386. ->with('config.ini');
  1387. $response->expects($this->at(3))
  1388. ->method('header')
  1389. ->with('Content-Transfer-Encoding', 'binary');
  1390. $response->expects($this->at(4))
  1391. ->method('header')
  1392. ->with('Accept-Ranges', 'bytes');
  1393. $response->expects($this->exactly(1))
  1394. ->method('_isActive')
  1395. ->will($this->returnValue(true));
  1396. $response->file(CONFIG . 'no_section.ini', [
  1397. 'name' => 'config.ini'
  1398. ]);
  1399. ob_start();
  1400. $result = $response->send();
  1401. $output = ob_get_clean();
  1402. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  1403. $this->assertNotSame(false, $result);
  1404. if ($currentUserAgent !== null) {
  1405. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1406. }
  1407. }
  1408. /**
  1409. * testFileWithUnknownFileNoDownload method
  1410. *
  1411. * @return void
  1412. */
  1413. public function testFileWithUnknownFileNoDownload()
  1414. {
  1415. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  1416. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  1417. $response = $this->getMockBuilder('Cake\Network\Response')
  1418. ->setMethods([
  1419. 'header',
  1420. 'type',
  1421. 'download',
  1422. '_sendHeader',
  1423. '_setContentType',
  1424. '_isActive',
  1425. ])
  1426. ->getMock();
  1427. $response->expects($this->exactly(1))
  1428. ->method('type')
  1429. ->with('ini')
  1430. ->will($this->returnValue(false));
  1431. $response->expects($this->at(1))
  1432. ->method('header')
  1433. ->with('Accept-Ranges', 'bytes');
  1434. $response->expects($this->never())
  1435. ->method('download');
  1436. $response->file(CONFIG . 'no_section.ini', [
  1437. 'download' => false
  1438. ]);
  1439. if ($currentUserAgent !== null) {
  1440. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  1441. }
  1442. }
  1443. /**
  1444. * test getFile method
  1445. *
  1446. * @return void
  1447. */
  1448. public function testGetFile()
  1449. {
  1450. $response = new Response();
  1451. $this->assertNull($response->getFile(), 'No file to get');
  1452. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1453. $file = $response->getFile();
  1454. $this->assertInstanceOf('Cake\Filesystem\File', $file, 'Should get a file');
  1455. $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path);
  1456. }
  1457. /**
  1458. * testConnectionAbortedOnBuffering method
  1459. *
  1460. * @return void
  1461. */
  1462. public function testConnectionAbortedOnBuffering()
  1463. {
  1464. $response = $this->getMockBuilder('Cake\Network\Response')
  1465. ->setMethods([
  1466. 'header',
  1467. 'type',
  1468. 'download',
  1469. '_sendHeader',
  1470. '_setContentType',
  1471. '_isActive',
  1472. ])
  1473. ->getMock();
  1474. $response->expects($this->any())
  1475. ->method('type')
  1476. ->with('css')
  1477. ->will($this->returnArgument(0));
  1478. $response->expects($this->at(0))
  1479. ->method('_isActive')
  1480. ->will($this->returnValue(false));
  1481. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1482. $result = $response->send();
  1483. $this->assertNull($result);
  1484. }
  1485. /**
  1486. * Test downloading files with UPPERCASE extensions.
  1487. *
  1488. * @return void
  1489. */
  1490. public function testFileUpperExtension()
  1491. {
  1492. $response = $this->getMockBuilder('Cake\Network\Response')
  1493. ->setMethods([
  1494. 'header',
  1495. 'type',
  1496. 'download',
  1497. '_sendHeader',
  1498. '_setContentType',
  1499. '_isActive',
  1500. ])
  1501. ->getMock();
  1502. $response->expects($this->any())
  1503. ->method('type')
  1504. ->with('jpg')
  1505. ->will($this->returnArgument(0));
  1506. $response->expects($this->at(0))
  1507. ->method('_isActive')
  1508. ->will($this->returnValue(true));
  1509. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1510. }
  1511. /**
  1512. * Test downloading files with extension not explicitly set.
  1513. *
  1514. * @return void
  1515. */
  1516. public function testFileExtensionNotSet()
  1517. {
  1518. $response = $this->getMockBuilder('Cake\Network\Response')
  1519. ->setMethods([
  1520. 'header',
  1521. 'type',
  1522. 'download',
  1523. '_sendHeader',
  1524. '_setContentType',
  1525. '_isActive',
  1526. ])
  1527. ->getMock();
  1528. $response->expects($this->any())
  1529. ->method('type')
  1530. ->with('jpg')
  1531. ->will($this->returnArgument(0));
  1532. $response->expects($this->at(0))
  1533. ->method('_isActive')
  1534. ->will($this->returnValue(true));
  1535. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1536. }
  1537. /**
  1538. * A data provider for testing various ranges
  1539. *
  1540. * @return array
  1541. */
  1542. public static function rangeProvider()
  1543. {
  1544. return [
  1545. // suffix-byte-range
  1546. [
  1547. 'bytes=-25', 25, 'bytes 13-37/38'
  1548. ],
  1549. [
  1550. 'bytes=0-', 38, 'bytes 0-37/38'
  1551. ],
  1552. [
  1553. 'bytes=10-', 28, 'bytes 10-37/38'
  1554. ],
  1555. [
  1556. 'bytes=10-20', 11, 'bytes 10-20/38'
  1557. ],
  1558. // Spaced out
  1559. [
  1560. 'bytes = 10 - 20', 11, 'bytes 10-20/38'
  1561. ],
  1562. ];
  1563. }
  1564. /**
  1565. * Test the various range offset types.
  1566. *
  1567. * @dataProvider rangeProvider
  1568. * @return void
  1569. */
  1570. public function testFileRangeOffsets($range, $length, $offsetResponse)
  1571. {
  1572. $_SERVER['HTTP_RANGE'] = $range;
  1573. $response = $this->getMockBuilder('Cake\Network\Response')
  1574. ->setMethods([
  1575. 'header',
  1576. 'type',
  1577. '_sendHeader',
  1578. '_setContentType',
  1579. '_isActive',
  1580. ])
  1581. ->getMock();
  1582. $response->expects($this->at(1))
  1583. ->method('header')
  1584. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1585. $response->expects($this->at(2))
  1586. ->method('header')
  1587. ->with('Content-Transfer-Encoding', 'binary');
  1588. $response->expects($this->at(3))
  1589. ->method('header')
  1590. ->with('Accept-Ranges', 'bytes');
  1591. $response->expects($this->at(4))
  1592. ->method('header')
  1593. ->with([
  1594. 'Content-Length' => $length,
  1595. 'Content-Range' => $offsetResponse,
  1596. ]);
  1597. $response->expects($this->any())
  1598. ->method('_isActive')
  1599. ->will($this->returnValue(true));
  1600. $response->file(
  1601. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1602. ['download' => true]
  1603. );
  1604. ob_start();
  1605. $result = $response->send();
  1606. ob_get_clean();
  1607. }
  1608. /**
  1609. * Test fetching ranges from a file.
  1610. *
  1611. * @return void
  1612. */
  1613. public function testFileRange()
  1614. {
  1615. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1616. $response = $this->getMockBuilder('Cake\Network\Response')
  1617. ->setMethods([
  1618. 'header',
  1619. 'type',
  1620. '_sendHeader',
  1621. '_setContentType',
  1622. '_isActive',
  1623. ])
  1624. ->getMock();
  1625. $response->expects($this->exactly(1))
  1626. ->method('type')
  1627. ->with('css')
  1628. ->will($this->returnArgument(0));
  1629. $response->expects($this->at(1))
  1630. ->method('header')
  1631. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1632. $response->expects($this->at(2))
  1633. ->method('header')
  1634. ->with('Content-Transfer-Encoding', 'binary');
  1635. $response->expects($this->at(3))
  1636. ->method('header')
  1637. ->with('Accept-Ranges', 'bytes');
  1638. $response->expects($this->at(4))
  1639. ->method('header')
  1640. ->with([
  1641. 'Content-Length' => 18,
  1642. 'Content-Range' => 'bytes 8-25/38',
  1643. ]);
  1644. $response->expects($this->any())
  1645. ->method('_isActive')
  1646. ->will($this->returnValue(true));
  1647. $response->file(
  1648. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1649. ['download' => true]
  1650. );
  1651. ob_start();
  1652. $result = $response->send();
  1653. $output = ob_get_clean();
  1654. $this->assertEquals(206, $response->statusCode());
  1655. $this->assertEquals("is the test asset ", $output);
  1656. $this->assertNotSame(false, $result);
  1657. }
  1658. /**
  1659. * Provider for invalid range header values.
  1660. *
  1661. * @return array
  1662. */
  1663. public function invalidFileRangeProvider()
  1664. {
  1665. return [
  1666. // malformed range
  1667. [
  1668. 'bytes=0,38'
  1669. ],
  1670. // malformed punctuation
  1671. [
  1672. 'bytes: 0 - 38'
  1673. ],
  1674. ];
  1675. }
  1676. /**
  1677. * Test invalid file ranges.
  1678. *
  1679. * @dataProvider invalidFileRangeProvider
  1680. * @return void
  1681. */
  1682. public function testFileRangeInvalid($range)
  1683. {
  1684. $_SERVER['HTTP_RANGE'] = $range;
  1685. $response = $this->getMockBuilder('Cake\Network\Response')
  1686. ->setMethods([
  1687. '_sendHeader',
  1688. '_isActive',
  1689. ])
  1690. ->getMock();
  1691. $response->file(
  1692. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1693. ['download' => true]
  1694. );
  1695. $expected = [
  1696. 'Content-Disposition' => 'attachment; filename="test_asset.css"',
  1697. 'Content-Transfer-Encoding' => 'binary',
  1698. 'Accept-Ranges' => 'bytes',
  1699. 'Content-Range' => 'bytes 0-37/38',
  1700. 'Content-Length' => 38,
  1701. ];
  1702. $this->assertEquals($expected, $response->header());
  1703. }
  1704. /**
  1705. * Test reversed file ranges.
  1706. *
  1707. * @return void
  1708. */
  1709. public function testFileRangeReversed()
  1710. {
  1711. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1712. $response = $this->getMockBuilder('Cake\Network\Response')
  1713. ->setMethods([
  1714. 'header',
  1715. 'type',
  1716. '_sendHeader',
  1717. '_setContentType',
  1718. '_isActive',
  1719. ])
  1720. ->getMock();
  1721. $response->expects($this->at(1))
  1722. ->method('header')
  1723. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1724. $response->expects($this->at(2))
  1725. ->method('header')
  1726. ->with('Content-Transfer-Encoding', 'binary');
  1727. $response->expects($this->at(3))
  1728. ->method('header')
  1729. ->with('Accept-Ranges', 'bytes');
  1730. $response->expects($this->at(4))
  1731. ->method('header')
  1732. ->with([
  1733. 'Content-Range' => 'bytes 0-37/38',
  1734. ]);
  1735. $response->file(
  1736. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1737. ['download' => true]
  1738. );
  1739. $this->assertEquals(416, $response->statusCode());
  1740. $result = $response->send();
  1741. }
  1742. /**
  1743. * testFileRangeOffsetsNoDownload method
  1744. *
  1745. * @dataProvider rangeProvider
  1746. * @return void
  1747. */
  1748. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  1749. {
  1750. $_SERVER['HTTP_RANGE'] = $range;
  1751. $response = $this->getMockBuilder('Cake\Network\Response')
  1752. ->setMethods([
  1753. 'header',
  1754. 'type',
  1755. '_sendHeader',
  1756. '_isActive',
  1757. ])
  1758. ->getMock();
  1759. $response->expects($this->at(1))
  1760. ->method('header')
  1761. ->with('Accept-Ranges', 'bytes');
  1762. $response->expects($this->at(2))
  1763. ->method('header')
  1764. ->with([
  1765. 'Content-Length' => $length,
  1766. 'Content-Range' => $offsetResponse,
  1767. ]);
  1768. $response->expects($this->any())
  1769. ->method('_isActive')
  1770. ->will($this->returnValue(true));
  1771. $response->file(
  1772. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1773. ['download' => false]
  1774. );
  1775. ob_start();
  1776. $result = $response->send();
  1777. ob_get_clean();
  1778. }
  1779. /**
  1780. * testFileRangeNoDownload method
  1781. *
  1782. * @return void
  1783. */
  1784. public function testFileRangeNoDownload()
  1785. {
  1786. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1787. $response = $this->getMockBuilder('Cake\Network\Response')
  1788. ->setMethods([
  1789. 'header',
  1790. 'type',
  1791. '_sendHeader',
  1792. '_setContentType',
  1793. '_isActive',
  1794. ])
  1795. ->getMock();
  1796. $response->expects($this->exactly(1))
  1797. ->method('type')
  1798. ->with('css')
  1799. ->will($this->returnArgument(0));
  1800. $response->expects($this->at(1))
  1801. ->method('header')
  1802. ->with('Accept-Ranges', 'bytes');
  1803. $response->expects($this->at(2))
  1804. ->method('header')
  1805. ->with([
  1806. 'Content-Length' => 18,
  1807. 'Content-Range' => 'bytes 8-25/38',
  1808. ]);
  1809. $response->expects($this->any())
  1810. ->method('_isActive')
  1811. ->will($this->returnValue(true));
  1812. $response->file(
  1813. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1814. ['download' => false]
  1815. );
  1816. ob_start();
  1817. $result = $response->send();
  1818. $output = ob_get_clean();
  1819. $this->assertEquals(206, $response->statusCode());
  1820. $this->assertEquals("is the test asset ", $output);
  1821. $this->assertNotSame(false, $result);
  1822. }
  1823. /**
  1824. * testFileRangeInvalidNoDownload method
  1825. *
  1826. * @return void
  1827. */
  1828. public function testFileRangeInvalidNoDownload()
  1829. {
  1830. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1831. $response = $this->getMockBuilder('Cake\Network\Response')
  1832. ->setMethods([
  1833. 'header',
  1834. 'type',
  1835. '_sendHeader',
  1836. '_setContentType',
  1837. '_isActive',
  1838. ])
  1839. ->getMock();
  1840. $response->expects($this->at(1))
  1841. ->method('header')
  1842. ->with('Accept-Ranges', 'bytes');
  1843. $response->expects($this->at(2))
  1844. ->method('header')
  1845. ->with([
  1846. 'Content-Range' => 'bytes 0-37/38',
  1847. ]);
  1848. $response->file(
  1849. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1850. ['download' => false]
  1851. );
  1852. $this->assertEquals(416, $response->statusCode());
  1853. $result = $response->send();
  1854. }
  1855. /**
  1856. * Test the location method.
  1857. *
  1858. * @return void
  1859. */
  1860. public function testLocation()
  1861. {
  1862. $response = new Response();
  1863. $this->assertNull($response->location(), 'No header should be set.');
  1864. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  1865. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  1866. }
  1867. }