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