RequestHandlerComponentTest.php 45 KB

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