PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/request_handler.test.php

https://github.com/hardsshah/bookmarks
PHP | 516 lines | 267 code | 33 blank | 216 comment | 1 complexity | 9515c151831c96767e517e6b1b5cf9b8 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * RequestHandlerComponentTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.controller.components
  21. * @since CakePHP(tm) v 1.2.0.5435
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', array('Controller'));
  28. App::import('Component', array('RequestHandler'));
  29. Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
  30. /**
  31. * RequestHandlerTestController class
  32. *
  33. * @package cake
  34. * @subpackage cake.tests.cases.libs.controller.components
  35. */
  36. class RequestHandlerTestController extends Controller {
  37. /**
  38. * uses property
  39. *
  40. * @var mixed null
  41. * @access public
  42. */
  43. var $uses = null;
  44. /**
  45. * construct method
  46. *
  47. * @param array $params
  48. * @access private
  49. * @return void
  50. */
  51. function __construct($params = array()) {
  52. foreach ($params as $key => $val) {
  53. $this->{$key} = $val;
  54. }
  55. parent::__construct();
  56. }
  57. /**
  58. * test method for ajax redirection
  59. *
  60. * @return void
  61. **/
  62. function destination() {
  63. $this->viewPath = 'posts';
  64. $this->render('index');
  65. }
  66. }
  67. /**
  68. * RequestHandlerTestDisabledController class
  69. *
  70. * @package cake
  71. * @subpackage cake.tests.cases.libs.controller.components
  72. */
  73. class RequestHandlerTestDisabledController extends Controller {
  74. /**
  75. * uses property
  76. *
  77. * @var mixed null
  78. * @access public
  79. */
  80. var $uses = null;
  81. /**
  82. * construct method
  83. *
  84. * @param array $params
  85. * @access private
  86. * @return void
  87. */
  88. function __construct($params = array()) {
  89. foreach ($params as $key => $val) {
  90. $this->{$key} = $val;
  91. }
  92. parent::__construct();
  93. }
  94. /**
  95. * beforeFilter method
  96. *
  97. * @return void
  98. * @access public
  99. */
  100. function beforeFilter() {
  101. $this->RequestHandler->enabled = false;
  102. }
  103. }
  104. /**
  105. * RequestHandlerComponentTest class
  106. *
  107. * @package cake
  108. * @subpackage cake.tests.cases.libs.controller.components
  109. */
  110. class RequestHandlerComponentTest extends CakeTestCase {
  111. /**
  112. * Controller property
  113. *
  114. * @var RequestHandlerTestController
  115. * @access public
  116. */
  117. var $Controller;
  118. /**
  119. * RequestHandler property
  120. *
  121. * @var RequestHandlerComponent
  122. * @access public
  123. */
  124. var $RequestHandler;
  125. /**
  126. * setUp method
  127. *
  128. * @access public
  129. * @return void
  130. */
  131. function setUp() {
  132. $this->_init();
  133. }
  134. /**
  135. * tearDown method
  136. *
  137. * @access public
  138. * @return void
  139. */
  140. function tearDown() {
  141. unset($this->RequestHandler);
  142. unset($this->Controller);
  143. if (!headers_sent()) {
  144. header('Content-type: text/html'); //reset content type.
  145. }
  146. }
  147. /**
  148. * testInitializeCallback method
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. function testInitializeCallback() {
  154. $this->assertNull($this->RequestHandler->ext);
  155. $this->_init();
  156. $this->Controller->params['url']['ext'] = 'rss';
  157. $this->RequestHandler->initialize($this->Controller);
  158. $this->assertEqual($this->RequestHandler->ext, 'rss');
  159. }
  160. /**
  161. * testDisabling method
  162. *
  163. * @access public
  164. * @return void
  165. */
  166. function testDisabling() {
  167. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  168. $this->_init();
  169. $this->Controller->Component->initialize($this->Controller);
  170. $this->Controller->beforeFilter();
  171. $this->Controller->Component->startup($this->Controller);
  172. $this->assertEqual($this->Controller->params, array('isAjax' => true));
  173. $this->Controller = new RequestHandlerTestDisabledController(array('components' => array('RequestHandler')));
  174. $this->Controller->constructClasses();
  175. $this->Controller->Component->initialize($this->Controller);
  176. $this->Controller->beforeFilter();
  177. $this->Controller->Component->startup($this->Controller);
  178. $this->assertEqual($this->Controller->params, array());
  179. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  180. }
  181. /**
  182. * testAutoResponseType method
  183. *
  184. * @access public
  185. * @return void
  186. */
  187. function testAutoResponseType() {
  188. $this->Controller->ext = '.thtml';
  189. $this->Controller->params['url']['ext'] = 'rss';
  190. $this->RequestHandler->initialize($this->Controller);
  191. $this->RequestHandler->startup($this->Controller);
  192. $this->assertEqual($this->Controller->ext, '.ctp');
  193. }
  194. /**
  195. * testStartupCallback method
  196. *
  197. * @access public
  198. * @return void
  199. */
  200. function testStartupCallback() {
  201. $_SERVER['REQUEST_METHOD'] = 'PUT';
  202. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  203. $this->RequestHandler->startup($this->Controller);
  204. $this->assertTrue(is_object($this->Controller->data));
  205. $this->assertEqual(strtolower(get_class($this->Controller->data)), 'xml');
  206. }
  207. /**
  208. * testStartupCallback with charset.
  209. *
  210. * @return void
  211. **/
  212. function testStartupCallbackCharset() {
  213. $_SERVER['REQUEST_METHOD'] = 'PUT';
  214. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  215. $this->RequestHandler->startup($this->Controller);
  216. $this->assertTrue(is_object($this->Controller->data));
  217. $this->assertEqual(strtolower(get_class($this->Controller->data)), 'xml');
  218. }
  219. /**
  220. * testNonAjaxRedirect method
  221. *
  222. * @access public
  223. * @return void
  224. */
  225. function testNonAjaxRedirect() {
  226. $this->RequestHandler->initialize($this->Controller);
  227. $this->RequestHandler->startup($this->Controller);
  228. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
  229. }
  230. /**
  231. * testRenderAs method
  232. *
  233. * @access public
  234. * @return void
  235. */
  236. function testRenderAs() {
  237. $this->assertFalse(in_array('Xml', $this->Controller->helpers));
  238. $this->RequestHandler->renderAs($this->Controller, 'xml');
  239. $this->assertTrue(in_array('Xml', $this->Controller->helpers));
  240. }
  241. /**
  242. * testRequestClientTypes method
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function testRequestClientTypes() {
  248. $this->assertFalse($this->RequestHandler->isFlash());
  249. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  250. $this->assertTrue($this->RequestHandler->isFlash());
  251. unset($_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_X_REQUESTED_WITH']);
  252. $this->assertFalse($this->RequestHandler->isAjax());
  253. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  254. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  255. $this->assertTrue($this->RequestHandler->isAjax());
  256. $this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');
  257. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  258. $this->assertFalse($this->RequestHandler->isAjax());
  259. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  260. }
  261. /**
  262. * Tests the detection of various Flash versions
  263. *
  264. * @access public
  265. * @return void
  266. */
  267. function testFlashDetection() {
  268. $_agent = env('HTTP_USER_AGENT');
  269. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  270. $this->assertTrue($this->RequestHandler->isFlash());
  271. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  272. $this->assertTrue($this->RequestHandler->isFlash());
  273. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 9';
  274. $this->assertTrue($this->RequestHandler->isFlash());
  275. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
  276. $this->assertTrue($this->RequestHandler->isFlash());
  277. $_SERVER['HTTP_USER_AGENT'] = 'Shock Flash';
  278. $this->assertFalse($this->RequestHandler->isFlash());
  279. $_SERVER['HTTP_USER_AGENT'] = $_agent;
  280. }
  281. /**
  282. * testRequestContentTypes method
  283. *
  284. * @access public
  285. * @return void
  286. */
  287. function testRequestContentTypes() {
  288. $_SERVER['REQUEST_METHOD'] = 'GET';
  289. $this->assertNull($this->RequestHandler->requestedWith());
  290. $_SERVER['REQUEST_METHOD'] = 'POST';
  291. $_SERVER['CONTENT_TYPE'] = 'application/json';
  292. $this->assertEqual($this->RequestHandler->requestedWith(), 'json');
  293. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  294. $this->assertEqual($result, 'json');
  295. $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
  296. $this->assertFalse($result);
  297. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  298. $this->_init();
  299. $this->assertTrue($this->RequestHandler->isXml());
  300. $this->assertFalse($this->RequestHandler->isAtom());
  301. $this->assertFalse($this->RequestHandler->isRSS());
  302. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  303. $this->_init();
  304. $this->assertTrue($this->RequestHandler->isAtom());
  305. $this->assertFalse($this->RequestHandler->isRSS());
  306. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  307. $this->_init();
  308. $this->assertFalse($this->RequestHandler->isAtom());
  309. $this->assertTrue($this->RequestHandler->isRSS());
  310. $this->assertFalse($this->RequestHandler->isWap());
  311. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  312. $this->_init();
  313. $this->assertTrue($this->RequestHandler->isWap());
  314. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  315. }
  316. /**
  317. * testResponseContentType method
  318. *
  319. * @access public
  320. * @return void
  321. */
  322. function testResponseContentType() {
  323. $this->assertNull($this->RequestHandler->responseType());
  324. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  325. $this->assertEqual($this->RequestHandler->responseType(), 'atom');
  326. }
  327. /**
  328. * testMobileDeviceDetection method
  329. *
  330. * @access public
  331. * @return void
  332. */
  333. function testMobileDeviceDetection() {
  334. $this->assertFalse($this->RequestHandler->isMobile());
  335. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
  336. $this->assertTrue($this->RequestHandler->isMobile());
  337. }
  338. /**
  339. * testRequestProperties method
  340. *
  341. * @access public
  342. * @return void
  343. */
  344. function testRequestProperties() {
  345. $_SERVER['HTTPS'] = 'on';
  346. $this->assertTrue($this->RequestHandler->isSSL());
  347. unset($_SERVER['HTTPS']);
  348. $this->assertFalse($this->RequestHandler->isSSL());
  349. $_ENV['SCRIPT_URI'] = 'https://localhost/';
  350. $s = $_SERVER;
  351. $_SERVER = array();
  352. $this->assertTrue($this->RequestHandler->isSSL());
  353. $_SERVER = $s;
  354. }
  355. /**
  356. * testRequestMethod method
  357. *
  358. * @access public
  359. * @return void
  360. */
  361. function testRequestMethod() {
  362. $_SERVER['REQUEST_METHOD'] = 'GET';
  363. $this->assertTrue($this->RequestHandler->isGet());
  364. $this->assertFalse($this->RequestHandler->isPost());
  365. $this->assertFalse($this->RequestHandler->isPut());
  366. $this->assertFalse($this->RequestHandler->isDelete());
  367. $_SERVER['REQUEST_METHOD'] = 'POST';
  368. $this->assertFalse($this->RequestHandler->isGet());
  369. $this->assertTrue($this->RequestHandler->isPost());
  370. $this->assertFalse($this->RequestHandler->isPut());
  371. $this->assertFalse($this->RequestHandler->isDelete());
  372. $_SERVER['REQUEST_METHOD'] = 'PUT';
  373. $this->assertFalse($this->RequestHandler->isGet());
  374. $this->assertFalse($this->RequestHandler->isPost());
  375. $this->assertTrue($this->RequestHandler->isPut());
  376. $this->assertFalse($this->RequestHandler->isDelete());
  377. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  378. $this->assertFalse($this->RequestHandler->isGet());
  379. $this->assertFalse($this->RequestHandler->isPost());
  380. $this->assertFalse($this->RequestHandler->isPut());
  381. $this->assertTrue($this->RequestHandler->isDelete());
  382. }
  383. /**
  384. * testClientContentPreference method
  385. *
  386. * @access public
  387. * @return void
  388. */
  389. function testClientContentPreference() {
  390. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  391. $this->_init();
  392. $this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
  393. $this->RequestHandler->ext = 'rss';
  394. $this->assertEqual($this->RequestHandler->prefers(), 'rss');
  395. $this->assertFalse($this->RequestHandler->prefers('xml'));
  396. $this->assertTrue($this->RequestHandler->accepts('xml'));
  397. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
  398. $this->_init();
  399. $this->assertEqual($this->RequestHandler->prefers(), 'xml');
  400. $this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
  401. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  402. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  403. $this->_init();
  404. $this->assertEqual($this->RequestHandler->prefers(), 'html');
  405. $this->assertFalse($this->RequestHandler->prefers('rss'));
  406. $this->assertFalse($this->RequestHandler->accepts('rss'));
  407. }
  408. /**
  409. * testCustomContent method
  410. *
  411. * @access public
  412. * @return void
  413. */
  414. function testCustomContent() {
  415. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  416. $this->_init();
  417. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  418. $this->RequestHandler->startup($this->Controller);
  419. $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
  420. $this->_init();
  421. $this->RequestHandler->setContent(array('mobile' => 'text/x-mobile'));
  422. $this->RequestHandler->startup($this->Controller);
  423. $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
  424. }
  425. /**
  426. * testClientProperties method
  427. *
  428. * @access public
  429. * @return void
  430. */
  431. function testClientProperties() {
  432. $_SERVER['HTTP_HOST'] = 'localhost:80';
  433. $this->assertEqual($this->RequestHandler->getReferrer(), 'localhost');
  434. $_SERVER['HTTP_HOST'] = null;
  435. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  436. $this->assertEqual($this->RequestHandler->getReferrer(), 'cakephp.org');
  437. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  438. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  439. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  440. $this->assertEqual($this->RequestHandler->getClientIP(false), '192.168.1.5');
  441. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
  442. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  443. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
  444. unset($_SERVER['HTTP_CLIENT_IP']);
  445. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.3');
  446. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  447. $this->assertEqual($this->RequestHandler->getClientIP(), '10.0.1.2');
  448. }
  449. /**
  450. * test that ajax requests involving redirects trigger requestAction instead.
  451. *
  452. * @return void
  453. **/
  454. function testAjaxRedirectAsRequestAction() {
  455. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  456. $this->_init();
  457. $_paths = Configure::read('viewPaths');
  458. $testDir = array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS);
  459. Configure::write('viewPaths', array_merge($testDir, $_paths));
  460. $this->Controller->RequestHandler = new NoStopRequestHandler($this);
  461. $this->Controller->RequestHandler->expectOnce('_stop');
  462. ob_start();
  463. $this->Controller->RequestHandler->beforeRedirect(
  464. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  465. );
  466. $result = ob_get_clean();
  467. $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
  468. Configure::write('viewPaths', $_paths);
  469. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  470. }
  471. /**
  472. * init method
  473. *
  474. * @access protected
  475. * @return void
  476. */
  477. function _init() {
  478. $this->Controller = new RequestHandlerTestController(array('components' => array('RequestHandler')));
  479. $this->Controller->constructClasses();
  480. $this->RequestHandler =& $this->Controller->RequestHandler;
  481. }
  482. }
  483. ?>