RequestHandlerComponentTest.php 31 KB

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