PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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