RequestHandlerComponentTest.php 31 KB

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