RequestHandlerComponentTest.php 30 KB

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