RequestHandlerComponentTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\RequestHandlerComponent;
  18. use Cake\Core\Configure;
  19. use Cake\Event\Event;
  20. use Cake\Network\Request;
  21. use Cake\Routing\DispatcherFactory;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use TestApp\Controller\RequestHandlerTestController;
  25. /**
  26. * RequestHandlerComponentTest class
  27. */
  28. class RequestHandlerComponentTest extends TestCase
  29. {
  30. /**
  31. * Controller property
  32. *
  33. * @var RequestHandlerTestController
  34. */
  35. public $Controller;
  36. /**
  37. * RequestHandler property
  38. *
  39. * @var RequestHandlerComponent
  40. */
  41. public $RequestHandler;
  42. /**
  43. * setUp method
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. parent::setUp();
  50. Configure::write('App.namespace', 'TestApp');
  51. DispatcherFactory::add('Routing');
  52. DispatcherFactory::add('ControllerFactory');
  53. $this->_init();
  54. }
  55. /**
  56. * init method
  57. *
  58. * @return void
  59. */
  60. protected function _init()
  61. {
  62. $request = new Request('controller_posts/index');
  63. $response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  64. $this->Controller = new RequestHandlerTestController($request, $response);
  65. $this->RequestHandler = $this->Controller->components()->load('RequestHandler');
  66. $this->request = $request;
  67. Router::scope('/', function ($routes) {
  68. $routes->extensions('json');
  69. $routes->fallbacks('InflectedRoute');
  70. });
  71. }
  72. /**
  73. * tearDown method
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. parent::tearDown();
  80. DispatcherFactory::clear();
  81. Router::reload();
  82. Router::$initialized = false;
  83. unset($this->RequestHandler, $this->Controller);
  84. }
  85. /**
  86. * Test that the constructor sets the config.
  87. *
  88. * @return void
  89. */
  90. public function testConstructorConfig()
  91. {
  92. $config = [
  93. 'viewClassMap' => ['json' => 'MyPlugin.MyJson']
  94. ];
  95. $controller = $this->getMock('Cake\Controller\Controller', ['redirect']);
  96. $collection = new ComponentRegistry($controller);
  97. $requestHandler = new RequestHandlerComponent($collection, $config);
  98. $this->assertEquals(['json' => 'MyPlugin.MyJson'], $requestHandler->config('viewClassMap'));
  99. }
  100. /**
  101. * testInitializeCallback method
  102. *
  103. * @return void
  104. */
  105. public function testInitializeCallback()
  106. {
  107. $this->assertNull($this->RequestHandler->ext);
  108. $this->Controller->request->params['_ext'] = 'rss';
  109. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  110. $this->assertEquals('rss', $this->RequestHandler->ext);
  111. }
  112. /**
  113. * test that a mapped Accept-type header will set $this->ext correctly.
  114. *
  115. * @return void
  116. */
  117. public function testInitializeContentTypeSettingExt()
  118. {
  119. Router::reload();
  120. Router::$initialized = true;
  121. $this->request->env('HTTP_ACCEPT', 'application/json');
  122. $this->RequestHandler->ext = null;
  123. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  124. $this->assertEquals('json', $this->RequestHandler->ext);
  125. }
  126. /**
  127. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
  128. *
  129. * @return void
  130. */
  131. public function testInitializeContentTypeWithjQueryAccept()
  132. {
  133. Router::reload();
  134. Router::$initialized = true;
  135. $this->request->env('HTTP_ACCEPT', 'application/json, application/javascript, */*; q=0.01');
  136. $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  137. $this->RequestHandler->ext = null;
  138. Router::extensions('json', false);
  139. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  140. $this->assertEquals('json', $this->RequestHandler->ext);
  141. }
  142. /**
  143. * Test that RequestHandler does not set extension to csv for text/plain mimetype
  144. *
  145. * @return void
  146. */
  147. public function testInitializeContentTypeWithjQueryTextPlainAccept()
  148. {
  149. Router::reload();
  150. Router::$initialized = true;
  151. $this->request->env('HTTP_ACCEPT', 'text/plain, */*; q=0.01');
  152. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  153. $this->assertNull($this->RequestHandler->ext);
  154. }
  155. /**
  156. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
  157. * and the application is configured to handle multiple extensions
  158. *
  159. * @return void
  160. */
  161. public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions()
  162. {
  163. Router::reload();
  164. Router::$initialized = true;
  165. $this->request->env('HTTP_ACCEPT', 'application/json, application/javascript, */*; q=0.01');
  166. $this->RequestHandler->ext = null;
  167. Router::extensions(['rss', 'json'], false);
  168. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  169. $this->assertEquals('json', $this->RequestHandler->ext);
  170. }
  171. /**
  172. * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
  173. *
  174. * @return void
  175. */
  176. public function testInitializeNoContentTypeWithSingleAccept()
  177. {
  178. Router::reload();
  179. Router::$initialized = true;
  180. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
  181. $this->assertNull($this->RequestHandler->ext);
  182. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  183. $this->assertNull($this->RequestHandler->ext);
  184. }
  185. /**
  186. * Test that ext is set to the first listed extension with multiple accepted
  187. * content types.
  188. * Having multiple types accepted with same weight, means the client lets the
  189. * server choose the returned content type.
  190. *
  191. * @return void
  192. */
  193. public function testInitializeNoContentTypeWithMultipleAcceptedTypes()
  194. {
  195. $this->request->env(
  196. 'HTTP_ACCEPT',
  197. 'application/json, application/javascript, application/xml, */*; q=0.01'
  198. );
  199. $this->RequestHandler->ext = null;
  200. Router::extensions(['xml', 'json'], false);
  201. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  202. $this->assertEquals('xml', $this->RequestHandler->ext);
  203. $this->RequestHandler->ext = null;
  204. Router::extensions(['json', 'xml'], false);
  205. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  206. $this->assertEquals('json', $this->RequestHandler->ext);
  207. }
  208. /**
  209. * Test that ext is set to type with highest weight
  210. *
  211. * @return void
  212. */
  213. public function testInitializeContentTypeWithMultipleAcceptedTypes()
  214. {
  215. Router::reload();
  216. Router::$initialized = true;
  217. $this->request->env(
  218. 'HTTP_ACCEPT',
  219. 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7'
  220. );
  221. $this->RequestHandler->ext = null;
  222. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  223. $this->assertEquals('json', $this->RequestHandler->ext);
  224. }
  225. /**
  226. * Test that ext is not set with confusing android accepts headers.
  227. *
  228. * @return void
  229. */
  230. public function testInitializeAmbiguousAndroidAccepts()
  231. {
  232. Router::reload();
  233. Router::$initialized = true;
  234. $this->request->env(
  235. 'HTTP_ACCEPT',
  236. 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  237. );
  238. $this->RequestHandler->ext = null;
  239. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  240. $this->assertNull($this->RequestHandler->ext);
  241. }
  242. /**
  243. * Test that the headers sent by firefox are not treated as XML requests.
  244. *
  245. * @return void
  246. */
  247. public function testInititalizeFirefoxHeaderNotXml()
  248. {
  249. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  250. Router::extensions(['xml', 'json'], false);
  251. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  252. $this->assertNull($this->RequestHandler->ext);
  253. }
  254. /**
  255. * Test that a type mismatch doesn't incorrectly set the ext
  256. *
  257. * @return void
  258. */
  259. public function testInitializeContentTypeAndExtensionMismatch()
  260. {
  261. $this->assertNull($this->RequestHandler->ext);
  262. $extensions = Router::extensions();
  263. Router::extensions('xml', false);
  264. $this->Controller->request = $this->getMock('Cake\Network\Request', ['accepts']);
  265. $this->Controller->request->expects($this->any())
  266. ->method('accepts')
  267. ->will($this->returnValue(['application/json']));
  268. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  269. $this->assertNull($this->RequestHandler->ext);
  270. call_user_func_array(['Cake\Routing\Router', 'extensions'], [$extensions, false]);
  271. }
  272. /**
  273. * testViewClassMap method
  274. *
  275. * @return void
  276. */
  277. public function testViewClassMap()
  278. {
  279. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  280. $this->RequestHandler->config(['viewClassMap' => ['json' => 'CustomJson']]);
  281. $this->RequestHandler->initialize([]);
  282. $result = $this->RequestHandler->viewClassMap();
  283. $expected = [
  284. 'json' => 'CustomJson',
  285. 'xml' => 'Xml',
  286. 'ajax' => 'Ajax'
  287. ];
  288. $this->assertEquals($expected, $result);
  289. $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
  290. $expected = [
  291. 'json' => 'CustomJson',
  292. 'xml' => 'Xml',
  293. 'ajax' => 'Ajax',
  294. 'xls' => 'Excel.Excel'
  295. ];
  296. $this->assertEquals($expected, $result);
  297. $this->RequestHandler->renderAs($this->Controller, 'json');
  298. $this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewClass);
  299. error_reporting($restore);
  300. }
  301. /**
  302. * Verify that isAjax is set on the request params for ajax requests
  303. *
  304. * @return void
  305. * @triggers Controller.startup $this->Controller
  306. */
  307. public function testIsAjaxParams()
  308. {
  309. $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  310. $event = new Event('Controller.startup', $this->Controller);
  311. $this->RequestHandler->initialize([]);
  312. $this->Controller->beforeFilter($event);
  313. $this->RequestHandler->startup($event);
  314. $this->assertEquals(true, $this->Controller->request->params['isAjax']);
  315. }
  316. /**
  317. * testAutoAjaxLayout method
  318. *
  319. * @return void
  320. * @triggers Controller.startup $this->Controller
  321. */
  322. public function testAutoAjaxLayout()
  323. {
  324. $event = new Event('Controller.startup', $this->Controller);
  325. $this->request->env('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
  326. $this->RequestHandler->initialize([]);
  327. $this->RequestHandler->startup($event);
  328. $event = new Event('Controller.beforeRender', $this->Controller);
  329. $this->RequestHandler->beforeRender($event);
  330. $this->assertEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  331. $view = $this->Controller->createView();
  332. $this->assertEquals('ajax', $view->layout);
  333. $this->_init();
  334. $this->Controller->request->params['_ext'] = 'js';
  335. $this->RequestHandler->initialize([]);
  336. $this->RequestHandler->startup($event);
  337. $this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  338. }
  339. /**
  340. * test custom JsonView class is loaded and correct.
  341. *
  342. * @return void
  343. * @triggers Controller.startup $this->Controller
  344. */
  345. public function testJsonViewLoaded()
  346. {
  347. Router::extensions(['json', 'xml', 'ajax'], false);
  348. $this->Controller->request->params['_ext'] = 'json';
  349. $event = new Event('Controller.startup', $this->Controller);
  350. $this->RequestHandler->initialize([]);
  351. $this->RequestHandler->startup($event);
  352. $event = new Event('Controller.beforeRender', $this->Controller);
  353. $this->RequestHandler->beforeRender($event);
  354. $this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
  355. $view = $this->Controller->createView();
  356. $this->assertEquals('json', $view->layoutPath);
  357. $this->assertEquals('json', $view->subDir);
  358. }
  359. /**
  360. * test custom XmlView class is loaded and correct.
  361. *
  362. * @return void
  363. * @triggers Controller.startup $this->Controller
  364. */
  365. public function testXmlViewLoaded()
  366. {
  367. Router::extensions(['json', 'xml', 'ajax'], false);
  368. $this->Controller->request->params['_ext'] = 'xml';
  369. $event = new Event('Controller.startup', $this->Controller);
  370. $this->RequestHandler->initialize([]);
  371. $this->RequestHandler->startup($event);
  372. $event = new Event('Controller.beforeRender', $this->Controller);
  373. $this->RequestHandler->beforeRender($event);
  374. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  375. $view = $this->Controller->createView();
  376. $this->assertEquals('xml', $view->layoutPath);
  377. $this->assertEquals('xml', $view->subDir);
  378. }
  379. /**
  380. * test custom AjaxView class is loaded and correct.
  381. *
  382. * @return void
  383. * @triggers Controller.startup $this->Controller
  384. */
  385. public function testAjaxViewLoaded()
  386. {
  387. Router::extensions(['json', 'xml', 'ajax'], false);
  388. $this->Controller->request->params['_ext'] = 'ajax';
  389. $event = new Event('Controller.startup', $this->Controller);
  390. $this->RequestHandler->initialize([]);
  391. $this->RequestHandler->startup($event);
  392. $event = new Event('Controller.beforeRender', $this->Controller);
  393. $this->RequestHandler->beforeRender($event);
  394. $this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
  395. $view = $this->Controller->createView();
  396. $this->assertEquals('ajax', $view->layout);
  397. }
  398. /**
  399. * test configured extension but no view class set.
  400. *
  401. * @return void
  402. * @triggers Controller.beforeRender $this->Controller
  403. */
  404. public function testNoViewClassExtension()
  405. {
  406. Router::extensions(['json', 'xml', 'ajax', 'csv'], false);
  407. $this->Controller->request->params['_ext'] = 'csv';
  408. $event = new Event('Controller.startup', $this->Controller);
  409. $this->RequestHandler->initialize([]);
  410. $this->RequestHandler->startup($event);
  411. $this->Controller->eventManager()->on('Controller.beforeRender', function () {
  412. return $this->Controller->response;
  413. });
  414. $this->Controller->render();
  415. $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewBuilder()->templatePath());
  416. $this->assertEquals('csv', $this->Controller->viewBuilder()->layoutPath());
  417. }
  418. /**
  419. * testStartupCallback method
  420. *
  421. * @return void
  422. * @triggers Controller.beforeRender $this->Controller
  423. */
  424. public function testStartupCallback()
  425. {
  426. $event = new Event('Controller.beforeRender', $this->Controller);
  427. $_SERVER['REQUEST_METHOD'] = 'PUT';
  428. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  429. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  430. $this->RequestHandler->beforeRender($event);
  431. $this->assertTrue(is_array($this->Controller->request->data));
  432. $this->assertFalse(is_object($this->Controller->request->data));
  433. }
  434. /**
  435. * testStartupCallback with charset.
  436. *
  437. * @return void
  438. * @triggers Controller.startup $this->Controller
  439. */
  440. public function testStartupCallbackCharset()
  441. {
  442. $event = new Event('Controller.startup', $this->Controller);
  443. $_SERVER['REQUEST_METHOD'] = 'PUT';
  444. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  445. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  446. $this->RequestHandler->startup($event);
  447. $this->assertTrue(is_array($this->Controller->request->data));
  448. $this->assertFalse(is_object($this->Controller->request->data));
  449. }
  450. /**
  451. * Test that processing data results in an array.
  452. *
  453. * @return void
  454. * @triggers Controller.startup $this->Controller
  455. */
  456. public function testStartupProcessData()
  457. {
  458. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  459. $this->Controller->request->expects($this->at(0))
  460. ->method('_readInput')
  461. ->will($this->returnValue(''));
  462. $this->Controller->request->expects($this->at(1))
  463. ->method('_readInput')
  464. ->will($this->returnValue('"invalid"'));
  465. $this->Controller->request->expects($this->at(2))
  466. ->method('_readInput')
  467. ->will($this->returnValue('{"valid":true}'));
  468. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  469. $this->Controller->request->env('CONTENT_TYPE', 'application/json');
  470. $event = new Event('Controller.startup', $this->Controller);
  471. $this->RequestHandler->startup($event);
  472. $this->assertEquals([], $this->Controller->request->data);
  473. $this->RequestHandler->startup($event);
  474. $this->assertEquals(['invalid'], $this->Controller->request->data);
  475. $this->RequestHandler->startup($event);
  476. $this->assertEquals(['valid' => true], $this->Controller->request->data);
  477. }
  478. /**
  479. * Test that file handles are ignored as XML data.
  480. *
  481. * @return void
  482. * @triggers Controller.startup $this->Controller
  483. */
  484. public function testStartupIgnoreFileAsXml()
  485. {
  486. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  487. $this->Controller->request->expects($this->any())
  488. ->method('_readInput')
  489. ->will($this->returnValue('/dev/random'));
  490. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  491. $this->Controller->request->env('CONTENT_TYPE', 'application/xml');
  492. $event = new Event('Controller.startup', $this->Controller);
  493. $this->RequestHandler->startup($event);
  494. $this->assertEquals([], $this->Controller->request->data);
  495. }
  496. /**
  497. * Test mapping a new type and having startup process it.
  498. *
  499. * @return void
  500. * @triggers Controller.startup $this->Controller
  501. */
  502. public function testStartupCustomTypeProcess()
  503. {
  504. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  505. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  506. $this->Controller->request->expects($this->once())
  507. ->method('_readInput')
  508. ->will($this->returnValue('"A","csv","string"'));
  509. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  510. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  511. $this->Controller->request->env('CONTENT_TYPE', 'text/csv');
  512. $event = new Event('Controller.startup', $this->Controller);
  513. $this->RequestHandler->startup($event);
  514. $expected = [
  515. 'A', 'csv', 'string'
  516. ];
  517. $this->assertEquals($expected, $this->Controller->request->data);
  518. error_reporting($restore);
  519. }
  520. /**
  521. * testNonAjaxRedirect method
  522. *
  523. * @return void
  524. * @triggers Controller.startup $this->Controller
  525. */
  526. public function testNonAjaxRedirect()
  527. {
  528. $event = new Event('Controller.startup', $this->Controller);
  529. $this->RequestHandler->initialize([]);
  530. $this->RequestHandler->startup($event);
  531. $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
  532. }
  533. /**
  534. * test that redirects with ajax and no URL don't do anything.
  535. *
  536. * @return void
  537. * @triggers Controller.startup $this->Controller
  538. */
  539. public function testAjaxRedirectWithNoUrl()
  540. {
  541. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  542. $event = new Event('Controller.startup', $this->Controller);
  543. $this->Controller->response = $this->getMock('Cake\Network\Response');
  544. $this->Controller->response->expects($this->never())
  545. ->method('body');
  546. $this->RequestHandler->initialize([]);
  547. $this->RequestHandler->startup($event);
  548. $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
  549. }
  550. /**
  551. * testRenderAs method
  552. *
  553. * @return void
  554. */
  555. public function testRenderAs()
  556. {
  557. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  558. $this->RequestHandler->renderAs($this->Controller, 'rss');
  559. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  560. $this->Controller->viewBuilder()->templatePath('request_handler_test\\rss');
  561. $this->RequestHandler->renderAs($this->Controller, 'js');
  562. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
  563. }
  564. /**
  565. * test that attachment headers work with renderAs
  566. *
  567. * @return void
  568. */
  569. public function testRenderAsWithAttachment()
  570. {
  571. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  572. $this->RequestHandler->request->expects($this->any())
  573. ->method('parseAccept')
  574. ->will($this->returnValue(['1.0' => ['application/xml']]));
  575. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download', 'charset']);
  576. $this->RequestHandler->response->expects($this->at(0))
  577. ->method('type')
  578. ->with('application/xml');
  579. $this->RequestHandler->response->expects($this->at(1))
  580. ->method('charset')
  581. ->with('UTF-8');
  582. $this->RequestHandler->response->expects($this->at(2))
  583. ->method('download')
  584. ->with('myfile.xml');
  585. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  586. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  587. }
  588. /**
  589. * test that respondAs works as expected.
  590. *
  591. * @return void
  592. */
  593. public function testRespondAs()
  594. {
  595. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type']);
  596. $this->RequestHandler->response->expects($this->at(0))->method('type')
  597. ->with('application/json');
  598. $this->RequestHandler->response->expects($this->at(1))->method('type')
  599. ->with('text/xml');
  600. $result = $this->RequestHandler->respondAs('json');
  601. $this->assertTrue($result);
  602. $result = $this->RequestHandler->respondAs('text/xml');
  603. $this->assertTrue($result);
  604. }
  605. /**
  606. * test that attachment headers work with respondAs
  607. *
  608. * @return void
  609. */
  610. public function testRespondAsWithAttachment()
  611. {
  612. $this->RequestHandler = $this->getMock(
  613. 'Cake\Controller\Component\RequestHandlerComponent',
  614. ['_header'],
  615. [$this->Controller->components()]
  616. );
  617. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download']);
  618. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  619. $this->RequestHandler->request->expects($this->once())
  620. ->method('parseAccept')
  621. ->will($this->returnValue(['1.0' => ['application/xml']]));
  622. $this->RequestHandler->response->expects($this->once())->method('download')
  623. ->with('myfile.xml');
  624. $this->RequestHandler->response->expects($this->once())->method('type')
  625. ->with('application/xml');
  626. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  627. $this->assertTrue($result);
  628. }
  629. /**
  630. * test that calling renderAs() more than once continues to work.
  631. *
  632. * @link #6466
  633. * @return void
  634. */
  635. public function testRenderAsCalledTwice()
  636. {
  637. $this->Controller->eventManager()->on('Controller.beforeRender', function (\Cake\Event\Event $e) {
  638. return $e->subject()->response;
  639. });
  640. $this->Controller->render();
  641. $this->RequestHandler->renderAs($this->Controller, 'print');
  642. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewBuilder()->templatePath());
  643. $this->assertEquals('print', $this->Controller->viewBuilder()->layoutPath());
  644. $this->RequestHandler->renderAs($this->Controller, 'js');
  645. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
  646. $this->assertEquals('js', $this->Controller->viewBuilder()->layoutPath());
  647. }
  648. /**
  649. * testRequestContentTypes method
  650. *
  651. * @return void
  652. */
  653. public function testRequestContentTypes()
  654. {
  655. $this->request->env('REQUEST_METHOD', 'GET');
  656. $this->assertNull($this->RequestHandler->requestedWith());
  657. $this->request->env('REQUEST_METHOD', 'POST');
  658. $this->request->env('CONTENT_TYPE', 'application/json');
  659. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  660. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  661. $this->assertEquals('json', $result);
  662. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  663. $this->assertFalse($result);
  664. $this->request->env('REQUEST_METHOD', 'PATCH');
  665. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  666. $this->request->env('REQUEST_METHOD', 'DELETE');
  667. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  668. $this->request->env('REQUEST_METHOD', 'POST');
  669. $this->request->env('CONTENT_TYPE', '');
  670. $this->request->env('HTTP_CONTENT_TYPE', 'application/json');
  671. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  672. $this->assertEquals('json', $result);
  673. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  674. $this->assertFalse($result);
  675. $this->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  676. $this->assertTrue($this->RequestHandler->isXml());
  677. $this->assertFalse($this->RequestHandler->isAtom());
  678. $this->assertFalse($this->RequestHandler->isRSS());
  679. $this->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  680. $this->assertTrue($this->RequestHandler->isAtom());
  681. $this->assertFalse($this->RequestHandler->isRSS());
  682. $this->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  683. $this->assertFalse($this->RequestHandler->isAtom());
  684. $this->assertTrue($this->RequestHandler->isRSS());
  685. $this->assertFalse($this->RequestHandler->isWap());
  686. $this->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  687. $this->assertTrue($this->RequestHandler->isWap());
  688. }
  689. /**
  690. * testResponseContentType method
  691. *
  692. * @return void
  693. */
  694. public function testResponseContentType()
  695. {
  696. $this->assertEquals('html', $this->RequestHandler->responseType());
  697. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  698. $this->assertEquals('atom', $this->RequestHandler->responseType());
  699. }
  700. /**
  701. * testMobileDeviceDetection method
  702. *
  703. * @return void
  704. */
  705. public function testMobileDeviceDetection()
  706. {
  707. $request = $this->getMock('Cake\Network\Request', ['is']);
  708. $request->expects($this->once())->method('is')
  709. ->with('mobile')
  710. ->will($this->returnValue(true));
  711. $this->RequestHandler->request = $request;
  712. $this->assertTrue($this->RequestHandler->isMobile());
  713. }
  714. /**
  715. * test that map alias converts aliases to content types.
  716. *
  717. * @return void
  718. */
  719. public function testMapAlias()
  720. {
  721. $result = $this->RequestHandler->mapAlias('xml');
  722. $this->assertEquals('application/xml', $result);
  723. $result = $this->RequestHandler->mapAlias('text/html');
  724. $this->assertNull($result);
  725. $result = $this->RequestHandler->mapAlias('wap');
  726. $this->assertEquals('text/vnd.wap.wml', $result);
  727. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  728. $expected = ['application/xml', 'application/javascript', 'application/json'];
  729. $this->assertEquals($expected, $result);
  730. }
  731. /**
  732. * test accepts() on the component
  733. *
  734. * @return void
  735. */
  736. public function testAccepts()
  737. {
  738. $this->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5');
  739. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  740. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  741. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  742. $this->assertFalse($this->RequestHandler->accepts('rss'));
  743. }
  744. /**
  745. * test accepts and prefers methods.
  746. *
  747. * @return void
  748. */
  749. public function testPrefers()
  750. {
  751. $this->request->env(
  752. 'HTTP_ACCEPT',
  753. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  754. );
  755. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  756. $this->RequestHandler->ext = 'rss';
  757. $this->assertEquals('rss', $this->RequestHandler->prefers());
  758. $this->assertFalse($this->RequestHandler->prefers('xml'));
  759. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  760. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  761. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  762. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  763. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  764. $this->_init();
  765. $this->request->env(
  766. 'HTTP_ACCEPT',
  767. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  768. );
  769. $this->assertEquals('xml', $this->RequestHandler->prefers());
  770. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  771. $this->assertEquals('html', $this->RequestHandler->prefers());
  772. $this->assertFalse($this->RequestHandler->prefers('rss'));
  773. }
  774. /**
  775. * test that AJAX requests involving redirects trigger requestAction instead.
  776. *
  777. * @return void
  778. * @triggers Controller.beforeRedirect $this->Controller
  779. */
  780. public function testAjaxRedirectAsRequestAction()
  781. {
  782. Configure::write('App.namespace', 'TestApp');
  783. Router::connect('/:controller/:action');
  784. $event = new Event('Controller.beforeRedirect', $this->Controller);
  785. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  786. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  787. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  788. $this->Controller->RequestHandler->request = $this->Controller->request;
  789. $this->Controller->RequestHandler->response = $this->Controller->response;
  790. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  791. $response = $this->Controller->RequestHandler->beforeRedirect(
  792. $event,
  793. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  794. $this->Controller->response
  795. );
  796. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  797. }
  798. /**
  799. * Test that AJAX requests involving redirects handle querystrings
  800. *
  801. * @return void
  802. * @triggers Controller.beforeRedirect $this->Controller
  803. */
  804. public function testAjaxRedirectAsRequestActionWithQueryString()
  805. {
  806. Configure::write('App.namespace', 'TestApp');
  807. Router::connect('/:controller/:action');
  808. $event = new Event('Controller.beforeRedirect', $this->Controller);
  809. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  810. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  811. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  812. $this->Controller->RequestHandler->request = $this->Controller->request;
  813. $this->Controller->RequestHandler->response = $this->Controller->response;
  814. $this->Controller->request->expects($this->any())
  815. ->method('is')
  816. ->with('ajax')
  817. ->will($this->returnValue(true));
  818. $response = $this->Controller->RequestHandler->beforeRedirect(
  819. $event,
  820. '/request_action/params_pass?a=b&x=y?ish',
  821. $this->Controller->response
  822. );
  823. $data = json_decode($response, true);
  824. $this->assertEquals('/request_action/params_pass', $data['here']);
  825. $response = $this->Controller->RequestHandler->beforeRedirect(
  826. $event,
  827. '/request_action/query_pass?a=b&x=y?ish',
  828. $this->Controller->response
  829. );
  830. $data = json_decode($response, true);
  831. $this->assertEquals('y?ish', $data['x']);
  832. }
  833. /**
  834. * Test that AJAX requests involving redirects handle cookie data
  835. *
  836. * @return void
  837. * @triggers Controller.beforeRedirect $this->Controller
  838. */
  839. public function testAjaxRedirectAsRequestActionWithCookieData()
  840. {
  841. Configure::write('App.namespace', 'TestApp');
  842. Router::connect('/:controller/:action');
  843. $event = new Event('Controller.beforeRedirect', $this->Controller);
  844. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  845. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  846. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  847. $this->Controller->RequestHandler->request = $this->Controller->request;
  848. $this->Controller->RequestHandler->response = $this->Controller->response;
  849. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  850. $cookies = [
  851. 'foo' => 'bar'
  852. ];
  853. $this->Controller->request->cookies = $cookies;
  854. $response = $this->Controller->RequestHandler->beforeRedirect(
  855. $event,
  856. '/request_action/cookie_pass',
  857. $this->Controller->response
  858. );
  859. $data = json_decode($response, true);
  860. $this->assertEquals($cookies, $data);
  861. }
  862. /**
  863. * Tests that AJAX requests involving redirects don't let the status code bleed through.
  864. *
  865. * @return void
  866. * @triggers Controller.beforeRedirect $this->Controller
  867. */
  868. public function testAjaxRedirectAsRequestActionStatusCode()
  869. {
  870. Configure::write('App.namespace', 'TestApp');
  871. Router::connect('/:controller/:action');
  872. $event = new Event('Controller.beforeRedirect', $this->Controller);
  873. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  874. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  875. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  876. $this->Controller->response->statusCode(302);
  877. $this->Controller->RequestHandler->request = $this->Controller->request;
  878. $this->Controller->RequestHandler->response = $this->Controller->response;
  879. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  880. $response = $this->Controller->RequestHandler->beforeRedirect(
  881. $event,
  882. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  883. $this->Controller->response
  884. );
  885. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  886. $this->assertSame(200, $response->statusCode());
  887. }
  888. /**
  889. * test that ajax requests involving redirects don't force no layout
  890. * this would cause the ajax layout to not be rendered.
  891. *
  892. * @return void
  893. * @triggers Controller.beforeRedirect $this->Controller
  894. */
  895. public function testAjaxRedirectAsRequestActionStillRenderingLayout()
  896. {
  897. Configure::write('App.namespace', 'TestApp');
  898. Router::connect('/:controller/:action');
  899. $event = new Event('Controller.beforeRedirect', $this->Controller);
  900. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  901. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  902. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  903. $this->Controller->RequestHandler->request = $this->Controller->request;
  904. $this->Controller->RequestHandler->response = $this->Controller->response;
  905. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  906. $response = $this->Controller->RequestHandler->beforeRedirect(
  907. $event,
  908. ['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
  909. $this->Controller->response
  910. );
  911. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  912. $this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
  913. }
  914. /**
  915. * test that the beforeRedirect callback properly converts
  916. * array URLs into their correct string ones, and adds base => false so
  917. * the correct URLs are generated.
  918. *
  919. * @return void
  920. * @triggers Controller.beforeRender $this->Controller
  921. */
  922. public function testBeforeRedirectCallbackWithArrayUrl()
  923. {
  924. Configure::write('App.namespace', 'TestApp');
  925. Router::connect('/:controller/:action/*');
  926. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  927. $event = new Event('Controller.beforeRender', $this->Controller);
  928. Router::setRequestInfo([
  929. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  930. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  931. ]);
  932. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  933. $RequestHandler->request = new Request('posts/index');
  934. $RequestHandler->response = $this->Controller->response;
  935. ob_start();
  936. $RequestHandler->beforeRedirect(
  937. $event,
  938. ['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
  939. $this->Controller->response
  940. );
  941. $result = ob_get_clean();
  942. $this->assertEquals('one: first two: second', $result);
  943. }
  944. /**
  945. * testAddInputTypeException method
  946. *
  947. * @expectedException \Cake\Core\Exception\Exception
  948. * @return void
  949. */
  950. public function testAddInputTypeException()
  951. {
  952. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  953. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  954. error_reporting($restore);
  955. }
  956. /**
  957. * Test checkNotModified method
  958. *
  959. * @return void
  960. * @triggers Controller.beforeRender $this->Controller
  961. */
  962. public function testCheckNotModifiedByEtagStar()
  963. {
  964. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  965. $event = new Event('Controller.beforeRender', $this->Controller);
  966. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  967. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  968. $RequestHandler->response->etag('something');
  969. $RequestHandler->response->expects($this->once())->method('notModified');
  970. $this->assertFalse($RequestHandler->beforeRender($event));
  971. }
  972. /**
  973. * Test checkNotModified method
  974. *
  975. * @return void
  976. * @triggers Controller.beforeRender
  977. */
  978. public function testCheckNotModifiedByEtagExact()
  979. {
  980. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  981. $event = new Event('Controller.beforeRender');
  982. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  983. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  984. $RequestHandler->response->etag('something', true);
  985. $RequestHandler->response->expects($this->once())->method('notModified');
  986. $this->assertFalse($RequestHandler->beforeRender($event));
  987. }
  988. /**
  989. * Test checkNotModified method
  990. *
  991. * @return void
  992. * @triggers Controller.beforeRender $this->Controller
  993. */
  994. public function testCheckNotModifiedByEtagAndTime()
  995. {
  996. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  997. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  998. $event = new Event('Controller.beforeRender', $this->Controller);
  999. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1000. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  1001. $RequestHandler->response->etag('something', true);
  1002. $RequestHandler->response->modified('2012-01-01 00:00:00');
  1003. $RequestHandler->response->expects($this->once())->method('notModified');
  1004. $this->assertFalse($RequestHandler->beforeRender($event));
  1005. }
  1006. /**
  1007. * Test checkNotModified method
  1008. *
  1009. * @return void
  1010. * @triggers Controller.beforeRender $this->Controller
  1011. */
  1012. public function testCheckNotModifiedNoInfo()
  1013. {
  1014. $event = new Event('Controller.beforeRender', $this->Controller);
  1015. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1016. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  1017. $RequestHandler->response->expects($this->never())->method('notModified');
  1018. $this->assertNull($RequestHandler->beforeRender($event));
  1019. }
  1020. /**
  1021. * Test default options in construction
  1022. *
  1023. * @return void
  1024. */
  1025. public function testConstructDefaultOptions()
  1026. {
  1027. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1028. $viewClass = $requestHandler->config('viewClassMap');
  1029. $expected = [
  1030. 'json' => 'Json',
  1031. 'xml' => 'Xml',
  1032. 'ajax' => 'Ajax',
  1033. ];
  1034. $this->assertEquals($expected, $viewClass);
  1035. $inputs = $requestHandler->config('inputTypeMap');
  1036. $this->assertArrayHasKey('json', $inputs);
  1037. $this->assertArrayHasKey('xml', $inputs);
  1038. }
  1039. /**
  1040. * Test options in constructor replace defaults
  1041. *
  1042. * @return void
  1043. */
  1044. public function testConstructReplaceOptions()
  1045. {
  1046. $requestHandler = new RequestHandlerComponent(
  1047. $this->Controller->components(),
  1048. [
  1049. 'viewClassMap' => ['json' => 'Json'],
  1050. 'inputTypeMap' => ['json' => ['json_decode', true]]
  1051. ]
  1052. );
  1053. $viewClass = $requestHandler->config('viewClassMap');
  1054. $expected = [
  1055. 'json' => 'Json',
  1056. ];
  1057. $this->assertEquals($expected, $viewClass);
  1058. $inputs = $requestHandler->config('inputTypeMap');
  1059. $this->assertArrayHasKey('json', $inputs);
  1060. $this->assertCount(1, $inputs);
  1061. }
  1062. }