RequestHandlerComponentTest.php 45 KB

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