RequestHandlerComponentTest.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 = new RequestHandlerComponent($this->Controller->components());
  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. $this->assertEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  320. $view = $this->Controller->createView();
  321. $this->assertEquals('ajax', $view->layout);
  322. $this->_init();
  323. $this->Controller->request->params['_ext'] = 'js';
  324. $this->RequestHandler->initialize([]);
  325. $this->RequestHandler->startup($event);
  326. $this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  327. }
  328. /**
  329. * test custom JsonView class is loaded and correct.
  330. *
  331. * @return void
  332. * @triggers Controller.startup $this->Controller
  333. */
  334. public function testJsonViewLoaded()
  335. {
  336. Router::extensions(['json', 'xml', 'ajax'], false);
  337. $this->Controller->request->params['_ext'] = 'json';
  338. $event = new Event('Controller.startup', $this->Controller);
  339. $this->RequestHandler->initialize([]);
  340. $this->RequestHandler->startup($event);
  341. $this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
  342. $view = $this->Controller->createView();
  343. $this->assertEquals('json', $view->layoutPath);
  344. $this->assertEquals('json', $view->subDir);
  345. }
  346. /**
  347. * test custom XmlView class is loaded and correct.
  348. *
  349. * @return void
  350. * @triggers Controller.startup $this->Controller
  351. */
  352. public function testXmlViewLoaded()
  353. {
  354. Router::extensions(['json', 'xml', 'ajax'], false);
  355. $this->Controller->request->params['_ext'] = 'xml';
  356. $event = new Event('Controller.startup', $this->Controller);
  357. $this->RequestHandler->initialize([]);
  358. $this->RequestHandler->startup($event);
  359. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  360. $view = $this->Controller->createView();
  361. $this->assertEquals('xml', $view->layoutPath);
  362. $this->assertEquals('xml', $view->subDir);
  363. }
  364. /**
  365. * test custom AjaxView class is loaded and correct.
  366. *
  367. * @return void
  368. * @triggers Controller.startup $this->Controller
  369. */
  370. public function testAjaxViewLoaded()
  371. {
  372. Router::extensions(['json', 'xml', 'ajax'], false);
  373. $this->Controller->request->params['_ext'] = 'ajax';
  374. $event = new Event('Controller.startup', $this->Controller);
  375. $this->RequestHandler->initialize([]);
  376. $this->RequestHandler->startup($event);
  377. $this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
  378. $view = $this->Controller->createView();
  379. $this->assertEquals('ajax', $view->layout);
  380. }
  381. /**
  382. * test configured extension but no view class set.
  383. *
  384. * @return void
  385. * @triggers Controller.startup $this->Controller
  386. */
  387. public function testNoViewClassExtension()
  388. {
  389. Router::extensions(['json', 'xml', 'ajax', 'csv'], false);
  390. $this->Controller->request->params['_ext'] = 'csv';
  391. $event = new Event('Controller.startup', $this->Controller);
  392. $this->RequestHandler->initialize([]);
  393. $this->RequestHandler->startup($event);
  394. $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewPath);
  395. $this->assertEquals('csv', $this->Controller->layoutPath);
  396. }
  397. /**
  398. * testStartupCallback method
  399. *
  400. * @return void
  401. * @triggers Controller.startup $this->Controller
  402. */
  403. public function testStartupCallback()
  404. {
  405. $event = new Event('Controller.startup', $this->Controller);
  406. $_SERVER['REQUEST_METHOD'] = 'PUT';
  407. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  408. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  409. $this->RequestHandler->startup($event);
  410. $this->assertTrue(is_array($this->Controller->request->data));
  411. $this->assertFalse(is_object($this->Controller->request->data));
  412. }
  413. /**
  414. * testStartupCallback with charset.
  415. *
  416. * @return void
  417. * @triggers Controller.startup $this->Controller
  418. */
  419. public function testStartupCallbackCharset()
  420. {
  421. $event = new Event('Controller.startup', $this->Controller);
  422. $_SERVER['REQUEST_METHOD'] = 'PUT';
  423. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  424. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  425. $this->RequestHandler->startup($event);
  426. $this->assertTrue(is_array($this->Controller->request->data));
  427. $this->assertFalse(is_object($this->Controller->request->data));
  428. }
  429. /**
  430. * Test that processing data results in an array.
  431. *
  432. * @return void
  433. * @triggers Controller.startup $this->Controller
  434. */
  435. public function testStartupProcessData()
  436. {
  437. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  438. $this->Controller->request->expects($this->at(0))
  439. ->method('_readInput')
  440. ->will($this->returnValue(''));
  441. $this->Controller->request->expects($this->at(1))
  442. ->method('_readInput')
  443. ->will($this->returnValue('"invalid"'));
  444. $this->Controller->request->expects($this->at(2))
  445. ->method('_readInput')
  446. ->will($this->returnValue('{"valid":true}'));
  447. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  448. $this->Controller->request->env('CONTENT_TYPE', 'application/json');
  449. $event = new Event('Controller.startup', $this->Controller);
  450. $this->RequestHandler->startup($event);
  451. $this->assertEquals([], $this->Controller->request->data);
  452. $this->RequestHandler->startup($event);
  453. $this->assertEquals(['invalid'], $this->Controller->request->data);
  454. $this->RequestHandler->startup($event);
  455. $this->assertEquals(['valid' => true], $this->Controller->request->data);
  456. }
  457. /**
  458. * Test that file handles are ignored as XML data.
  459. *
  460. * @return void
  461. * @triggers Controller.startup $this->Controller
  462. */
  463. public function testStartupIgnoreFileAsXml()
  464. {
  465. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  466. $this->Controller->request->expects($this->any())
  467. ->method('_readInput')
  468. ->will($this->returnValue('/dev/random'));
  469. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  470. $this->Controller->request->env('CONTENT_TYPE', 'application/xml');
  471. $event = new Event('Controller.startup', $this->Controller);
  472. $this->RequestHandler->startup($event);
  473. $this->assertEquals([], $this->Controller->request->data);
  474. }
  475. /**
  476. * Test mapping a new type and having startup process it.
  477. *
  478. * @return void
  479. * @triggers Controller.startup $this->Controller
  480. */
  481. public function testStartupCustomTypeProcess()
  482. {
  483. if (!function_exists('str_getcsv')) {
  484. $this->markTestSkipped('Need "str_getcsv" for this test.');
  485. }
  486. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  487. $this->Controller->request->expects($this->once())
  488. ->method('_readInput')
  489. ->will($this->returnValue('"A","csv","string"'));
  490. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  491. $this->Controller->request->env('REQUEST_METHOD', 'POST');
  492. $this->Controller->request->env('CONTENT_TYPE', 'text/csv');
  493. $event = new Event('Controller.startup', $this->Controller);
  494. $this->RequestHandler->startup($event);
  495. $expected = [
  496. 'A', 'csv', 'string'
  497. ];
  498. $this->assertEquals($expected, $this->Controller->request->data);
  499. }
  500. /**
  501. * testNonAjaxRedirect method
  502. *
  503. * @return void
  504. * @triggers Controller.startup $this->Controller
  505. */
  506. public function testNonAjaxRedirect()
  507. {
  508. $event = new Event('Controller.startup', $this->Controller);
  509. $this->RequestHandler->initialize([]);
  510. $this->RequestHandler->startup($event);
  511. $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
  512. }
  513. /**
  514. * test that redirects with ajax and no URL don't do anything.
  515. *
  516. * @return void
  517. * @triggers Controller.startup $this->Controller
  518. */
  519. public function testAjaxRedirectWithNoUrl()
  520. {
  521. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  522. $event = new Event('Controller.startup', $this->Controller);
  523. $this->Controller->response = $this->getMock('Cake\Network\Response');
  524. $this->Controller->response->expects($this->never())
  525. ->method('body');
  526. $this->RequestHandler->initialize([]);
  527. $this->RequestHandler->startup($event);
  528. $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
  529. }
  530. /**
  531. * testRenderAs method
  532. *
  533. * @return void
  534. */
  535. public function testRenderAs()
  536. {
  537. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  538. $this->RequestHandler->renderAs($this->Controller, 'rss');
  539. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  540. $this->Controller->viewPath = 'request_handler_test\\rss';
  541. $this->RequestHandler->renderAs($this->Controller, 'js');
  542. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
  543. }
  544. /**
  545. * test that attachment headers work with renderAs
  546. *
  547. * @return void
  548. */
  549. public function testRenderAsWithAttachment()
  550. {
  551. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  552. $this->RequestHandler->request->expects($this->any())
  553. ->method('parseAccept')
  554. ->will($this->returnValue(['1.0' => ['application/xml']]));
  555. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download', 'charset']);
  556. $this->RequestHandler->response->expects($this->at(0))
  557. ->method('type')
  558. ->with('application/xml');
  559. $this->RequestHandler->response->expects($this->at(1))
  560. ->method('charset')
  561. ->with('UTF-8');
  562. $this->RequestHandler->response->expects($this->at(2))
  563. ->method('download')
  564. ->with('myfile.xml');
  565. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  566. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  567. }
  568. /**
  569. * test that respondAs works as expected.
  570. *
  571. * @return void
  572. */
  573. public function testRespondAs()
  574. {
  575. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type']);
  576. $this->RequestHandler->response->expects($this->at(0))->method('type')
  577. ->with('application/json');
  578. $this->RequestHandler->response->expects($this->at(1))->method('type')
  579. ->with('text/xml');
  580. $result = $this->RequestHandler->respondAs('json');
  581. $this->assertTrue($result);
  582. $result = $this->RequestHandler->respondAs('text/xml');
  583. $this->assertTrue($result);
  584. }
  585. /**
  586. * test that attachment headers work with respondAs
  587. *
  588. * @return void
  589. */
  590. public function testRespondAsWithAttachment()
  591. {
  592. $this->RequestHandler = $this->getMock(
  593. 'Cake\Controller\Component\RequestHandlerComponent',
  594. ['_header'],
  595. [$this->Controller->components()]
  596. );
  597. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download']);
  598. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  599. $this->RequestHandler->request->expects($this->once())
  600. ->method('parseAccept')
  601. ->will($this->returnValue(['1.0' => ['application/xml']]));
  602. $this->RequestHandler->response->expects($this->once())->method('download')
  603. ->with('myfile.xml');
  604. $this->RequestHandler->response->expects($this->once())->method('type')
  605. ->with('application/xml');
  606. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  607. $this->assertTrue($result);
  608. }
  609. /**
  610. * test that calling renderAs() more than once continues to work.
  611. *
  612. * @link #6466
  613. * @return void
  614. */
  615. public function testRenderAsCalledTwice()
  616. {
  617. $this->RequestHandler->renderAs($this->Controller, 'print');
  618. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
  619. $this->assertEquals('print', $this->Controller->layoutPath);
  620. $this->RequestHandler->renderAs($this->Controller, 'js');
  621. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
  622. $this->assertEquals('js', $this->Controller->layoutPath);
  623. }
  624. /**
  625. * testRequestContentTypes method
  626. *
  627. * @return void
  628. */
  629. public function testRequestContentTypes()
  630. {
  631. $this->request->env('REQUEST_METHOD', 'GET');
  632. $this->assertNull($this->RequestHandler->requestedWith());
  633. $this->request->env('REQUEST_METHOD', 'POST');
  634. $this->request->env('CONTENT_TYPE', 'application/json');
  635. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  636. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  637. $this->assertEquals('json', $result);
  638. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  639. $this->assertFalse($result);
  640. $this->request->env('REQUEST_METHOD', 'PATCH');
  641. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  642. $this->request->env('REQUEST_METHOD', 'DELETE');
  643. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  644. $this->request->env('REQUEST_METHOD', 'POST');
  645. $this->request->env('CONTENT_TYPE', '');
  646. $this->request->env('HTTP_CONTENT_TYPE', 'application/json');
  647. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  648. $this->assertEquals('json', $result);
  649. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  650. $this->assertFalse($result);
  651. $this->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  652. $this->assertTrue($this->RequestHandler->isXml());
  653. $this->assertFalse($this->RequestHandler->isAtom());
  654. $this->assertFalse($this->RequestHandler->isRSS());
  655. $this->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  656. $this->assertTrue($this->RequestHandler->isAtom());
  657. $this->assertFalse($this->RequestHandler->isRSS());
  658. $this->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  659. $this->assertFalse($this->RequestHandler->isAtom());
  660. $this->assertTrue($this->RequestHandler->isRSS());
  661. $this->assertFalse($this->RequestHandler->isWap());
  662. $this->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  663. $this->assertTrue($this->RequestHandler->isWap());
  664. }
  665. /**
  666. * testResponseContentType method
  667. *
  668. * @return void
  669. */
  670. public function testResponseContentType()
  671. {
  672. $this->assertEquals('html', $this->RequestHandler->responseType());
  673. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  674. $this->assertEquals('atom', $this->RequestHandler->responseType());
  675. }
  676. /**
  677. * testMobileDeviceDetection method
  678. *
  679. * @return void
  680. */
  681. public function testMobileDeviceDetection()
  682. {
  683. $request = $this->getMock('Cake\Network\Request', ['is']);
  684. $request->expects($this->once())->method('is')
  685. ->with('mobile')
  686. ->will($this->returnValue(true));
  687. $this->RequestHandler->request = $request;
  688. $this->assertTrue($this->RequestHandler->isMobile());
  689. }
  690. /**
  691. * test that map alias converts aliases to content types.
  692. *
  693. * @return void
  694. */
  695. public function testMapAlias()
  696. {
  697. $result = $this->RequestHandler->mapAlias('xml');
  698. $this->assertEquals('application/xml', $result);
  699. $result = $this->RequestHandler->mapAlias('text/html');
  700. $this->assertNull($result);
  701. $result = $this->RequestHandler->mapAlias('wap');
  702. $this->assertEquals('text/vnd.wap.wml', $result);
  703. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  704. $expected = ['application/xml', 'application/javascript', 'application/json'];
  705. $this->assertEquals($expected, $result);
  706. }
  707. /**
  708. * test accepts() on the component
  709. *
  710. * @return void
  711. */
  712. public function testAccepts()
  713. {
  714. $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');
  715. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  716. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  717. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  718. $this->assertFalse($this->RequestHandler->accepts('rss'));
  719. }
  720. /**
  721. * test accepts and prefers methods.
  722. *
  723. * @return void
  724. */
  725. public function testPrefers()
  726. {
  727. $this->request->env(
  728. 'HTTP_ACCEPT',
  729. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  730. );
  731. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  732. $this->RequestHandler->ext = 'rss';
  733. $this->assertEquals('rss', $this->RequestHandler->prefers());
  734. $this->assertFalse($this->RequestHandler->prefers('xml'));
  735. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  736. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  737. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  738. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  739. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  740. $this->_init();
  741. $this->request->env(
  742. 'HTTP_ACCEPT',
  743. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  744. );
  745. $this->assertEquals('xml', $this->RequestHandler->prefers());
  746. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  747. $this->assertEquals('html', $this->RequestHandler->prefers());
  748. $this->assertFalse($this->RequestHandler->prefers('rss'));
  749. }
  750. /**
  751. * test that AJAX requests involving redirects trigger requestAction instead.
  752. *
  753. * @return void
  754. * @triggers Controller.beforeRedirect $this->Controller
  755. */
  756. public function testAjaxRedirectAsRequestAction()
  757. {
  758. Configure::write('App.namespace', 'TestApp');
  759. Router::connect('/:controller/:action');
  760. $event = new Event('Controller.beforeRedirect', $this->Controller);
  761. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  762. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  763. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  764. $this->Controller->RequestHandler->request = $this->Controller->request;
  765. $this->Controller->RequestHandler->response = $this->Controller->response;
  766. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  767. $response = $this->Controller->RequestHandler->beforeRedirect(
  768. $event,
  769. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  770. $this->Controller->response
  771. );
  772. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  773. }
  774. /**
  775. * Tests that AJAX requests involving redirects don't let the status code bleed through.
  776. *
  777. * @return void
  778. * @triggers Controller.beforeRedirect $this->Controller
  779. */
  780. public function testAjaxRedirectAsRequestActionStatusCode()
  781. {
  782. Configure::write('App.namespace', 'TestApp');
  783. Router::connect('/:controller/:action');
  784. $event = new Event('Controller.beforeRedirect', $this->Controller);
  785. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  786. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  787. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  788. $this->Controller->response->statusCode(302);
  789. $this->Controller->RequestHandler->request = $this->Controller->request;
  790. $this->Controller->RequestHandler->response = $this->Controller->response;
  791. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  792. $response = $this->Controller->RequestHandler->beforeRedirect(
  793. $event,
  794. ['controller' => 'RequestHandlerTest', 'action' => 'destination'],
  795. $this->Controller->response
  796. );
  797. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  798. $this->assertSame(200, $response->statusCode());
  799. }
  800. /**
  801. * test that ajax requests involving redirects don't force no layout
  802. * this would cause the ajax layout to not be rendered.
  803. *
  804. * @return void
  805. * @triggers Controller.beforeRedirect $this->Controller
  806. */
  807. public function testAjaxRedirectAsRequestActionStillRenderingLayout()
  808. {
  809. Configure::write('App.namespace', 'TestApp');
  810. Router::connect('/:controller/:action');
  811. $event = new Event('Controller.beforeRedirect', $this->Controller);
  812. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  813. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  814. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  815. $this->Controller->RequestHandler->request = $this->Controller->request;
  816. $this->Controller->RequestHandler->response = $this->Controller->response;
  817. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  818. $response = $this->Controller->RequestHandler->beforeRedirect(
  819. $event,
  820. ['controller' => 'RequestHandlerTest', 'action' => 'ajax2_layout'],
  821. $this->Controller->response
  822. );
  823. $this->assertRegExp('/posts index/', $response->body(), 'RequestAction redirect failed.');
  824. $this->assertRegExp('/Ajax!/', $response->body(), 'Layout was not rendered.');
  825. }
  826. /**
  827. * test that the beforeRedirect callback properly converts
  828. * array URLs into their correct string ones, and adds base => false so
  829. * the correct URLs are generated.
  830. *
  831. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  832. * @return void
  833. * @triggers Controller.beforeRender $this->Controller
  834. */
  835. public function testBeforeRedirectCallbackWithArrayUrl()
  836. {
  837. Configure::write('App.namespace', 'TestApp');
  838. Router::connect('/:controller/:action/*');
  839. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  840. $event = new Event('Controller.beforeRender', $this->Controller);
  841. Router::setRequestInfo([
  842. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  843. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  844. ]);
  845. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  846. $RequestHandler->request = new Request('posts/index');
  847. $RequestHandler->response = $this->Controller->response;
  848. ob_start();
  849. $RequestHandler->beforeRedirect(
  850. $event,
  851. ['controller' => 'RequestHandlerTest', 'action' => 'param_method', 'first', 'second'],
  852. $this->Controller->response
  853. );
  854. $result = ob_get_clean();
  855. $this->assertEquals('one: first two: second', $result);
  856. }
  857. /**
  858. * testAddInputTypeException method
  859. *
  860. * @expectedException \Cake\Core\Exception\Exception
  861. * @return void
  862. */
  863. public function testAddInputTypeException()
  864. {
  865. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  866. }
  867. /**
  868. * Test checkNotModified method
  869. *
  870. * @return void
  871. * @triggers Controller.beforeRender $this->Controller
  872. */
  873. public function testCheckNotModifiedByEtagStar()
  874. {
  875. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  876. $event = new Event('Controller.beforeRender', $this->Controller);
  877. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  878. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  879. $RequestHandler->response->etag('something');
  880. $RequestHandler->response->expects($this->once())->method('notModified');
  881. $this->assertFalse($RequestHandler->beforeRender($event));
  882. }
  883. /**
  884. * Test checkNotModified method
  885. *
  886. * @return void
  887. * @triggers Controller.beforeRender
  888. */
  889. public function testCheckNotModifiedByEtagExact()
  890. {
  891. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  892. $event = new Event('Controller.beforeRender');
  893. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  894. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  895. $RequestHandler->response->etag('something', true);
  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 $this->Controller
  904. */
  905. public function testCheckNotModifiedByEtagAndTime()
  906. {
  907. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  908. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  909. $event = new Event('Controller.beforeRender', $this->Controller);
  910. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  911. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  912. $RequestHandler->response->etag('something', true);
  913. $RequestHandler->response->modified('2012-01-01 00:00:00');
  914. $RequestHandler->response->expects($this->once())->method('notModified');
  915. $this->assertFalse($RequestHandler->beforeRender($event));
  916. }
  917. /**
  918. * Test checkNotModified method
  919. *
  920. * @return void
  921. * @triggers Controller.beforeRender $this->Controller
  922. */
  923. public function testCheckNotModifiedNoInfo()
  924. {
  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->expects($this->never())->method('notModified');
  929. $this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
  930. }
  931. }