RequestHandlerComponentTest.php 31 KB

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