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('stop'));
  64. $this->Controller = new RequestHandlerTestController($request, $response);
  65. $this->Controller->constructClasses();
  66. $this->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  67. $this->_extensions = Router::extensions();
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. parent::tearDown();
  76. DispatcherFactory::clear();
  77. $this->_init();
  78. unset($this->RequestHandler, $this->Controller);
  79. if (!headers_sent()) {
  80. header('Content-type: text/html'); //reset content type.
  81. }
  82. call_user_func_array('Cake\Routing\Router::parseExtensions', [$this->_extensions, false]);
  83. }
  84. /**
  85. * Test that the constructor sets the config.
  86. *
  87. * @return void
  88. */
  89. public function testConstructorConfig() {
  90. $config = array(
  91. 'viewClassMap' => array('json' => 'MyPlugin.MyJson')
  92. );
  93. $controller = $this->getMock('Cake\Controller\Controller');
  94. $collection = new ComponentRegistry($controller);
  95. $requestHandler = new RequestHandlerComponent($collection, $config);
  96. $this->assertEquals(array('json' => 'MyPlugin.MyJson'), $requestHandler->config('viewClassMap'));
  97. }
  98. /**
  99. * testInitializeCallback method
  100. *
  101. * @return void
  102. */
  103. public function testInitializeCallback() {
  104. $event = new Event('Controller.initialize', $this->Controller);
  105. $this->assertNull($this->RequestHandler->ext);
  106. $this->Controller->request->params['_ext'] = 'rss';
  107. $this->RequestHandler->initialize($event);
  108. $this->assertEquals('rss', $this->RequestHandler->ext);
  109. }
  110. /**
  111. * test that a mapped Accept-type header will set $this->ext correctly.
  112. *
  113. * @return void
  114. */
  115. public function testInitializeContentTypeSettingExt() {
  116. $event = new Event('Controller.initialize', $this->Controller);
  117. $_SERVER['HTTP_ACCEPT'] = 'application/json';
  118. Router::parseExtensions('json', false);
  119. $this->assertNull($this->RequestHandler->ext);
  120. $this->RequestHandler->initialize($event);
  121. $this->assertEquals('json', $this->RequestHandler->ext);
  122. }
  123. /**
  124. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
  125. *
  126. * @return void
  127. */
  128. public function testInitializeContentTypeWithjQueryAccept() {
  129. $_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
  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. public function testJsonViewLoaded() {
  317. Router::parseExtensions(['json', 'xml', 'ajax'], false);
  318. $this->Controller->request->params['_ext'] = 'json';
  319. $event = new Event('Controller.startup', $this->Controller);
  320. $this->RequestHandler->initialize($event);
  321. $this->RequestHandler->startup($event);
  322. $this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
  323. $view = $this->Controller->createView();
  324. $this->assertEquals('json', $view->layoutPath);
  325. $this->assertEquals('json', $view->subDir);
  326. }
  327. /**
  328. * test custom XmlView class is loaded and correct.
  329. */
  330. public function testXmlViewLoaded() {
  331. Router::parseExtensions(['json', 'xml', 'ajax'], false);
  332. $this->Controller->request->params['_ext'] = 'xml';
  333. $event = new Event('Controller.startup', $this->Controller);
  334. $this->RequestHandler->initialize($event);
  335. $this->RequestHandler->startup($event);
  336. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  337. $view = $this->Controller->createView();
  338. $this->assertEquals('xml', $view->layoutPath);
  339. $this->assertEquals('xml', $view->subDir);
  340. }
  341. /**
  342. * test custom AjaxView class is loaded and correct.
  343. */
  344. public function testAjaxViewLoaded() {
  345. Router::parseExtensions(['json', 'xml', 'ajax'], false);
  346. $this->Controller->request->params['_ext'] = 'ajax';
  347. $event = new Event('Controller.startup', $this->Controller);
  348. $this->RequestHandler->initialize($event);
  349. $this->RequestHandler->startup($event);
  350. $this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
  351. $view = $this->Controller->createView();
  352. $this->assertEquals('ajax', $view->layout);
  353. }
  354. /**
  355. * test configured extension but no view class set.
  356. */
  357. public function testNoViewClassExtension() {
  358. Router::parseExtensions(['json', 'xml', 'ajax', 'csv'], false);
  359. $this->Controller->request->params['_ext'] = 'csv';
  360. $event = new Event('Controller.startup', $this->Controller);
  361. $this->RequestHandler->initialize($event);
  362. $this->RequestHandler->startup($event);
  363. $this->assertEquals('RequestHandlerTest/csv', $this->Controller->viewPath);
  364. $this->assertEquals('csv', $this->Controller->layoutPath);
  365. }
  366. /**
  367. * testStartupCallback method
  368. *
  369. * @return void
  370. */
  371. public function testStartupCallback() {
  372. $event = new Event('Controller.startup', $this->Controller);
  373. $_SERVER['REQUEST_METHOD'] = 'PUT';
  374. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  375. $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
  376. $this->RequestHandler->startup($event);
  377. $this->assertTrue(is_array($this->Controller->request->data));
  378. $this->assertFalse(is_object($this->Controller->request->data));
  379. }
  380. /**
  381. * testStartupCallback with charset.
  382. *
  383. * @return void
  384. */
  385. public function testStartupCallbackCharset() {
  386. $event = new Event('Controller.startup', $this->Controller);
  387. $_SERVER['REQUEST_METHOD'] = 'PUT';
  388. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  389. $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
  390. $this->RequestHandler->startup($event);
  391. $this->assertTrue(is_array($this->Controller->request->data));
  392. $this->assertFalse(is_object($this->Controller->request->data));
  393. }
  394. /**
  395. * Test mapping a new type and having startup process it.
  396. *
  397. * @return void
  398. */
  399. public function testStartupCustomTypeProcess() {
  400. if (!function_exists('str_getcsv')) {
  401. $this->markTestSkipped('Need "str_getcsv" for this test.');
  402. }
  403. $event = new Event('Controller.startup', $this->Controller);
  404. $this->Controller->request = $this->getMock('Cake\Network\Request', array('_readInput'));
  405. $this->Controller->request->expects($this->once())
  406. ->method('_readInput')
  407. ->will($this->returnValue('"A","csv","string"'));
  408. $this->RequestHandler->addInputType('csv', array('str_getcsv'));
  409. $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
  410. $this->RequestHandler->request->env('CONTENT_TYPE', 'text/csv');
  411. $this->RequestHandler->startup($event);
  412. $expected = array(
  413. 'A', 'csv', 'string'
  414. );
  415. $this->assertEquals($expected, $this->Controller->request->data);
  416. }
  417. /**
  418. * testNonAjaxRedirect method
  419. *
  420. * @return void
  421. */
  422. public function testNonAjaxRedirect() {
  423. $event = new Event('Controller.startup', $this->Controller);
  424. $this->RequestHandler->initialize($event);
  425. $this->RequestHandler->startup($event);
  426. $this->assertNull($this->RequestHandler->beforeRedirect($event, '/', $this->Controller->response));
  427. }
  428. /**
  429. * test that redirects with ajax and no URL don't do anything.
  430. *
  431. * @return void
  432. */
  433. public function testAjaxRedirectWithNoUrl() {
  434. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  435. $event = new Event('Controller.startup', $this->Controller);
  436. $this->Controller->response = $this->getMock('CakeResponse');
  437. $this->Controller->response->expects($this->never())
  438. ->method('body');
  439. $this->RequestHandler->initialize($event);
  440. $this->RequestHandler->startup($event);
  441. $this->assertNull($this->RequestHandler->beforeRedirect($event, null, $this->Controller->response));
  442. }
  443. /**
  444. * testRenderAs method
  445. *
  446. * @return void
  447. */
  448. public function testRenderAs() {
  449. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  450. $this->RequestHandler->renderAs($this->Controller, 'rss');
  451. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  452. $this->Controller->viewPath = 'request_handler_test\\rss';
  453. $this->RequestHandler->renderAs($this->Controller, 'js');
  454. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
  455. }
  456. /**
  457. * test that attachment headers work with renderAs
  458. *
  459. * @return void
  460. */
  461. public function testRenderAsWithAttachment() {
  462. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  463. $this->RequestHandler->request->expects($this->any())
  464. ->method('parseAccept')
  465. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  466. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type', 'download', 'charset'));
  467. $this->RequestHandler->response->expects($this->at(0))
  468. ->method('type')
  469. ->with('application/xml');
  470. $this->RequestHandler->response->expects($this->at(1))
  471. ->method('charset')
  472. ->with('UTF-8');
  473. $this->RequestHandler->response->expects($this->at(2))
  474. ->method('download')
  475. ->with('myfile.xml');
  476. $this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
  477. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  478. }
  479. /**
  480. * test that respondAs works as expected.
  481. *
  482. * @return void
  483. */
  484. public function testRespondAs() {
  485. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type'));
  486. $this->RequestHandler->response->expects($this->at(0))->method('type')
  487. ->with('application/json');
  488. $this->RequestHandler->response->expects($this->at(1))->method('type')
  489. ->with('text/xml');
  490. $result = $this->RequestHandler->respondAs('json');
  491. $this->assertTrue($result);
  492. $result = $this->RequestHandler->respondAs('text/xml');
  493. $this->assertTrue($result);
  494. }
  495. /**
  496. * test that attachment headers work with respondAs
  497. *
  498. * @return void
  499. */
  500. public function testRespondAsWithAttachment() {
  501. $this->RequestHandler = $this->getMock(
  502. 'Cake\Controller\Component\RequestHandlerComponent',
  503. array('_header'),
  504. array($this->Controller->components())
  505. );
  506. $this->RequestHandler->response = $this->getMock('Cake\Network\Response', array('type', 'download'));
  507. $this->RequestHandler->request = $this->getMock('Cake\Network\Request', ['parseAccept']);
  508. $this->RequestHandler->request->expects($this->once())
  509. ->method('parseAccept')
  510. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  511. $this->RequestHandler->response->expects($this->once())->method('download')
  512. ->with('myfile.xml');
  513. $this->RequestHandler->response->expects($this->once())->method('type')
  514. ->with('application/xml');
  515. $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  516. $this->assertTrue($result);
  517. }
  518. /**
  519. * test that calling renderAs() more than once continues to work.
  520. *
  521. * @link #6466
  522. * @return void
  523. */
  524. public function testRenderAsCalledTwice() {
  525. $this->RequestHandler->renderAs($this->Controller, 'print');
  526. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
  527. $this->assertEquals('print', $this->Controller->layoutPath);
  528. $this->RequestHandler->renderAs($this->Controller, 'js');
  529. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
  530. $this->assertEquals('js', $this->Controller->layoutPath);
  531. }
  532. /**
  533. * testRequestClientTypes method
  534. *
  535. * @return void
  536. */
  537. public function testRequestClientTypes() {
  538. $this->RequestHandler->request->env('HTTP_X_PROTOTYPE_VERSION', '1.5');
  539. $this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion());
  540. $this->RequestHandler->request->env('HTTP_X_REQUESTED_WITH', false);
  541. $this->RequestHandler->request->env('HTTP_X_PROTOTYPE_VERSION', false);
  542. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  543. }
  544. /**
  545. * testRequestContentTypes method
  546. *
  547. * @return void
  548. */
  549. public function testRequestContentTypes() {
  550. $this->RequestHandler->request->env('REQUEST_METHOD', 'GET');
  551. $this->assertNull($this->RequestHandler->requestedWith());
  552. $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
  553. $this->RequestHandler->request->env('CONTENT_TYPE', 'application/json');
  554. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  555. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  556. $this->assertEquals('json', $result);
  557. $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
  558. $this->assertFalse($result);
  559. $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
  560. $this->RequestHandler->request->env('CONTENT_TYPE', '');
  561. $this->RequestHandler->request->env('HTTP_CONTENT_TYPE', 'application/json');
  562. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  563. $this->assertEquals('json', $result);
  564. $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
  565. $this->assertFalse($result);
  566. $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  567. $this->assertTrue($this->RequestHandler->isXml());
  568. $this->assertFalse($this->RequestHandler->isAtom());
  569. $this->assertFalse($this->RequestHandler->isRSS());
  570. $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  571. $this->assertTrue($this->RequestHandler->isAtom());
  572. $this->assertFalse($this->RequestHandler->isRSS());
  573. $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  574. $this->assertFalse($this->RequestHandler->isAtom());
  575. $this->assertTrue($this->RequestHandler->isRSS());
  576. $this->assertFalse($this->RequestHandler->isWap());
  577. $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  578. $this->assertTrue($this->RequestHandler->isWap());
  579. }
  580. /**
  581. * testResponseContentType method
  582. *
  583. * @return void
  584. */
  585. public function testResponseContentType() {
  586. $this->assertEquals('html', $this->RequestHandler->responseType());
  587. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  588. $this->assertEquals('atom', $this->RequestHandler->responseType());
  589. }
  590. /**
  591. * testMobileDeviceDetection method
  592. *
  593. * @return void
  594. */
  595. public function testMobileDeviceDetection() {
  596. $request = $this->getMock('Cake\Network\Request', ['is']);
  597. $request->expects($this->once())->method('is')
  598. ->with('mobile')
  599. ->will($this->returnValue(true));
  600. $this->RequestHandler->request = $request;
  601. $this->assertTrue($this->RequestHandler->isMobile());
  602. }
  603. /**
  604. * test that map alias converts aliases to content types.
  605. *
  606. * @return void
  607. */
  608. public function testMapAlias() {
  609. $result = $this->RequestHandler->mapAlias('xml');
  610. $this->assertEquals('application/xml', $result);
  611. $result = $this->RequestHandler->mapAlias('text/html');
  612. $this->assertNull($result);
  613. $result = $this->RequestHandler->mapAlias('wap');
  614. $this->assertEquals('text/vnd.wap.wml', $result);
  615. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  616. $expected = array('application/xml', 'application/javascript', 'application/json');
  617. $this->assertEquals($expected, $result);
  618. }
  619. /**
  620. * test accepts() on the component
  621. *
  622. * @return void
  623. */
  624. public function testAccepts() {
  625. $_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';
  626. $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  627. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  628. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  629. $this->assertFalse($this->RequestHandler->accepts('rss'));
  630. }
  631. /**
  632. * test accepts and prefers methods.
  633. *
  634. * @return void
  635. */
  636. public function testPrefers() {
  637. $this->RequestHandler->request->env(
  638. 'HTTP_ACCEPT',
  639. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  640. );
  641. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  642. $this->RequestHandler->ext = 'rss';
  643. $this->assertEquals('rss', $this->RequestHandler->prefers());
  644. $this->assertFalse($this->RequestHandler->prefers('xml'));
  645. $this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
  646. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  647. $this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
  648. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  649. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  650. $this->_init();
  651. $this->RequestHandler->request->env(
  652. 'HTTP_ACCEPT',
  653. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  654. );
  655. $this->assertEquals('xml', $this->RequestHandler->prefers());
  656. $this->RequestHandler->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  657. $this->assertEquals('html', $this->RequestHandler->prefers());
  658. $this->assertFalse($this->RequestHandler->prefers('rss'));
  659. }
  660. /**
  661. * test that ajax requests involving redirects trigger requestAction instead.
  662. *
  663. * @return void
  664. */
  665. public function testAjaxRedirectAsRequestAction() {
  666. Configure::write('App.namespace', 'TestApp');
  667. Router::connect('/:controller/:action');
  668. $event = new Event('Controller.beforeRedirect', $this->Controller);
  669. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  670. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  671. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  672. $this->Controller->RequestHandler->request = $this->Controller->request;
  673. $this->Controller->RequestHandler->response = $this->Controller->response;
  674. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  675. $this->Controller->response->expects($this->once())->method('stop');
  676. ob_start();
  677. $this->Controller->RequestHandler->beforeRedirect(
  678. $event,
  679. array('controller' => 'request_handler_test', 'action' => 'destination'),
  680. $this->Controller->response
  681. );
  682. $result = ob_get_clean();
  683. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  684. }
  685. /**
  686. * test that ajax requests involving redirects don't force no layout
  687. * this would cause the ajax layout to not be rendered.
  688. *
  689. * @return void
  690. */
  691. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  692. Configure::write('App.namespace', 'TestApp');
  693. Router::connect('/:controller/:action');
  694. $event = new Event('Controller.beforeRedirect', $this->Controller);
  695. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  696. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  697. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  698. $this->Controller->RequestHandler->request = $this->Controller->request;
  699. $this->Controller->RequestHandler->response = $this->Controller->response;
  700. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  701. $this->Controller->response->expects($this->once())->method('stop');
  702. ob_start();
  703. $this->Controller->RequestHandler->beforeRedirect(
  704. $event,
  705. array('controller' => 'request_handler_test', 'action' => 'ajax2_layout'),
  706. $this->Controller->response
  707. );
  708. $result = ob_get_clean();
  709. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  710. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  711. }
  712. /**
  713. * test that the beforeRedirect callback properly converts
  714. * array URLs into their correct string ones, and adds base => false so
  715. * the correct URLs are generated.
  716. *
  717. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  718. * @return void
  719. */
  720. public function testBeforeRedirectCallbackWithArrayUrl() {
  721. Configure::write('App.namespace', 'TestApp');
  722. Router::connect('/:controller/:action/*');
  723. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  724. $event = new Event('Controller.beforeRender', $this->Controller);
  725. Router::setRequestInfo(array(
  726. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
  727. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  728. ));
  729. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  730. $RequestHandler->request = new Request('posts/index');
  731. $RequestHandler->response = $this->Controller->response;
  732. ob_start();
  733. $RequestHandler->beforeRedirect(
  734. $event,
  735. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'),
  736. $this->Controller->response
  737. );
  738. $result = ob_get_clean();
  739. $this->assertEquals('one: first two: second', $result);
  740. }
  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. }