RequestHandlerComponentTest.php 29 KB

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