RequestHandlerComponentTest.php 24 KB

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