RequestHandlerComponentTest.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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', 'application/json');
  695. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  696. $this->assertEquals('json', $result);
  697. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  698. $this->assertFalse($result);
  699. $this->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  700. $this->assertTrue($this->RequestHandler->isXml());
  701. $this->assertFalse($this->RequestHandler->isAtom());
  702. $this->assertFalse($this->RequestHandler->isRSS());
  703. $this->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  704. $this->assertTrue($this->RequestHandler->isAtom());
  705. $this->assertFalse($this->RequestHandler->isRSS());
  706. $this->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  707. $this->assertFalse($this->RequestHandler->isAtom());
  708. $this->assertTrue($this->RequestHandler->isRSS());
  709. $this->assertFalse($this->RequestHandler->isWap());
  710. $this->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  711. $this->assertTrue($this->RequestHandler->isWap());
  712. }
  713. /**
  714. * testResponseContentType method
  715. *
  716. * @return void
  717. */
  718. public function testResponseContentType()
  719. {
  720. $this->assertEquals('html', $this->RequestHandler->responseType());
  721. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  722. $this->assertEquals('atom', $this->RequestHandler->responseType());
  723. }
  724. /**
  725. * testMobileDeviceDetection method
  726. *
  727. * @return void
  728. */
  729. public function testMobileDeviceDetection()
  730. {
  731. $request = $this->getMockBuilder('Cake\Network\Request')
  732. ->setMethods(['is'])
  733. ->getMock();
  734. $request->expects($this->once())->method('is')
  735. ->with('mobile')
  736. ->will($this->returnValue(true));
  737. $this->RequestHandler->request = $request;
  738. $this->assertTrue($this->RequestHandler->isMobile());
  739. }
  740. /**
  741. * test that map alias converts aliases to content types.
  742. *
  743. * @return void
  744. */
  745. public function testMapAlias()
  746. {
  747. $result = $this->RequestHandler->mapAlias('xml');
  748. $this->assertEquals('application/xml', $result);
  749. $result = $this->RequestHandler->mapAlias('text/html');
  750. $this->assertNull($result);
  751. $result = $this->RequestHandler->mapAlias('wap');
  752. $this->assertEquals('text/vnd.wap.wml', $result);
  753. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  754. $expected = ['application/xml', 'application/javascript', 'application/json'];
  755. $this->assertEquals($expected, $result);
  756. }
  757. /**
  758. * test accepts() on the component
  759. *
  760. * @return void
  761. */
  762. public function testAccepts()
  763. {
  764. $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');
  765. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  766. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  767. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  768. $this->assertFalse($this->RequestHandler->accepts('rss'));
  769. }
  770. /**
  771. * test accepts and prefers methods.
  772. *
  773. * @return void
  774. */
  775. public function testPrefers()
  776. {
  777. $this->request->env(
  778. 'HTTP_ACCEPT',
  779. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  780. );
  781. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  782. $this->RequestHandler->ext = 'rss';
  783. $this->assertEquals('rss', $this->RequestHandler->prefers());
  784. $this->assertFalse($this->RequestHandler->prefers('xml'));
  785. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  786. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  787. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  788. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  789. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  790. $this->_init();
  791. $this->request->env(
  792. 'HTTP_ACCEPT',
  793. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  794. );
  795. $this->assertEquals('xml', $this->RequestHandler->prefers());
  796. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  797. $this->assertEquals('html', $this->RequestHandler->prefers());
  798. $this->assertFalse($this->RequestHandler->prefers('rss'));
  799. }
  800. /**
  801. * test that AJAX requests involving redirects trigger requestAction instead.
  802. *
  803. * @return void
  804. * @triggers Controller.beforeRedirect $this->Controller
  805. */
  806. public function testAjaxRedirectAsRequestAction()
  807. {
  808. Configure::write('App.namespace', 'TestApp');
  809. Router::connect('/:controller/:action');
  810. $event = new Event('Controller.beforeRedirect', $this->Controller);
  811. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  812. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  813. ->setMethods(['is'])
  814. ->getMock();
  815. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  816. ->setMethods(['_sendHeader', 'stop'])
  817. ->getMock();
  818. $this->Controller->RequestHandler->request = $this->Controller->request;
  819. $this->Controller->RequestHandler->response = $this->Controller->response;
  820. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  821. $response = $this->Controller->RequestHandler->beforeRedirect(
  822. $event,
  823. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  824. $this->Controller->response
  825. );
  826. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  827. }
  828. /**
  829. * Test that AJAX requests involving redirects handle querystrings
  830. *
  831. * @return void
  832. * @triggers Controller.beforeRedirect $this->Controller
  833. */
  834. public function testAjaxRedirectAsRequestActionWithQueryString()
  835. {
  836. Configure::write('App.namespace', 'TestApp');
  837. Router::connect('/:controller/:action');
  838. $event = new Event('Controller.beforeRedirect', $this->Controller);
  839. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  840. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  841. ->setMethods(['is'])
  842. ->getMock();
  843. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  844. ->setMethods(['_sendHeader', 'stop'])
  845. ->getMock();
  846. $this->Controller->RequestHandler->request = $this->Controller->request;
  847. $this->Controller->RequestHandler->response = $this->Controller->response;
  848. $this->Controller->request->expects($this->any())
  849. ->method('is')
  850. ->with('ajax')
  851. ->will($this->returnValue(true));
  852. $response = $this->Controller->RequestHandler->beforeRedirect(
  853. $event,
  854. '/request_action/params_pass?a=b&x=y?ish',
  855. $this->Controller->response
  856. );
  857. $data = json_decode($response, true);
  858. $this->assertEquals('/request_action/params_pass', $data['here']);
  859. $response = $this->Controller->RequestHandler->beforeRedirect(
  860. $event,
  861. '/request_action/query_pass?a=b&x=y?ish',
  862. $this->Controller->response
  863. );
  864. $data = json_decode($response, true);
  865. $this->assertEquals('y?ish', $data['x']);
  866. }
  867. /**
  868. * Test that AJAX requests involving redirects handle cookie data
  869. *
  870. * @return void
  871. * @triggers Controller.beforeRedirect $this->Controller
  872. */
  873. public function testAjaxRedirectAsRequestActionWithCookieData()
  874. {
  875. Configure::write('App.namespace', 'TestApp');
  876. Router::connect('/:controller/:action');
  877. $event = new Event('Controller.beforeRedirect', $this->Controller);
  878. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  879. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  880. ->setMethods(['is'])
  881. ->getMock();
  882. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  883. ->setMethods(['_sendHeader', 'stop'])
  884. ->getMock();
  885. $this->Controller->RequestHandler->request = $this->Controller->request;
  886. $this->Controller->RequestHandler->response = $this->Controller->response;
  887. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  888. $cookies = [
  889. 'foo' => 'bar'
  890. ];
  891. $this->Controller->request->cookies = $cookies;
  892. $response = $this->Controller->RequestHandler->beforeRedirect(
  893. $event,
  894. '/request_action/cookie_pass',
  895. $this->Controller->response
  896. );
  897. $data = json_decode($response, true);
  898. $this->assertEquals($cookies, $data);
  899. }
  900. /**
  901. * Tests that AJAX requests involving redirects don't let the status code bleed through.
  902. *
  903. * @return void
  904. * @triggers Controller.beforeRedirect $this->Controller
  905. */
  906. public function testAjaxRedirectAsRequestActionStatusCode()
  907. {
  908. Configure::write('App.namespace', 'TestApp');
  909. Router::connect('/:controller/:action');
  910. $event = new Event('Controller.beforeRedirect', $this->Controller);
  911. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  912. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  913. ->setMethods(['is'])
  914. ->getMock();
  915. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  916. ->setMethods(['_sendHeader', 'stop'])
  917. ->getMock();
  918. $this->Controller->response->statusCode(302);
  919. $this->Controller->RequestHandler->request = $this->Controller->request;
  920. $this->Controller->RequestHandler->response = $this->Controller->response;
  921. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  922. $response = $this->Controller->RequestHandler->beforeRedirect(
  923. $event,
  924. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  925. $this->Controller->response
  926. );
  927. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  928. $this->assertSame(200, $response->statusCode());
  929. }
  930. /**
  931. * test that ajax requests involving redirects don't force no layout
  932. * this would cause the ajax layout to not be rendered.
  933. *
  934. * @return void
  935. * @triggers Controller.beforeRedirect $this->Controller
  936. */
  937. public function testAjaxRedirectAsRequestActionStillRenderingLayout()
  938. {
  939. Configure::write('App.namespace', 'TestApp');
  940. Router::connect('/:controller/:action');
  941. $event = new Event('Controller.beforeRedirect', $this->Controller);
  942. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  943. $this->Controller->request = $this->getMockBuilder('Cake\Network\Request')
  944. ->setMethods(['is'])
  945. ->getMock();
  946. $this->Controller->response = $this->getMockBuilder('Cake\Network\Response')
  947. ->setMethods(['_sendHeader', 'stop'])
  948. ->getMock();
  949. $this->Controller->RequestHandler->request = $this->Controller->request;
  950. $this->Controller->RequestHandler->response = $this->Controller->response;
  951. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  952. $response = $this->Controller->RequestHandler->beforeRedirect(
  953. $event,
  954. ['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
  955. $this->Controller->response
  956. );
  957. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  958. $this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
  959. }
  960. /**
  961. * test that the beforeRedirect callback properly converts
  962. * array URLs into their correct string ones, and adds base => false so
  963. * the correct URLs are generated.
  964. *
  965. * @return void
  966. * @triggers Controller.beforeRender $this->Controller
  967. */
  968. public function testBeforeRedirectCallbackWithArrayUrl()
  969. {
  970. Configure::write('App.namespace', 'TestApp');
  971. Router::connect('/:controller/:action/*');
  972. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  973. $event = new Event('Controller.beforeRender', $this->Controller);
  974. Router::setRequestInfo([
  975. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  976. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  977. ]);
  978. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  979. $RequestHandler->request = new Request('posts/index');
  980. $RequestHandler->response = $this->Controller->response;
  981. ob_start();
  982. $RequestHandler->beforeRedirect(
  983. $event,
  984. ['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
  985. $this->Controller->response
  986. );
  987. $result = ob_get_clean();
  988. $this->assertEquals('one: first two: second', $result);
  989. }
  990. /**
  991. * testAddInputTypeException method
  992. *
  993. * @expectedException \Cake\Core\Exception\Exception
  994. * @return void
  995. */
  996. public function testAddInputTypeException()
  997. {
  998. $restore = error_reporting(E_ALL & ~E_USER_DEPRECATED);
  999. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  1000. error_reporting($restore);
  1001. }
  1002. /**
  1003. * Test checkNotModified method
  1004. *
  1005. * @return void
  1006. * @triggers Controller.beforeRender $this->Controller
  1007. */
  1008. public function testCheckNotModifiedByEtagStar()
  1009. {
  1010. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  1011. $event = new Event('Controller.beforeRender', $this->Controller);
  1012. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1013. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1014. ->setMethods(['notModified', 'stop'])
  1015. ->getMock();
  1016. $RequestHandler->response->etag('something');
  1017. $RequestHandler->response->expects($this->once())->method('notModified');
  1018. $this->assertFalse($RequestHandler->beforeRender($event));
  1019. }
  1020. /**
  1021. * Test checkNotModified method
  1022. *
  1023. * @return void
  1024. * @triggers Controller.beforeRender
  1025. */
  1026. public function testCheckNotModifiedByEtagExact()
  1027. {
  1028. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1029. $event = new Event('Controller.beforeRender');
  1030. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1031. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1032. ->setMethods(['notModified', 'stop'])
  1033. ->getMock();
  1034. $RequestHandler->response->etag('something', true);
  1035. $RequestHandler->response->expects($this->once())->method('notModified');
  1036. $this->assertFalse($RequestHandler->beforeRender($event));
  1037. }
  1038. /**
  1039. * Test checkNotModified method
  1040. *
  1041. * @return void
  1042. * @triggers Controller.beforeRender $this->Controller
  1043. */
  1044. public function testCheckNotModifiedByEtagAndTime()
  1045. {
  1046. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  1047. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  1048. $event = new Event('Controller.beforeRender', $this->Controller);
  1049. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1050. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1051. ->setMethods(['notModified', 'stop'])
  1052. ->getMock();
  1053. $RequestHandler->response->etag('something', true);
  1054. $RequestHandler->response->modified('2012-01-01 00:00:00');
  1055. $RequestHandler->response->expects($this->once())->method('notModified');
  1056. $this->assertFalse($RequestHandler->beforeRender($event));
  1057. }
  1058. /**
  1059. * Test checkNotModified method
  1060. *
  1061. * @return void
  1062. * @triggers Controller.beforeRender $this->Controller
  1063. */
  1064. public function testCheckNotModifiedNoInfo()
  1065. {
  1066. $event = new Event('Controller.beforeRender', $this->Controller);
  1067. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  1068. $RequestHandler->response = $this->getMockBuilder('Cake\Network\Response')
  1069. ->setMethods(['notModified', 'stop'])
  1070. ->getMock();
  1071. $RequestHandler->response->expects($this->never())->method('notModified');
  1072. $this->assertNull($RequestHandler->beforeRender($event));
  1073. }
  1074. /**
  1075. * Test default options in construction
  1076. *
  1077. * @return void
  1078. */
  1079. public function testConstructDefaultOptions()
  1080. {
  1081. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1082. $viewClass = $requestHandler->config('viewClassMap');
  1083. $expected = [
  1084. 'json' => 'Json',
  1085. 'xml' => 'Xml',
  1086. 'ajax' => 'Ajax',
  1087. ];
  1088. $this->assertEquals($expected, $viewClass);
  1089. $inputs = $requestHandler->config('inputTypeMap');
  1090. $this->assertArrayHasKey('json', $inputs);
  1091. $this->assertArrayHasKey('xml', $inputs);
  1092. }
  1093. /**
  1094. * Test options in constructor replace defaults
  1095. *
  1096. * @return void
  1097. */
  1098. public function testConstructReplaceOptions()
  1099. {
  1100. $requestHandler = new RequestHandlerComponent(
  1101. $this->Controller->components(),
  1102. [
  1103. 'viewClassMap' => ['json' => 'Json'],
  1104. 'inputTypeMap' => ['json' => ['json_decode', true]]
  1105. ]
  1106. );
  1107. $viewClass = $requestHandler->config('viewClassMap');
  1108. $expected = [
  1109. 'json' => 'Json',
  1110. ];
  1111. $this->assertEquals($expected, $viewClass);
  1112. $inputs = $requestHandler->config('inputTypeMap');
  1113. $this->assertArrayHasKey('json', $inputs);
  1114. $this->assertCount(1, $inputs);
  1115. }
  1116. /**
  1117. * test beforeRender() doesn't override response type set in controller action
  1118. *
  1119. * @return void
  1120. */
  1121. public function testBeforeRender()
  1122. {
  1123. $this->Controller->set_response_type();
  1124. $event = new Event('Controller.beforeRender', $this->Controller);
  1125. $this->RequestHandler->beforeRender($event);
  1126. $this->assertEquals('text/plain', $this->Controller->response->type());
  1127. }
  1128. }