RequestHandlerComponentTest.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller\Component;
  17. use Cake\Controller\Component\RequestHandlerComponent;
  18. use Cake\Controller\ComponentRegistry;
  19. use Cake\Event\Event;
  20. use Cake\Http\Response;
  21. use Cake\Http\ServerRequest;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\View\AjaxView;
  25. use Cake\View\JsonView;
  26. use Cake\View\XmlView;
  27. use TestApp\Controller\RequestHandlerTestController;
  28. use Zend\Diactoros\Stream;
  29. class RequestHandlerComponentExt extends RequestHandlerComponent
  30. {
  31. public function getExt()
  32. {
  33. return $this->ext;
  34. }
  35. public function setExt($ext)
  36. {
  37. $this->ext = $ext;
  38. }
  39. }
  40. /**
  41. * RequestHandlerComponentTest class
  42. */
  43. class RequestHandlerComponentTest extends TestCase
  44. {
  45. /**
  46. * Controller property
  47. *
  48. * @var RequestHandlerTestController
  49. */
  50. public $Controller;
  51. /**
  52. * RequestHandler property
  53. *
  54. * @var RequestHandlerComponent
  55. */
  56. public $RequestHandler;
  57. /**
  58. * @var ServerRequest
  59. */
  60. public $request;
  61. /**
  62. * Backup of $_SERVER
  63. *
  64. * @var array
  65. */
  66. protected $server = [];
  67. /**
  68. * setUp method
  69. *
  70. * @return void
  71. */
  72. public function setUp(): void
  73. {
  74. parent::setUp();
  75. $this->server = $_SERVER;
  76. static::setAppNamespace();
  77. $this->_init();
  78. }
  79. /**
  80. * init method
  81. *
  82. * @return void
  83. */
  84. protected function _init(): void
  85. {
  86. $request = new ServerRequest(['url' => 'controller_posts/index']);
  87. $response = $this->getMockBuilder('Cake\Http\Response')
  88. ->setMethods(['_sendHeader', 'stop'])
  89. ->getMock();
  90. $this->Controller = new RequestHandlerTestController($request, $response);
  91. $this->RequestHandler = $this->Controller->components()->load(RequestHandlerComponentExt::class);
  92. $this->request = $request;
  93. Router::scope('/', function ($routes): void {
  94. $routes->setExtensions('json');
  95. $routes->fallbacks('InflectedRoute');
  96. });
  97. }
  98. /**
  99. * tearDown method
  100. *
  101. * @return void
  102. */
  103. public function tearDown(): void
  104. {
  105. parent::tearDown();
  106. Router::reload();
  107. $_SERVER = $this->server;
  108. unset($this->RequestHandler, $this->Controller);
  109. }
  110. /**
  111. * Test that the constructor sets the config.
  112. *
  113. * @return void
  114. */
  115. public function testConstructorConfig(): void
  116. {
  117. $config = [
  118. 'viewClassMap' => ['json' => 'MyPlugin.MyJson'],
  119. ];
  120. $controller = $this->getMockBuilder('Cake\Controller\Controller')
  121. ->setMethods(['redirect'])
  122. ->getMock();
  123. $collection = new ComponentRegistry($controller);
  124. $requestHandler = new RequestHandlerComponent($collection, $config);
  125. $this->assertEquals(['json' => 'MyPlugin.MyJson'], $requestHandler->getConfig('viewClassMap'));
  126. }
  127. /**
  128. * testInitializeCallback method
  129. *
  130. * @return void
  131. */
  132. public function testInitializeCallback(): void
  133. {
  134. $this->assertNull($this->RequestHandler->ext);
  135. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'rss'));
  136. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  137. $this->assertEquals('rss', $this->RequestHandler->ext);
  138. }
  139. /**
  140. * test that a mapped Accept-type header will set $this->ext correctly.
  141. *
  142. * @return void
  143. */
  144. public function testInitializeContentTypeSettingExt(): void
  145. {
  146. Router::reload();
  147. $this->Controller->setRequest($this->request->withHeader('Accept', 'application/json'));
  148. $this->RequestHandler->setExt(null);
  149. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  150. $this->assertEquals('json', $this->RequestHandler->getExt());
  151. }
  152. /**
  153. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
  154. *
  155. * @return void
  156. */
  157. public function testInitializeContentTypeWithjQueryAccept(): void
  158. {
  159. Router::reload();
  160. $this->Controller->setRequest($this->request
  161. ->withHeader('Accept', 'application/json, application/javascript, */*; q=0.01')
  162. ->withHeader('X-Requested-With', 'XMLHttpRequest'));
  163. $this->RequestHandler->setExt(null);
  164. Router::extensions('json', false);
  165. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  166. $this->assertEquals('json', $this->RequestHandler->ext);
  167. }
  168. /**
  169. * Test that RequestHandler does not set extension to csv for text/plain mimetype
  170. *
  171. * @return void
  172. */
  173. public function testInitializeContentTypeWithjQueryTextPlainAccept(): void
  174. {
  175. Router::reload();
  176. $this->Controller->setRequest($this->request->withHeader('Accept', 'text/plain, */*; q=0.01'));
  177. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  178. $this->assertNull($this->RequestHandler->ext);
  179. }
  180. /**
  181. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
  182. * and the application is configured to handle multiple extensions
  183. *
  184. * @return void
  185. */
  186. public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions(): void
  187. {
  188. Router::reload();
  189. $this->Controller->setRequest($this->request->withHeader('Accept', 'application/json, application/javascript, */*; q=0.01'));
  190. $this->RequestHandler->setExt(null);
  191. Router::extensions(['rss', 'json'], false);
  192. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  193. $this->assertEquals('json', $this->RequestHandler->ext);
  194. }
  195. /**
  196. * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
  197. *
  198. * @return void
  199. */
  200. public function testInitializeNoContentTypeWithSingleAccept(): void
  201. {
  202. Router::reload();
  203. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
  204. $this->assertNull($this->RequestHandler->ext);
  205. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  206. $this->assertNull($this->RequestHandler->ext);
  207. }
  208. /**
  209. * Test that ext is set to the first listed extension with multiple accepted
  210. * content types.
  211. * Having multiple types accepted with same weight, means the client lets the
  212. * server choose the returned content type.
  213. *
  214. * @return void
  215. */
  216. public function testInitializeNoContentTypeWithMultipleAcceptedTypes(): void
  217. {
  218. $this->Controller->setRequest($this->request->withHeader(
  219. 'Accept',
  220. 'application/json, application/javascript, application/xml, */*; q=0.01'
  221. ));
  222. $this->RequestHandler->setExt(null);
  223. Router::extensions(['xml', 'json'], false);
  224. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  225. $this->assertEquals('xml', $this->RequestHandler->ext);
  226. $this->RequestHandler->setExt(null);
  227. Router::extensions(['json', 'xml'], false);
  228. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  229. $this->assertEquals('json', $this->RequestHandler->ext);
  230. }
  231. /**
  232. * Test that ext is set to type with highest weight
  233. *
  234. * @return void
  235. */
  236. public function testInitializeContentTypeWithMultipleAcceptedTypes(): void
  237. {
  238. Router::reload();
  239. $this->Controller->setRequest($this->request->withHeader(
  240. 'Accept',
  241. 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7'
  242. ));
  243. $this->RequestHandler->setExt(null);
  244. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  245. $this->assertEquals('json', $this->RequestHandler->ext);
  246. }
  247. /**
  248. * Test that ext is not set with confusing android accepts headers.
  249. *
  250. * @return void
  251. */
  252. public function testInitializeAmbiguousAndroidAccepts(): void
  253. {
  254. Router::reload();
  255. $this->request = $this->request->withEnv(
  256. 'HTTP_ACCEPT',
  257. 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  258. );
  259. $this->RequestHandler->setExt(null);
  260. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  261. $this->assertNull($this->RequestHandler->ext);
  262. }
  263. /**
  264. * Test that the headers sent by firefox are not treated as XML requests.
  265. *
  266. * @return void
  267. */
  268. public function testInititalizeFirefoxHeaderNotXml(): void
  269. {
  270. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  271. Router::extensions(['xml', 'json'], false);
  272. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  273. $this->assertNull($this->RequestHandler->ext);
  274. }
  275. /**
  276. * Test that a type mismatch doesn't incorrectly set the ext
  277. *
  278. * @return void
  279. */
  280. public function testInitializeContentTypeAndExtensionMismatch(): void
  281. {
  282. $this->assertNull($this->RequestHandler->ext);
  283. $extensions = Router::extensions();
  284. Router::extensions('xml', false);
  285. $this->Controller->setRequest($this->getMockBuilder('Cake\Http\ServerRequest')
  286. ->setMethods(['accepts'])
  287. ->getMock());
  288. $this->Controller->getRequest()->expects($this->any())
  289. ->method('accepts')
  290. ->will($this->returnValue(['application/json']));
  291. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  292. $this->assertNull($this->RequestHandler->ext);
  293. Router::extensions($extensions, false);
  294. }
  295. /**
  296. * testViewClassMap
  297. *
  298. * @return void
  299. */
  300. public function testViewClassMap(): void
  301. {
  302. $this->RequestHandler->setConfig(['viewClassMap' => ['json' => 'CustomJson']]);
  303. $result = $this->RequestHandler->getConfig('viewClassMap');
  304. $expected = [
  305. 'json' => 'CustomJson',
  306. 'xml' => 'Xml',
  307. 'ajax' => 'Ajax',
  308. ];
  309. $this->assertEquals($expected, $result);
  310. $this->RequestHandler->setConfig(['viewClassMap' => ['xls' => 'Excel.Excel']]);
  311. $result = $this->RequestHandler->getConfig('viewClassMap');
  312. $expected = [
  313. 'json' => 'CustomJson',
  314. 'xml' => 'Xml',
  315. 'ajax' => 'Ajax',
  316. 'xls' => 'Excel.Excel',
  317. ];
  318. $this->assertEquals($expected, $result);
  319. $this->RequestHandler->renderAs($this->Controller, 'json');
  320. $this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewBuilder()->getClassName());
  321. }
  322. /**
  323. * Verify that isAjax is set on the request params for ajax requests
  324. *
  325. * @return void
  326. * @triggers Controller.startup $this->Controller
  327. */
  328. public function testIsAjaxParams(): void
  329. {
  330. $this->Controller->setRequest($this->request->withHeader('X-Requested-With', 'XMLHttpRequest'));
  331. $event = new Event('Controller.startup', $this->Controller);
  332. $this->RequestHandler->initialize([]);
  333. $this->Controller->beforeFilter($event);
  334. $this->RequestHandler->startup($event);
  335. $this->assertTrue($this->Controller->getRequest()->getParam('isAjax'));
  336. }
  337. /**
  338. * testAutoAjaxLayout method
  339. *
  340. * @return void
  341. * @triggers Controller.startup $this->Controller
  342. */
  343. public function testAutoAjaxLayout(): void
  344. {
  345. $event = new Event('Controller.startup', $this->Controller);
  346. $this->Controller->setRequest($this->request->withHeader('X-Requested-With', 'XMLHttpRequest'));
  347. $this->RequestHandler->initialize([]);
  348. $this->RequestHandler->startup($event);
  349. $event = new Event('Controller.beforeRender', $this->Controller);
  350. $this->RequestHandler->beforeRender($event);
  351. $view = $this->Controller->createView();
  352. $this->assertInstanceOf(AjaxView::class, $view);
  353. $this->assertEquals('ajax', $view->getLayout());
  354. $this->_init();
  355. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'js'));
  356. $this->RequestHandler->initialize([]);
  357. $this->RequestHandler->startup($event);
  358. $this->assertNotEquals(AjaxView::class, $this->Controller->viewBuilder()->getClassName());
  359. }
  360. /**
  361. * test custom JsonView class is loaded and correct.
  362. *
  363. * @return void
  364. * @triggers Controller.startup $this->Controller
  365. */
  366. public function testJsonViewLoaded(): void
  367. {
  368. Router::extensions(['json', 'xml', 'ajax'], false);
  369. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'json'));
  370. $event = new Event('Controller.startup', $this->Controller);
  371. $this->RequestHandler->initialize([]);
  372. $this->RequestHandler->startup($event);
  373. $event = new Event('Controller.beforeRender', $this->Controller);
  374. $this->RequestHandler->beforeRender($event);
  375. $view = $this->Controller->createView();
  376. $this->assertInstanceOf(JsonView::class, $view);
  377. $this->assertEquals('json', $view->getLayoutPath());
  378. $this->assertEquals('json', $view->getSubDir());
  379. }
  380. /**
  381. * test custom XmlView class is loaded and correct.
  382. *
  383. * @return void
  384. * @triggers Controller.startup $this->Controller
  385. */
  386. public function testXmlViewLoaded(): void
  387. {
  388. Router::extensions(['json', 'xml', 'ajax'], false);
  389. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'xml'));
  390. $event = new Event('Controller.startup', $this->Controller);
  391. $this->RequestHandler->initialize([]);
  392. $this->RequestHandler->startup($event);
  393. $event = new Event('Controller.beforeRender', $this->Controller);
  394. $this->RequestHandler->beforeRender($event);
  395. $view = $this->Controller->createView();
  396. $this->assertInstanceOf(XmlView::class, $view);
  397. $this->assertEquals('xml', $view->getLayoutPath());
  398. $this->assertEquals('xml', $view->getSubDir());
  399. }
  400. /**
  401. * test custom AjaxView class is loaded and correct.
  402. *
  403. * @return void
  404. * @triggers Controller.startup $this->Controller
  405. */
  406. public function testAjaxViewLoaded(): void
  407. {
  408. Router::extensions(['json', 'xml', 'ajax'], false);
  409. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'ajax'));
  410. $event = new Event('Controller.startup', $this->Controller);
  411. $this->RequestHandler->initialize([]);
  412. $this->RequestHandler->startup($event);
  413. $event = new Event('Controller.beforeRender', $this->Controller);
  414. $this->RequestHandler->beforeRender($event);
  415. $view = $this->Controller->createView();
  416. $this->assertInstanceOf(AjaxView::class, $view);
  417. $this->assertEquals('ajax', $view->getLayout());
  418. }
  419. /**
  420. * test configured extension but no view class set.
  421. *
  422. * @return void
  423. * @triggers Controller.beforeRender $this->Controller
  424. */
  425. public function testNoViewClassExtension(): void
  426. {
  427. Router::extensions(['json', 'xml', 'ajax', 'csv'], false);
  428. $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'csv'));
  429. $event = new Event('Controller.startup', $this->Controller);
  430. $this->RequestHandler->initialize([]);
  431. $this->RequestHandler->startup($event);
  432. $this->Controller->getEventManager()->on('Controller.beforeRender', function () {
  433. return $this->Controller->getResponse();
  434. });
  435. $this->Controller->render();
  436. $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewBuilder()->getTemplatePath());
  437. $this->assertEquals('csv', $this->Controller->viewBuilder()->getLayoutPath());
  438. }
  439. /**
  440. * testStartupCallback method
  441. *
  442. * @return void
  443. * @triggers Controller.beforeRender $this->Controller
  444. */
  445. public function testStartupCallback(): void
  446. {
  447. $event = new Event('Controller.beforeRender', $this->Controller);
  448. $_SERVER['REQUEST_METHOD'] = 'PUT';
  449. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  450. $this->Controller->setRequest(new ServerRequest());
  451. $this->RequestHandler->beforeRender($event);
  452. $this->assertInternalType('array', $this->Controller->getRequest()->getData());
  453. $this->assertNotInternalType('object', $this->Controller->getRequest()->getData());
  454. }
  455. /**
  456. * testStartupCallback with charset.
  457. *
  458. * @return void
  459. * @triggers Controller.startup $this->Controller
  460. */
  461. public function testStartupCallbackCharset(): void
  462. {
  463. $event = new Event('Controller.startup', $this->Controller);
  464. $_SERVER['REQUEST_METHOD'] = 'PUT';
  465. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  466. $this->Controller->setRequest(new ServerRequest());
  467. $this->RequestHandler->startup($event);
  468. $this->assertInternalType('array', $this->Controller->getRequest()->getData());
  469. $this->assertNotInternalType('object', $this->Controller->getRequest()->getData());
  470. }
  471. /**
  472. * Test that processing data results in an array.
  473. *
  474. * @return void
  475. * @triggers Controller.startup $this->Controller
  476. */
  477. public function testStartupProcessDataInvalid(): void
  478. {
  479. $this->Controller->setRequest(new ServerRequest([
  480. 'environment' => [
  481. 'REQUEST_METHOD' => 'POST',
  482. 'CONTENT_TYPE' => 'application/json',
  483. ],
  484. ]));
  485. $event = new Event('Controller.startup', $this->Controller);
  486. $this->RequestHandler->startup($event);
  487. $this->assertEquals([], $this->Controller->getRequest()->getData());
  488. $stream = new Stream('php://memory', 'w');
  489. $stream->write('"invalid"');
  490. $this->Controller->setRequest($this->Controller->getRequest()->withBody($stream));
  491. $this->RequestHandler->startup($event);
  492. $this->assertEquals(['invalid'], $this->Controller->getRequest()->getData());
  493. }
  494. /**
  495. * Test that processing data results in an array.
  496. *
  497. * @return void
  498. * @triggers Controller.startup $this->Controller
  499. */
  500. public function testStartupProcessData(): void
  501. {
  502. $this->Controller->setRequest(new ServerRequest([
  503. 'environment' => [
  504. 'REQUEST_METHOD' => 'POST',
  505. 'CONTENT_TYPE' => 'application/json',
  506. ],
  507. ]));
  508. $stream = new Stream('php://memory', 'w');
  509. $stream->write('{"valid":true}');
  510. $this->Controller->setRequest($this->Controller->getRequest()->withBody($stream));
  511. $event = new Event('Controller.startup', $this->Controller);
  512. $this->RequestHandler->startup($event);
  513. $this->assertEquals(['valid' => true], $this->Controller->getRequest()->getData());
  514. }
  515. /**
  516. * Test that file handles are ignored as XML data.
  517. *
  518. * @return void
  519. * @triggers Controller.startup $this->Controller
  520. */
  521. public function testStartupIgnoreFileAsXml(): void
  522. {
  523. $this->Controller->setRequest(new ServerRequest([
  524. 'input' => '/dev/random',
  525. 'environment' => [
  526. 'REQUEST_METHOD' => 'POST',
  527. 'CONTENT_TYPE' => 'application/xml',
  528. ],
  529. ]));
  530. $event = new Event('Controller.startup', $this->Controller);
  531. $this->RequestHandler->startup($event);
  532. $this->assertEquals([], $this->Controller->getRequest()->getData());
  533. }
  534. /**
  535. * Test that input xml is parsed
  536. *
  537. * @return void
  538. */
  539. public function testStartupConvertXmlDataWrapper(): void
  540. {
  541. $xml = <<<XML
  542. <?xml version="1.0" encoding="utf-8"?>
  543. <data>
  544. <article id="1" title="first"></article>
  545. </data>
  546. XML;
  547. $this->Controller->setRequest((new ServerRequest(['input' => $xml]))
  548. ->withEnv('REQUEST_METHOD', 'POST')
  549. ->withEnv('CONTENT_TYPE', 'application/xml'));
  550. $event = new Event('Controller.startup', $this->Controller);
  551. $this->RequestHandler->startup($event);
  552. $expected = [
  553. 'data' => [
  554. 'article' => [
  555. '@id' => 1,
  556. '@title' => 'first',
  557. ],
  558. ],
  559. ];
  560. $this->assertEquals($expected, $this->Controller->getRequest()->getData());
  561. }
  562. /**
  563. * Test that input xml is parsed
  564. *
  565. * @return void
  566. */
  567. public function testStartupConvertXmlElements(): void
  568. {
  569. $xml = <<<XML
  570. <?xml version="1.0" encoding="utf-8"?>
  571. <article>
  572. <id>1</id>
  573. <title><![CDATA[first]]></title>
  574. </article>
  575. XML;
  576. $this->Controller->setRequest((new ServerRequest(['input' => $xml]))
  577. ->withEnv('REQUEST_METHOD', 'POST')
  578. ->withEnv('CONTENT_TYPE', 'application/xml'));
  579. $event = new Event('Controller.startup', $this->Controller);
  580. $this->RequestHandler->startup($event);
  581. $expected = [
  582. 'article' => [
  583. 'id' => 1,
  584. 'title' => 'first',
  585. ],
  586. ];
  587. $this->assertEquals($expected, $this->Controller->getRequest()->getData());
  588. }
  589. /**
  590. * Test that input xml is parsed
  591. *
  592. * @return void
  593. */
  594. public function testStartupConvertXmlIgnoreEntities(): void
  595. {
  596. $xml = <<<XML
  597. <?xml version="1.0" encoding="UTF-8"?>
  598. <!DOCTYPE item [
  599. <!ENTITY item "item">
  600. <!ENTITY item1 "&item;&item;&item;&item;&item;&item;">
  601. <!ENTITY item2 "&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;">
  602. <!ENTITY item3 "&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;">
  603. <!ENTITY item4 "&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;">
  604. <!ENTITY item5 "&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;">
  605. <!ENTITY item6 "&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;">
  606. <!ENTITY item7 "&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;">
  607. <!ENTITY item8 "&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;">
  608. ]>
  609. <item>
  610. <description>&item8;</description>
  611. </item>
  612. XML;
  613. $this->Controller->setRequest((new ServerRequest(['input' => $xml]))
  614. ->withEnv('REQUEST_METHOD', 'POST')
  615. ->withEnv('CONTENT_TYPE', 'application/xml'));
  616. $event = new Event('Controller.startup', $this->Controller);
  617. $this->RequestHandler->startup($event);
  618. $this->assertEquals([], $this->Controller->getRequest()->getData());
  619. }
  620. /**
  621. * Test that data isn't processed when parsed data already exists.
  622. *
  623. * @return void
  624. * @triggers Controller.startup $this->Controller
  625. */
  626. public function testStartupSkipDataProcess(): void
  627. {
  628. $this->Controller->setRequest(new ServerRequest([
  629. 'environment' => [
  630. 'REQUEST_METHOD' => 'POST',
  631. 'CONTENT_TYPE' => 'application/json',
  632. ],
  633. ]));
  634. $event = new Event('Controller.startup', $this->Controller);
  635. $this->RequestHandler->startup($event);
  636. $this->assertEquals([], $this->Controller->getRequest()->getData());
  637. $stream = new Stream('php://memory', 'w');
  638. $stream->write('{"new": "data"}');
  639. $this->Controller->setRequest($this->Controller->getRequest()
  640. ->withBody($stream)
  641. ->withParsedBody(['old' => 'news']));
  642. $this->RequestHandler->startup($event);
  643. $this->assertEquals(['old' => 'news'], $this->Controller->getRequest()->getData());
  644. }
  645. /**
  646. * testRenderAs method
  647. *
  648. * @return void
  649. */
  650. public function testRenderAs(): void
  651. {
  652. $this->RequestHandler->renderAs($this->Controller, 'rss');
  653. $this->Controller->viewBuilder()->setTemplatePath('request_handler_test\\rss');
  654. $this->RequestHandler->renderAs($this->Controller, 'js');
  655. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewBuilder()->getTemplatePath());
  656. }
  657. /**
  658. * test that attachment headers work with renderAs
  659. *
  660. * @return void
  661. */
  662. public function testRenderAsWithAttachment(): void
  663. {
  664. $this->Controller->setRequest($this->request->withHeader('Accept', 'application/xml;q=1.0'));
  665. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  666. $this->assertEquals(XmlView::class, $this->Controller->viewBuilder()->getClassName());
  667. $this->assertEquals('application/xml', $this->Controller->getResponse()->getType());
  668. $this->assertEquals('UTF-8', $this->Controller->getResponse()->getCharset());
  669. $this->assertContains('myfile.xml', $this->Controller->getResponse()->getHeaderLine('Content-Disposition'));
  670. }
  671. /**
  672. * test that respondAs works as expected.
  673. *
  674. * @return void
  675. */
  676. public function testRespondAs(): void
  677. {
  678. $result = $this->RequestHandler->respondAs('json');
  679. $this->assertTrue($result);
  680. $this->assertEquals('application/json', $this->Controller->getResponse()->getType());
  681. $result = $this->RequestHandler->respondAs('text/xml');
  682. $this->assertTrue($result);
  683. $this->assertEquals('text/xml', $this->Controller->getResponse()->getType());
  684. }
  685. /**
  686. * test that attachment headers work with respondAs
  687. *
  688. * @return void
  689. */
  690. public function testRespondAsWithAttachment(): void
  691. {
  692. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  693. $this->assertTrue($result);
  694. $response = $this->Controller->getResponse();
  695. $this->assertContains('myfile.xml', $response->getHeaderLine('Content-Disposition'));
  696. $this->assertContains('application/xml', $response->getType());
  697. }
  698. /**
  699. * test that calling renderAs() more than once continues to work.
  700. *
  701. * @link #6466
  702. * @return void
  703. */
  704. public function testRenderAsCalledTwice(): void
  705. {
  706. $this->Controller->getEventManager()->on('Controller.beforeRender', function (\Cake\Event\EventInterface $e) {
  707. return $e->getSubject()->getResponse();
  708. });
  709. $this->Controller->render();
  710. $this->RequestHandler->renderAs($this->Controller, 'print');
  711. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewBuilder()->getTemplatePath());
  712. $this->assertEquals('print', $this->Controller->viewBuilder()->getLayoutPath());
  713. $this->RequestHandler->renderAs($this->Controller, 'js');
  714. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewBuilder()->getTemplatePath());
  715. $this->assertEquals('js', $this->Controller->viewBuilder()->getLayoutPath());
  716. }
  717. /**
  718. * testRequestContentTypes method
  719. *
  720. * @return void
  721. */
  722. public function testRequestContentTypes(): void
  723. {
  724. $this->Controller->setRequest($this->request->withEnv('REQUEST_METHOD', 'GET'));
  725. $this->assertNull($this->RequestHandler->requestedWith());
  726. $this->Controller->setRequest($this->request->withEnv('REQUEST_METHOD', 'POST')
  727. ->withEnv('CONTENT_TYPE', 'application/json'));
  728. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  729. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  730. $this->assertEquals('json', $result);
  731. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  732. $this->assertFalse($result);
  733. $this->Controller->setRequest($this->request
  734. ->withEnv('REQUEST_METHOD', 'PATCH')
  735. ->withEnv('CONTENT_TYPE', 'application/json'));
  736. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  737. $this->Controller->setRequest($this->request
  738. ->withEnv('REQUEST_METHOD', 'DELETE')
  739. ->withEnv('CONTENT_TYPE', 'application/json'));
  740. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  741. $this->Controller->setRequest($this->request
  742. ->withEnv('REQUEST_METHOD', 'POST')
  743. ->withEnv('CONTENT_TYPE', 'application/json'));
  744. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  745. $this->assertEquals('json', $result);
  746. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  747. $this->assertFalse($result);
  748. $this->Controller->setRequest($this->request->withHeader(
  749. 'Accept',
  750. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  751. ));
  752. $this->assertTrue($this->RequestHandler->prefers('xml'));
  753. $this->assertFalse($this->RequestHandler->prefers('atom'));
  754. $this->assertFalse($this->RequestHandler->prefers('rss'));
  755. $this->Controller->setRequest($this->request->withHeader(
  756. 'Accept',
  757. 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  758. ));
  759. $this->assertTrue($this->RequestHandler->prefers('atom'));
  760. $this->assertFalse($this->RequestHandler->prefers('rss'));
  761. $this->Controller->setRequest($this->request->withHeader(
  762. 'Accept',
  763. 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  764. ));
  765. $this->assertFalse($this->RequestHandler->prefers('atom'));
  766. $this->assertTrue($this->RequestHandler->prefers('rss'));
  767. }
  768. /**
  769. * test that map alias converts aliases to content types.
  770. *
  771. * @return void
  772. */
  773. public function testMapAlias(): void
  774. {
  775. $result = $this->RequestHandler->mapAlias('xml');
  776. $this->assertEquals('application/xml', $result);
  777. $result = $this->RequestHandler->mapAlias('text/html');
  778. $this->assertNull($result);
  779. $result = $this->RequestHandler->mapAlias('wap');
  780. $this->assertEquals('text/vnd.wap.wml', $result);
  781. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  782. $expected = ['application/xml', 'application/javascript', 'application/json'];
  783. $this->assertEquals($expected, $result);
  784. }
  785. /**
  786. * test accepts() on the component
  787. *
  788. * @return void
  789. */
  790. public function testAccepts(): void
  791. {
  792. $this->Controller->setRequest($this->request->withHeader(
  793. 'Accept',
  794. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  795. ));
  796. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  797. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  798. $this->Controller->setRequest($this->request->withHeader('Accept', '*/*;q=0.5'));
  799. $this->assertFalse($this->RequestHandler->accepts('rss'));
  800. }
  801. /**
  802. * test accepts and prefers methods.
  803. *
  804. * @return void
  805. */
  806. public function testPrefers(): void
  807. {
  808. $this->Controller->setRequest($this->request->withHeader(
  809. 'Accept',
  810. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  811. ));
  812. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  813. $this->RequestHandler->setExt('rss');
  814. $this->assertEquals('rss', $this->RequestHandler->prefers());
  815. $this->assertFalse($this->RequestHandler->prefers('xml'));
  816. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  817. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  818. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  819. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  820. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  821. $this->_init();
  822. $this->Controller->setRequest($this->request->withHeader(
  823. 'Accept',
  824. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  825. ));
  826. $this->assertEquals('xml', $this->RequestHandler->prefers());
  827. $this->Controller->setRequest($this->request->withHeader('Accept', '*/*;q=0.5'));
  828. $this->assertEquals('html', $this->RequestHandler->prefers());
  829. $this->assertFalse($this->RequestHandler->prefers('rss'));
  830. $this->Controller->setRequest($this->request->withEnv('HTTP_ACCEPT', ''));
  831. $this->RequestHandler->setExt('json');
  832. $this->assertFalse($this->RequestHandler->prefers('xml'));
  833. }
  834. /**
  835. * Test checkNotModified method
  836. *
  837. * @return void
  838. * @triggers Controller.beforeRender $this->Controller
  839. */
  840. public function testCheckNotModifiedByEtagStar(): void
  841. {
  842. $response = new Response();
  843. $response = $response->withEtag('something')
  844. ->withHeader('Content-Type', 'text/plain')
  845. ->withStringBody('keeper');
  846. $this->Controller->setResponse($response);
  847. $this->Controller->setRequest($this->request->withHeader('If-None-Match', '*'));
  848. $event = new Event('Controller.beforeRender', $this->Controller);
  849. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  850. $this->assertNull($requestHandler->beforeRender($event));
  851. $this->assertTrue($event->isStopped());
  852. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  853. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  854. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-Type'), 'header should not be removed.');
  855. }
  856. /**
  857. * Test checkNotModified method
  858. *
  859. * @return void
  860. * @triggers Controller.beforeRender
  861. */
  862. public function testCheckNotModifiedByEtagExact(): void
  863. {
  864. $response = new Response();
  865. $response = $response->withEtag('something', true)
  866. ->withHeader('Content-Type', 'text/plain')
  867. ->withStringBody('keeper');
  868. $this->Controller->setResponse($response);
  869. $this->Controller->setRequest($this->request->withHeader('If-None-Match', 'W/"something", "other"'));
  870. $event = new Event('Controller.beforeRender', $this->Controller);
  871. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  872. $this->assertNull($requestHandler->beforeRender($event));
  873. $this->assertTrue($event->isStopped());
  874. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  875. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  876. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-Type'));
  877. }
  878. /**
  879. * Test checkNotModified method
  880. *
  881. * @return void
  882. * @triggers Controller.beforeRender $this->Controller
  883. */
  884. public function testCheckNotModifiedByEtagAndTime(): void
  885. {
  886. $this->Controller->setRequest($this->request
  887. ->withHeader('If-None-Match', 'W/"something", "other"')
  888. ->withHeader('If-Modified-Since', '2012-01-01 00:00:00'));
  889. $response = new Response();
  890. $response = $response->withEtag('something', true)
  891. ->withHeader('Content-type', 'text/plain')
  892. ->withStringBody('should be removed')
  893. ->withModified('2012-01-01 00:00:00');
  894. $this->Controller->setResponse($response);
  895. $event = new Event('Controller.beforeRender', $this->Controller);
  896. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  897. $this->assertNull($requestHandler->beforeRender($event));
  898. $this->assertTrue($event->isStopped());
  899. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  900. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  901. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-type'));
  902. }
  903. /**
  904. * Test checkNotModified method
  905. *
  906. * @return void
  907. * @triggers Controller.beforeRender $this->Controller
  908. */
  909. public function testCheckNotModifiedNoInfo(): void
  910. {
  911. $response = $this->getMockBuilder('Cake\Http\Response')
  912. ->setMethods(['notModified', 'stop'])
  913. ->getMock();
  914. $response->expects($this->never())->method('notModified');
  915. $this->Controller->setResponse($response);
  916. $event = new Event('Controller.beforeRender', $this->Controller);
  917. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  918. $this->assertNull($requestHandler->beforeRender($event));
  919. }
  920. /**
  921. * Test default options in construction
  922. *
  923. * @return void
  924. */
  925. public function testConstructDefaultOptions(): void
  926. {
  927. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  928. $viewClass = $requestHandler->getConfig('viewClassMap');
  929. $expected = [
  930. 'json' => 'Json',
  931. 'xml' => 'Xml',
  932. 'ajax' => 'Ajax',
  933. ];
  934. $this->assertEquals($expected, $viewClass);
  935. $inputs = $requestHandler->getConfig('inputTypeMap');
  936. $this->assertArrayHasKey('json', $inputs);
  937. $this->assertArrayHasKey('xml', $inputs);
  938. }
  939. /**
  940. * Test options in constructor replace defaults
  941. *
  942. * @return void
  943. */
  944. public function testConstructReplaceOptions(): void
  945. {
  946. $requestHandler = new RequestHandlerComponent(
  947. $this->Controller->components(),
  948. [
  949. 'viewClassMap' => ['json' => 'Json'],
  950. 'inputTypeMap' => ['json' => ['json_decode', true]],
  951. ]
  952. );
  953. $viewClass = $requestHandler->getConfig('viewClassMap');
  954. $expected = [
  955. 'json' => 'Json',
  956. ];
  957. $this->assertEquals($expected, $viewClass);
  958. $inputs = $requestHandler->getConfig('inputTypeMap');
  959. $this->assertArrayHasKey('json', $inputs);
  960. $this->assertCount(1, $inputs);
  961. }
  962. /**
  963. * test beforeRender() doesn't override response type set in controller action
  964. *
  965. * @return void
  966. */
  967. public function testBeforeRender(): void
  968. {
  969. $this->Controller->set_response_type();
  970. $event = new Event('Controller.beforeRender', $this->Controller);
  971. $this->RequestHandler->beforeRender($event);
  972. $this->assertEquals('text/plain', $this->Controller->getResponse()->getType());
  973. }
  974. /**
  975. * tests beforeRender automatically uses renderAs when a supported extension is found
  976. *
  977. * @return void
  978. */
  979. public function testBeforeRenderAutoRenderAs(): void
  980. {
  981. $this->Controller->setRequest($this->request->withParam('_ext', 'csv'));
  982. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  983. $event = new Event('Controller.beforeRender', $this->Controller);
  984. $this->RequestHandler->beforeRender($event);
  985. $this->assertEquals('text/csv', $this->Controller->getResponse()->getType());
  986. }
  987. }