RequestHandlerComponentTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. * @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. * testRequestClientTypes method
  542. *
  543. * @return void
  544. */
  545. public function testRequestClientTypes() {
  546. $this->RequestHandler->request->env('HTTP_X_PROTOTYPE_VERSION', '1.5');
  547. $this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion());
  548. $this->RequestHandler->request->env('HTTP_X_REQUESTED_WITH', false);
  549. $this->RequestHandler->request->env('HTTP_X_PROTOTYPE_VERSION', false);
  550. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  551. }
  552. /**
  553. * testRequestContentTypes method
  554. *
  555. * @return void
  556. */
  557. public function testRequestContentTypes() {
  558. $this->RequestHandler->request->env('REQUEST_METHOD', 'GET');
  559. $this->assertNull($this->RequestHandler->requestedWith());
  560. $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
  561. $this->RequestHandler->request->env('CONTENT_TYPE', 'application/json');
  562. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  563. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  564. $this->assertEquals('json', $result);
  565. $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
  566. $this->assertFalse($result);
  567. $this->RequestHandler->request->env('REQUEST_METHOD', 'POST');
  568. $this->RequestHandler->request->env('CONTENT_TYPE', '');
  569. $this->RequestHandler->request->env('HTTP_CONTENT_TYPE', 'application/json');
  570. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  571. $this->assertEquals('json', $result);
  572. $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
  573. $this->assertFalse($result);
  574. $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  575. $this->assertTrue($this->RequestHandler->isXml());
  576. $this->assertFalse($this->RequestHandler->isAtom());
  577. $this->assertFalse($this->RequestHandler->isRSS());
  578. $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  579. $this->assertTrue($this->RequestHandler->isAtom());
  580. $this->assertFalse($this->RequestHandler->isRSS());
  581. $this->RequestHandler->request->env('HTTP_ACCEPT', 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*');
  582. $this->assertFalse($this->RequestHandler->isAtom());
  583. $this->assertTrue($this->RequestHandler->isRSS());
  584. $this->assertFalse($this->RequestHandler->isWap());
  585. $this->RequestHandler->request->env('HTTP_ACCEPT', 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*');
  586. $this->assertTrue($this->RequestHandler->isWap());
  587. }
  588. /**
  589. * testResponseContentType method
  590. *
  591. * @return void
  592. */
  593. public function testResponseContentType() {
  594. $this->assertEquals('html', $this->RequestHandler->responseType());
  595. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  596. $this->assertEquals('atom', $this->RequestHandler->responseType());
  597. }
  598. /**
  599. * testMobileDeviceDetection method
  600. *
  601. * @return void
  602. */
  603. public function testMobileDeviceDetection() {
  604. $request = $this->getMock('Cake\Network\Request', ['is']);
  605. $request->expects($this->once())->method('is')
  606. ->with('mobile')
  607. ->will($this->returnValue(true));
  608. $this->RequestHandler->request = $request;
  609. $this->assertTrue($this->RequestHandler->isMobile());
  610. }
  611. /**
  612. * test that map alias converts aliases to content types.
  613. *
  614. * @return void
  615. */
  616. public function testMapAlias() {
  617. $result = $this->RequestHandler->mapAlias('xml');
  618. $this->assertEquals('application/xml', $result);
  619. $result = $this->RequestHandler->mapAlias('text/html');
  620. $this->assertNull($result);
  621. $result = $this->RequestHandler->mapAlias('wap');
  622. $this->assertEquals('text/vnd.wap.wml', $result);
  623. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  624. $expected = array('application/xml', 'application/javascript', 'application/json');
  625. $this->assertEquals($expected, $result);
  626. }
  627. /**
  628. * test accepts() on the component
  629. *
  630. * @return void
  631. */
  632. public function testAccepts() {
  633. $_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';
  634. $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  635. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  636. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  637. $this->assertFalse($this->RequestHandler->accepts('rss'));
  638. }
  639. /**
  640. * test accepts and prefers methods.
  641. *
  642. * @return void
  643. */
  644. public function testPrefers() {
  645. $this->RequestHandler->request->env(
  646. 'HTTP_ACCEPT',
  647. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  648. );
  649. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  650. $this->RequestHandler->ext = 'rss';
  651. $this->assertEquals('rss', $this->RequestHandler->prefers());
  652. $this->assertFalse($this->RequestHandler->prefers('xml'));
  653. $this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
  654. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  655. $this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
  656. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  657. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  658. $this->_init();
  659. $this->RequestHandler->request->env(
  660. 'HTTP_ACCEPT',
  661. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  662. );
  663. $this->assertEquals('xml', $this->RequestHandler->prefers());
  664. $this->RequestHandler->request->env('HTTP_ACCEPT', '*/*;q=0.5');
  665. $this->assertEquals('html', $this->RequestHandler->prefers());
  666. $this->assertFalse($this->RequestHandler->prefers('rss'));
  667. }
  668. /**
  669. * test that ajax requests involving redirects trigger requestAction instead.
  670. *
  671. * @return void
  672. */
  673. public function testAjaxRedirectAsRequestAction() {
  674. Configure::write('App.namespace', 'TestApp');
  675. Router::connect('/:controller/:action');
  676. $event = new Event('Controller.beforeRedirect', $this->Controller);
  677. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  678. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  679. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  680. $this->Controller->RequestHandler->request = $this->Controller->request;
  681. $this->Controller->RequestHandler->response = $this->Controller->response;
  682. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  683. $this->Controller->response->expects($this->once())->method('stop');
  684. ob_start();
  685. $this->Controller->RequestHandler->beforeRedirect(
  686. $event,
  687. array('controller' => 'request_handler_test', 'action' => 'destination'),
  688. $this->Controller->response
  689. );
  690. $result = ob_get_clean();
  691. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  692. }
  693. /**
  694. * test that ajax requests involving redirects don't force no layout
  695. * this would cause the ajax layout to not be rendered.
  696. *
  697. * @return void
  698. */
  699. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  700. Configure::write('App.namespace', 'TestApp');
  701. Router::connect('/:controller/:action');
  702. $event = new Event('Controller.beforeRedirect', $this->Controller);
  703. $this->Controller->RequestHandler = new RequestHandlerComponent($this->Controller->components());
  704. $this->Controller->request = $this->getMock('Cake\Network\Request', ['is']);
  705. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
  706. $this->Controller->RequestHandler->request = $this->Controller->request;
  707. $this->Controller->RequestHandler->response = $this->Controller->response;
  708. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  709. $this->Controller->response->expects($this->once())->method('stop');
  710. ob_start();
  711. $this->Controller->RequestHandler->beforeRedirect(
  712. $event,
  713. array('controller' => 'request_handler_test', 'action' => 'ajax2_layout'),
  714. $this->Controller->response
  715. );
  716. $result = ob_get_clean();
  717. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  718. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  719. }
  720. /**
  721. * test that the beforeRedirect callback properly converts
  722. * array URLs into their correct string ones, and adds base => false so
  723. * the correct URLs are generated.
  724. *
  725. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  726. * @return void
  727. */
  728. public function testBeforeRedirectCallbackWithArrayUrl() {
  729. Configure::write('App.namespace', 'TestApp');
  730. Router::connect('/:controller/:action/*');
  731. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  732. $event = new Event('Controller.beforeRender', $this->Controller);
  733. Router::setRequestInfo(array(
  734. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array()),
  735. array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
  736. ));
  737. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  738. $RequestHandler->request = new Request('posts/index');
  739. $RequestHandler->response = $this->Controller->response;
  740. ob_start();
  741. $RequestHandler->beforeRedirect(
  742. $event,
  743. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second'),
  744. $this->Controller->response
  745. );
  746. $result = ob_get_clean();
  747. $this->assertEquals('one: first two: second', $result);
  748. }
  749. /**
  750. * testAddInputTypeException method
  751. *
  752. * @expectedException \Cake\Error\Exception
  753. * @return void
  754. */
  755. public function testAddInputTypeException() {
  756. $this->RequestHandler->addInputType('csv', array('I am not callable'));
  757. }
  758. /**
  759. * Test checkNotModified method
  760. *
  761. * @return void
  762. */
  763. public function testCheckNotModifiedByEtagStar() {
  764. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  765. $event = new Event('Controller.beforeRender', $this->Controller);
  766. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  767. $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
  768. $RequestHandler->response->etag('something');
  769. $RequestHandler->response->expects($this->once())->method('notModified');
  770. $this->assertFalse($RequestHandler->beforeRender($event));
  771. }
  772. /**
  773. * Test checkNotModified method
  774. *
  775. * @return void
  776. */
  777. public function testCheckNotModifiedByEtagExact() {
  778. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  779. $event = new Event('Controller.beforeRender');
  780. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  781. $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
  782. $RequestHandler->response->etag('something', true);
  783. $RequestHandler->response->expects($this->once())->method('notModified');
  784. $this->assertFalse($RequestHandler->beforeRender($event));
  785. }
  786. /**
  787. * Test checkNotModified method
  788. *
  789. * @return void
  790. */
  791. public function testCheckNotModifiedByEtagAndTime() {
  792. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  793. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  794. $event = new Event('Controller.beforeRender', $this->Controller);
  795. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  796. $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
  797. $RequestHandler->response->etag('something', true);
  798. $RequestHandler->response->modified('2012-01-01 00:00:00');
  799. $RequestHandler->response->expects($this->once())->method('notModified');
  800. $this->assertFalse($RequestHandler->beforeRender($event));
  801. }
  802. /**
  803. * Test checkNotModified method
  804. *
  805. * @return void
  806. */
  807. public function testCheckNotModifiedNoInfo() {
  808. $event = new Event('Controller.beforeRender', $this->Controller);
  809. $RequestHandler = new RequestHandlerComponent($this->Controller->components());
  810. $RequestHandler->response = $this->getMock('Cake\Network\Response', array('notModified', 'stop'));
  811. $RequestHandler->response->expects($this->never())->method('notModified');
  812. $this->assertNull($RequestHandler->beforeRender($event, '', $RequestHandler->response));
  813. }
  814. }