ResponseTest.php 64 KB

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