ResponseTest.php 53 KB

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