ResponseTest.php 53 KB

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