RequestHandlerComponentTest.php 32 KB

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