RequestHandlerComponentTest.php 49 KB

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