RequestHandlerComponentTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  197. $this->assertEquals('xml', $this->RequestHandler->ext);
  198. $this->RequestHandler->ext = null;
  199. Router::extensions(['json', 'xml'], false);
  200. $this->RequestHandler->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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->initialize([]);
  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 mapping a new type and having startup process it.
  431. *
  432. * @return void
  433. * @triggers Controller.startup $this->Controller
  434. */
  435. public function testStartupCustomTypeProcess()
  436. {
  437. if (!function_exists('str_getcsv')) {
  438. $this->markTestSkipped('Need "str_getcsv" for this test.');
  439. }
  440. $event = new Event('Controller.startup', $this->Controller);
  441. $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']);
  442. $this->Controller->request->expects($this->once())
  443. ->method('_readInput')
  444. ->will($this->returnValue('"A","csv","string"'));
  445. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  446. $this->request->env('REQUEST_METHOD', 'POST');
  447. $this->request->env('CONTENT_TYPE', 'text/csv');
  448. $this->RequestHandler->startup($event);
  449. $expected = [
  450. 'A', 'csv', 'string'
  451. ];
  452. $this->assertEquals($expected, $this->Controller->request->data);
  453. }
  454. /**
  455. * testNonAjaxRedirect method
  456. *
  457. * @return void
  458. * @triggers Controller.startup $this->Controller
  459. */
  460. public function testNonAjaxRedirect()
  461. {
  462. $event = new Event('Controller.startup', $this->Controller);
  463. $this->RequestHandler->initialize([]);
  464. $this->RequestHandler->startup($event);
  465. $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
  466. }
  467. /**
  468. * test that redirects with ajax and no URL don't do anything.
  469. *
  470. * @return void
  471. * @triggers Controller.startup $this->Controller
  472. */
  473. public function testAjaxRedirectWithNoUrl()
  474. {
  475. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  476. $event = new Event('Controller.startup', $this->Controller);
  477. $this->Controller->response = $this->getMock('Cake\Network\Response');
  478. $this->Controller->response->expects($this->never())
  479. ->method('body');
  480. $this->RequestHandler->initialize([]);
  481. $this->RequestHandler->startup($event);
  482. $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
  483. }
  484. /**
  485. * testRenderAs method
  486. *
  487. * @return void
  488. */
  489. public function testRenderAs()
  490. {
  491. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  492. $this->RequestHandler->renderAs($this->Controller, 'rss');
  493. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  494. $this->Controller->viewPath = 'request_handler_test\\rss';
  495. $this->RequestHandler->renderAs($this->Controller, 'js');
  496. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
  497. }
  498. /**
  499. * test that attachment headers work with renderAs
  500. *
  501. * @return void
  502. */
  503. public function testRenderAsWithAttachment()
  504. {
  505. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  506. $this->RequestHandler->request->expects($this->any())
  507. ->method('parseAccept')
  508. ->will($this->returnValue(['1.0' => ['application/xml']]));
  509. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download', 'charset']);
  510. $this->RequestHandler->response->expects($this->at(0))
  511. ->method('type')
  512. ->with('application/xml');
  513. $this->RequestHandler->response->expects($this->at(1))
  514. ->method('charset')
  515. ->with('UTF-8');
  516. $this->RequestHandler->response->expects($this->at(2))
  517. ->method('download')
  518. ->with('myfile.xml');
  519. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  520. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  521. }
  522. /**
  523. * test that respondAs works as expected.
  524. *
  525. * @return void
  526. */
  527. public function testRespondAs()
  528. {
  529. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type']);
  530. $this->RequestHandler->response->expects($this->at(0))->method('type')
  531. ->with('application/json');
  532. $this->RequestHandler->response->expects($this->at(1))->method('type')
  533. ->with('text/xml');
  534. $result = $this->RequestHandler->respondAs('json');
  535. $this->assertTrue($result);
  536. $result = $this->RequestHandler->respondAs('text/xml');
  537. $this->assertTrue($result);
  538. }
  539. /**
  540. * test that attachment headers work with respondAs
  541. *
  542. * @return void
  543. */
  544. public function testRespondAsWithAttachment()
  545. {
  546. $this->RequestHandler = $this->getMock(
  547. 'Cake\Controller\Component\RequestHandlerComponent',
  548. ['_header'],
  549. [$this->Controller->components()]
  550. );
  551. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', ['type', 'download']);
  552. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  553. $this->RequestHandler->request->expects($this->once())
  554. ->method('parseAccept')
  555. ->will($this->returnValue(['1.0' => ['application/xml']]));
  556. $this->RequestHandler->response->expects($this->once())->method('download')
  557. ->with('myfile.xml');
  558. $this->RequestHandler->response->expects($this->once())->method('type')
  559. ->with('application/xml');
  560. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  561. $this->assertTrue($result);
  562. }
  563. /**
  564. * test that calling renderAs() more than once continues to work.
  565. *
  566. * @link #6466
  567. * @return void
  568. */
  569. public function testRenderAsCalledTwice()
  570. {
  571. $this->RequestHandler->renderAs($this->Controller, 'print');
  572. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
  573. $this->assertEquals('print', $this->Controller->layoutPath);
  574. $this->RequestHandler->renderAs($this->Controller, 'js');
  575. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
  576. $this->assertEquals('js', $this->Controller->layoutPath);
  577. }
  578. /**
  579. * testRequestContentTypes method
  580. *
  581. * @return void
  582. */
  583. public function testRequestContentTypes()
  584. {
  585. $this->request->env('REQUEST_METHOD', 'GET');
  586. $this->assertNull($this->RequestHandler->requestedWith());
  587. $this->request->env('REQUEST_METHOD', 'POST');
  588. $this->request->env('CONTENT_TYPE', 'application/json');
  589. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  590. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  591. $this->assertEquals('json', $result);
  592. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  593. $this->assertFalse($result);
  594. $this->request->env('REQUEST_METHOD', 'PATCH');
  595. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  596. $this->request->env('REQUEST_METHOD', 'DELETE');
  597. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  598. $this->request->env('REQUEST_METHOD', 'POST');
  599. $this->request->env('CONTENT_TYPE', '');
  600. $this->request->env('HTTP_CONTENT_TYPE', 'application/json');
  601. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  602. $this->assertEquals('json', $result);
  603. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  604. $this->assertFalse($result);
  605. $this->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  606. $this->assertTrue($this->RequestHandler->isXml());
  607. $this->assertFalse($this->RequestHandler->isAtom());
  608. $this->assertFalse($this->RequestHandler->isRSS());
  609. $this->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  610. $this->assertTrue($this->RequestHandler->isAtom());
  611. $this->assertFalse($this->RequestHandler->isRSS());
  612. $this->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  613. $this->assertFalse($this->RequestHandler->isAtom());
  614. $this->assertTrue($this->RequestHandler->isRSS());
  615. $this->assertFalse($this->RequestHandler->isWap());
  616. $this->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  617. $this->assertTrue($this->RequestHandler->isWap());
  618. }
  619. /**
  620. * testResponseContentType method
  621. *
  622. * @return void
  623. */
  624. public function testResponseContentType()
  625. {
  626. $this->assertEquals('html', $this->RequestHandler->responseType());
  627. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  628. $this->assertEquals('atom', $this->RequestHandler->responseType());
  629. }
  630. /**
  631. * testMobileDeviceDetection method
  632. *
  633. * @return void
  634. */
  635. public function testMobileDeviceDetection()
  636. {
  637. $request = $this->getMock('Cake\Network\Request', ['is']);
  638. $request->expects($this->once())->method('is')
  639. ->with('mobile')
  640. ->will($this->returnValue(true));
  641. $this->RequestHandler->request = $request;
  642. $this->assertTrue($this->RequestHandler->isMobile());
  643. }
  644. /**
  645. * test that map alias converts aliases to content types.
  646. *
  647. * @return void
  648. */
  649. public function testMapAlias()
  650. {
  651. $result = $this->RequestHandler->mapAlias('xml');
  652. $this->assertEquals('application/xml', $result);
  653. $result = $this->RequestHandler->mapAlias('text/html');
  654. $this->assertNull($result);
  655. $result = $this->RequestHandler->mapAlias('wap');
  656. $this->assertEquals('text/vnd.wap.wml', $result);
  657. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  658. $expected = ['application/xml', 'application/javascript', 'application/json'];
  659. $this->assertEquals($expected, $result);
  660. }
  661. /**
  662. * test accepts() on the component
  663. *
  664. * @return void
  665. */
  666. public function testAccepts()
  667. {
  668. $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');
  669. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  670. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  671. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  672. $this->assertFalse($this->RequestHandler->accepts('rss'));
  673. }
  674. /**
  675. * test accepts and prefers methods.
  676. *
  677. * @return void
  678. */
  679. public function testPrefers()
  680. {
  681. $this->request->env(
  682. 'HTTP_ACCEPT',
  683. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  684. );
  685. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  686. $this->RequestHandler->ext = 'rss';
  687. $this->assertEquals('rss', $this->RequestHandler->prefers());
  688. $this->assertFalse($this->RequestHandler->prefers('xml'));
  689. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  690. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  691. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  692. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  693. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  694. $this->_init();
  695. $this->request->env(
  696. 'HTTP_ACCEPT',
  697. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  698. );
  699. $this->assertEquals('xml', $this->RequestHandler->prefers());
  700. $this->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  701. $this->assertEquals('html', $this->RequestHandler->prefers());
  702. $this->assertFalse($this->RequestHandler->prefers('rss'));
  703. }
  704. /**
  705. * test that ajax requests involving redirects trigger requestAction instead.
  706. *
  707. * @return void
  708. * @triggers Controller.beforeRedirect $this->Controller
  709. */
  710. public function testAjaxRedirectAsRequestAction()
  711. {
  712. Configure::write('App.namespace', 'TestApp');
  713. Router::connect('/:controller/:action');
  714. $event = new Event('Controller.beforeRedirect', $this->Controller);
  715. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  716. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  717. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  718. $this->Controller->RequestHandler->request = $this->Controller->request;
  719. $this->Controller->RequestHandler->response = $this->Controller->response;
  720. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  721. $this->Controller->response->expects($this->once())->method('stop');
  722. ob_start();
  723. $this->Controller->RequestHandler->beforeRedirect(
  724. $event,
  725. ['controller' => 'request_handler_test', 'action' => 'destination'],
  726. $this->Controller->response
  727. );
  728. $result = ob_get_clean();
  729. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  730. }
  731. /**
  732. * test that ajax requests involving redirects don't force no layout
  733. * this would cause the ajax layout to not be rendered.
  734. *
  735. * @return void
  736. * @triggers Controller.beforeRedirect $this->Controller
  737. */
  738. public function testAjaxRedirectAsRequestActionStillRenderingLayout()
  739. {
  740. Configure::write('App.namespace', 'TestApp');
  741. Router::connect('/:controller/:action');
  742. $event = new Event('Controller.beforeRedirect', $this->Controller);
  743. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  744. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  745. $this->Controller->response = $this->getMock('Cake\Network\Response', ['_sendHeader', 'stop']);
  746. $this->Controller->RequestHandler->request = $this->Controller->request;
  747. $this->Controller->RequestHandler->response = $this->Controller->response;
  748. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  749. $this->Controller->response->expects($this->once())->method('stop');
  750. ob_start();
  751. $this->Controller->RequestHandler->beforeRedirect(
  752. $event,
  753. ['controller' => 'request_handler_test', 'action' => 'ajax2_layout'],
  754. $this->Controller->response
  755. );
  756. $result = ob_get_clean();
  757. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  758. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  759. }
  760. /**
  761. * test that the beforeRedirect callback properly converts
  762. * array URLs into their correct string ones, and adds base => false so
  763. * the correct URLs are generated.
  764. *
  765. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  766. * @return void
  767. * @triggers Controller.beforeRender $this->Controller
  768. */
  769. public function testBeforeRedirectCallbackWithArrayUrl()
  770. {
  771. Configure::write('App.namespace', 'TestApp');
  772. Router::connect('/:controller/:action/*');
  773. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  774. $event = new Event('Controller.beforeRender', $this->Controller);
  775. Router::setRequestInfo([
  776. ['plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => []],
  777. ['base' => '', 'here' => '/accounts/', 'webroot' => '/']
  778. ]);
  779. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  780. $RequestHandler->request = new Request('posts/index');
  781. $RequestHandler->response = $this->Controller->response;
  782. ob_start();
  783. $RequestHandler->beforeRedirect(
  784. $event,
  785. ['controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'],
  786. $this->Controller->response
  787. );
  788. $result = ob_get_clean();
  789. $this->assertEquals('one: first two: second', $result);
  790. }
  791. /**
  792. * testAddInputTypeException method
  793. *
  794. * @expectedException \Cake\Core\Exception\Exception
  795. * @return void
  796. */
  797. public function testAddInputTypeException()
  798. {
  799. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  800. }
  801. /**
  802. * Test checkNotModified method
  803. *
  804. * @return void
  805. * @triggers Controller.beforeRender $this->Controller
  806. */
  807. public function testCheckNotModifiedByEtagStar()
  808. {
  809. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  810. $event = new Event('Controller.beforeRender', $this->Controller);
  811. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  812. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  813. $RequestHandler->response->etag('something');
  814. $RequestHandler->response->expects($this->once())->method('notModified');
  815. $this->assertFalse($RequestHandler->beforeRender($event));
  816. }
  817. /**
  818. * Test checkNotModified method
  819. *
  820. * @return void
  821. * @triggers Controller.beforeRender
  822. */
  823. public function testCheckNotModifiedByEtagExact()
  824. {
  825. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  826. $event = new Event('Controller.beforeRender');
  827. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  828. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  829. $RequestHandler->response->etag('something', true);
  830. $RequestHandler->response->expects($this->once())->method('notModified');
  831. $this->assertFalse($RequestHandler->beforeRender($event));
  832. }
  833. /**
  834. * Test checkNotModified method
  835. *
  836. * @return void
  837. * @triggers Controller.beforeRender $this->Controller
  838. */
  839. public function testCheckNotModifiedByEtagAndTime()
  840. {
  841. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  842. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  843. $event = new Event('Controller.beforeRender', $this->Controller);
  844. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  845. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  846. $RequestHandler->response->etag('something', true);
  847. $RequestHandler->response->modified('2012-01-01 00:00:00');
  848. $RequestHandler->response->expects($this->once())->method('notModified');
  849. $this->assertFalse($RequestHandler->beforeRender($event));
  850. }
  851. /**
  852. * Test checkNotModified method
  853. *
  854. * @return void
  855. * @triggers Controller.beforeRender $this->Controller
  856. */
  857. public function testCheckNotModifiedNoInfo()
  858. {
  859. $event = new Event('Controller.beforeRender', $this->Controller);
  860. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  861. $RequestHandler->response = $this->getMock('Cake\Network\Response', ['notModified', 'stop']);
  862. $RequestHandler->response->expects($this->never())->method('notModified');
  863. $this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
  864. }
  865. }