RequestHandlerComponentTest.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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 = $this->getMockBuilder('Cake\Network\Request')
  436. ->setMethods(['_readInput'])
  437. ->getMock();
  438. $this->RequestHandler->beforeRender($event);
  439. $this->assertTrue(is_array($this->Controller->request->data));
  440. $this->assertFalse(is_object($this->Controller->request->data));
  441. }
  442. /**
  443. * testStartupCallback with charset.
  444. *
  445. * @return void
  446. * @triggers Controller.startup $this->Controller
  447. */
  448. public function testStartupCallbackCharset()
  449. {
  450. $event = new Event('Controller.startup', $this->Controller);
  451. $_SERVER['REQUEST_METHOD'] = 'PUT';
  452. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  453. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  454. ->setMethods(['_readInput'])
  455. ->getMock();
  456. $this->RequestHandler->startup($event);
  457. $this->assertTrue(is_array($this->Controller->request->data));
  458. $this->assertFalse(is_object($this->Controller->request->data));
  459. }
  460. /**
  461. * Test that processing data results in an array.
  462. *
  463. * @return void
  464. * @triggers Controller.startup $this->Controller
  465. */
  466. public function testStartupProcessData()
  467. {
  468. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  469. ->setMethods(['_readInput'])
  470. ->getMock();
  471. $this->Controller->request->expects($this->at(0))
  472. ->method('_readInput')
  473. ->will($this->returnValue(''));
  474. $this->Controller->request->expects($this->at(1))
  475. ->method('_readInput')
  476. ->will($this->returnValue('"invalid"'));
  477. $this->Controller->request->expects($this->at(2))
  478. ->method('_readInput')
  479. ->will($this->returnValue('{"valid":true}'));
  480. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  481. $this->Controller->request->env('CONTENT_TYPE', 'application/json');
  482. $event = new Event('Controller.startup', $this->Controller);
  483. $this->RequestHandler->startup($event);
  484. $this->assertEquals([], $this->Controller->request->data);
  485. $this->RequestHandler->startup($event);
  486. $this->assertEquals(['invalid'], $this->Controller->request->data);
  487. $this->RequestHandler->startup($event);
  488. $this->assertEquals(['valid' => true], $this->Controller->request->data);
  489. }
  490. /**
  491. * Test that file handles are ignored as XML data.
  492. *
  493. * @return void
  494. * @triggers Controller.startup $this->Controller
  495. */
  496. public function testStartupIgnoreFileAsXml()
  497. {
  498. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  499. ->setMethods(['_readInput'])
  500. ->getMock();
  501. $this->Controller->request->expects($this->any())
  502. ->method('_readInput')
  503. ->will($this->returnValue('/dev/random'));
  504. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  505. $this->Controller->request->env('CONTENT_TYPE', 'application/xml');
  506. $event = new Event('Controller.startup', $this->Controller);
  507. $this->RequestHandler->startup($event);
  508. $this->assertEquals([], $this->Controller->request->data);
  509. }
  510. /**
  511. * Test mapping a new type and having startup process it.
  512. *
  513. * @return void
  514. * @triggers Controller.startup $this->Controller
  515. */
  516. public function testStartupCustomTypeProcess()
  517. {
  518. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  519. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  520. ->setMethods(['_readInput'])
  521. ->getMock();
  522. $this->Controller->request->expects($this->once())
  523. ->method('_readInput')
  524. ->will($this->returnValue('"A","csv","string"'));
  525. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  526. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  527. $this->Controller->request->env('CONTENT_TYPE', 'text/csv');
  528. $event = new Event('Controller.startup', $this->Controller);
  529. $this->RequestHandler->startup($event);
  530. $expected = [
  531. 'A', 'csv', 'string'
  532. ];
  533. $this->assertEquals($expected, $this->Controller->request->data);
  534. error_reporting($restore);
  535. }
  536. /**
  537. * testNonAjaxRedirect method
  538. *
  539. * @return void
  540. * @triggers Controller.startup $this->Controller
  541. */
  542. public function testNonAjaxRedirect()
  543. {
  544. $event = new Event('Controller.startup', $this->Controller);
  545. $this->RequestHandler->initialize([]);
  546. $this->RequestHandler->startup($event);
  547. $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
  548. }
  549. /**
  550. * test that redirects with ajax and no URL don't do anything.
  551. *
  552. * @return void
  553. * @triggers Controller.startup $this->Controller
  554. */
  555. public function testAjaxRedirectWithNoUrl()
  556. {
  557. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  558. $event = new Event('Controller.startup', $this->Controller);
  559. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')->getMock();
  560. $this->Controller->response->expects($this->never())
  561. ->method('body');
  562. $this->RequestHandler->initialize([]);
  563. $this->RequestHandler->startup($event);
  564. $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
  565. }
  566. /**
  567. * testRenderAs method
  568. *
  569. * @return void
  570. */
  571. public function testRenderAs()
  572. {
  573. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  574. $this->RequestHandler->renderAs($this->Controller, 'rss');
  575. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  576. $this->Controller->viewBuilder()->templatePath('request_handler_test\\rss');
  577. $this->RequestHandler->renderAs($this->Controller, 'js');
  578. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
  579. }
  580. /**
  581. * test that attachment headers work with renderAs
  582. *
  583. * @return void
  584. */
  585. public function testRenderAsWithAttachment()
  586. {
  587. $this->RequestHandler->request = $this->getMockBuilder('Cake\Network\Request')
  588. ->setMethods(['parseAccept'])
  589. ->getMock();
  590. $this->RequestHandler->request->expects($this->any())
  591. ->method('parseAccept')
  592. ->will($this->returnValue(['1.0' => ['application/xml']]));
  593. $this->RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  594. ->setMethods(['type', 'download', 'charset'])
  595. ->getMock();
  596. $this->RequestHandler->response->expects($this->at(0))
  597. ->method('type')
  598. ->with('application/xml');
  599. $this->RequestHandler->response->expects($this->at(1))
  600. ->method('charset')
  601. ->with('UTF-8');
  602. $this->RequestHandler->response->expects($this->at(2))
  603. ->method('download')
  604. ->with('myfile.xml');
  605. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  606. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  607. }
  608. /**
  609. * test that respondAs works as expected.
  610. *
  611. * @return void
  612. */
  613. public function testRespondAs()
  614. {
  615. $this->RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  616. ->setMethods(['type'])
  617. ->getMock();
  618. $this->RequestHandler->response->expects($this->at(0))->method('type')
  619. ->with('application/json');
  620. $this->RequestHandler->response->expects($this->at(1))->method('type')
  621. ->with('text/xml');
  622. $result = $this->RequestHandler->respondAs('json');
  623. $this->assertTrue($result);
  624. $result = $this->RequestHandler->respondAs('text/xml');
  625. $this->assertTrue($result);
  626. }
  627. /**
  628. * test that attachment headers work with respondAs
  629. *
  630. * @return void
  631. */
  632. public function testRespondAsWithAttachment()
  633. {
  634. $this->RequestHandler = $this->getMockBuilder('Cake\Controller\Component\RequestHandlerComponent')
  635. ->setMethods(['_header'])
  636. ->setConstructorArgs([$this->Controller->components()])
  637. ->getMock();
  638. $this->RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  639. ->setMethods(['type', 'download'])
  640. ->getMock();
  641. $this->RequestHandler->request = $this->getMockBuilder('Cake\Network\Request')
  642. ->setMethods(['parseAccept'])
  643. ->getMock();
  644. $this->RequestHandler->request->expects($this->once())
  645. ->method('parseAccept')
  646. ->will($this->returnValue(['1.0' => ['application/xml']]));
  647. $this->RequestHandler->response->expects($this->once())->method('download')
  648. ->with('myfile.xml');
  649. $this->RequestHandler->response->expects($this->once())->method('type')
  650. ->with('application/xml');
  651. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  652. $this->assertTrue($result);
  653. }
  654. /**
  655. * test that calling renderAs() more than once continues to work.
  656. *
  657. * @link #6466
  658. * @return void
  659. */
  660. public function testRenderAsCalledTwice()
  661. {
  662. $this->Controller->eventManager()->on('Controller.beforeRender', function (\Cake\Event\Event $e) {
  663. return $e->subject()->response;
  664. });
  665. $this->Controller->render();
  666. $this->RequestHandler->renderAs($this->Controller, 'print');
  667. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewBuilder()->templatePath());
  668. $this->assertEquals('print', $this->Controller->viewBuilder()->layoutPath());
  669. $this->RequestHandler->renderAs($this->Controller, 'js');
  670. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewBuilder()->templatePath());
  671. $this->assertEquals('js', $this->Controller->viewBuilder()->layoutPath());
  672. }
  673. /**
  674. * testRequestContentTypes method
  675. *
  676. * @return void
  677. */
  678. public function testRequestContentTypes()
  679. {
  680. $this->request->env('REQUEST_METHOD', 'GET');
  681. $this->assertNull($this->RequestHandler->requestedWith());
  682. $this->request->env('REQUEST_METHOD', 'POST');
  683. $this->request->env('CONTENT_TYPE', 'application/json');
  684. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  685. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  686. $this->assertEquals('json', $result);
  687. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  688. $this->assertFalse($result);
  689. $this->request->env('REQUEST_METHOD', 'PATCH');
  690. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  691. $this->request->env('REQUEST_METHOD', 'DELETE');
  692. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  693. $this->request->env('REQUEST_METHOD', 'POST');
  694. $this->request->env('CONTENT_TYPE', '');
  695. $this->request->env('HTTP_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\Network\Request')
  733. ->setMethods(['is'])
  734. ->getMock();
  735. $request->expects($this->once())->method('is')
  736. ->with('mobile')
  737. ->will($this->returnValue(true));
  738. $this->RequestHandler->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. Configure::write('App.namespace', 'TestApp');
  810. Router::connect('/:controller/:action');
  811. $event = new Event('Controller.beforeRedirect', $this->Controller);
  812. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  813. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  814. ->setMethods(['is'])
  815. ->getMock();
  816. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  817. ->setMethods(['_sendHeader', 'stop'])
  818. ->getMock();
  819. $this->Controller->RequestHandler->request = $this->Controller->request;
  820. $this->Controller->RequestHandler->response = $this->Controller->response;
  821. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  822. $response = $this->Controller->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. Configure::write('App.namespace', 'TestApp');
  838. Router::connect('/:controller/:action');
  839. $event = new Event('Controller.beforeRedirect', $this->Controller);
  840. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  841. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  842. ->setMethods(['is'])
  843. ->getMock();
  844. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  845. ->setMethods(['_sendHeader', 'stop'])
  846. ->getMock();
  847. $this->Controller->RequestHandler->request = $this->Controller->request;
  848. $this->Controller->RequestHandler->response = $this->Controller->response;
  849. $this->Controller->request->expects($this->any())
  850. ->method('is')
  851. ->with('ajax')
  852. ->will($this->returnValue(true));
  853. $response = $this->Controller->RequestHandler->beforeRedirect(
  854. $event,
  855. '/request_action/params_pass?a=b&x=y?ish',
  856. $this->Controller->response
  857. );
  858. $data = json_decode($response, true);
  859. $this->assertEquals('/request_action/params_pass', $data['here']);
  860. $response = $this->Controller->RequestHandler->beforeRedirect(
  861. $event,
  862. '/request_action/query_pass?a=b&x=y?ish',
  863. $this->Controller->response
  864. );
  865. $data = json_decode($response, true);
  866. $this->assertEquals('y?ish', $data['x']);
  867. }
  868. /**
  869. * Test that AJAX requests involving redirects handle cookie data
  870. *
  871. * @return void
  872. * @triggers Controller.beforeRedirect $this->Controller
  873. */
  874. public function testAjaxRedirectAsRequestActionWithCookieData()
  875. {
  876. Configure::write('App.namespace', 'TestApp');
  877. Router::connect('/:controller/:action');
  878. $event = new Event('Controller.beforeRedirect', $this->Controller);
  879. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  880. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  881. ->setMethods(['is'])
  882. ->getMock();
  883. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  884. ->setMethods(['_sendHeader', 'stop'])
  885. ->getMock();
  886. $this->Controller->RequestHandler->request = $this->Controller->request;
  887. $this->Controller->RequestHandler->response = $this->Controller->response;
  888. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  889. $cookies = [
  890. 'foo' => 'bar'
  891. ];
  892. $this->Controller->request->cookies = $cookies;
  893. $response = $this->Controller->RequestHandler->beforeRedirect(
  894. $event,
  895. '/request_action/cookie_pass',
  896. $this->Controller->response
  897. );
  898. $data = json_decode($response, true);
  899. $this->assertEquals($cookies, $data);
  900. }
  901. /**
  902. * Tests that AJAX requests involving redirects don't let the status code bleed through.
  903. *
  904. * @return void
  905. * @triggers Controller.beforeRedirect $this->Controller
  906. */
  907. public function testAjaxRedirectAsRequestActionStatusCode()
  908. {
  909. Configure::write('App.namespace', 'TestApp');
  910. Router::connect('/:controller/:action');
  911. $event = new Event('Controller.beforeRedirect', $this->Controller);
  912. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  913. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  914. ->setMethods(['is'])
  915. ->getMock();
  916. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  917. ->setMethods(['_sendHeader', 'stop'])
  918. ->getMock();
  919. $this->Controller->response->statusCode(302);
  920. $this->Controller->RequestHandler->request = $this->Controller->request;
  921. $this->Controller->RequestHandler->response = $this->Controller->response;
  922. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  923. $response = $this->Controller->RequestHandler->beforeRedirect(
  924. $event,
  925. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  926. $this->Controller->response
  927. );
  928. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  929. $this->assertSame(200, $response->statusCode());
  930. }
  931. /**
  932. * test that ajax requests involving redirects don't force no layout
  933. * this would cause the ajax layout to not be rendered.
  934. *
  935. * @return void
  936. * @triggers Controller.beforeRedirect $this->Controller
  937. */
  938. public function testAjaxRedirectAsRequestActionStillRenderingLayout()
  939. {
  940. Configure::write('App.namespace', 'TestApp');
  941. Router::connect('/:controller/:action');
  942. $event = new Event('Controller.beforeRedirect', $this->Controller);
  943. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  944. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  945. ->setMethods(['is'])
  946. ->getMock();
  947. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  948. ->setMethods(['_sendHeader', 'stop'])
  949. ->getMock();
  950. $this->Controller->RequestHandler->request = $this->Controller->request;
  951. $this->Controller->RequestHandler->response = $this->Controller->response;
  952. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  953. $response = $this->Controller->RequestHandler->beforeRedirect(
  954. $event,
  955. ['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
  956. $this->Controller->response
  957. );
  958. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  959. $this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
  960. }
  961. /**
  962. * test that the beforeRedirect callback properly converts
  963. * array URLs into their correct string ones, and adds base => false so
  964. * the correct URLs are generated.
  965. *
  966. * @return void
  967. * @triggers Controller.beforeRender $this->Controller
  968. */
  969. public function testBeforeRedirectCallbackWithArrayUrl()
  970. {
  971. Configure::write('App.namespace', 'TestApp');
  972. Router::connect('/:controller/:action/*');
  973. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  974. $event = new Event('Controller.beforeRender', $this->Controller);
  975. Router::setRequestInfo([
  976. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  977. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  978. ]);
  979. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  980. $RequestHandler->request = new Request('posts/index');
  981. $RequestHandler->response = $this->Controller->response;
  982. ob_start();
  983. $RequestHandler->beforeRedirect(
  984. $event,
  985. ['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
  986. $this->Controller->response
  987. );
  988. $result = ob_get_clean();
  989. $this->assertEquals('one: first two: second', $result);
  990. }
  991. /**
  992. * testAddInputTypeException method
  993. *
  994. * @expectedException \Cake\Core\Exception\Exception
  995. * @return void
  996. */
  997. public function testAddInputTypeException()
  998. {
  999. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  1000. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  1001. error_reporting($restore);
  1002. }
  1003. /**
  1004. * Test checkNotModified method
  1005. *
  1006. * @return void
  1007. * @triggers Controller.beforeRender $this->Controller
  1008. */
  1009. public function testCheckNotModifiedByEtagStar()
  1010. {
  1011. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  1012. $event = new Event('Controller.beforeRender', $this->Controller);
  1013. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1014. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1015. ->setMethods(['notModified', 'stop'])
  1016. ->getMock();
  1017. $RequestHandler->response->etag('something');
  1018. $RequestHandler->response->expects($this->once())->method('notModified');
  1019. $this->assertFalse($RequestHandler->beforeRender($event));
  1020. }
  1021. /**
  1022. * Test checkNotModified method
  1023. *
  1024. * @return void
  1025. * @triggers Controller.beforeRender
  1026. */
  1027. public function testCheckNotModifiedByEtagExact()
  1028. {
  1029. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1030. $event = new Event('Controller.beforeRender');
  1031. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1032. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1033. ->setMethods(['notModified', 'stop'])
  1034. ->getMock();
  1035. $RequestHandler->response->etag('something', true);
  1036. $RequestHandler->response->expects($this->once())->method('notModified');
  1037. $this->assertFalse($RequestHandler->beforeRender($event));
  1038. }
  1039. /**
  1040. * Test checkNotModified method
  1041. *
  1042. * @return void
  1043. * @triggers Controller.beforeRender $this->Controller
  1044. */
  1045. public function testCheckNotModifiedByEtagAndTime()
  1046. {
  1047. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1048. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1049. $event = new Event('Controller.beforeRender', $this->Controller);
  1050. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1051. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1052. ->setMethods(['notModified', 'stop'])
  1053. ->getMock();
  1054. $RequestHandler->response->etag('something', true);
  1055. $RequestHandler->response->modified('2012-01-01 00:00:00');
  1056. $RequestHandler->response->expects($this->once())->method('notModified');
  1057. $this->assertFalse($RequestHandler->beforeRender($event));
  1058. }
  1059. /**
  1060. * Test checkNotModified method
  1061. *
  1062. * @return void
  1063. * @triggers Controller.beforeRender $this->Controller
  1064. */
  1065. public function testCheckNotModifiedNoInfo()
  1066. {
  1067. $event = new Event('Controller.beforeRender', $this->Controller);
  1068. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1069. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1070. ->setMethods(['notModified', 'stop'])
  1071. ->getMock();
  1072. $RequestHandler->response->expects($this->never())->method('notModified');
  1073. $this->assertNull($RequestHandler->beforeRender($event));
  1074. }
  1075. /**
  1076. * Test default options in construction
  1077. *
  1078. * @return void
  1079. */
  1080. public function testConstructDefaultOptions()
  1081. {
  1082. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1083. $viewClass = $requestHandler->config('viewClassMap');
  1084. $expected = [
  1085. 'json' => 'Json',
  1086. 'xml' => 'Xml',
  1087. 'ajax' => 'Ajax',
  1088. ];
  1089. $this->assertEquals($expected, $viewClass);
  1090. $inputs = $requestHandler->config('inputTypeMap');
  1091. $this->assertArrayHasKey('json', $inputs);
  1092. $this->assertArrayHasKey('xml', $inputs);
  1093. }
  1094. /**
  1095. * Test options in constructor replace defaults
  1096. *
  1097. * @return void
  1098. */
  1099. public function testConstructReplaceOptions()
  1100. {
  1101. $requestHandler = new RequestHandlerComponent(
  1102. $this->Controller->components(),
  1103. [
  1104. 'viewClassMap' => ['json' => 'Json'],
  1105. 'inputTypeMap' => ['json' => ['json_decode', true]]
  1106. ]
  1107. );
  1108. $viewClass = $requestHandler->config('viewClassMap');
  1109. $expected = [
  1110. 'json' => 'Json',
  1111. ];
  1112. $this->assertEquals($expected, $viewClass);
  1113. $inputs = $requestHandler->config('inputTypeMap');
  1114. $this->assertArrayHasKey('json', $inputs);
  1115. $this->assertCount(1, $inputs);
  1116. }
  1117. /**
  1118. * test beforeRender() doesn't override response type set in controller action
  1119. *
  1120. * @return void
  1121. */
  1122. public function testBeforeRender()
  1123. {
  1124. $this->Controller->set_response_type();
  1125. $event = new Event('Controller.beforeRender', $this->Controller);
  1126. $this->RequestHandler->beforeRender($event);
  1127. $this->assertEquals('text/plain', $this->Controller->response->type());
  1128. }
  1129. }