RequestHandlerComponentTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. protected 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. $result = $this->RequestHandler->respondAs('json');
  383. $this->assertTrue($result);
  384. $result = $this->RequestHandler->respondAs('text/xml');
  385. $this->assertTrue($result);
  386. }
  387. /**
  388. * test that attachment headers work with respondAs
  389. *
  390. * @return void
  391. */
  392. public function testRespondAsWithAttachment() {
  393. $this->RequestHandler = $this->getMock(
  394. 'RequestHandlerComponent',
  395. array('_header'),
  396. array(&$this->Controller->Components)
  397. );
  398. $this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
  399. $this->RequestHandler->request = $this->getMock('CakeRequest');
  400. $this->RequestHandler->request->expects($this->once())
  401. ->method('parseAccept')
  402. ->will($this->returnValue(array('1.0' => array('application/xml'))));
  403. $this->RequestHandler->response->expects($this->once())->method('download')
  404. ->with('myfile.xml');
  405. $this->RequestHandler->response->expects($this->once())->method('type')
  406. ->with('application/xml');
  407. $result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  408. $this->assertTrue($result);
  409. }
  410. /**
  411. * test that calling renderAs() more than once continues to work.
  412. *
  413. * @link #6466
  414. * @return void
  415. */
  416. public function testRenderAsCalledTwice() {
  417. $this->RequestHandler->renderAs($this->Controller, 'print');
  418. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
  419. $this->assertEquals('print', $this->Controller->layoutPath);
  420. $this->RequestHandler->renderAs($this->Controller, 'js');
  421. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
  422. $this->assertEquals('js', $this->Controller->layoutPath);
  423. $this->assertTrue(in_array('Js', $this->Controller->helpers));
  424. }
  425. /**
  426. * testRequestClientTypes method
  427. *
  428. * @return void
  429. */
  430. public function testRequestClientTypes() {
  431. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  432. $this->assertEquals($this->RequestHandler->getAjaxVersion(), '1.5');
  433. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  434. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  435. }
  436. /**
  437. * Tests the detection of various Flash versions
  438. *
  439. * @return void
  440. */
  441. public function testFlashDetection() {
  442. $request = $this->getMock('CakeRequest');
  443. $request->expects($this->once())->method('is')
  444. ->with('flash')
  445. ->will($this->returnValue(true));
  446. $this->RequestHandler->request = $request;
  447. $this->assertTrue($this->RequestHandler->isFlash());
  448. }
  449. /**
  450. * testRequestContentTypes method
  451. *
  452. * @return void
  453. */
  454. public function testRequestContentTypes() {
  455. $_SERVER['REQUEST_METHOD'] = 'GET';
  456. $this->assertNull($this->RequestHandler->requestedWith());
  457. $_SERVER['REQUEST_METHOD'] = 'POST';
  458. $_SERVER['CONTENT_TYPE'] = 'application/json';
  459. $this->assertEquals($this->RequestHandler->requestedWith(), 'json');
  460. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  461. $this->assertEquals($result, 'json');
  462. $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
  463. $this->assertFalse($result);
  464. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  465. $this->assertTrue($this->RequestHandler->isXml());
  466. $this->assertFalse($this->RequestHandler->isAtom());
  467. $this->assertFalse($this->RequestHandler->isRSS());
  468. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  469. $this->assertTrue($this->RequestHandler->isAtom());
  470. $this->assertFalse($this->RequestHandler->isRSS());
  471. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  472. $this->assertFalse($this->RequestHandler->isAtom());
  473. $this->assertTrue($this->RequestHandler->isRSS());
  474. $this->assertFalse($this->RequestHandler->isWap());
  475. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  476. $this->assertTrue($this->RequestHandler->isWap());
  477. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  478. }
  479. /**
  480. * testResponseContentType method
  481. *
  482. * @return void
  483. */
  484. public function testResponseContentType() {
  485. $this->assertEquals('html', $this->RequestHandler->responseType());
  486. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  487. $this->assertEquals($this->RequestHandler->responseType(), 'atom');
  488. }
  489. /**
  490. * testMobileDeviceDetection method
  491. *
  492. * @return void
  493. */
  494. public function testMobileDeviceDetection() {
  495. $request = $this->getMock('CakeRequest');
  496. $request->expects($this->once())->method('is')
  497. ->with('mobile')
  498. ->will($this->returnValue(true));
  499. $this->RequestHandler->request = $request;
  500. $this->assertTrue($this->RequestHandler->isMobile());
  501. }
  502. /**
  503. * testRequestProperties method
  504. *
  505. * @return void
  506. */
  507. public function testRequestProperties() {
  508. $request = $this->getMock('CakeRequest');
  509. $request->expects($this->once())->method('is')
  510. ->with('ssl')
  511. ->will($this->returnValue(true));
  512. $this->RequestHandler->request = $request;
  513. $this->assertTrue($this->RequestHandler->isSsl());
  514. }
  515. /**
  516. * testRequestMethod method
  517. *
  518. * @return void
  519. */
  520. public function testRequestMethod() {
  521. $request = $this->getMock('CakeRequest');
  522. $request->expects($this->at(0))->method('is')
  523. ->with('get')
  524. ->will($this->returnValue(true));
  525. $request->expects($this->at(1))->method('is')
  526. ->with('post')
  527. ->will($this->returnValue(false));
  528. $request->expects($this->at(2))->method('is')
  529. ->with('delete')
  530. ->will($this->returnValue(true));
  531. $request->expects($this->at(3))->method('is')
  532. ->with('put')
  533. ->will($this->returnValue(false));
  534. $this->RequestHandler->request = $request;
  535. $this->assertTrue($this->RequestHandler->isGet());
  536. $this->assertFalse($this->RequestHandler->isPost());
  537. $this->assertTrue($this->RequestHandler->isDelete());
  538. $this->assertFalse($this->RequestHandler->isPut());
  539. }
  540. /**
  541. * test that map alias converts aliases to content types.
  542. *
  543. * @return void
  544. */
  545. public function testMapAlias() {
  546. $result = $this->RequestHandler->mapAlias('xml');
  547. $this->assertEquals('application/xml', $result);
  548. $result = $this->RequestHandler->mapAlias('text/html');
  549. $this->assertNull($result);
  550. $result = $this->RequestHandler->mapAlias('wap');
  551. $this->assertEquals('text/vnd.wap.wml', $result);
  552. $result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
  553. $expected = array('application/xml', 'text/javascript', 'application/json');
  554. $this->assertEquals($expected, $result);
  555. }
  556. /**
  557. * test accepts() on the component
  558. *
  559. * @return void
  560. */
  561. public function testAccepts() {
  562. $_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';
  563. $this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
  564. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  565. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  566. $this->assertFalse($this->RequestHandler->accepts('rss'));
  567. }
  568. /**
  569. * test accepts and prefers methods.
  570. *
  571. * @return void
  572. */
  573. public function testPrefers() {
  574. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  575. $this->assertNotEquals($this->RequestHandler->prefers(), 'rss');
  576. $this->RequestHandler->ext = 'rss';
  577. $this->assertEquals($this->RequestHandler->prefers(), 'rss');
  578. $this->assertFalse($this->RequestHandler->prefers('xml'));
  579. $this->assertEquals($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
  580. $this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
  581. $this->assertEquals($this->RequestHandler->prefers(array('js', 'json', 'xhtml')), 'xhtml');
  582. $this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
  583. $this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
  584. $_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';
  585. $this->_init();
  586. $this->assertEquals($this->RequestHandler->prefers(), 'xml');
  587. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  588. $this->assertEquals($this->RequestHandler->prefers(), 'html');
  589. $this->assertFalse($this->RequestHandler->prefers('rss'));
  590. }
  591. /**
  592. * testCustomContent method
  593. *
  594. * @return void
  595. */
  596. public function testCustomContent() {
  597. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  598. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  599. $this->RequestHandler->startup($this->Controller);
  600. $this->assertEquals($this->RequestHandler->prefers(), 'mobile');
  601. }
  602. /**
  603. * testClientProperties method
  604. *
  605. * @return void
  606. */
  607. public function testClientProperties() {
  608. $request = $this->getMock('CakeRequest');
  609. $request->expects($this->once())->method('referer');
  610. $request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
  611. $this->RequestHandler->request = $request;
  612. $this->RequestHandler->getReferer();
  613. $this->RequestHandler->getClientIP(false);
  614. }
  615. /**
  616. * test that ajax requests involving redirects trigger requestAction instead.
  617. *
  618. * @return void
  619. */
  620. public function testAjaxRedirectAsRequestAction() {
  621. App::build(array(
  622. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  623. ), App::RESET);
  624. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  625. $this->Controller->request = $this->getMock('CakeRequest');
  626. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  627. $this->Controller->RequestHandler->request = $this->Controller->request;
  628. $this->Controller->RequestHandler->response = $this->Controller->response;
  629. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  630. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  631. ob_start();
  632. $this->Controller->RequestHandler->beforeRedirect(
  633. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  634. );
  635. $result = ob_get_clean();
  636. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  637. App::build();
  638. }
  639. /**
  640. * test that ajax requests involving redirects don't force no layout
  641. * this would cause the ajax layout to not be rendered.
  642. *
  643. * @return void
  644. */
  645. public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  646. App::build(array(
  647. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  648. ), App::RESET);
  649. $this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  650. $this->Controller->request = $this->getMock('CakeRequest');
  651. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  652. $this->Controller->RequestHandler->request = $this->Controller->request;
  653. $this->Controller->RequestHandler->response = $this->Controller->response;
  654. $this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
  655. $this->Controller->RequestHandler->expects($this->once())->method('_stop');
  656. ob_start();
  657. $this->Controller->RequestHandler->beforeRedirect(
  658. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
  659. );
  660. $result = ob_get_clean();
  661. $this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
  662. $this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
  663. App::build();
  664. }
  665. /**
  666. * test that the beforeRedirect callback properly converts
  667. * array urls into their correct string ones, and adds base => false so
  668. * the correct urls are generated.
  669. *
  670. * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  671. * @return void
  672. */
  673. public function testBeforeRedirectCallbackWithArrayUrl() {
  674. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  675. Router::setRequestInfo(array(
  676. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  677. array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
  678. ));
  679. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  680. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  681. $RequestHandler->request = new CakeRequest('posts/index');
  682. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
  683. ob_start();
  684. $RequestHandler->beforeRedirect(
  685. $this->Controller,
  686. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
  687. );
  688. $result = ob_get_clean();
  689. $this->assertEquals($result, 'one: first two: second');
  690. }
  691. /**
  692. * assure that beforeRedirect with a status code will correctly set the status header
  693. *
  694. * @return void
  695. */
  696. public function testBeforeRedirectCallingHeader() {
  697. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  698. $controller = $this->getMock('Controller', array('header'));
  699. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  700. $RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader','statusCode'));
  701. $RequestHandler->request = $this->getMock('CakeRequest');
  702. $RequestHandler->request->expects($this->once())->method('is')
  703. ->with('ajax')
  704. ->will($this->returnValue(true));
  705. $RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
  706. ob_start();
  707. $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
  708. $result = ob_get_clean();
  709. }
  710. /**
  711. * @expectedException CakeException
  712. * @return void
  713. */
  714. public function testAddInputTypeException() {
  715. $this->RequestHandler->addInputType('csv', array('I am not callable'));
  716. }
  717. /**
  718. * Test checkNotModified method
  719. *
  720. * @return void
  721. **/
  722. public function testCheckNotModifiedByEtagStar() {
  723. $_SERVER['HTTP_IF_NONE_MATCH'] = '*';
  724. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  725. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  726. $RequestHandler->response->etag('something');
  727. $RequestHandler->response->expects($this->once())->method('notModified');
  728. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  729. }
  730. /**
  731. * Test checkNotModified method
  732. *
  733. * @return void
  734. **/
  735. public function testCheckNotModifiedByEtagExact() {
  736. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  737. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  738. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  739. $RequestHandler->response->etag('something', true);
  740. $RequestHandler->response->expects($this->once())->method('notModified');
  741. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  742. }
  743. /**
  744. * Test checkNotModified method
  745. *
  746. * @return void
  747. **/
  748. public function testCheckNotModifiedByEtagAndTime() {
  749. $_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
  750. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
  751. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  752. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  753. $RequestHandler->response->etag('something', true);
  754. $RequestHandler->response->modified('2012-01-01 00:00:00');
  755. $RequestHandler->response->expects($this->once())->method('notModified');
  756. $this->assertFalse($RequestHandler->beforeRender($this->Controller));
  757. }
  758. /**
  759. * Test checkNotModified method
  760. *
  761. * @return void
  762. **/
  763. public function testCheckNotModifiedNoInfo() {
  764. $RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
  765. $RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
  766. $RequestHandler->response->expects($this->never())->method('notModified');
  767. $this->assertNull($RequestHandler->beforeRender($this->Controller));
  768. }
  769. }