RequestHandlerComponentTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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->assertEqual($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->assertEqual($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 a type mismatch doesn't incorrectly set the ext
  209. *
  210. * @return void
  211. */
  212. public function testInitializeContentTypeAndExtensionMismatch() {
  213. $this->assertNull($this->RequestHandler->ext);
  214. $extensions = Router::extensions();
  215. Router::parseExtensions('xml');
  216. $this->Controller->request = $this->getMock('CakeRequest');
  217. $this->Controller->request->expects($this->any())
  218. ->method('accepts')
  219. ->will($this->returnValue(array('application/json')));
  220. $this->RequestHandler->initialize($this->Controller);
  221. $this->assertNull($this->RequestHandler->ext);
  222. call_user_func_array(array('Router', 'parseExtensions'), $extensions);
  223. }
  224. /**
  225. * testDisabling method
  226. *
  227. * @return void
  228. */
  229. public function testDisabling() {
  230. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  231. $this->_init();
  232. $this->RequestHandler->initialize($this->Controller);
  233. $this->Controller->beforeFilter();
  234. $this->RequestHandler->startup($this->Controller);
  235. $this->assertEqual($this->Controller->params['isAjax'], true);
  236. }
  237. /**
  238. * testAutoResponseType method
  239. *
  240. * @return void
  241. */
  242. public function testAutoResponseType() {
  243. $this->Controller->ext = '.thtml';
  244. $this->Controller->request->params['ext'] = 'rss';
  245. $this->RequestHandler->initialize($this->Controller);
  246. $this->RequestHandler->startup($this->Controller);
  247. $this->assertEqual($this->Controller->ext, '.ctp');
  248. }
  249. /**
  250. * testAutoAjaxLayout method
  251. *
  252. * @return void
  253. */
  254. public function testAutoAjaxLayout() {
  255. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  256. $this->RequestHandler->startup($this->Controller);
  257. $this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout);
  258. $this->_init();
  259. $this->Controller->request->params['ext'] = 'js';
  260. $this->RequestHandler->initialize($this->Controller);
  261. $this->RequestHandler->startup($this->Controller);
  262. $this->assertNotEqual($this->Controller->layout, 'ajax');
  263. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  264. }
  265. /**
  266. * testStartupCallback method
  267. *
  268. * @return void
  269. */
  270. public function testStartupCallback() {
  271. $_SERVER['REQUEST_METHOD'] = 'PUT';
  272. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  273. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  274. $this->RequestHandler->startup($this->Controller);
  275. $this->assertTrue(is_array($this->Controller->data));
  276. $this->assertFalse(is_object($this->Controller->data));
  277. }
  278. /**
  279. * testStartupCallback with charset.
  280. *
  281. * @return void
  282. */
  283. public function testStartupCallbackCharset() {
  284. $_SERVER['REQUEST_METHOD'] = 'PUT';
  285. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  286. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  287. $this->RequestHandler->startup($this->Controller);
  288. $this->assertTrue(is_array($this->Controller->data));
  289. $this->assertFalse(is_object($this->Controller->data));
  290. }
  291. /**
  292. * Test mapping a new type and having startup process it.
  293. *
  294. * @return void
  295. */
  296. public function testStartupCustomTypeProcess() {
  297. if (!function_exists('str_getcsv')) {
  298. $this->markTestSkipped('Need "str_getcsv" for this test.');
  299. }
  300. $_SERVER['REQUEST_METHOD'] = 'POST';
  301. $_SERVER['CONTENT_TYPE'] = 'text/csv';
  302. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  303. $this->Controller->request->expects($this->once())
  304. ->method('_readInput')
  305. ->will($this->returnValue('"A","csv","string"'));
  306. $this->RequestHandler->addInputType('csv', array('str_getcsv'));
  307. $this->RequestHandler->startup($this->Controller);
  308. $expected = array(
  309. 'A', 'csv', 'string'
  310. );
  311. $this->assertEquals($expected, $this->Controller->request->data);
  312. }
  313. /**
  314. * testNonAjaxRedirect method
  315. *
  316. * @return void
  317. */
  318. public function testNonAjaxRedirect() {
  319. $this->RequestHandler->initialize($this->Controller);
  320. $this->RequestHandler->startup($this->Controller);
  321. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
  322. }
  323. /**
  324. * testRenderAs method
  325. *
  326. * @return void
  327. */
  328. public function testRenderAs() {
  329. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  330. $this->RequestHandler->renderAs($this->Controller, 'rss');
  331. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  332. $this->Controller->viewPath = 'request_handler_test\\rss';
  333. $this->RequestHandler->renderAs($this->Controller, 'js');
  334. $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
  335. }
  336. /**
  337. * test that attachment headers work with renderAs
  338. *
  339. * @return void
  340. */
  341. public function testRenderAsWithAttachment() {
  342. $this->RequestHandler->request = $this->getMock('CakeRequest');
  343. $this->RequestHandler->request->expects($this->any())
  344. ->method('parseAccept')
  345. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  346. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
  347. $this->RequestHandler->response->expects($this->at(0))
  348. ->method('type')
  349. ->with('application/xml');
  350. $this->RequestHandler->response->expects($this->at(1))
  351. ->method('charset')
  352. ->with('UTF-8');
  353. $this->RequestHandler->response->expects($this->at(2))
  354. ->method('download')
  355. ->with('myfile.xml');
  356. $this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
  357. $expected = 'RequestHandlerTest' . DS . 'xml';
  358. $this->assertEquals($expected, $this->Controller->viewPath);
  359. }
  360. /**
  361. * test that respondAs works as expected.
  362. *
  363. * @return void
  364. */
  365. public function testRespondAs() {
  366. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
  367. $this->RequestHandler->response->expects($this->at(0))->method('type')
  368. ->with('application/json');
  369. $this->RequestHandler->response->expects($this->at(1))->method('type')
  370. ->with('text/xml');
  371. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'), array(&$this->Controller->Components));
  372. $result = $this->RequestHandler->respondAs('json');
  373. $this->assertTrue($result);
  374. $result = $this->RequestHandler->respondAs('text/xml');
  375. $this->assertTrue($result);
  376. }
  377. /**
  378. * test that attachment headers work with respondAs
  379. *
  380. * @return void
  381. */
  382. public function testRespondAsWithAttachment() {
  383. $this->RequestHandler = $this->getMock(
  384. 'RequestHandlerComponent',
  385. array('_header'),
  386. array(&$this->Controller->Components)
  387. );
  388. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
  389. $this->RequestHandler->request = $this->getMock('CakeRequest');
  390. $this->RequestHandler->request->expects($this->once())
  391. ->method('parseAccept')
  392. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  393. $this->RequestHandler->response->expects($this->once())->method('download')
  394. ->with('myfile.xml');
  395. $this->RequestHandler->response->expects($this->once())->method('type')
  396. ->with('application/xml');
  397. $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  398. $this->assertTrue($result);
  399. }
  400. /**
  401. * test that calling renderAs() more than once continues to work.
  402. *
  403. * @link #6466
  404. * @return void
  405. */
  406. public function testRenderAsCalledTwice() {
  407. $this->RequestHandler->renderAs($this->Controller, 'xml');
  408. $this->assertEqual($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'xml');
  409. $this->assertEqual($this->Controller->layoutPath, 'xml');
  410. $this->RequestHandler->renderAs($this->Controller, 'js');
  411. $this->assertEqual($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'js');
  412. $this->assertEqual($this->Controller->layoutPath, 'js');
  413. $this->assertTrue(in_array('Js', $this->Controller->helpers));
  414. }
  415. /**
  416. * testRequestClientTypes method
  417. *
  418. * @return void
  419. */
  420. public function testRequestClientTypes() {
  421. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  422. $this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');
  423. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  424. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  425. }
  426. /**
  427. * Tests the detection of various Flash versions
  428. *
  429. * @return void
  430. */
  431. public function testFlashDetection() {
  432. $request = $this->getMock('CakeRequest');
  433. $request->expects($this->once())->method('is')
  434. ->with('flash')
  435. ->will($this->returnValue(true));
  436. $this->RequestHandler->request = $request;
  437. $this->assertTrue($this->RequestHandler->isFlash());
  438. }
  439. /**
  440. * testRequestContentTypes method
  441. *
  442. * @return void
  443. */
  444. public function testRequestContentTypes() {
  445. $_SERVER['REQUEST_METHOD'] = 'GET';
  446. $this->assertNull($this->RequestHandler->requestedWith());
  447. $_SERVER['REQUEST_METHOD'] = 'POST';
  448. $_SERVER['CONTENT_TYPE'] = 'application/json';
  449. $this->assertEqual($this->RequestHandler->requestedWith(), 'json');
  450. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  451. $this->assertEqual($result, 'json');
  452. $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
  453. $this->assertFalse($result);
  454. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  455. $this->assertTrue($this->RequestHandler->isXml());
  456. $this->assertFalse($this->RequestHandler->isAtom());
  457. $this->assertFalse($this->RequestHandler->isRSS());
  458. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  459. $this->assertTrue($this->RequestHandler->isAtom());
  460. $this->assertFalse($this->RequestHandler->isRSS());
  461. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  462. $this->assertFalse($this->RequestHandler->isAtom());
  463. $this->assertTrue($this->RequestHandler->isRSS());
  464. $this->assertFalse($this->RequestHandler->isWap());
  465. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  466. $this->assertTrue($this->RequestHandler->isWap());
  467. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  468. }
  469. /**
  470. * testResponseContentType method
  471. *
  472. * @return void
  473. */
  474. public function testResponseContentType() {
  475. $this->assertEquals('html', $this->RequestHandler->responseType());
  476. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  477. $this->assertEqual($this->RequestHandler->responseType(), 'atom');
  478. }
  479. /**
  480. * testMobileDeviceDetection method
  481. *
  482. * @return void
  483. */
  484. public function testMobileDeviceDetection() {
  485. $request = $this->getMock('CakeRequest');
  486. $request->expects($this->once())->method('is')
  487. ->with('mobile')
  488. ->will($this->returnValue(true));
  489. $this->RequestHandler->request = $request;
  490. $this->assertTrue($this->RequestHandler->isMobile());
  491. }
  492. /**
  493. * testRequestProperties method
  494. *
  495. * @return void
  496. */
  497. public 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. * @return void
  509. */
  510. public function testRequestMethod() {
  511. $request = $this->getMock('CakeRequest');
  512. $request->expects($this->at(0))->method('is')
  513. ->with('get')
  514. ->will($this->returnValue(true));
  515. $request->expects($this->at(1))->method('is')
  516. ->with('post')
  517. ->will($this->returnValue(false));
  518. $request->expects($this->at(2))->method('is')
  519. ->with('delete')
  520. ->will($this->returnValue(true));
  521. $request->expects($this->at(3))->method('is')
  522. ->with('put')
  523. ->will($this->returnValue(false));
  524. $this->RequestHandler->request = $request;
  525. $this->assertTrue($this->RequestHandler->isGet());
  526. $this->assertFalse($this->RequestHandler->isPost());
  527. $this->assertTrue($this->RequestHandler->isDelete());
  528. $this->assertFalse($this->RequestHandler->isPut());
  529. }
  530. /**
  531. * test that map alias converts aliases to content types.
  532. *
  533. * @return void
  534. */
  535. public function testMapAlias() {
  536. $result = $this->RequestHandler->mapAlias('xml');
  537. $this->assertEquals('application/xml', $result);
  538. $result = $this->RequestHandler->mapAlias('text/html');
  539. $this->assertNull($result);
  540. $result = $this->RequestHandler->mapAlias('wap');
  541. $this->assertEquals('text/vnd.wap.wml', $result);
  542. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  543. $expected = array('application/xml', 'text/javascript', 'application/json');
  544. $this->assertEquals($expected, $result);
  545. }
  546. /**
  547. * test accepts() on the component
  548. *
  549. * @return void
  550. */
  551. public function testAccepts() {
  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->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  554. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  555. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  556. $this->assertFalse($this->RequestHandler->accepts('rss'));
  557. }
  558. /**
  559. * test accepts and prefers methods.
  560. *
  561. * @return void
  562. */
  563. public function testPrefers() {
  564. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  565. $this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
  566. $this->RequestHandler->ext = 'rss';
  567. $this->assertEqual($this->RequestHandler->prefers(), 'rss');
  568. $this->assertFalse($this->RequestHandler->prefers('xml'));
  569. $this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
  570. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  571. $this->assertEqual($this->RequestHandler->prefers(array('js', 'json', 'xhtml')), 'xhtml');
  572. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  573. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  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. * @return void
  585. */
  586. public function testCustomContent() {
  587. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  588. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  589. $this->RequestHandler->startup($this->Controller);
  590. $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
  591. }
  592. /**
  593. * testClientProperties method
  594. *
  595. * @return void
  596. */
  597. public function testClientProperties() {
  598. $request = $this->getMock('CakeRequest');
  599. $request->expects($this->once())->method('referer');
  600. $request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
  601. $this->RequestHandler->request = $request;
  602. $this->RequestHandler->getReferer();
  603. $this->RequestHandler->getClientIP(false);
  604. }
  605. /**
  606. * test that ajax requests involving redirects trigger requestAction instead.
  607. *
  608. * @return void
  609. */
  610. public function testAjaxRedirectAsRequestAction() {
  611. App::build(array(
  612. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  613. ), true);
  614. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  615. $this->Controller->request = $this->getMock('CakeRequest');
  616. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  617. $this->Controller->RequestHandler->request = $this->Controller->request;
  618. $this->Controller->RequestHandler->response = $this->Controller->response;
  619. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  620. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  621. ob_start();
  622. $this->Controller->RequestHandler->beforeRedirect(
  623. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  624. );
  625. $result = ob_get_clean();
  626. $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
  627. App::build();
  628. }
  629. /**
  630. * test that ajax requests involving redirects don't force no layout
  631. * this would cause the ajax layout to not be rendered.
  632. *
  633. * @return void
  634. */
  635. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  636. App::build(array(
  637. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  638. ), true);
  639. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  640. $this->Controller->request = $this->getMock('CakeRequest');
  641. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  642. $this->Controller->RequestHandler->request = $this->Controller->request;
  643. $this->Controller->RequestHandler->response = $this->Controller->response;
  644. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  645. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  646. ob_start();
  647. $this->Controller->RequestHandler->beforeRedirect(
  648. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
  649. );
  650. $result = ob_get_clean();
  651. $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
  652. $this->assertPattern('/Ajax!/', $result, 'Layout was not rendered.');
  653. App::build();
  654. }
  655. /**
  656. * test that the beforeRedirect callback properly converts
  657. * array urls into their correct string ones, and adds base => false so
  658. * the correct urls are generated.
  659. *
  660. * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  661. * @return void
  662. */
  663. public function testBeforeRedirectCallbackWithArrayUrl() {
  664. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  665. Router::setRequestInfo(array(
  666. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  667. array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
  668. ));
  669. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  670. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  671. $RequestHandler->request = new CakeRequest('posts/index');
  672. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  673. ob_start();
  674. $RequestHandler->beforeRedirect(
  675. $this->Controller,
  676. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
  677. );
  678. $result = ob_get_clean();
  679. $this->assertEqual($result, 'one: first two: second');
  680. }
  681. /**
  682. * assure that beforeRedirect with a status code will correctly set the status header
  683. *
  684. * @return void
  685. */
  686. public function testBeforeRedirectCallingHeader() {
  687. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  688. $controller = $this->getMock('Controller', array('header'));
  689. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  690. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader','statusCode'));
  691. $RequestHandler->request = $this->getMock('CakeRequest');
  692. $RequestHandler->request->expects($this->once())->method('is')
  693. ->with('ajax')
  694. ->will($this->returnValue(true));
  695. $RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
  696. ob_start();
  697. $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
  698. $result = ob_get_clean();
  699. }
  700. /**
  701. * @expectedException CakeException
  702. * @return void
  703. */
  704. public function testAddInputTypeException() {
  705. $this->RequestHandler->addInputType('csv', array('I am not callable'));
  706. }
  707. }