PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Network/CakeResponseTest.php

http://github.com/cakephp/cakephp
PHP | 503 lines | 344 code | 55 blank | 104 comment | 7 complexity | c6313928484e86ed66278dc2f264dc6c MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * CakeResponse Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Network
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeResponse', 'Network');
  20. class CakeResponseTest extends CakeTestCase {
  21. /**
  22. * Setup for tests
  23. *
  24. * @return void
  25. */
  26. public function setUp() {
  27. ob_start();
  28. }
  29. /**
  30. * Cleanup after tests
  31. *
  32. * @return void
  33. */
  34. public function tearDown() {
  35. ob_end_clean();
  36. }
  37. /**
  38. * Tests the request object constructor
  39. *
  40. */
  41. public function testConstruct() {
  42. $response = new CakeResponse();
  43. $this->assertNull($response->body());
  44. $this->assertEquals($response->charset(), 'UTF-8');
  45. $this->assertEquals($response->type(), 'text/html');
  46. $this->assertEquals($response->statusCode(), 200);
  47. $options = array(
  48. 'body' => 'This is the body',
  49. 'charset' => 'my-custom-charset',
  50. 'type' => 'mp3',
  51. 'status' => '203'
  52. );
  53. $response = new CakeResponse($options);
  54. $this->assertEquals($response->body(), 'This is the body');
  55. $this->assertEquals($response->charset(), 'my-custom-charset');
  56. $this->assertEquals($response->type(), 'audio/mpeg');
  57. $this->assertEquals($response->statusCode(), 203);
  58. }
  59. /**
  60. * Tests the body method
  61. *
  62. */
  63. public function testBody() {
  64. $response = new CakeResponse();
  65. $this->assertNull($response->body());
  66. $response->body('Response body');
  67. $this->assertEquals($response->body(), 'Response body');
  68. $this->assertEquals($response->body('Changed Body'), 'Changed Body');
  69. }
  70. /**
  71. * Tests the charset method
  72. *
  73. */
  74. public function testCharset() {
  75. $response = new CakeResponse();
  76. $this->assertEquals($response->charset(), 'UTF-8');
  77. $response->charset('iso-8859-1');
  78. $this->assertEquals($response->charset(), 'iso-8859-1');
  79. $this->assertEquals($response->charset('UTF-16'), 'UTF-16');
  80. }
  81. /**
  82. * Tests the statusCode method
  83. *
  84. * @expectedException CakeException
  85. */
  86. public function testStatusCode() {
  87. $response = new CakeResponse();
  88. $this->assertEquals($response->statusCode(), 200);
  89. $response->statusCode(404);
  90. $this->assertEquals($response->statusCode(), 404);
  91. $this->assertEquals($response->statusCode(500), 500);
  92. //Throws exception
  93. $response->statusCode(1001);
  94. }
  95. /**
  96. * Tests the type method
  97. *
  98. */
  99. public function testType() {
  100. $response = new CakeResponse();
  101. $this->assertEquals($response->type(), 'text/html');
  102. $response->type('pdf');
  103. $this->assertEquals($response->type(), 'application/pdf');
  104. $this->assertEquals($response->type('application/crazy-mime'), 'application/crazy-mime');
  105. $this->assertEquals($response->type('json'), 'application/json');
  106. $this->assertEquals($response->type('wap'), 'text/vnd.wap.wml');
  107. $this->assertEquals($response->type('xhtml-mobile'), 'application/vnd.wap.xhtml+xml');
  108. $this->assertEquals($response->type('csv'), 'text/csv');
  109. $response->type(array('keynote' => 'application/keynote'));
  110. $this->assertEquals($response->type('keynote'), 'application/keynote');
  111. $this->assertFalse($response->type('wackytype'));
  112. }
  113. /**
  114. * Tests the header method
  115. *
  116. */
  117. public function testHeader() {
  118. $response = new CakeResponse();
  119. $headers = array();
  120. $this->assertEquals($response->header(), $headers);
  121. $response->header('Location', 'http://example.com');
  122. $headers += array('Location' => 'http://example.com');
  123. $this->assertEquals($response->header(), $headers);
  124. //Headers with the same name are overwritten
  125. $response->header('Location', 'http://example2.com');
  126. $headers = array('Location' => 'http://example2.com');
  127. $this->assertEquals($response->header(), $headers);
  128. $response->header(array('WWW-Authenticate' => 'Negotiate'));
  129. $headers += array('WWW-Authenticate' => 'Negotiate');
  130. $this->assertEquals($response->header(), $headers);
  131. $response->header(array('WWW-Authenticate' => 'Not-Negotiate'));
  132. $headers['WWW-Authenticate'] = 'Not-Negotiate';
  133. $this->assertEquals($response->header(), $headers);
  134. $response->header(array('Age' => 12, 'Allow' => 'GET, HEAD'));
  135. $headers += array('Age' => 12, 'Allow' => 'GET, HEAD');
  136. $this->assertEquals($response->header(), $headers);
  137. // String headers are allowed
  138. $response->header('Content-Language: da');
  139. $headers += array('Content-Language' => 'da');
  140. $this->assertEquals($response->header(), $headers);
  141. $response->header('Content-Language: da');
  142. $headers += array('Content-Language' => 'da');
  143. $this->assertEquals($response->header(), $headers);
  144. $response->header(array('Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache'));
  145. $headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
  146. $this->assertEquals($response->header(), $headers);
  147. }
  148. /**
  149. * Tests the send method
  150. *
  151. */
  152. public function testSend() {
  153. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  154. $response->header(array(
  155. 'Content-Language' => 'es',
  156. 'WWW-Authenticate' => 'Negotiate'
  157. ));
  158. $response->body('the response body');
  159. $response->expects($this->once())->method('_sendContent')->with('the response body');
  160. $response->expects($this->at(0))
  161. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  162. $response->expects($this->at(1))
  163. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  164. $response->expects($this->at(2))
  165. ->method('_sendHeader')->with('Content-Language', 'es');
  166. $response->expects($this->at(3))
  167. ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
  168. $response->send();
  169. }
  170. /**
  171. * Tests the send method and changing the content type
  172. *
  173. */
  174. public function testSendChangingContentYype() {
  175. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  176. $response->type('mp3');
  177. $response->body('the response body');
  178. $response->expects($this->once())->method('_sendContent')->with('the response body');
  179. $response->expects($this->at(0))
  180. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  181. $response->expects($this->at(1))
  182. ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8');
  183. $response->send();
  184. }
  185. /**
  186. * Tests the send method and changing the content type
  187. *
  188. */
  189. public function testSendChangingContentType() {
  190. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  191. $response->type('mp3');
  192. $response->body('the response body');
  193. $response->expects($this->once())->method('_sendContent')->with('the response body');
  194. $response->expects($this->at(0))
  195. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  196. $response->expects($this->at(1))
  197. ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8');
  198. $response->send();
  199. }
  200. /**
  201. * Tests the send method and changing the content type
  202. *
  203. */
  204. public function testSendWithLocation() {
  205. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  206. $response->header('Location', 'http://www.example.com');
  207. $response->expects($this->at(0))
  208. ->method('_sendHeader')->with('HTTP/1.1 302 Found');
  209. $response->expects($this->at(1))
  210. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  211. $response->expects($this->at(2))
  212. ->method('_sendHeader')->with('Location', 'http://www.example.com');
  213. $response->send();
  214. }
  215. /**
  216. * Tests the disableCache method
  217. *
  218. */
  219. public function testDisableCache() {
  220. $response = new CakeResponse();
  221. $expected = array(
  222. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  223. 'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
  224. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  225. 'Pragma' => 'no-cache'
  226. );
  227. $response->disableCache();
  228. $this->assertEquals($response->header(), $expected);
  229. }
  230. /**
  231. * Tests the cache method
  232. *
  233. */
  234. public function testCache() {
  235. $response = new CakeResponse();
  236. $since = time();
  237. $time = '+1 day';
  238. $expected = array(
  239. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  240. 'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  241. 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
  242. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
  243. 'Pragma' => 'cache'
  244. );
  245. $response->cache($since);
  246. $this->assertEquals($response->header(), $expected);
  247. $response = new CakeResponse();
  248. $since = time();
  249. $time = '+5 day';
  250. $expected = array(
  251. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  252. 'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  253. 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
  254. 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
  255. 'Pragma' => 'cache'
  256. );
  257. $response->cache($since, $time);
  258. $this->assertEquals($response->header(), $expected);
  259. $response = new CakeResponse();
  260. $since = time();
  261. $time = time();
  262. $expected = array(
  263. 'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  264. 'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
  265. 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
  266. 'Cache-Control' => 'public, max-age=0',
  267. 'Pragma' => 'cache'
  268. );
  269. $response->cache($since, $time);
  270. $this->assertEquals($response->header(), $expected);
  271. }
  272. /**
  273. * Tests the compress method
  274. *
  275. * @return void
  276. */
  277. public function testCompress() {
  278. if (php_sapi_name() !== 'cli') {
  279. $this->markTestSkipped('The response compression can only be tested in cli.');
  280. }
  281. $response = new CakeResponse();
  282. if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
  283. $this->assertFalse($response->compress());
  284. $this->markTestSkipped('Is not possible to test output compression');
  285. }
  286. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  287. $result = $response->compress();
  288. $this->assertFalse($result);
  289. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  290. $result = $response->compress();
  291. $this->assertTrue($result);
  292. $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
  293. ob_get_clean();
  294. }
  295. /**
  296. * Tests the httpCodes method
  297. *
  298. */
  299. public function testHttpCodes() {
  300. $response = new CakeResponse();
  301. $result = $response->httpCodes();
  302. $this->assertEquals(count($result), 39);
  303. $result = $response->httpCodes(100);
  304. $expected = array(100 => 'Continue');
  305. $this->assertEquals($expected, $result);
  306. $codes = array(
  307. 1337 => 'Undefined Unicorn',
  308. 1729 => 'Hardy-Ramanujan Located'
  309. );
  310. $result = $response->httpCodes($codes);
  311. $this->assertTrue($result);
  312. $this->assertEquals(count($response->httpCodes()), 41);
  313. $result = $response->httpCodes(1337);
  314. $expected = array(1337 => 'Undefined Unicorn');
  315. $this->assertEquals($expected, $result);
  316. $codes = array(404 => 'Sorry Bro');
  317. $result = $response->httpCodes($codes);
  318. $this->assertTrue($result);
  319. $this->assertEquals(count($response->httpCodes()), 41);
  320. $result = $response->httpCodes(404);
  321. $expected = array(404 => 'Sorry Bro');
  322. $this->assertEquals($expected, $result);
  323. }
  324. /**
  325. * Tests the download method
  326. *
  327. */
  328. public function testDownload() {
  329. $response = new CakeResponse();
  330. $expected = array(
  331. 'Content-Disposition' => 'attachment; filename="myfile.mp3"'
  332. );
  333. $response->download('myfile.mp3');
  334. $this->assertEquals($response->header(), $expected);
  335. }
  336. /**
  337. * Tests the mapType method
  338. *
  339. */
  340. public function testMapType() {
  341. $response = new CakeResponse();
  342. $this->assertEquals('wav', $response->mapType('audio/x-wav'));
  343. $this->assertEquals('pdf', $response->mapType('application/pdf'));
  344. $this->assertEquals('xml', $response->mapType('text/xml'));
  345. $this->assertEquals('html', $response->mapType('*/*'));
  346. $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
  347. $expected = array('json', 'xhtml', 'css');
  348. $result = $response->mapType(array('application/json', 'application/xhtml+xml', 'text/css'));
  349. $this->assertEquals($expected, $result);
  350. }
  351. /**
  352. * Tests the outputCompressed method
  353. *
  354. */
  355. public function testOutputCompressed() {
  356. $response = new CakeResponse();
  357. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  358. $result = $response->outputCompressed();
  359. $this->assertFalse($result);
  360. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  361. $result = $response->outputCompressed();
  362. $this->assertFalse($result);
  363. if (!extension_loaded("zlib")) {
  364. $this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
  365. }
  366. if (php_sapi_name() !== 'cli') {
  367. $this->markTestSkipped('Testing outputCompressed method with compression enabled done only in cli');
  368. }
  369. if (ini_get("zlib.output_compression") !== '1') {
  370. ob_start('ob_gzhandler');
  371. }
  372. $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
  373. $result = $response->outputCompressed();
  374. $this->assertTrue($result);
  375. $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
  376. $result = $response->outputCompressed();
  377. $this->assertFalse($result);
  378. if (ini_get("zlib.output_compression") !== '1') {
  379. ob_get_clean();
  380. }
  381. }
  382. /**
  383. * Tests the send and setting of Content-Length
  384. *
  385. */
  386. public function testSendContentLength() {
  387. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  388. $response->body('the response body');
  389. $response->expects($this->once())->method('_sendContent')->with('the response body');
  390. $response->expects($this->at(0))
  391. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  392. $response->expects($this->at(1))
  393. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  394. $response->expects($this->at(2))
  395. ->method('_sendHeader')->with('Content-Length', strlen('the response body'));
  396. $response->send();
  397. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  398. $body = '??????Subject????folding????????????????????????';
  399. $response->body($body);
  400. $response->expects($this->once())->method('_sendContent')->with($body);
  401. $response->expects($this->at(0))
  402. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  403. $response->expects($this->at(1))
  404. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  405. $response->expects($this->at(2))
  406. ->method('_sendHeader')->with('Content-Length', 116);
  407. $response->send();
  408. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', 'outputCompressed'));
  409. $body = '??????Subject????folding????????????????????????';
  410. $response->body($body);
  411. $response->expects($this->once())->method('outputCompressed')->will($this->returnValue(true));
  412. $response->expects($this->once())->method('_sendContent')->with($body);
  413. $response->expects($this->exactly(2))->method('_sendHeader');
  414. $response->send();
  415. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', 'outputCompressed'));
  416. $body = 'hwy';
  417. $response->body($body);
  418. $response->header('Content-Length', 1);
  419. $response->expects($this->never())->method('outputCompressed');
  420. $response->expects($this->once())->method('_sendContent')->with($body);
  421. $response->expects($this->at(2))
  422. ->method('_sendHeader')->with('Content-Length', 1);
  423. $response->send();
  424. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  425. $body = 'content';
  426. $response->statusCode(301);
  427. $response->body($body);
  428. $response->expects($this->once())->method('_sendContent')->with($body);
  429. $response->expects($this->exactly(2))->method('_sendHeader');
  430. $response->send();
  431. ob_start();
  432. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  433. $goofyOutput = 'I am goofily sending output in the controller';
  434. echo $goofyOutput;
  435. $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
  436. $body = '??????Subject????folding????????????????????????';
  437. $response->body($body);
  438. $response->expects($this->once())->method('_sendContent')->with($body);
  439. $response->expects($this->at(0))
  440. ->method('_sendHeader')->with('HTTP/1.1 200 OK');
  441. $response->expects($this->at(1))
  442. ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
  443. $response->expects($this->at(2))
  444. ->method('_sendHeader')->with('Content-Length', strlen($goofyOutput) + 116);
  445. $response->send();
  446. ob_end_clean();
  447. }
  448. }