RequestHandlerComponentTest.php 29 KB

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