2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* RequestHandlerComponentTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5435
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('RequestHandlerComponent', 'Controller/Component');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2011-01-28 06:36:30 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2012-08-10 08:29:55 +00:00
|
|
|
App::uses('JsonView', 'View');
|
2009-03-01 06:11:03 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* RequestHandlerTestController class
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class RequestHandlerTestController extends Controller {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-01 06:11:03 +00:00
|
|
|
/**
|
|
|
|
* test method for ajax redirection
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function destination() {
|
2011-05-16 21:32:59 +00:00
|
|
|
$this->viewPath = 'Posts';
|
2009-03-01 06:11:03 +00:00
|
|
|
$this->render('index');
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2010-02-03 01:39:05 +00:00
|
|
|
/**
|
|
|
|
* test method for ajax redirection + parameter parsing
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function param_method($one = null, $two = null) {
|
2010-02-03 01:39:05 +00:00
|
|
|
echo "one: $one two: $two";
|
|
|
|
$this->autoRender = false;
|
|
|
|
}
|
2010-06-08 03:43:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test method for testing layout rendering when isAjax()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function ajax2_layout() {
|
2010-06-08 03:43:35 +00:00
|
|
|
if ($this->autoLayout) {
|
|
|
|
$this->layout = 'ajax2';
|
|
|
|
}
|
|
|
|
$this->destination();
|
|
|
|
}
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-08-10 08:29:55 +00:00
|
|
|
/**
|
2012-08-10 09:05:19 +00:00
|
|
|
* CustomJsonView class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2012-08-10 08:29:55 +00:00
|
|
|
*/
|
2012-08-11 01:50:37 +00:00
|
|
|
class CustomJsonView extends JsonView {
|
|
|
|
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* RequestHandlerComponentTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class RequestHandlerComponentTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* Controller property
|
|
|
|
*
|
|
|
|
* @var RequestHandlerTestController
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $Controller;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* RequestHandler property
|
|
|
|
*
|
|
|
|
* @var RequestHandlerComponent
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $RequestHandler;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-08-02 23:37:26 +00:00
|
|
|
* setUp method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2011-10-13 03:07:02 +00:00
|
|
|
parent::setUp();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_init();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2009-07-14 03:52:06 +00:00
|
|
|
* init method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
protected function _init() {
|
2010-05-08 04:59:40 +00:00
|
|
|
$request = new CakeRequest('controller_posts/index');
|
2010-08-02 23:37:26 +00:00
|
|
|
$response = new CakeResponse();
|
|
|
|
$this->Controller = new RequestHandlerTestController($request, $response);
|
2011-11-01 02:41:43 +00:00
|
|
|
$this->Controller->constructClasses();
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->RequestHandler = new RequestHandlerComponent($this->Controller->Components);
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->_extensions = Router::extensions();
|
2009-07-14 03:52:06 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-14 03:52:06 +00:00
|
|
|
/**
|
2012-04-11 14:50:54 +00:00
|
|
|
* tearDown method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2011-10-13 03:07:02 +00:00
|
|
|
parent::tearDown();
|
|
|
|
unset($this->RequestHandler, $this->Controller);
|
2009-03-18 17:55:58 +00:00
|
|
|
if (!headers_sent()) {
|
|
|
|
header('Content-type: text/html'); //reset content type.
|
|
|
|
}
|
2011-10-13 03:07:02 +00:00
|
|
|
call_user_func_array('Router::parseExtensions', $this->_extensions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-05 00:31:23 +00:00
|
|
|
/**
|
|
|
|
* Test that the constructor sets the settings.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstructorSettings() {
|
2010-07-05 00:31:23 +00:00
|
|
|
$settings = array(
|
2012-08-10 08:29:55 +00:00
|
|
|
'ajaxLayout' => 'test_ajax',
|
|
|
|
'viewClassMap' => array('json' => 'MyPlugin.MyJson')
|
2010-07-05 00:31:23 +00:00
|
|
|
);
|
|
|
|
$Collection = new ComponentCollection();
|
2011-11-01 02:41:43 +00:00
|
|
|
$Collection->init($this->Controller);
|
2010-07-05 00:31:23 +00:00
|
|
|
$RequestHandler = new RequestHandlerComponent($Collection, $settings);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('test_ajax', $RequestHandler->ajaxLayout);
|
2012-08-10 08:29:55 +00:00
|
|
|
$this->assertEquals(array('json' => 'MyPlugin.MyJson'), $RequestHandler->settings['viewClassMap']);
|
2010-07-05 00:31:23 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testInitializeCallback method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitializeCallback() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
2011-08-27 14:36:11 +00:00
|
|
|
$this->Controller->request->params['ext'] = 'rss';
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('rss', $this->RequestHandler->ext);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-01 16:01:46 +00:00
|
|
|
/**
|
|
|
|
* test that a mapped Accept-type header will set $this->ext correctly.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitializeContentTypeSettingExt() {
|
2010-07-01 16:01:46 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
2011-10-13 03:07:02 +00:00
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json';
|
2010-07-01 16:01:46 +00:00
|
|
|
Router::parseExtensions('json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->ext);
|
2011-10-13 03:07:02 +00:00
|
|
|
}
|
2010-07-01 16:01:46 +00:00
|
|
|
|
2011-10-13 03:07:02 +00:00
|
|
|
/**
|
|
|
|
* Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeContentTypeWithjQueryAccept() {
|
2012-07-06 11:17:15 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->ext);
|
|
|
|
}
|
|
|
|
|
2015-07-07 19:00:50 +00:00
|
|
|
/**
|
|
|
|
* Test that RequestHandler does not set extension to csv for text/plain mimetype
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeContentTypeWithjQueryTextPlainAccept() {
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/plain, */*; q=0.01';
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('csv');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
}
|
|
|
|
|
2011-10-19 13:36:00 +00:00
|
|
|
/**
|
|
|
|
* Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
|
2012-02-23 23:29:53 +00:00
|
|
|
* and the application is configured to handle multiple extensions
|
2011-10-19 13:36:00 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions() {
|
2012-07-06 11:17:15 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
|
2011-10-19 13:36:00 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('rss', 'json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->ext);
|
|
|
|
}
|
|
|
|
|
2011-10-13 03:07:02 +00:00
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* Test that RequestHandler does not set $this->ext when multiple accepts are sent.
|
2011-10-13 03:07:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeNoContentTypeWithSingleAccept() {
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
}
|
2011-11-16 03:48:54 +00:00
|
|
|
|
2011-10-13 03:07:02 +00:00
|
|
|
/**
|
2013-06-27 12:22:49 +00:00
|
|
|
* Test that ext is set to the first listed extension with multiple accepted
|
|
|
|
* content types.
|
|
|
|
* Having multiple types accepted with same weight, means the client lets the
|
|
|
|
* server choose the returned content type.
|
2011-10-13 03:07:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeNoContentTypeWithMultipleAcceptedTypes() {
|
2012-07-06 11:17:15 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, application/xml, */*; q=0.01';
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('xml', 'json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
2013-06-27 12:22:49 +00:00
|
|
|
$this->assertEquals('xml', $this->RequestHandler->ext);
|
|
|
|
|
|
|
|
$this->RequestHandler->ext = null;
|
|
|
|
Router::setExtensions(array('json', 'xml'), false);
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that ext is set to type with highest weight
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeContentTypeWithMultipleAcceptedTypes() {
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7';
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
2013-06-27 12:22:49 +00:00
|
|
|
Router::parseExtensions('xml', 'json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->ext);
|
2010-07-01 16:01:46 +00:00
|
|
|
}
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2011-11-16 03:48:54 +00:00
|
|
|
/**
|
|
|
|
* Test that ext is not set with confusing android accepts headers.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInitializeAmbiguousAndroidAccepts() {
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
Router::parseExtensions('html', 'xml');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
}
|
2010-07-01 16:01:46 +00:00
|
|
|
|
2013-10-02 15:28:55 +00:00
|
|
|
/**
|
|
|
|
* Test that the headers sent by firefox are not treated as XML requests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInititalizeFirefoxHeaderNotXml() {
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
|
|
|
|
Router::parseExtensions('xml', 'json');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
}
|
|
|
|
|
2010-07-01 16:01:46 +00:00
|
|
|
/**
|
|
|
|
* Test that a type mismatch doesn't incorrectly set the ext
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitializeContentTypeAndExtensionMismatch() {
|
2010-07-01 16:01:46 +00:00
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
$extensions = Router::extensions();
|
|
|
|
Router::parseExtensions('xml');
|
|
|
|
|
|
|
|
$this->Controller->request = $this->getMock('CakeRequest');
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->Controller->request->expects($this->any())
|
|
|
|
->method('accepts')
|
2010-07-01 16:01:46 +00:00
|
|
|
->will($this->returnValue(array('application/json')));
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->ext);
|
|
|
|
|
|
|
|
call_user_func_array(array('Router', 'parseExtensions'), $extensions);
|
|
|
|
}
|
|
|
|
|
2012-08-10 08:29:55 +00:00
|
|
|
/**
|
|
|
|
* testViewClassMap method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testViewClassMap() {
|
2012-08-27 09:22:06 +00:00
|
|
|
$this->RequestHandler->settings = array('viewClassMap' => array('json' => 'CustomJson'));
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
2012-08-10 08:29:55 +00:00
|
|
|
$result = $this->RequestHandler->viewClassMap();
|
|
|
|
$expected = array(
|
|
|
|
'json' => 'CustomJson',
|
|
|
|
'xml' => 'Xml'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
|
|
|
|
$expected = array(
|
|
|
|
'json' => 'CustomJson',
|
|
|
|
'xml' => 'Xml',
|
|
|
|
'xls' => 'Excel.Excel'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'json');
|
|
|
|
$this->assertEquals('CustomJson', $this->Controller->viewClass);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDisabling method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDisabling() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->_init();
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
2008-06-12 18:36:08 +00:00
|
|
|
$this->Controller->beforeFilter();
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(true, $this->Controller->params['isAjax']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-20 18:51:17 +00:00
|
|
|
/**
|
|
|
|
* testAutoAjaxLayout method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAutoAjaxLayout() {
|
2010-09-20 18:51:17 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2010-09-25 03:27:22 +00:00
|
|
|
$this->assertEquals($this->Controller->layout, $this->RequestHandler->ajaxLayout);
|
2010-09-20 18:51:17 +00:00
|
|
|
|
|
|
|
$this->_init();
|
2011-08-27 14:36:11 +00:00
|
|
|
$this->Controller->request->params['ext'] = 'js';
|
2010-09-20 18:51:17 +00:00
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertNotEquals('ajax', $this->Controller->layout);
|
2010-09-20 18:51:17 +00:00
|
|
|
|
|
|
|
unset($_SERVER['HTTP_X_REQUESTED_WITH']);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testStartupCallback method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStartupCallback() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
|
|
|
$_SERVER['CONTENT_TYPE'] = 'application/xml';
|
2011-09-25 01:56:37 +00:00
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2009-12-31 12:06:58 +00:00
|
|
|
$this->assertTrue(is_array($this->Controller->data));
|
|
|
|
$this->assertFalse(is_object($this->Controller->data));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-08-09 18:32:59 +00:00
|
|
|
/**
|
|
|
|
* testStartupCallback with charset.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStartupCallbackCharset() {
|
2008-08-09 18:32:59 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
|
|
|
$_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
|
2011-09-25 01:56:37 +00:00
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
|
2008-08-09 18:32:59 +00:00
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2009-12-31 12:06:58 +00:00
|
|
|
$this->assertTrue(is_array($this->Controller->data));
|
|
|
|
$this->assertFalse(is_object($this->Controller->data));
|
2008-08-09 18:32:59 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-05-01 13:59:47 +00:00
|
|
|
/**
|
|
|
|
* Test mapping a new type and having startup process it.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStartupCustomTypeProcess() {
|
2011-05-01 13:59:47 +00:00
|
|
|
if (!function_exists('str_getcsv')) {
|
|
|
|
$this->markTestSkipped('Need "str_getcsv" for this test.');
|
|
|
|
}
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
$_SERVER['CONTENT_TYPE'] = 'text/csv';
|
2011-09-25 01:56:37 +00:00
|
|
|
$this->Controller->request = $this->getMock('CakeRequest', array('_readInput'));
|
2011-05-01 13:59:47 +00:00
|
|
|
$this->Controller->request->expects($this->once())
|
2011-09-25 01:56:37 +00:00
|
|
|
->method('_readInput')
|
2011-05-01 13:59:47 +00:00
|
|
|
->will($this->returnValue('"A","csv","string"'));
|
|
|
|
$this->RequestHandler->addInputType('csv', array('str_getcsv'));
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
|
|
|
$expected = array(
|
|
|
|
'A', 'csv', 'string'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $this->Controller->request->data);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testNonAjaxRedirect method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNonAjaxRedirect() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2013-05-17 20:32:46 +00:00
|
|
|
/**
|
2013-10-08 03:17:58 +00:00
|
|
|
* test that redirects with ajax and no URL don't do anything.
|
2013-05-17 20:32:46 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAjaxRedirectWithNoUrl() {
|
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
|
|
|
$this->Controller->response = $this->getMock('CakeResponse');
|
|
|
|
|
|
|
|
$this->Controller->response->expects($this->never())
|
|
|
|
->method('body');
|
|
|
|
|
|
|
|
$this->RequestHandler->initialize($this->Controller);
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
|
|
|
$this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, null));
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRenderAs method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderAs() {
|
2010-10-14 03:09:55 +00:00
|
|
|
$this->assertFalse(in_array('Rss', $this->Controller->helpers));
|
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'rss');
|
|
|
|
$this->assertTrue(in_array('Rss', $this->Controller->helpers));
|
2009-10-13 04:01:09 +00:00
|
|
|
|
2010-10-14 03:09:55 +00:00
|
|
|
$this->Controller->viewPath = 'request_handler_test\\rss';
|
2009-10-13 04:01:09 +00:00
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'js');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewPath);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-10 01:31:34 +00:00
|
|
|
/**
|
|
|
|
* test that attachment headers work with renderAs
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderAsWithAttachment() {
|
2010-09-10 01:31:34 +00:00
|
|
|
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
|
|
|
$this->RequestHandler->request->expects($this->any())
|
2011-08-31 23:19:54 +00:00
|
|
|
->method('parseAccept')
|
|
|
|
->will($this->returnValue(array('1.0' => array('application/xml'))));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-09-10 01:31:34 +00:00
|
|
|
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
|
|
|
|
$this->RequestHandler->response->expects($this->at(0))
|
|
|
|
->method('type')
|
|
|
|
->with('application/xml');
|
|
|
|
$this->RequestHandler->response->expects($this->at(1))
|
|
|
|
->method('charset')
|
|
|
|
->with('UTF-8');
|
|
|
|
$this->RequestHandler->response->expects($this->at(2))
|
|
|
|
->method('download')
|
|
|
|
->with('myfile.xml');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-09-10 01:31:34 +00:00
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
|
|
|
|
|
2011-11-08 04:03:36 +00:00
|
|
|
$this->assertEquals('Xml', $this->Controller->viewClass);
|
2010-09-10 01:31:34 +00:00
|
|
|
}
|
|
|
|
|
2010-07-01 02:34:33 +00:00
|
|
|
/**
|
|
|
|
* test that respondAs works as expected.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRespondAs() {
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
|
|
|
|
$this->RequestHandler->response->expects($this->at(0))->method('type')
|
|
|
|
->with('application/json');
|
|
|
|
$this->RequestHandler->response->expects($this->at(1))->method('type')
|
|
|
|
->with('text/xml');
|
2010-07-01 02:47:27 +00:00
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
$result = $this->RequestHandler->respondAs('json');
|
2010-07-01 02:34:33 +00:00
|
|
|
$this->assertTrue($result);
|
2010-08-02 23:40:01 +00:00
|
|
|
$result = $this->RequestHandler->respondAs('text/xml');
|
2010-07-01 02:34:33 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that attachment headers work with respondAs
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRespondAsWithAttachment() {
|
2010-09-10 01:31:34 +00:00
|
|
|
$this->RequestHandler = $this->getMock(
|
2011-04-17 10:35:21 +00:00
|
|
|
'RequestHandlerComponent',
|
|
|
|
array('_header'),
|
2010-09-10 01:31:34 +00:00
|
|
|
array(&$this->Controller->Components)
|
|
|
|
);
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
|
2010-08-28 04:01:41 +00:00
|
|
|
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-09-10 01:31:34 +00:00
|
|
|
$this->RequestHandler->request->expects($this->once())
|
2011-08-31 23:19:54 +00:00
|
|
|
->method('parseAccept')
|
|
|
|
->will($this->returnValue(array('1.0' => array('application/xml'))));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->RequestHandler->response->expects($this->once())->method('download')
|
|
|
|
->with('myfile.xml');
|
|
|
|
$this->RequestHandler->response->expects($this->once())->method('type')
|
|
|
|
->with('application/xml');
|
2010-07-01 02:47:27 +00:00
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
|
2010-07-01 02:34:33 +00:00
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2009-07-14 03:52:06 +00:00
|
|
|
/**
|
|
|
|
* test that calling renderAs() more than once continues to work.
|
|
|
|
*
|
|
|
|
* @link #6466
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderAsCalledTwice() {
|
2011-11-29 02:36:44 +00:00
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'print');
|
|
|
|
$this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
|
|
|
|
$this->assertEquals('print', $this->Controller->layoutPath);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-14 03:52:06 +00:00
|
|
|
$this->RequestHandler->renderAs($this->Controller, 'js');
|
2011-11-29 02:36:44 +00:00
|
|
|
$this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
|
|
|
|
$this->assertEquals('js', $this->Controller->layoutPath);
|
2009-07-14 03:52:06 +00:00
|
|
|
$this->assertTrue(in_array('Js', $this->Controller->helpers));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequestClientTypes method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequestClientTypes() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('1.5', $this->RequestHandler->getAjaxVersion());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
|
|
|
|
$this->assertFalse($this->RequestHandler->getAjaxVersion());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-12-10 13:55:58 +00:00
|
|
|
/**
|
|
|
|
* Tests the detection of various Flash versions
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFlashDetection() {
|
2010-07-01 03:37:54 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
|
|
|
$request->expects($this->once())->method('is')
|
|
|
|
->with('flash')
|
|
|
|
->will($this->returnValue(true));
|
2008-12-10 13:55:58 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->request = $request;
|
2008-12-10 13:55:58 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->isFlash());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequestContentTypes method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequestContentTypes() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
$this->assertNull($this->RequestHandler->requestedWith());
|
|
|
|
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
$_SERVER['CONTENT_TYPE'] = 'application/json';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('json', $this->RequestHandler->requestedWith());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('json', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-12 02:20:25 +00:00
|
|
|
$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2014-07-30 01:53:55 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'DELETE';
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->requestedWith());
|
2014-07-29 14:34:11 +00:00
|
|
|
|
2016-05-27 12:50:23 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'PATCH';
|
|
|
|
$this->assertEquals('json', $this->RequestHandler->requestedWith());
|
|
|
|
|
2013-09-20 10:20:47 +00:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
unset($_SERVER['CONTENT_TYPE']);
|
|
|
|
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json';
|
|
|
|
|
|
|
|
$result = $this->RequestHandler->requestedWith(array('json', 'xml'));
|
|
|
|
$this->assertEquals('json', $result);
|
|
|
|
|
|
|
|
$result = $this->RequestHandler->requestedWith(array('rss', 'atom'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
|
|
|
|
$this->assertTrue($this->RequestHandler->isXml());
|
|
|
|
$this->assertFalse($this->RequestHandler->isAtom());
|
|
|
|
$this->assertFalse($this->RequestHandler->isRSS());
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
|
|
|
|
$this->assertTrue($this->RequestHandler->isAtom());
|
|
|
|
$this->assertFalse($this->RequestHandler->isRSS());
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
|
|
|
|
$this->assertFalse($this->RequestHandler->isAtom());
|
|
|
|
$this->assertTrue($this->RequestHandler->isRSS());
|
|
|
|
|
|
|
|
$this->assertFalse($this->RequestHandler->isWap());
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
|
|
|
|
$this->assertTrue($this->RequestHandler->isWap());
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testResponseContentType method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testResponseContentType() {
|
2010-08-02 23:51:52 +00:00
|
|
|
$this->assertEquals('html', $this->RequestHandler->responseType());
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->respondAs('atom'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('atom', $this->RequestHandler->responseType());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testMobileDeviceDetection method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMobileDeviceDetection() {
|
2010-07-01 03:37:54 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
|
|
|
$request->expects($this->once())->method('is')
|
|
|
|
->with('mobile')
|
|
|
|
->will($this->returnValue(true));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->request = $request;
|
2010-03-14 20:28:38 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->isMobile());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequestProperties method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequestProperties() {
|
2010-07-01 03:37:54 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
|
|
|
$request->expects($this->once())->method('is')
|
|
|
|
->with('ssl')
|
|
|
|
->will($this->returnValue(true));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->request = $request;
|
|
|
|
$this->assertTrue($this->RequestHandler->isSsl());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRequestMethod method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRequestMethod() {
|
2010-07-01 03:37:54 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
|
|
|
$request->expects($this->at(0))->method('is')
|
|
|
|
->with('get')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$request->expects($this->at(1))->method('is')
|
|
|
|
->with('post')
|
|
|
|
->will($this->returnValue(false));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-01 03:37:54 +00:00
|
|
|
$request->expects($this->at(2))->method('is')
|
|
|
|
->with('delete')
|
|
|
|
->will($this->returnValue(true));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-01 03:37:54 +00:00
|
|
|
$request->expects($this->at(3))->method('is')
|
|
|
|
->with('put')
|
|
|
|
->will($this->returnValue(false));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->request = $request;
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->isGet());
|
|
|
|
$this->assertFalse($this->RequestHandler->isPost());
|
|
|
|
$this->assertTrue($this->RequestHandler->isDelete());
|
|
|
|
$this->assertFalse($this->RequestHandler->isPut());
|
|
|
|
}
|
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
/**
|
|
|
|
* test that map alias converts aliases to content types.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testMapAlias() {
|
2010-07-01 04:13:13 +00:00
|
|
|
$result = $this->RequestHandler->mapAlias('xml');
|
|
|
|
$this->assertEquals('application/xml', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
$result = $this->RequestHandler->mapAlias('text/html');
|
|
|
|
$this->assertNull($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
$result = $this->RequestHandler->mapAlias('wap');
|
|
|
|
$this->assertEquals('text/vnd.wap.wml', $result);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-01 04:13:13 +00:00
|
|
|
$result = $this->RequestHandler->mapAlias(array('xml', 'js', 'json'));
|
2012-07-06 11:17:15 +00:00
|
|
|
$expected = array('application/xml', 'application/javascript', 'application/json');
|
2010-07-01 04:13:13 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test accepts() on the component
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAccepts() {
|
2010-07-01 04:13:13 +00:00
|
|
|
$_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';
|
2011-11-04 01:16:29 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->accepts(array('js', 'xml', 'html')));
|
2010-07-01 04:13:13 +00:00
|
|
|
$this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
|
|
|
|
$this->assertFalse($this->RequestHandler->accepts('rss'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2010-07-01 03:37:54 +00:00
|
|
|
* test accepts and prefers methods.
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPrefers() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertNotEquals('rss', $this->RequestHandler->prefers());
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->RequestHandler->ext = 'rss';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('rss', $this->RequestHandler->prefers());
|
2008-09-19 19:48:22 +00:00
|
|
|
$this->assertFalse($this->RequestHandler->prefers('xml'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('xml', $this->RequestHandler->prefers(array('js', 'xml', 'xhtml')));
|
2010-07-01 04:46:28 +00:00
|
|
|
$this->assertFalse($this->RequestHandler->prefers(array('red', 'blue')));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('xhtml', $this->RequestHandler->prefers(array('js', 'json', 'xhtml')));
|
2011-10-13 03:07:02 +00:00
|
|
|
$this->assertTrue($this->RequestHandler->prefers(array('rss')), 'Should return true if input matches ext.');
|
|
|
|
$this->assertFalse($this->RequestHandler->prefers(array('html')), 'No match with ext, return false.');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$_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';
|
|
|
|
$this->_init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('xml', $this->RequestHandler->prefers());
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('html', $this->RequestHandler->prefers());
|
2008-09-19 19:48:22 +00:00
|
|
|
$this->assertFalse($this->RequestHandler->prefers('rss'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testCustomContent method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCustomContent() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
|
|
|
|
$this->RequestHandler->setContent('mobile', 'text/x-mobile');
|
|
|
|
$this->RequestHandler->startup($this->Controller);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('mobile', $this->RequestHandler->prefers());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* testClientProperties method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testClientProperties() {
|
2010-07-01 03:37:54 +00:00
|
|
|
$request = $this->getMock('CakeRequest');
|
|
|
|
$request->expects($this->once())->method('referer');
|
|
|
|
$request->expects($this->once())->method('clientIp')->will($this->returnValue(false));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->request = $request;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->RequestHandler->getReferer();
|
|
|
|
$this->RequestHandler->getClientIP(false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-01 06:11:03 +00:00
|
|
|
/**
|
|
|
|
* test that ajax requests involving redirects trigger requestAction instead.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAjaxRedirectAsRequestAction() {
|
2009-11-03 22:19:02 +00:00
|
|
|
App::build(array(
|
2012-03-12 02:20:25 +00:00
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2009-03-01 06:11:03 +00:00
|
|
|
|
2010-07-05 00:31:23 +00:00
|
|
|
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2010-07-01 03:37:54 +00:00
|
|
|
$this->Controller->request = $this->getMock('CakeRequest');
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
2010-05-08 04:59:40 +00:00
|
|
|
$this->Controller->RequestHandler->request = $this->Controller->request;
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->Controller->RequestHandler->response = $this->Controller->response;
|
2010-07-01 03:37:54 +00:00
|
|
|
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
|
2010-06-09 04:02:49 +00:00
|
|
|
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
|
2009-03-01 06:11:03 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->Controller->RequestHandler->beforeRedirect(
|
|
|
|
$this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
|
|
|
|
);
|
|
|
|
$result = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
|
2009-03-01 06:11:03 +00:00
|
|
|
|
2009-11-03 22:19:02 +00:00
|
|
|
App::build();
|
2009-03-01 06:11:03 +00:00
|
|
|
}
|
2010-02-03 01:39:05 +00:00
|
|
|
|
2010-06-08 03:43:35 +00:00
|
|
|
/**
|
|
|
|
* test that ajax requests involving redirects don't force no layout
|
|
|
|
* this would cause the ajax layout to not be rendered.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAjaxRedirectAsRequestActionStillRenderingLayout() {
|
2010-06-08 03:43:35 +00:00
|
|
|
App::build(array(
|
2012-03-12 02:20:25 +00:00
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2010-06-08 03:43:35 +00:00
|
|
|
|
2010-07-05 00:31:23 +00:00
|
|
|
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2010-07-01 03:37:54 +00:00
|
|
|
$this->Controller->request = $this->getMock('CakeRequest');
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
2010-07-01 03:37:54 +00:00
|
|
|
$this->Controller->RequestHandler->request = $this->Controller->request;
|
2010-08-02 23:40:01 +00:00
|
|
|
$this->Controller->RequestHandler->response = $this->Controller->response;
|
2010-07-01 03:37:54 +00:00
|
|
|
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
|
2010-07-01 02:47:27 +00:00
|
|
|
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
|
2010-06-08 03:43:35 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->Controller->RequestHandler->beforeRedirect(
|
|
|
|
$this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
|
|
|
|
);
|
|
|
|
$result = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/posts index/', $result, 'RequestAction redirect failed.');
|
|
|
|
$this->assertRegExp('/Ajax!/', $result, 'Layout was not rendered.');
|
2010-06-08 03:43:35 +00:00
|
|
|
|
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
2010-02-20 06:32:04 +00:00
|
|
|
/**
|
|
|
|
* test that the beforeRedirect callback properly converts
|
2013-04-29 09:05:17 +00:00
|
|
|
* array URLs into their correct string ones, and adds base => false so
|
|
|
|
* the correct URLs are generated.
|
2010-02-20 06:32:04 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBeforeRedirectCallbackWithArrayUrl() {
|
2010-02-20 06:32:04 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
2010-05-04 02:07:13 +00:00
|
|
|
|
2010-02-20 06:32:04 +00:00
|
|
|
Router::setRequestInfo(array(
|
2010-06-08 03:43:35 +00:00
|
|
|
array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
|
2010-02-20 06:32:04 +00:00
|
|
|
array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
|
|
|
|
));
|
|
|
|
|
2010-07-05 00:31:23 +00:00
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2010-08-02 23:40:01 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
2010-05-08 04:59:40 +00:00
|
|
|
$RequestHandler->request = new CakeRequest('posts/index');
|
2010-08-28 04:01:41 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
2010-02-20 06:32:04 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$RequestHandler->beforeRedirect(
|
|
|
|
$this->Controller,
|
|
|
|
array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
|
|
|
|
);
|
|
|
|
$result = ob_get_clean();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('one: first two: second', $result);
|
2010-05-04 02:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-01 02:47:27 +00:00
|
|
|
* assure that beforeRedirect with a status code will correctly set the status header
|
2010-05-04 02:07:13 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBeforeRedirectCallingHeader() {
|
2010-06-09 04:02:49 +00:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
|
|
|
|
|
|
|
|
$controller = $this->getMock('Controller', array('header'));
|
2010-07-05 00:31:23 +00:00
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2013-06-09 15:39:48 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader', 'statusCode'));
|
2010-06-29 03:41:48 +00:00
|
|
|
$RequestHandler->request = $this->getMock('CakeRequest');
|
|
|
|
$RequestHandler->request->expects($this->once())->method('is')
|
|
|
|
->with('ajax')
|
|
|
|
->will($this->returnValue(true));
|
2010-05-04 02:07:13 +00:00
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
|
2010-05-04 02:07:13 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
|
2013-01-23 12:45:50 +00:00
|
|
|
ob_get_clean();
|
2009-03-01 06:11:03 +00:00
|
|
|
}
|
2010-02-03 01:39:05 +00:00
|
|
|
|
2011-05-01 13:59:47 +00:00
|
|
|
/**
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAddInputTypeException() {
|
2011-05-01 13:59:47 +00:00
|
|
|
$this->RequestHandler->addInputType('csv', array('I am not callable'));
|
|
|
|
}
|
2012-01-13 06:06:31 +00:00
|
|
|
|
2012-01-19 05:02:34 +00:00
|
|
|
/**
|
|
|
|
* Test checkNotModified method
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-01-19 05:02:34 +00:00
|
|
|
public function testCheckNotModifiedByEtagStar() {
|
|
|
|
$_SERVER['HTTP_IF_NONE_MATCH'] = '*';
|
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2012-03-12 02:20:25 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
|
2012-01-19 05:02:34 +00:00
|
|
|
$RequestHandler->response->etag('something');
|
|
|
|
$RequestHandler->response->expects($this->once())->method('notModified');
|
|
|
|
$this->assertFalse($RequestHandler->beforeRender($this->Controller));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkNotModified method
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-01-19 05:02:34 +00:00
|
|
|
public function testCheckNotModifiedByEtagExact() {
|
|
|
|
$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
|
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2012-03-12 02:20:25 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
|
2012-01-19 05:02:34 +00:00
|
|
|
$RequestHandler->response->etag('something', true);
|
|
|
|
$RequestHandler->response->expects($this->once())->method('notModified');
|
|
|
|
$this->assertFalse($RequestHandler->beforeRender($this->Controller));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkNotModified method
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-01-19 05:02:34 +00:00
|
|
|
public function testCheckNotModifiedByEtagAndTime() {
|
|
|
|
$_SERVER['HTTP_IF_NONE_MATCH'] = 'W/"something", "other"';
|
|
|
|
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = '2012-01-01 00:00:00';
|
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2012-03-12 02:20:25 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
|
2012-01-19 05:02:34 +00:00
|
|
|
$RequestHandler->response->etag('something', true);
|
|
|
|
$RequestHandler->response->modified('2012-01-01 00:00:00');
|
|
|
|
$RequestHandler->response->expects($this->once())->method('notModified');
|
|
|
|
$this->assertFalse($RequestHandler->beforeRender($this->Controller));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test checkNotModified method
|
|
|
|
*
|
|
|
|
* @return void
|
2013-01-11 14:06:54 +00:00
|
|
|
*/
|
2012-01-19 05:02:34 +00:00
|
|
|
public function testCheckNotModifiedNoInfo() {
|
|
|
|
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'), array(&$this->Controller->Components));
|
2012-03-12 02:20:25 +00:00
|
|
|
$RequestHandler->response = $this->getMock('CakeResponse', array('notModified'));
|
2012-01-19 05:02:34 +00:00
|
|
|
$RequestHandler->response->expects($this->never())->method('notModified');
|
|
|
|
$this->assertNull($RequestHandler->beforeRender($this->Controller));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|