RequestHandlerComponentTest.php 45 KB

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