RequestHandlerComponentTest.php 45 KB

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