RequestHandlerComponentTest.php 30 KB

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