PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/Component/RequestHandlerComponentTest.php

http://github.com/cakephp/cakephp
PHP | 827 lines | 452 code | 108 blank | 267 comment | 3 complexity | 87226c2fbfc9cfa45f284fdd11244b40 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * RequestHandlerComponentTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  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://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Controller.Component
  16. * @since CakePHP(tm) v 1.2.0.5435
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('RequestHandlerComponent', 'Controller/Component');
  21. App::uses('CakeRequest', 'Network');
  22. App::uses('CakeResponse', 'Network');
  23. App::uses('Router', 'Routing');
  24. /**
  25. * RequestHandlerTestController class
  26. *
  27. * @package Cake.Test.Case.Controller.Component
  28. */
  29. class RequestHandlerTestController extends Controller {
  30. /**
  31. * uses property
  32. *
  33. * @var mixed null
  34. */
  35. public $uses = null;
  36. /**
  37. * test method for ajax redirection
  38. *
  39. * @return void
  40. */
  41. public function destination() {
  42. $this->viewPath = 'Posts';
  43. $this->render('index');
  44. }
  45. /**
  46. * test method for ajax redirection + parameter parsing
  47. *
  48. * @return void
  49. */
  50. public function param_method($one = null, $two = null) {
  51. echo "one: $one two: $two";
  52. $this->autoRender = false;
  53. }
  54. /**
  55. * test method for testing layout rendering when isAjax()
  56. *
  57. * @return void
  58. */
  59. public function ajax2_layout() {
  60. if ($this->autoLayout) {
  61. $this->layout = 'ajax2';
  62. }
  63. $this->destination();
  64. }
  65. }
  66. /**
  67. * RequestHandlerComponentTest class
  68. *
  69. * @package Cake.Test.Case.Controller.Component
  70. */
  71. class RequestHandlerComponentTest extends CakeTestCase {
  72. /**
  73. * Controller property
  74. *
  75. * @var RequestHandlerTestController
  76. */
  77. public $Controller;
  78. /**
  79. * RequestHandler property
  80. *
  81. * @var RequestHandlerComponent
  82. */
  83. public $RequestHandler;
  84. /**
  85. * setUp method
  86. *
  87. * @return void
  88. */
  89. public function setUp() {
  90. parent::setUp();
  91. $this->_server = $_SERVER;
  92. $this->_init();
  93. }
  94. /**
  95. * init method
  96. *
  97. * @return void
  98. */
  99. function _init() {
  100. $request = new CakeRequest('controller_posts/index');
  101. $response = new CakeResponse();
  102. $this->Controller = new RequestHandlerTestController($request, $response);
  103. $this->Controller->constructClasses();
  104. $this->RequestHandler = new RequestHandlerComponent($this->Controller->Components);
  105. $this->_extensions = Router::extensions();
  106. }
  107. /**
  108. * endTest method
  109. *
  110. * @return void
  111. */
  112. public function tearDown() {
  113. parent::tearDown();
  114. unset($this->RequestHandler, $this->Controller);
  115. if (!headers_sent()) {
  116. header('Content-type: text/html'); //reset content type.
  117. }
  118. $_SERVER = $this->_server;
  119. call_user_func_array('Router::parseExtensions', $this->_extensions);
  120. }
  121. /**
  122. * Test that the constructor sets the settings.
  123. *
  124. * @return void
  125. */
  126. public function testConstructorSettings() {
  127. $settings = array(
  128. 'ajaxLayout' => 'test_ajax'
  129. );
  130. $Collection = new ComponentCollection();
  131. $Collection->init($this->Controller);
  132. $RequestHandler = new RequestHandlerComponent($Collection, $settings);
  133. $this->assertEquals($RequestHandler->ajaxLayout, 'test_ajax');
  134. }
  135. /**
  136. * testInitializeCallback method
  137. *
  138. * @return void
  139. */
  140. public function testInitializeCallback() {
  141. $this->assertNull($this->RequestHandler->ext);
  142. $this->Controller->request->params['ext'] = 'rss';
  143. $this->RequestHandler->initialize($this->Controller);
  144. $this->assertEquals($this->RequestHandler->ext, 'rss');
  145. }
  146. /**
  147. * test that a mapped Accept-type header will set $this->ext correctly.
  148. *
  149. * @return void
  150. */
  151. public function testInitializeContentTypeSettingExt() {
  152. $this->assertNull($this->RequestHandler->ext);
  153. $_SERVER['HTTP_ACCEPT'] = 'application/json';
  154. Router::parseExtensions('json');
  155. $this->RequestHandler->initialize($this->Controller);
  156. $this->assertEquals('json', $this->RequestHandler->ext);
  157. }
  158. /**
  159. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
  160. *
  161. * @return void
  162. */
  163. public function testInitializeContentTypeWithjQueryAccept() {
  164. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  165. $this->assertNull($this->RequestHandler->ext);
  166. Router::parseExtensions('json');
  167. $this->RequestHandler->initialize($this->Controller);
  168. $this->assertEquals('json', $this->RequestHandler->ext);
  169. }
  170. /**
  171. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
  172. * and the application is configured to handle multiplate extensions
  173. *
  174. * @return void
  175. */
  176. public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions() {
  177. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, */*; q=0.01';
  178. $this->assertNull($this->RequestHandler->ext);
  179. Router::parseExtensions('rss', 'json');
  180. $this->RequestHandler->initialize($this->Controller);
  181. $this->assertEquals('json', $this->RequestHandler->ext);
  182. }
  183. /**
  184. * Test that RequestHandler does not set $this->ext when multple accepts are sent.
  185. *
  186. * @return void
  187. */
  188. public function testInitializeNoContentTypeWithSingleAccept() {
  189. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
  190. $this->assertNull($this->RequestHandler->ext);
  191. Router::parseExtensions('json');
  192. $this->RequestHandler->initialize($this->Controller);
  193. $this->assertNull($this->RequestHandler->ext);
  194. }
  195. /**
  196. * Test that ext is not set with multiple accepted content types.
  197. *
  198. * @return void
  199. */
  200. public function testInitializeNoContentTypeWithMultipleAcceptedTypes() {
  201. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/javascript, application/xml, */*; q=0.01';
  202. $this->assertNull($this->RequestHandler->ext);
  203. Router::parseExtensions('xml', 'json');
  204. $this->RequestHandler->initialize($this->Controller);
  205. $this->assertNull($this->RequestHandler->ext);
  206. }
  207. /**
  208. * Test that ext is not set with confusing android accepts headers.
  209. *
  210. * @return void
  211. */
  212. public function testInitializeAmbiguousAndroidAccepts() {
  213. $_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
  214. $this->assertNull($this->RequestHandler->ext);
  215. Router::parseExtensions('html', 'xml');
  216. $this->RequestHandler->initialize($this->Controller);
  217. $this->assertNull($this->RequestHandler->ext);
  218. }
  219. /**
  220. * Test that a type mismatch doesn't incorrectly set the ext
  221. *
  222. * @return void
  223. */
  224. public function testInitializeContentTypeAndExtensionMismatch() {
  225. $this->assertNull($this->RequestHandler->ext);
  226. $extensions = Router::extensions();
  227. Router::parseExtensions('xml');
  228. $this->Controller->request = $this->getMock('CakeRequest');
  229. $this->Controller->request->expects($this->any())
  230. ->method('accepts')
  231. ->will($this->returnValue(array('application/json')));
  232. $this->RequestHandler->initialize($this->Controller);
  233. $this->assertNull($this->RequestHandler->ext);
  234. call_user_func_array(array('Router', 'parseExtensions'), $extensions);
  235. }
  236. /**
  237. * testDisabling method
  238. *
  239. * @return void
  240. */
  241. public function testDisabling() {
  242. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  243. $this->_init();
  244. $this->RequestHandler->initialize($this->Controller);
  245. $this->Controller->beforeFilter();
  246. $this->RequestHandler->startup($this->Controller);
  247. $this->assertEquals($this->Controller->params['isAjax'], true);
  248. }
  249. /**
  250. * testAutoResponseType method
  251. *
  252. * @return void
  253. */
  254. public function testAutoResponseType() {
  255. $this->Controller->ext = '.thtml';
  256. $this->Controller->request->params['ext'] = 'rss';
  257. $this->RequestHandler->initialize($this->Controller);
  258. $this->RequestHandler->startup($this->Controller);
  259. $this->assertEquals($this->Controller->ext, '.ctp');
  260. }
  261. /**
  262. * testAutoAjaxLayout method
  263. *
  264. * @return void
  265. */
  266. public function testAutoAjaxLayout() {
  267. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  268. $this->RequestHandler->startup($this->Controller);
  269. $this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout);
  270. $this->_init();
  271. $this->Controller->request->params['ext'] = 'js';
  272. $this->RequestHandler->initialize($this->Controller);
  273. $this->RequestHandler->startup($this->Controller);
  274. $this->assertNotEquals($this->Controller->layout, 'ajax');
  275. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  276. }
  277. /**
  278. * testStartupCallback method
  279. *
  280. * @return void
  281. */
  282. public function testStartupCallback() {
  283. $_SERVER['REQUEST_METHOD'] = 'PUT';
  284. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  285. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  286. $this->RequestHandler->startup($this->Controller);
  287. $this->assertTrue(is_array($this->Controller->data));
  288. $this->assertFalse(is_object($this->Controller->data));
  289. }
  290. /**
  291. * testStartupCallback with charset.
  292. *
  293. * @return void
  294. */
  295. public function testStartupCallbackCharset() {
  296. $_SERVER['REQUEST_METHOD'] = 'PUT';
  297. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  298. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  299. $this->RequestHandler->startup($this->Controller);
  300. $this->assertTrue(is_array($this->Controller->data));
  301. $this->assertFalse(is_object($this->Controller->data));
  302. }
  303. /**
  304. * Test mapping a new type and having startup process it.
  305. *
  306. * @return void
  307. */
  308. public function testStartupCustomTypeProcess() {
  309. if (!function_exists('str_getcsv')) {
  310. $this->markTestSkipped('Need "str_getcsv" for this test.');
  311. }
  312. $_SERVER['REQUEST_METHOD'] = 'POST';
  313. $_SERVER['CONTENT_TYPE'] = 'text/csv';
  314. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  315. $this->Controller->request->expects($this->once())
  316. ->method('_readInput')
  317. ->will($this->returnValue('"A","csv","string"'));
  318. $this->RequestHandler->addInputType('csv', array('str_getcsv'));
  319. $this->RequestHandler->startup($this->Controller);
  320. $expected = array(
  321. 'A', 'csv', 'string'
  322. );
  323. $this->assertEquals($expected, $this->Controller->request->data);
  324. }
  325. /**
  326. * testNonAjaxRedirect method
  327. *
  328. * @return void
  329. */
  330. public function testNonAjaxRedirect() {
  331. $this->RequestHandler->initialize($this->Controller);
  332. $this->RequestHandler->startup($this->Controller);
  333. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
  334. }
  335. /**
  336. * testRenderAs method
  337. *
  338. * @return void
  339. */
  340. public function testRenderAs() {
  341. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  342. $this->RequestHandler->renderAs($this->Controller, 'rss');
  343. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  344. $this->Controller->viewPath = 'request_handler_test\\rss';
  345. $this->RequestHandler->renderAs($this->Controller, 'js');
  346. $this->assertEquals($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
  347. }
  348. /**
  349. * test that attachment headers work with renderAs
  350. *
  351. * @return void
  352. */
  353. public function testRenderAsWithAttachment() {
  354. $this->RequestHandler->request = $this->getMock('CakeRequest');
  355. $this->RequestHandler->request->expects($this->any())
  356. ->method('parseAccept')
  357. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  358. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
  359. $this->RequestHandler->response->expects($this->at(0))
  360. ->method('type')
  361. ->with('application/xml');
  362. $this->RequestHandler->response->expects($this->at(1))
  363. ->method('charset')
  364. ->with('UTF-8');
  365. $this->RequestHandler->response->expects($this->at(2))
  366. ->method('download')
  367. ->with('myfile.xml');
  368. $this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
  369. $expected = 'RequestHandlerTest' . DS . 'xml';
  370. $this->assertEquals($expected, $this->Controller->viewPath);
  371. }
  372. /**
  373. * test that respondAs works as expected.
  374. *
  375. * @return void
  376. */
  377. public function testRespondAs() {
  378. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
  379. $this->RequestHandler->response->expects($this->at(0))->method('type')
  380. ->with('application/json');
  381. $this->RequestHandler->response->expects($this->at(1))->method('type')
  382. ->with('text/xml');
  383. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'), array(&$this->Controller->Components));
  384. $result = $this->RequestHandler->respondAs('json');
  385. $this->assertTrue($result);
  386. $result = $this->RequestHandler->respondAs('text/xml');
  387. $this->assertTrue($result);
  388. }
  389. /**
  390. * test that attachment headers work with respondAs
  391. *
  392. * @return void
  393. */
  394. public function testRespondAsWithAttachment() {
  395. $this->RequestHandler = $this->getMock(
  396. 'RequestHandlerComponent',
  397. array('_header'),
  398. array(&$this->Controller->Components)
  399. );
  400. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
  401. $this->RequestHandler->request = $this->getMock('CakeRequest');
  402. $this->RequestHandler->request->expects($this->once())
  403. ->method('parseAccept')
  404. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  405. $this->RequestHandler->response->expects($this->once())->method('download')
  406. ->with('myfile.xml');
  407. $this->RequestHandler->response->expects($this->once())->method('type')
  408. ->with('application/xml');
  409. $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  410. $this->assertTrue($result);
  411. }
  412. /**
  413. * test that calling renderAs() more than once continues to work.
  414. *
  415. * @link #6466
  416. * @return void
  417. */
  418. public function testRenderAsCalledTwice() {
  419. $this->RequestHandler->renderAs($this->Controller, 'xml');
  420. $this->assertEquals($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'xml');
  421. $this->assertEquals($this->Controller->layoutPath, 'xml');
  422. $this->RequestHandler->renderAs($this->Controller, 'js');
  423. $this->assertEquals($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'js');
  424. $this->assertEquals($this->Controller->layoutPath, 'js');
  425. $this->assertTrue(in_array('Js', $this->Controller->helpers));
  426. }
  427. /**
  428. * testRequestClientTypes method
  429. *
  430. * @return void
  431. */
  432. public function testRequestClientTypes() {
  433. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  434. $this->assertEquals($this->RequestHandler->getAjaxVersion(), '1.5');
  435. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  436. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  437. }
  438. /**
  439. * Tests the detection of various Flash versions
  440. *
  441. * @return void
  442. */
  443. public function testFlashDetection() {
  444. $request = $this->getMock('CakeRequest');
  445. $request->expects($this->once())->method('is')
  446. ->with('flash')
  447. ->will($this->returnValue(true));
  448. $this->RequestHandler->request = $request;
  449. $this->assertTrue($this->RequestHandler->isFlash());
  450. }
  451. /**
  452. * testRequestContentTypes method
  453. *
  454. * @return void
  455. */
  456. public function testRequestContentTypes() {
  457. $_SERVER['REQUEST_METHOD'] = 'GET';
  458. $this->assertNull($this->RequestHandler->requestedWith());
  459. $_SERVER['REQUEST_METHOD'] = 'POST';
  460. $_SERVER['CONTENT_TYPE'] = 'application/json';
  461. $this->assertEquals($this->RequestHandler->requestedWith(), 'json');
  462. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  463. $this->assertEquals($result, 'json');
  464. $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
  465. $this->assertFalse($result);
  466. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  467. $this->assertTrue($this->RequestHandler->isXml());
  468. $this->assertFalse($this->RequestHandler->isAtom());
  469. $this->assertFalse($this->RequestHandler->isRSS());
  470. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  471. $this->assertTrue($this->RequestHandler->isAtom());
  472. $this->assertFalse($this->RequestHandler->isRSS());
  473. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  474. $this->assertFalse($this->RequestHandler->isAtom());
  475. $this->assertTrue($this->RequestHandler->isRSS());
  476. $this->assertFalse($this->RequestHandler->isWap());
  477. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  478. $this->assertTrue($this->RequestHandler->isWap());
  479. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  480. }
  481. /**
  482. * testResponseContentType method
  483. *
  484. * @return void
  485. */
  486. public function testResponseContentType() {
  487. $this->assertEquals('html', $this->RequestHandler->responseType());
  488. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  489. $this->assertEquals($this->RequestHandler->responseType(), 'atom');
  490. }
  491. /**
  492. * testMobileDeviceDetection method
  493. *
  494. * @return void
  495. */
  496. public function testMobileDeviceDetection() {
  497. $request = $this->getMock('CakeRequest');
  498. $request->expects($this->once())->method('is')
  499. ->with('mobile')
  500. ->will($this->returnValue(true));
  501. $this->RequestHandler->request = $request;
  502. $this->assertTrue($this->RequestHandler->isMobile());
  503. }
  504. /**
  505. * testRequestProperties method
  506. *
  507. * @return void
  508. */
  509. public function testRequestProperties() {
  510. $request = $this->getMock('CakeRequest');
  511. $request->expects($this->once())->method('is')
  512. ->with('ssl')
  513. ->will($this->returnValue(true));
  514. $this->RequestHandler->request = $request;
  515. $this->assertTrue($this->RequestHandler->isSsl());
  516. }
  517. /**
  518. * testRequestMethod method
  519. *
  520. * @return void
  521. */
  522. public function testRequestMethod() {
  523. $request = $this->getMock('CakeRequest');
  524. $request->expects($this->at(0))->method('is')
  525. ->with('get')
  526. ->will($this->returnValue(true));
  527. $request->expects($this->at(1))->method('is')
  528. ->with('post')
  529. ->will($this->returnValue(false));
  530. $request->expects($this->at(2))->method('is')
  531. ->with('delete')
  532. ->will($this->returnValue(true));
  533. $request->expects($this->at(3))->method('is')
  534. ->with('put')
  535. ->will($this->returnValue(false));
  536. $this->RequestHandler->request = $request;
  537. $this->assertTrue($this->RequestHandler->isGet());
  538. $this->assertFalse($this->RequestHandler->isPost());
  539. $this->assertTrue($this->RequestHandler->isDelete());
  540. $this->assertFalse($this->RequestHandler->isPut());
  541. }
  542. /**
  543. * test that map alias converts aliases to content types.
  544. *
  545. * @return void
  546. */
  547. public function testMapAlias() {
  548. $result = $this->RequestHandler->mapAlias('xml');
  549. $this->assertEquals('application/xml', $result);
  550. $result = $this->RequestHandler->mapAlias('text/html');
  551. $this->assertNull($result);
  552. $result = $this->RequestHandler->mapAlias('wap');
  553. $this->assertEquals('text/vnd.wap.wml', $result);
  554. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  555. $expected = array('application/xml', 'text/javascript', 'application/json');
  556. $this->assertEquals($expected, $result);
  557. }
  558. /**
  559. * test accepts() on the component
  560. *
  561. * @return void
  562. */
  563. public function testAccepts() {
  564. $_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';
  565. $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  566. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  567. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  568. $this->assertFalse($this->RequestHandler->accepts('rss'));
  569. }
  570. /**
  571. * test accepts and prefers methods.
  572. *
  573. * @return void
  574. */
  575. public function testPrefers() {
  576. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  577. $this->assertNotEquals($this->RequestHandler->prefers(), 'rss');
  578. $this->RequestHandler->ext = 'rss';
  579. $this->assertEquals($this->RequestHandler->prefers(), 'rss');
  580. $this->assertFalse($this->RequestHandler->prefers('xml'));
  581. $this->assertEquals($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
  582. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  583. $this->assertEquals($this->RequestHandler->prefers(array('js', 'json', 'xhtml')), 'xhtml');
  584. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  585. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  586. $_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';
  587. $this->_init();
  588. $this->assertEquals($this->RequestHandler->prefers(), 'xml');
  589. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  590. $this->assertEquals($this->RequestHandler->prefers(), 'html');
  591. $this->assertFalse($this->RequestHandler->prefers('rss'));
  592. }
  593. /**
  594. * testCustomContent method
  595. *
  596. * @return void
  597. */
  598. public function testCustomContent() {
  599. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  600. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  601. $this->RequestHandler->startup($this->Controller);
  602. $this->assertEquals($this->RequestHandler->prefers(), 'mobile');
  603. }
  604. /**
  605. * testClientProperties method
  606. *
  607. * @return void
  608. */
  609. public function testClientProperties() {
  610. $request = $this->getMock('CakeRequest');
  611. $request->expects($this->once())->method('referer');
  612. $request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
  613. $this->RequestHandler->request = $request;
  614. $this->RequestHandler->getReferer();
  615. $this->RequestHandler->getClientIP(false);
  616. }
  617. /**
  618. * test that ajax requests involving redirects trigger requestAction instead.
  619. *
  620. * @return void
  621. */
  622. public function testAjaxRedirectAsRequestAction() {
  623. App::build(array(
  624. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  625. ), true);
  626. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  627. $this->Controller->request = $this->getMock('CakeRequest');
  628. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  629. $this->Controller->RequestHandler->request = $this->Controller->request;
  630. $this->Controller->RequestHandler->response = $this->Controller->response;
  631. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  632. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  633. ob_start();
  634. $this->Controller->RequestHandler->beforeRedirect(
  635. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  636. );
  637. $result = ob_get_clean();
  638. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  639. App::build();
  640. }
  641. /**
  642. * test that ajax requests involving redirects don't force no layout
  643. * this would cause the ajax layout to not be rendered.
  644. *
  645. * @return void
  646. */
  647. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  648. App::build(array(
  649. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  650. ), true);
  651. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  652. $this->Controller->request = $this->getMock('CakeRequest');
  653. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  654. $this->Controller->RequestHandler->request = $this->Controller->request;
  655. $this->Controller->RequestHandler->response = $this->Controller->response;
  656. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  657. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  658. ob_start();
  659. $this->Controller->RequestHandler->beforeRedirect(
  660. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
  661. );
  662. $result = ob_get_clean();
  663. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  664. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  665. App::build();
  666. }
  667. /**
  668. * test that the beforeRedirect callback properly converts
  669. * array urls into their correct string ones, and adds base => false so
  670. * the correct urls are generated.
  671. *
  672. * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  673. * @return void
  674. */
  675. public function testBeforeRedirectCallbackWithArrayUrl() {
  676. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  677. Router::setRequestInfo(array(
  678. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  679. array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
  680. ));
  681. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  682. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  683. $RequestHandler->request = new CakeRequest('posts/index');
  684. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  685. ob_start();
  686. $RequestHandler->beforeRedirect(
  687. $this->Controller,
  688. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
  689. );
  690. $result = ob_get_clean();
  691. $this->assertEquals($result, 'one: first two: second');
  692. }
  693. /**
  694. * assure that beforeRedirect with a status code will correctly set the status header
  695. *
  696. * @return void
  697. */
  698. public function testBeforeRedirectCallingHeader() {
  699. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  700. $controller = $this->getMock('Controller', array('header'));
  701. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  702. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader','statusCode'));
  703. $RequestHandler->request = $this->getMock('CakeRequest');
  704. $RequestHandler->request->expects($this->once())->method('is')
  705. ->with('ajax')
  706. ->will($this->returnValue(true));
  707. $RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
  708. ob_start();
  709. $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
  710. $result = ob_get_clean();
  711. }
  712. /**
  713. * @expectedException CakeException
  714. * @return void
  715. */
  716. public function testAddInputTypeException() {
  717. $this->RequestHandler->addInputType('csv', array('I am not callable'));
  718. }
  719. }