RequestHandlerComponentTest.php 28 KB

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