RequestHandlerComponentTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 a type mismatch doesn't incorrectly set the ext
  249. *
  250. * @return void
  251. */
  252. public function testInitializeContentTypeAndExtensionMismatch() {
  253. $this->assertNull($this->RequestHandler->ext);
  254. $extensions = Router::extensions();
  255. Router::parseExtensions('xml');
  256. $this->Controller->request = $this->getMock('CakeRequest');
  257. $this->Controller->request->expects($this->any())
  258. ->method('accepts')
  259. ->will($this->returnValue(array('application/json')));
  260. $this->RequestHandler->initialize($this->Controller);
  261. $this->assertNull($this->RequestHandler->ext);
  262. call_user_func_array(array('Router', 'parseExtensions'), $extensions);
  263. }
  264. /**
  265. * testViewClassMap method
  266. *
  267. * @return void
  268. */
  269. public function testViewClassMap() {
  270. $this->RequestHandler->settings = array('viewClassMap' => array('json' => 'CustomJson'));
  271. $this->RequestHandler->initialize($this->Controller);
  272. $result = $this->RequestHandler->viewClassMap();
  273. $expected = array(
  274. 'json' => 'CustomJson',
  275. 'xml' => 'Xml'
  276. );
  277. $this->assertEquals($expected, $result);
  278. $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
  279. $expected = array(
  280. 'json' => 'CustomJson',
  281. 'xml' => 'Xml',
  282. 'xls' => 'Excel.Excel'
  283. );
  284. $this->assertEquals($expected, $result);
  285. $this->RequestHandler->renderAs($this->Controller, 'json');
  286. $this->assertEquals('CustomJson', $this->Controller->viewClass);
  287. }
  288. /**
  289. * testDisabling method
  290. *
  291. * @return void
  292. */
  293. public function testDisabling() {
  294. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  295. $this->_init();
  296. $this->RequestHandler->initialize($this->Controller);
  297. $this->Controller->beforeFilter();
  298. $this->RequestHandler->startup($this->Controller);
  299. $this->assertEquals(true, $this->Controller->params['isAjax']);
  300. }
  301. /**
  302. * testAutoResponseType method
  303. *
  304. * @return void
  305. */
  306. public function testAutoResponseType() {
  307. $this->Controller->ext = '.thtml';
  308. $this->Controller->request->params['ext'] = 'rss';
  309. $this->RequestHandler->initialize($this->Controller);
  310. $this->RequestHandler->startup($this->Controller);
  311. $this->assertEquals('.ctp', $this->Controller->ext);
  312. }
  313. /**
  314. * testAutoAjaxLayout method
  315. *
  316. * @return void
  317. */
  318. public function testAutoAjaxLayout() {
  319. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  320. $this->RequestHandler->startup($this->Controller);
  321. $this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout);
  322. $this->_init();
  323. $this->Controller->request->params['ext'] = 'js';
  324. $this->RequestHandler->initialize($this->Controller);
  325. $this->RequestHandler->startup($this->Controller);
  326. $this->assertNotEquals('ajax', $this->Controller->layout);
  327. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  328. }
  329. /**
  330. * testStartupCallback method
  331. *
  332. * @return void
  333. */
  334. public function testStartupCallback() {
  335. $_SERVER['REQUEST_METHOD'] = 'PUT';
  336. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  337. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  338. $this->RequestHandler->startup($this->Controller);
  339. $this->assertTrue(is_array($this->Controller->data));
  340. $this->assertFalse(is_object($this->Controller->data));
  341. }
  342. /**
  343. * testStartupCallback with charset.
  344. *
  345. * @return void
  346. */
  347. public function testStartupCallbackCharset() {
  348. $_SERVER['REQUEST_METHOD'] = 'PUT';
  349. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  350. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  351. $this->RequestHandler->startup($this->Controller);
  352. $this->assertTrue(is_array($this->Controller->data));
  353. $this->assertFalse(is_object($this->Controller->data));
  354. }
  355. /**
  356. * Test mapping a new type and having startup process it.
  357. *
  358. * @return void
  359. */
  360. public function testStartupCustomTypeProcess() {
  361. if (!function_exists('str_getcsv')) {
  362. $this->markTestSkipped('Need "str_getcsv" for this test.');
  363. }
  364. $_SERVER['REQUEST_METHOD'] = 'POST';
  365. $_SERVER['CONTENT_TYPE'] = 'text/csv';
  366. $this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
  367. $this->Controller->request->expects($this->once())
  368. ->method('_readInput')
  369. ->will($this->returnValue('"A","csv","string"'));
  370. $this->RequestHandler->addInputType('csv', array('str_getcsv'));
  371. $this->RequestHandler->startup($this->Controller);
  372. $expected = array(
  373. 'A', 'csv', 'string'
  374. );
  375. $this->assertEquals($expected, $this->Controller->request->data);
  376. }
  377. /**
  378. * testNonAjaxRedirect method
  379. *
  380. * @return void
  381. */
  382. public function testNonAjaxRedirect() {
  383. $this->RequestHandler->initialize($this->Controller);
  384. $this->RequestHandler->startup($this->Controller);
  385. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
  386. }
  387. /**
  388. * test that redirects with ajax and no url don't do anything.
  389. *
  390. * @return void
  391. */
  392. public function testAjaxRedirectWithNoUrl() {
  393. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  394. $this->Controller->response = $this->getMock('CakeResponse');
  395. $this->Controller->response->expects($this->never())
  396. ->method('body');
  397. $this->RequestHandler->initialize($this->Controller);
  398. $this->RequestHandler->startup($this->Controller);
  399. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, null));
  400. }
  401. /**
  402. * testRenderAs method
  403. *
  404. * @return void
  405. */
  406. public function testRenderAs() {
  407. $this->assertFalse(in_array('Rss', $this->Controller->helpers));
  408. $this->RequestHandler->renderAs($this->Controller, 'rss');
  409. $this->assertTrue(in_array('Rss', $this->Controller->helpers));
  410. $this->Controller->viewPath = 'request_handler_test\\rss';
  411. $this->RequestHandler->renderAs($this->Controller, 'js');
  412. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
  413. }
  414. /**
  415. * test that attachment headers work with renderAs
  416. *
  417. * @return void
  418. */
  419. public function testRenderAsWithAttachment() {
  420. $this->RequestHandler->request = $this->getMock('CakeRequest');
  421. $this->RequestHandler->request->expects($this->any())
  422. ->method('parseAccept')
  423. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  424. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
  425. $this->RequestHandler->response->expects($this->at(0))
  426. ->method('type')
  427. ->with('application/xml');
  428. $this->RequestHandler->response->expects($this->at(1))
  429. ->method('charset')
  430. ->with('UTF-8');
  431. $this->RequestHandler->response->expects($this->at(2))
  432. ->method('download')
  433. ->with('myfile.xml');
  434. $this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
  435. $this->assertEquals('Xml', $this->Controller->viewClass);
  436. }
  437. /**
  438. * test that respondAs works as expected.
  439. *
  440. * @return void
  441. */
  442. public function testRespondAs() {
  443. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
  444. $this->RequestHandler->response->expects($this->at(0))->method('type')
  445. ->with('application/json');
  446. $this->RequestHandler->response->expects($this->at(1))->method('type')
  447. ->with('text/xml');
  448. $result = $this->RequestHandler->respondAs('json');
  449. $this->assertTrue($result);
  450. $result = $this->RequestHandler->respondAs('text/xml');
  451. $this->assertTrue($result);
  452. }
  453. /**
  454. * test that attachment headers work with respondAs
  455. *
  456. * @return void
  457. */
  458. public function testRespondAsWithAttachment() {
  459. $this->RequestHandler = $this->getMock(
  460. 'RequestHandlerComponent',
  461. array('_header'),
  462. array(&$this->Controller->Components)
  463. );
  464. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
  465. $this->RequestHandler->request = $this->getMock('CakeRequest');
  466. $this->RequestHandler->request->expects($this->once())
  467. ->method('parseAccept')
  468. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  469. $this->RequestHandler->response->expects($this->once())->method('download')
  470. ->with('myfile.xml');
  471. $this->RequestHandler->response->expects($this->once())->method('type')
  472. ->with('application/xml');
  473. $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  474. $this->assertTrue($result);
  475. }
  476. /**
  477. * test that calling renderAs() more than once continues to work.
  478. *
  479. * @link #6466
  480. * @return void
  481. */
  482. public function testRenderAsCalledTwice() {
  483. $this->RequestHandler->renderAs($this->Controller, 'print');
  484. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
  485. $this->assertEquals('print', $this->Controller->layoutPath);
  486. $this->RequestHandler->renderAs($this->Controller, 'js');
  487. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
  488. $this->assertEquals('js', $this->Controller->layoutPath);
  489. $this->assertTrue(in_array('Js', $this->Controller->helpers));
  490. }
  491. /**
  492. * testRequestClientTypes method
  493. *
  494. * @return void
  495. */
  496. public function testRequestClientTypes() {
  497. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  498. $this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion());
  499. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  500. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  501. }
  502. /**
  503. * Tests the detection of various Flash versions
  504. *
  505. * @return void
  506. */
  507. public function testFlashDetection() {
  508. $request = $this->getMock('CakeRequest');
  509. $request->expects($this->once())->method('is')
  510. ->with('flash')
  511. ->will($this->returnValue(true));
  512. $this->RequestHandler->request = $request;
  513. $this->assertTrue($this->RequestHandler->isFlash());
  514. }
  515. /**
  516. * testRequestContentTypes method
  517. *
  518. * @return void
  519. */
  520. public function testRequestContentTypes() {
  521. $_SERVER['REQUEST_METHOD'] = 'GET';
  522. $this->assertNull($this->RequestHandler->requestedWith());
  523. $_SERVER['REQUEST_METHOD'] = 'POST';
  524. $_SERVER['CONTENT_TYPE'] = 'application/json';
  525. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  526. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  527. $this->assertEquals('json', $result);
  528. $result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
  529. $this->assertFalse($result);
  530. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  531. $this->assertTrue($this->RequestHandler->isXml());
  532. $this->assertFalse($this->RequestHandler->isAtom());
  533. $this->assertFalse($this->RequestHandler->isRSS());
  534. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  535. $this->assertTrue($this->RequestHandler->isAtom());
  536. $this->assertFalse($this->RequestHandler->isRSS());
  537. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  538. $this->assertFalse($this->RequestHandler->isAtom());
  539. $this->assertTrue($this->RequestHandler->isRSS());
  540. $this->assertFalse($this->RequestHandler->isWap());
  541. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  542. $this->assertTrue($this->RequestHandler->isWap());
  543. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  544. }
  545. /**
  546. * testResponseContentType method
  547. *
  548. * @return void
  549. */
  550. public function testResponseContentType() {
  551. $this->assertEquals('html', $this->RequestHandler->responseType());
  552. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  553. $this->assertEquals('atom', $this->RequestHandler->responseType());
  554. }
  555. /**
  556. * testMobileDeviceDetection method
  557. *
  558. * @return void
  559. */
  560. public function testMobileDeviceDetection() {
  561. $request = $this->getMock('CakeRequest');
  562. $request->expects($this->once())->method('is')
  563. ->with('mobile')
  564. ->will($this->returnValue(true));
  565. $this->RequestHandler->request = $request;
  566. $this->assertTrue($this->RequestHandler->isMobile());
  567. }
  568. /**
  569. * testRequestProperties method
  570. *
  571. * @return void
  572. */
  573. public function testRequestProperties() {
  574. $request = $this->getMock('CakeRequest');
  575. $request->expects($this->once())->method('is')
  576. ->with('ssl')
  577. ->will($this->returnValue(true));
  578. $this->RequestHandler->request = $request;
  579. $this->assertTrue($this->RequestHandler->isSsl());
  580. }
  581. /**
  582. * testRequestMethod method
  583. *
  584. * @return void
  585. */
  586. public function testRequestMethod() {
  587. $request = $this->getMock('CakeRequest');
  588. $request->expects($this->at(0))->method('is')
  589. ->with('get')
  590. ->will($this->returnValue(true));
  591. $request->expects($this->at(1))->method('is')
  592. ->with('post')
  593. ->will($this->returnValue(false));
  594. $request->expects($this->at(2))->method('is')
  595. ->with('delete')
  596. ->will($this->returnValue(true));
  597. $request->expects($this->at(3))->method('is')
  598. ->with('put')
  599. ->will($this->returnValue(false));
  600. $this->RequestHandler->request = $request;
  601. $this->assertTrue($this->RequestHandler->isGet());
  602. $this->assertFalse($this->RequestHandler->isPost());
  603. $this->assertTrue($this->RequestHandler->isDelete());
  604. $this->assertFalse($this->RequestHandler->isPut());
  605. }
  606. /**
  607. * test that map alias converts aliases to content types.
  608. *
  609. * @return void
  610. */
  611. public function testMapAlias() {
  612. $result = $this->RequestHandler->mapAlias('xml');
  613. $this->assertEquals('application/xml', $result);
  614. $result = $this->RequestHandler->mapAlias('text/html');
  615. $this->assertNull($result);
  616. $result = $this->RequestHandler->mapAlias('wap');
  617. $this->assertEquals('text/vnd.wap.wml', $result);
  618. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  619. $expected = array('application/xml', 'application/javascript', 'application/json');
  620. $this->assertEquals($expected, $result);
  621. }
  622. /**
  623. * test accepts() on the component
  624. *
  625. * @return void
  626. */
  627. public function testAccepts() {
  628. $_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';
  629. $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  630. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  631. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  632. $this->assertFalse($this->RequestHandler->accepts('rss'));
  633. }
  634. /**
  635. * test accepts and prefers methods.
  636. *
  637. * @return void
  638. */
  639. public function testPrefers() {
  640. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  641. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  642. $this->RequestHandler->ext = 'rss';
  643. $this->assertEquals('rss', $this->RequestHandler->prefers());
  644. $this->assertFalse($this->RequestHandler->prefers('xml'));
  645. $this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
  646. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  647. $this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
  648. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  649. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  650. $_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';
  651. $this->_init();
  652. $this->assertEquals('xml', $this->RequestHandler->prefers());
  653. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  654. $this->assertEquals('html', $this->RequestHandler->prefers());
  655. $this->assertFalse($this->RequestHandler->prefers('rss'));
  656. }
  657. /**
  658. * testCustomContent method
  659. *
  660. * @return void
  661. */
  662. public function testCustomContent() {
  663. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  664. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  665. $this->RequestHandler->startup($this->Controller);
  666. $this->assertEquals('mobile', $this->RequestHandler->prefers());
  667. }
  668. /**
  669. * testClientProperties method
  670. *
  671. * @return void
  672. */
  673. public function testClientProperties() {
  674. $request = $this->getMock('CakeRequest');
  675. $request->expects($this->once())->method('referer');
  676. $request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
  677. $this->RequestHandler->request = $request;
  678. $this->RequestHandler->getReferer();
  679. $this->RequestHandler->getClientIP(false);
  680. }
  681. /**
  682. * test that ajax requests involving redirects trigger requestAction instead.
  683. *
  684. * @return void
  685. */
  686. public function testAjaxRedirectAsRequestAction() {
  687. App::build(array(
  688. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  689. ), App::RESET);
  690. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  691. $this->Controller->request = $this->getMock('CakeRequest');
  692. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  693. $this->Controller->RequestHandler->request = $this->Controller->request;
  694. $this->Controller->RequestHandler->response = $this->Controller->response;
  695. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  696. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  697. ob_start();
  698. $this->Controller->RequestHandler->beforeRedirect(
  699. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  700. );
  701. $result = ob_get_clean();
  702. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  703. App::build();
  704. }
  705. /**
  706. * test that ajax requests involving redirects don't force no layout
  707. * this would cause the ajax layout to not be rendered.
  708. *
  709. * @return void
  710. */
  711. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  712. App::build(array(
  713. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  714. ), App::RESET);
  715. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  716. $this->Controller->request = $this->getMock('CakeRequest');
  717. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  718. $this->Controller->RequestHandler->request = $this->Controller->request;
  719. $this->Controller->RequestHandler->response = $this->Controller->response;
  720. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  721. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  722. ob_start();
  723. $this->Controller->RequestHandler->beforeRedirect(
  724. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
  725. );
  726. $result = ob_get_clean();
  727. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  728. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  729. App::build();
  730. }
  731. /**
  732. * test that the beforeRedirect callback properly converts
  733. * array URLs into their correct string ones, and adds base => false so
  734. * the correct URLs are generated.
  735. *
  736. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  737. * @return void
  738. */
  739. public function testBeforeRedirectCallbackWithArrayUrl() {
  740. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  741. Router::setRequestInfo(array(
  742. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  743. array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
  744. ));
  745. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  746. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  747. $RequestHandler->request = new CakeRequest('posts/index');
  748. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  749. ob_start();
  750. $RequestHandler->beforeRedirect(
  751. $this->Controller,
  752. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
  753. );
  754. $result = ob_get_clean();
  755. $this->assertEquals('one: first two: second', $result);
  756. }
  757. /**
  758. * assure that beforeRedirect with a status code will correctly set the status header
  759. *
  760. * @return void
  761. */
  762. public function testBeforeRedirectCallingHeader() {
  763. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  764. $controller = $this->getMock('Controller', array('header'));
  765. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  766. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader', 'statusCode'));
  767. $RequestHandler->request = $this->getMock('CakeRequest');
  768. $RequestHandler->request->expects($this->once())->method('is')
  769. ->with('ajax')
  770. ->will($this->returnValue(true));
  771. $RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
  772. ob_start();
  773. $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
  774. ob_get_clean();
  775. }
  776. /**
  777. * @expectedException CakeException
  778. * @return void
  779. */
  780. public function testAddInputTypeException() {
  781. $this->RequestHandler->addInputType('csv', array('I am not callable'));
  782. }
  783. /**
  784. * Test checkNotModified method
  785. *
  786. * @return void
  787. */
  788. public function testCheckNotModifiedByEtagStar() {
  789. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  790. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  791. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  792. $RequestHandler->response->etag('something');
  793. $RequestHandler->response->expects($this->once())->method('notModified');
  794. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  795. }
  796. /**
  797. * Test checkNotModified method
  798. *
  799. * @return void
  800. */
  801. public function testCheckNotModifiedByEtagExact() {
  802. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  803. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  804. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  805. $RequestHandler->response->etag('something', true);
  806. $RequestHandler->response->expects($this->once())->method('notModified');
  807. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  808. }
  809. /**
  810. * Test checkNotModified method
  811. *
  812. * @return void
  813. */
  814. public function testCheckNotModifiedByEtagAndTime() {
  815. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  816. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  817. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  818. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  819. $RequestHandler->response->etag('something', true);
  820. $RequestHandler->response->modified('2012-01-01 00:00:00');
  821. $RequestHandler->response->expects($this->once())->method('notModified');
  822. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  823. }
  824. /**
  825. * Test checkNotModified method
  826. *
  827. * @return void
  828. */
  829. public function testCheckNotModifiedNoInfo() {
  830. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  831. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  832. $RequestHandler->response->expects($this->never())->method('notModified');
  833. $this->assertNull($RequestHandler->beforeRender($this->Controller));
  834. }
  835. }