RequestHandlerComponentTest.php 30 KB

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