RequestHandlerComponentTest.php 30 KB

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