RequestHandlerComponentTest.php 31 KB

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