RequestHandlerComponentTest.php 31 KB

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