RequestHandlerComponentTest.php 45 KB

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