PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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