RequestHandlerComponentTest.php 40 KB

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