Moving tests and helper methods from Dispatcher into CakeRequest

This commit is contained in:
Mark Story 2010-05-01 01:06:09 -04:00
parent 1b5c0cca4d
commit 558aea0221

View file

@ -800,4 +800,372 @@ class CakeRequestTestCase extends CakeTestCase {
$this->assertEqual($request->webroot, '/dbhauser/app/webroot/');
}
/**
* testEnvironmentDetection method
*
* @return void
*/
public function testEnvironmentDetection() {
$dispatcher =& new Dispatcher();
$environments = array(
'IIS' => array(
'No rewrite base path' => array(
'App' => array(
'base' => false,
'baseUrl' => '/index.php?',
'server' => 'IIS'
),
'SERVER' => array(
'HTTPS' => 'off',
'SCRIPT_NAME' => '/index.php',
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
'QUERY_STRING' => '',
'REMOTE_ADDR' => '127.0.0.1',
'REMOTE_HOST' => '127.0.0.1',
'REQUEST_METHOD' => 'GET',
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => '80',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_SOFTWARE' => 'Microsoft-IIS/5.1',
'APPL_PHYSICAL_PATH' => 'C:\\Inetpub\\wwwroot\\',
'REQUEST_URI' => '/index.php',
'URL' => '/index.php',
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
'ORIG_PATH_INFO' => '/index.php',
'PATH_INFO' => '',
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
'PHP_SELF' => '/index.php',
'HTTP_ACCEPT' => '*/*',
'HTTP_ACCEPT_LANGUAGE' => 'en-us',
'HTTP_CONNECTION' => 'Keep-Alive',
'HTTP_HOST' => 'localhost',
'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'argv' => array(),
'argc' => 0
),
'reload' => true,
'base' => '/index.php?',
'webroot' => '/',
'url' => ''
),
'No rewrite with path' => array(
'SERVER' => array(
'QUERY_STRING' => '/posts/add',
'REQUEST_URI' => '/index.php?/posts/add',
'URL' => '/index.php?/posts/add',
'argv' => array('/posts/add'),
'argc' => 1
),
'reload' => false,
'url' => 'posts/add',
'base' => '/index.php?',
'webroot' => '/'
),
'No rewrite sub dir 1' => array(
'GET' => array(),
'SERVER' => array(
'QUERY_STRING' => '',
'REQUEST_URI' => '/index.php',
'URL' => '/index.php',
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
'ORIG_PATH_INFO' => '/index.php',
'PATH_INFO' => '',
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
'PHP_SELF' => '/index.php',
'argv' => array(),
'argc' => 0
),
'reload' => false,
'url' => '',
'base' => '/index.php?',
'webroot' => '/'
),
'No rewrite sub dir 1 with path' => array(
'GET' => array('/posts/add' => ''),
'SERVER' => array(
'QUERY_STRING' => '/posts/add',
'REQUEST_URI' => '/index.php?/posts/add',
'URL' => '/index.php?/posts/add',
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
'argv' => array('/posts/add'),
'argc' => 1
),
'reload' => false,
'url' => 'posts/add',
'base' => '/index.php?',
'webroot' => '/'
),
'No rewrite sub dir 2' => array(
'App' => array(
'base' => false,
'baseUrl' => '/site/index.php?',
'dir' => 'app',
'webroot' => 'webroot',
'server' => 'IIS'
),
'GET' => array(),
'POST' => array(),
'SERVER' => array(
'SCRIPT_NAME' => '/site/index.php',
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
'QUERY_STRING' => '',
'REQUEST_URI' => '/site/index.php',
'URL' => '/site/index.php',
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
'PHP_SELF' => '/site/index.php',
'argv' => array(),
'argc' => 0
),
'reload' => false,
'url' => '',
'base' => '/site/index.php?',
'webroot' => '/site/app/webroot/'
),
'No rewrite sub dir 2 with path' => array(
'GET' => array('/posts/add' => ''),
'SERVER' => array(
'SCRIPT_NAME' => '/site/index.php',
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
'QUERY_STRING' => '/posts/add',
'REQUEST_URI' => '/site/index.php?/posts/add',
'URL' => '/site/index.php?/posts/add',
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
'PHP_SELF' => '/site/index.php',
'argv' => array('/posts/add'),
'argc' => 1
),
'reload' => false,
'url' => 'posts/add',
'base' => '/site/index.php?',
'webroot' => '/site/app/webroot/'
)
),
'Apache' => array(
'No rewrite base path' => array(
'App' => array(
'base' => false,
'baseUrl' => '/index.php',
'dir' => 'app',
'webroot' => 'webroot'
),
'SERVER' => array(
'SERVER_NAME' => 'localhost',
'SERVER_ADDR' => '::1',
'SERVER_PORT' => '80',
'REMOTE_ADDR' => '::1',
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot',
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => '',
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '/index.php',
'PHP_SELF' => '/index.php',
'argv' => array(),
'argc' => 0
),
'reload' => true,
'url' => '',
'base' => '/index.php',
'webroot' => '/'
),
'No rewrite with path' => array(
'SERVER' => array(
'UNIQUE_ID' => 'VardGqn@17IAAAu7LY8AAAAK',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.5 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6',
'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'HTTP_ACCEPT_LANGUAGE' => 'en-us',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_HOST' => 'localhost',
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot',
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/officespace/app/webroot/index.php',
'QUERY_STRING' => '',
'REQUEST_URI' => '/index.php/posts/add',
'SCRIPT_NAME' => '/index.php',
'PATH_INFO' => '/posts/add',
'PHP_SELF' => '/index.php/posts/add',
'argv' => array(),
'argc' => 0
),
'reload' => false,
'url' => 'posts/add',
'base' => '/index.php',
'webroot' => '/'
),
'GET Request at base domain' => array(
'App' => array(
'base' => false,
'baseUrl' => null,
'dir' => 'app',
'webroot' => 'webroot'
),
'SERVER' => array(
'UNIQUE_ID' => '2A-v8sCoAQ8AAAc-2xUAAAAB',
'HTTP_ACCEPT_LANGUAGE' => 'en-us',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_COOKIE' => 'CAKEPHP=jcbv51apn84kd9ucv5aj2ln3t3',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_HOST' => 'cake.1.2',
'SERVER_NAME' => 'cake.1.2',
'SERVER_ADDR' => '127.0.0.1',
'SERVER_PORT' => '80',
'REMOTE_ADDR' => '127.0.0.1',
'DOCUMENT_ROOT' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot',
'SERVER_ADMIN' => 'you@example.com',
'SCRIPT_FILENAME' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot/index.php',
'REMOTE_PORT' => '53550',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'a=b',
'REQUEST_URI' => '/?a=b',
'SCRIPT_NAME' => '/index.php',
'PHP_SELF' => '/index.php'
),
'GET' => array('a' => 'b'),
'POST' => array(),
'reload' => true,
'url' => '',
'base' => '',
'webroot' => '/',
'urlParams' => array('a' => 'b'),
'environment' => array('CGI_MODE' => false)
),
'New CGI no mod_rewrite' => array(
'App' => array(
'base' => false,
'baseUrl' => '/limesurvey20/index.php',
'dir' => 'app',
'webroot' => 'webroot'
),
'SERVER' => array(
'DOCUMENT_ROOT' => '/home/.sites/110/site313/web',
'PATH_INFO' => '/installations',
'PATH_TRANSLATED' => '/home/.sites/110/site313/web/limesurvey20/index.php',
'PHPRC' => '/home/.sites/110/site313',
'QUERY_STRING' => '',
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/limesurvey20/index.php/installations',
'SCRIPT_FILENAME' => '/home/.sites/110/site313/web/limesurvey20/index.php',
'SCRIPT_NAME' => '/limesurvey20/index.php',
'SCRIPT_URI' => 'http://www.gisdat-umfragen.at/limesurvey20/index.php/installations',
'PHP_SELF' => '/limesurvey20/index.php/installations',
'CGI_MODE' => true
),
'GET' => array(),
'POST' => array(),
'reload' => true,
'webroot' => '/limesurvey20/app/webroot/',
'base' => '/limesurvey20/index.php',
'url' => 'installations',
'urlParams' => array(),
'environment' => array('CGI_MODE' => true)
)
)
);
$backup = $this->__backupEnvironment();
foreach ($environments as $name => $env) {
foreach ($env as $descrip => $settings) {
if ($settings['reload']) {
$this->__reloadEnvironment();
}
$this->__loadEnvironment($settings);
$request = new CakeRequest();
$this->assertEqual($request->url, $settings['url'], "%s url on env: {$name} on setting {$descrip}");
$this->assertEqual($request->base, $settings['base'], "%s base on env: {$name} on setting {$descrip}");
$this->assertEqual($request->webroot, $settings['webroot'], "%s webroot on env: {$name} on setting {$descrip}");
if (isset($settings['urlParams'])) {
$this->assertEqual($_GET, $settings['urlParams'], "%s on environment: {$name}, on setting: {$descrip}");
}
if (isset($settings['environment'])) {
foreach ($settings['environment'] as $key => $val) {
$this->assertEqual(env($key), $val, "%s on key {$key} on environment: {$name}, on setting: {$descrip}");
}
}
}
}
$this->__loadEnvironment(array_merge(array('reload' => true), $backup));
}
/**
* backupEnvironment method
*
* @return void
* @access private
*/
function __backupEnvironment() {
return array(
'App' => Configure::read('App'),
'GET' => $_GET,
'POST' => $_POST,
'SERVER' => $_SERVER
);
}
/**
* reloadEnvironment method
*
* @return void
* @access private
*/
function __reloadEnvironment() {
foreach ($_GET as $key => $val) {
unset($_GET[$key]);
}
foreach ($_POST as $key => $val) {
unset($_POST[$key]);
}
foreach ($_SERVER as $key => $val) {
unset($_SERVER[$key]);
}
Configure::write('App', array());
}
/**
* loadEnvironment method
*
* @param mixed $env
* @return void
* @access private
*/
function __loadEnvironment($env) {
if ($env['reload']) {
$this->__reloadEnvironment();
}
if (isset($env['App'])) {
Configure::write('App', $env['App']);
}
if (isset($env['GET'])) {
foreach ($env['GET'] as $key => $val) {
$_GET[$key] = $val;
}
}
if (isset($env['POST'])) {
foreach ($env['POST'] as $key => $val) {
$_POST[$key] = $val;
}
}
if (isset($env['SERVER'])) {
foreach ($env['SERVER'] as $key => $val) {
$_SERVER[$key] = $val;
}
}
}
}