ResponseTest.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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. * testConnectionAbortedOnBuffering method
  1373. *
  1374. * @return void
  1375. */
  1376. public function testConnectionAbortedOnBuffering()
  1377. {
  1378. $response = $this->getMock('Cake\Network\Response', [
  1379. 'header',
  1380. 'type',
  1381. 'download',
  1382. '_sendHeader',
  1383. '_setContentType',
  1384. '_isActive',
  1385. '_clearBuffer',
  1386. '_flushBuffer'
  1387. ]);
  1388. $response->expects($this->any())
  1389. ->method('type')
  1390. ->with('css')
  1391. ->will($this->returnArgument(0));
  1392. $response->expects($this->at(0))
  1393. ->method('_isActive')
  1394. ->will($this->returnValue(false));
  1395. $response->expects($this->once())->method('_clearBuffer');
  1396. $response->expects($this->never())->method('_flushBuffer');
  1397. $response->file(TEST_APP . 'vendor/css/test_asset.css');
  1398. $result = $response->send();
  1399. $this->assertNull($result);
  1400. }
  1401. /**
  1402. * Test downloading files with UPPERCASE extensions.
  1403. *
  1404. * @return void
  1405. */
  1406. public function testFileUpperExtension()
  1407. {
  1408. $response = $this->getMock('Cake\Network\Response', [
  1409. 'header',
  1410. 'type',
  1411. 'download',
  1412. '_sendHeader',
  1413. '_setContentType',
  1414. '_isActive',
  1415. '_clearBuffer',
  1416. '_flushBuffer'
  1417. ]);
  1418. $response->expects($this->any())
  1419. ->method('type')
  1420. ->with('jpg')
  1421. ->will($this->returnArgument(0));
  1422. $response->expects($this->at(0))
  1423. ->method('_isActive')
  1424. ->will($this->returnValue(true));
  1425. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1426. }
  1427. /**
  1428. * Test downloading files with extension not explicitly set.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFileExtensionNotSet()
  1433. {
  1434. $response = $this->getMock('Cake\Network\Response', [
  1435. 'header',
  1436. 'type',
  1437. 'download',
  1438. '_sendHeader',
  1439. '_setContentType',
  1440. '_isActive',
  1441. '_clearBuffer',
  1442. '_flushBuffer'
  1443. ]);
  1444. $response->expects($this->any())
  1445. ->method('type')
  1446. ->with('jpg')
  1447. ->will($this->returnArgument(0));
  1448. $response->expects($this->at(0))
  1449. ->method('_isActive')
  1450. ->will($this->returnValue(true));
  1451. $response->file(TEST_APP . 'vendor/img/test_2.JPG');
  1452. }
  1453. /**
  1454. * A data provider for testing various ranges
  1455. *
  1456. * @return array
  1457. */
  1458. public static function rangeProvider()
  1459. {
  1460. return [
  1461. // suffix-byte-range
  1462. [
  1463. 'bytes=-25', 25, 'bytes 13-37/38'
  1464. ],
  1465. [
  1466. 'bytes=0-', 38, 'bytes 0-37/38'
  1467. ],
  1468. [
  1469. 'bytes=10-', 28, 'bytes 10-37/38'
  1470. ],
  1471. [
  1472. 'bytes=10-20', 11, 'bytes 10-20/38'
  1473. ],
  1474. ];
  1475. }
  1476. /**
  1477. * Test the various range offset types.
  1478. *
  1479. * @dataProvider rangeProvider
  1480. * @return void
  1481. */
  1482. public function testFileRangeOffsets($range, $length, $offsetResponse)
  1483. {
  1484. $_SERVER['HTTP_RANGE'] = $range;
  1485. $response = $this->getMock('Cake\Network\Response', [
  1486. 'header',
  1487. 'type',
  1488. '_sendHeader',
  1489. '_isActive',
  1490. '_clearBuffer',
  1491. '_flushBuffer'
  1492. ]);
  1493. $response->expects($this->at(1))
  1494. ->method('header')
  1495. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1496. $response->expects($this->at(2))
  1497. ->method('header')
  1498. ->with('Content-Transfer-Encoding', 'binary');
  1499. $response->expects($this->at(3))
  1500. ->method('header')
  1501. ->with('Accept-Ranges', 'bytes');
  1502. $response->expects($this->at(4))
  1503. ->method('header')
  1504. ->with([
  1505. 'Content-Length' => $length,
  1506. 'Content-Range' => $offsetResponse,
  1507. ]);
  1508. $response->expects($this->any())
  1509. ->method('_isActive')
  1510. ->will($this->returnValue(true));
  1511. $response->file(
  1512. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1513. ['download' => true]
  1514. );
  1515. ob_start();
  1516. $result = $response->send();
  1517. ob_get_clean();
  1518. }
  1519. /**
  1520. * Test fetching ranges from a file.
  1521. *
  1522. * @return void
  1523. */
  1524. public function testFileRange()
  1525. {
  1526. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1527. $response = $this->getMock('Cake\Network\Response', [
  1528. 'header',
  1529. 'type',
  1530. '_sendHeader',
  1531. '_setContentType',
  1532. '_isActive',
  1533. '_clearBuffer',
  1534. '_flushBuffer'
  1535. ]);
  1536. $response->expects($this->exactly(1))
  1537. ->method('type')
  1538. ->with('css')
  1539. ->will($this->returnArgument(0));
  1540. $response->expects($this->at(1))
  1541. ->method('header')
  1542. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1543. $response->expects($this->at(2))
  1544. ->method('header')
  1545. ->with('Content-Transfer-Encoding', 'binary');
  1546. $response->expects($this->at(3))
  1547. ->method('header')
  1548. ->with('Accept-Ranges', 'bytes');
  1549. $response->expects($this->at(4))
  1550. ->method('header')
  1551. ->with([
  1552. 'Content-Length' => 18,
  1553. 'Content-Range' => 'bytes 8-25/38',
  1554. ]);
  1555. $response->expects($this->once())->method('_clearBuffer');
  1556. $response->expects($this->any())
  1557. ->method('_isActive')
  1558. ->will($this->returnValue(true));
  1559. $response->file(
  1560. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1561. ['download' => true]
  1562. );
  1563. ob_start();
  1564. $result = $response->send();
  1565. $output = ob_get_clean();
  1566. $this->assertEquals(206, $response->statusCode());
  1567. $this->assertEquals("is the test asset ", $output);
  1568. $this->assertNotSame(false, $result);
  1569. }
  1570. /**
  1571. * Test invalid file ranges.
  1572. *
  1573. * @return void
  1574. */
  1575. public function testFileRangeInvalid()
  1576. {
  1577. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1578. $response = $this->getMock('Cake\Network\Response', [
  1579. 'header',
  1580. 'type',
  1581. '_sendHeader',
  1582. '_setContentType',
  1583. '_isActive',
  1584. '_clearBuffer',
  1585. '_flushBuffer'
  1586. ]);
  1587. $response->expects($this->at(1))
  1588. ->method('header')
  1589. ->with('Content-Disposition', 'attachment; filename="test_asset.css"');
  1590. $response->expects($this->at(2))
  1591. ->method('header')
  1592. ->with('Content-Transfer-Encoding', 'binary');
  1593. $response->expects($this->at(3))
  1594. ->method('header')
  1595. ->with('Accept-Ranges', 'bytes');
  1596. $response->expects($this->at(4))
  1597. ->method('header')
  1598. ->with([
  1599. 'Content-Range' => 'bytes 0-37/38',
  1600. ]);
  1601. $response->file(
  1602. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1603. ['download' => true]
  1604. );
  1605. $this->assertEquals(416, $response->statusCode());
  1606. $result = $response->send();
  1607. }
  1608. /**
  1609. * testFileRangeOffsetsNoDownload method
  1610. *
  1611. * @dataProvider rangeProvider
  1612. * @return void
  1613. */
  1614. public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
  1615. {
  1616. $_SERVER['HTTP_RANGE'] = $range;
  1617. $response = $this->getMock('Cake\Network\Response', [
  1618. 'header',
  1619. 'type',
  1620. '_sendHeader',
  1621. '_isActive',
  1622. '_clearBuffer',
  1623. '_flushBuffer'
  1624. ]);
  1625. $response->expects($this->at(1))
  1626. ->method('header')
  1627. ->with('Accept-Ranges', 'bytes');
  1628. $response->expects($this->at(2))
  1629. ->method('header')
  1630. ->with([
  1631. 'Content-Length' => $length,
  1632. 'Content-Range' => $offsetResponse,
  1633. ]);
  1634. $response->expects($this->any())
  1635. ->method('_isActive')
  1636. ->will($this->returnValue(true));
  1637. $response->file(
  1638. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1639. ['download' => false]
  1640. );
  1641. ob_start();
  1642. $result = $response->send();
  1643. ob_get_clean();
  1644. }
  1645. /**
  1646. * testFileRangeNoDownload method
  1647. *
  1648. * @return void
  1649. */
  1650. public function testFileRangeNoDownload()
  1651. {
  1652. $_SERVER['HTTP_RANGE'] = 'bytes=8-25';
  1653. $response = $this->getMock('Cake\Network\Response', [
  1654. 'header',
  1655. 'type',
  1656. '_sendHeader',
  1657. '_setContentType',
  1658. '_isActive',
  1659. '_clearBuffer',
  1660. '_flushBuffer'
  1661. ]);
  1662. $response->expects($this->exactly(1))
  1663. ->method('type')
  1664. ->with('css')
  1665. ->will($this->returnArgument(0));
  1666. $response->expects($this->at(1))
  1667. ->method('header')
  1668. ->with('Accept-Ranges', 'bytes');
  1669. $response->expects($this->at(2))
  1670. ->method('header')
  1671. ->with([
  1672. 'Content-Length' => 18,
  1673. 'Content-Range' => 'bytes 8-25/38',
  1674. ]);
  1675. $response->expects($this->once())->method('_clearBuffer');
  1676. $response->expects($this->any())
  1677. ->method('_isActive')
  1678. ->will($this->returnValue(true));
  1679. $response->file(
  1680. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1681. ['download' => false]
  1682. );
  1683. ob_start();
  1684. $result = $response->send();
  1685. $output = ob_get_clean();
  1686. $this->assertEquals(206, $response->statusCode());
  1687. $this->assertEquals("is the test asset ", $output);
  1688. $this->assertNotSame(false, $result);
  1689. }
  1690. /**
  1691. * testFileRangeInvalidNoDownload method
  1692. *
  1693. * @return void
  1694. */
  1695. public function testFileRangeInvalidNoDownload()
  1696. {
  1697. $_SERVER['HTTP_RANGE'] = 'bytes=30-2';
  1698. $response = $this->getMock('Cake\Network\Response', [
  1699. 'header',
  1700. 'type',
  1701. '_sendHeader',
  1702. '_setContentType',
  1703. '_isActive',
  1704. '_clearBuffer',
  1705. '_flushBuffer'
  1706. ]);
  1707. $response->expects($this->at(1))
  1708. ->method('header')
  1709. ->with('Accept-Ranges', 'bytes');
  1710. $response->expects($this->at(2))
  1711. ->method('header')
  1712. ->with([
  1713. 'Content-Range' => 'bytes 0-37/38',
  1714. ]);
  1715. $response->file(
  1716. TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
  1717. ['download' => false]
  1718. );
  1719. $this->assertEquals(416, $response->statusCode());
  1720. $result = $response->send();
  1721. }
  1722. /**
  1723. * Test the location method.
  1724. *
  1725. * @return void
  1726. */
  1727. public function testLocation()
  1728. {
  1729. $response = new Response();
  1730. $this->assertNull($response->location(), 'No header should be set.');
  1731. $this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
  1732. $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
  1733. }
  1734. }