RequestHandlerComponentTest.php 24 KB

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