cakephp2-php8/cake/tests/cases/libs/cake_request.test.php

580 lines
14 KiB
PHP
Raw Normal View History

<?php
App::import('Core', 'CakeRequest');
class CakeRequestTestCase extends CakeTestCase {
/**
* setup callback
*
* @return void
*/
function startTest() {
$this->_server = $_SERVER;
$this->_get = $_GET;
$this->_post = $_POST;
$this->_files = $_FILES;
}
/**
* end test
*
* @return void
*/
function endTest() {
$_SERVER = $this->_server;
$_GET = $this->_get;
$_POST = $this->_post;
$_FILES = $this->_files;
}
/**
* test construction
*
* @return void
*/
function testConstructionGetParsing() {
$_GET = array(
'one' => 'param',
'two' => 'banana'
);
$request = new CakeRequest();
$this->assertEqual($request->query, $_GET);
$_GET = array(
'one' => 'param',
'two' => 'banana',
'url' => '/some/path/here'
);
$request = new CakeRequest();
$this->assertEqual($request->query, $_GET);
$this->assertEqual($request->url, $_GET['url']);
}
/**
* test parsing POST data into the object.
*
* @return void
*/
function testPostParsing() {
$_POST = array('data' => array(
'Article' => array('title')
));
$request = new CakeRequest();
$this->assertEqual($request->data, $_POST['data']);
$_POST = array('one' => 1, 'two' => 'three');
$request = new CakeRequest();
$this->assertEqual($request->params['form'], $_POST);
}
/**
* test parsing of FILES array
*
* @return void
*/
function testFILESParsing() {
$_FILES = array('data' => array('name' => array(
'File' => array(
array('data' => 'cake_mssql_patch.patch'),
array('data' => 'controller.diff'),
array('data' => ''),
array('data' => ''),
),
'Post' => array('attachment' => 'jquery-1.2.1.js'),
),
'type' => array(
'File' => array(
array('data' => ''),
array('data' => ''),
array('data' => ''),
array('data' => ''),
),
'Post' => array('attachment' => 'application/x-javascript'),
),
'tmp_name' => array(
'File' => array(
array('data' => '/private/var/tmp/phpy05Ywj'),
array('data' => '/private/var/tmp/php7MBztY'),
array('data' => ''),
array('data' => ''),
),
'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
),
'error' => array(
'File' => array(
array('data' => 0),
array('data' => 0),
array('data' => 4),
array('data' => 4)
),
'Post' => array('attachment' => 0)
),
'size' => array(
'File' => array(
array('data' => 6271),
array('data' => 350),
array('data' => 0),
array('data' => 0),
),
'Post' => array('attachment' => 80469)
),
));
$request = new CakeRequest();
$expected = array(
'File' => array(
array('data' => array(
'name' => 'cake_mssql_patch.patch',
'type' => '',
'tmp_name' => '/private/var/tmp/phpy05Ywj',
'error' => 0,
'size' => 6271,
)),
array(
'data' => array(
'name' => 'controller.diff',
'type' => '',
'tmp_name' => '/private/var/tmp/php7MBztY',
'error' => 0,
'size' => 350,
)),
array('data' => array(
'name' => '',
'type' => '',
'tmp_name' => '',
'error' => 4,
'size' => 0,
)),
array('data' => array(
'name' => '',
'type' => '',
'tmp_name' => '',
'error' => 4,
'size' => 0,
)),
),
'Post' => array('attachment' => array(
'name' => 'jquery-1.2.1.js',
'type' => 'application/x-javascript',
'tmp_name' => '/private/var/tmp/phpEwlrIo',
'error' => 0,
'size' => 80469,
))
);
$this->assertEqual($request->data, $expected);
$_FILES = array(
'data' => array(
'name' => array(
'Document' => array(
1 => array(
'birth_cert' => 'born on.txt',
'passport' => 'passport.txt',
'drivers_license' => 'ugly pic.jpg'
),
2 => array(
'birth_cert' => 'aunt betty.txt',
'passport' => 'betty-passport.txt',
'drivers_license' => 'betty-photo.jpg'
),
),
),
'type' => array(
'Document' => array(
1 => array(
'birth_cert' => 'application/octet-stream',
'passport' => 'application/octet-stream',
'drivers_license' => 'application/octet-stream',
),
2 => array(
'birth_cert' => 'application/octet-stream',
'passport' => 'application/octet-stream',
'drivers_license' => 'application/octet-stream',
)
)
),
'tmp_name' => array(
'Document' => array(
1 => array(
'birth_cert' => '/private/var/tmp/phpbsUWfH',
'passport' => '/private/var/tmp/php7f5zLt',
'drivers_license' => '/private/var/tmp/phpMXpZgT',
),
2 => array(
'birth_cert' => '/private/var/tmp/php5kHZt0',
'passport' => '/private/var/tmp/phpnYkOuM',
'drivers_license' => '/private/var/tmp/php9Rq0P3',
)
)
),
'error' => array(
'Document' => array(
1 => array(
'birth_cert' => 0,
'passport' => 0,
'drivers_license' => 0,
),
2 => array(
'birth_cert' => 0,
'passport' => 0,
'drivers_license' => 0,
)
)
),
'size' => array(
'Document' => array(
1 => array(
'birth_cert' => 123,
'passport' => 458,
'drivers_license' => 875,
),
2 => array(
'birth_cert' => 876,
'passport' => 976,
'drivers_license' => 9783,
)
)
)
)
);
$request = new CakeRequest();
$expected = array(
'Document' => array(
1 => array(
'birth_cert' => array(
'name' => 'born on.txt',
'tmp_name' => '/private/var/tmp/phpbsUWfH',
'error' => 0,
'size' => 123,
'type' => 'application/octet-stream',
),
'passport' => array(
'name' => 'passport.txt',
'tmp_name' => '/private/var/tmp/php7f5zLt',
'error' => 0,
'size' => 458,
'type' => 'application/octet-stream',
),
'drivers_license' => array(
'name' => 'ugly pic.jpg',
'tmp_name' => '/private/var/tmp/phpMXpZgT',
'error' => 0,
'size' => 875,
'type' => 'application/octet-stream',
),
),
2 => array(
'birth_cert' => array(
'name' => 'aunt betty.txt',
'tmp_name' => '/private/var/tmp/php5kHZt0',
'error' => 0,
'size' => 876,
'type' => 'application/octet-stream',
),
'passport' => array(
'name' => 'betty-passport.txt',
'tmp_name' => '/private/var/tmp/phpnYkOuM',
'error' => 0,
'size' => 976,
'type' => 'application/octet-stream',
),
'drivers_license' => array(
'name' => 'betty-photo.jpg',
'tmp_name' => '/private/var/tmp/php9Rq0P3',
'error' => 0,
'size' => 9783,
'type' => 'application/octet-stream',
),
),
)
);
$this->assertEqual($request->data, $expected);
$_FILES = array(
'data' => array(
'name' => array('birth_cert' => 'born on.txt'),
'type' => array('birth_cert' => 'application/octet-stream'),
'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
'error' => array('birth_cert' => 0),
'size' => array('birth_cert' => 123)
)
);
$request = new CakeRequest();
$expected = array(
'birth_cert' => array(
'name' => 'born on.txt',
'type' => 'application/octet-stream',
'tmp_name' => '/private/var/tmp/phpbsUWfH',
'error' => 0,
'size' => 123
)
);
$this->assertEqual($request->data, $expected);
$_FILES = array(
'something' => array(
'name' => 'something.txt',
'type' => 'text/plain',
'tmp_name' => '/some/file',
'error' => 0,
'size' => 123
)
);
$request = new CakeRequest();
$this->assertEqual($request->params['form'], $_FILES);
}
/**
* test method overrides coming in from POST data.
*
* @return void
*/
function testMethodOverrides() {
$_POST = array('_method' => 'POST');
$request = new CakeRequest();
$this->assertEqual(env('REQUEST_METHOD'), 'POST');
$_POST = array('_method' => 'DELETE');
$request = new CakeRequest();
$this->assertEqual(env('REQUEST_METHOD'), 'DELETE');
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
$request = new CakeRequest();
$this->assertEqual(env('REQUEST_METHOD'), 'PUT');
}
/**
* test the getClientIp method.
*
* @return void
*/
function testGetClientIp() {
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
$_SERVER['REMOTE_ADDR'] = '192.168.1.3';
$request = new CakeRequest();
$this->assertEqual($request->getClientIP(false), '192.168.1.5');
$this->assertEqual($request->getClientIP(), '192.168.1.2');
unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$this->assertEqual($request->getClientIP(), '192.168.1.2');
unset($_SERVER['HTTP_CLIENT_IP']);
$this->assertEqual($request->getClientIP(), '192.168.1.3');
$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
$this->assertEqual($request->getClientIP(), '10.0.1.2');
}
/**
* test the referer function.
*
* @return void
*/
function testReferer() {
$request = new CakeRequest();
$_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
$result = $request->referer();
$this->assertIdentical($result, 'http://cakephp.org');
$_SERVER['HTTP_REFERER'] = '';
$result = $request->referer();
$this->assertIdentical($result, '/');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
$result = $request->referer(true);
$this->assertIdentical($result, '/some/path');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
$result = $request->referer();
$this->assertIdentical($result, FULL_BASE_URL . '/some/path');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'some/path';
$result = $request->referer(true);
$this->assertIdentical($result, '/some/path');
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL . 'recipes/add';
$result = $request->referer(true);
$this->assertIdentical($result, '/recipes/add');
}
/**
* test the simple uses of is()
*
* @return void
*/
function testIsHttpMethods() {
$request = new CakeRequest();
$this->assertFalse($request->is('undefined-behavior'));
$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertTrue($request->is('get'));
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertTrue($request->is('POST'));
$_SERVER['REQUEST_METHOD'] = 'PUT';
$this->assertTrue($request->is('put'));
$this->assertFalse($request->is('get'));
$_SERVER['REQUEST_METHOD'] = 'DELETE';
$this->assertTrue($request->is('delete'));
$this->assertTrue($request->isDelete());
$_SERVER['REQUEST_METHOD'] = 'delete';
$this->assertFalse($request->is('delete'));
}
/**
* test ajax, flash and friends
*
* @return void
*/
function testisAjaxFlashAndFriends() {
$request = new CakeRequest();
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
$this->assertTrue($request->is('flash'));
$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
$this->assertTrue($request->is('flash'));
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->assertTrue($request->is('ajax'));
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
$this->assertFalse($request->is('ajax'));
$this->assertFalse($request->isAjax());
$_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
$this->assertTrue($request->is('mobile'));
$this->assertTrue($request->isMobile());
}
/**
* test is(ssl)
*
* @return void
*/
function testIsSsl() {
$request = new CakeRequest();
$_SERVER['HTTPS'] = 1;
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = 'on';
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = '1';
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = 'I am not empty';
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = 1;
$this->assertTrue($request->is('ssl'));
$_SERVER['HTTPS'] = 'off';
$this->assertFalse($request->is('ssl'));
$_SERVER['HTTPS'] = false;
$this->assertFalse($request->is('ssl'));
$_SERVER['HTTPS'] = '';
$this->assertFalse($request->is('ssl'));
}
2010-04-27 03:37:36 +00:00
/**
* test getting request params with object properties.
*
* @return void
*/
function test__get() {
$request = new CakeRequest();
$request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
$this->assertEqual($request->controller, 'posts');
$this->assertEqual($request->action, 'view');
$this->assertEqual($request->plugin, 'blogs');
$this->assertIdentical($request->banana, null);
}
/**
* test the array access implementation
*
* @return void
*/
function testArrayAccess() {
$request = new CakeRequest();
$request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
$this->assertEqual($request['controller'], 'posts');
$request['slug'] = 'speedy-slug';
$this->assertEqual($request->slug, 'speedy-slug');
$this->assertEqual($request['slug'], 'speedy-slug');
$this->assertTrue(isset($request['action']));
$this->assertFalse(isset($request['wrong-param']));
$this->assertTrue(isset($request['plugin']));
unset($request['plugin']);
$this->assertFalse(isset($request['plugin']));
$this->assertNull($request['plugin']);
$this->assertNull($request->plugin);
}
/**
* test adding detectors and having them work.
*
* @return void
*/
function testAddDetector() {
$request = new CakeRequest();
$request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
$_SERVER['TEST_VAR'] = 'something';
$this->assertTrue($request->is('compare'), 'Value match failed %s.');
$_SERVER['TEST_VAR'] = 'wrong';
$this->assertFalse($request->is('compare'), 'Value mis-match failed %s.');
$request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
$_SERVER['TEST_VAR'] = 'banana';
$this->assertTrue($request->isBanana());
$_SERVER['TEST_VAR'] = 'wrong value';
$this->assertFalse($request->isBanana());
$request->addDetector('mobile', array('options' => array('Imagination')));
$_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
$this->assertTrue($request->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
$this->assertTrue($request->isMobile());
$request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback')));
$request->return = true;
$this->assertTrue($request->isCallMe());
$request->return = false;
$this->assertFalse($request->isCallMe());
}
/**
* helper function for testing callbacks.
*
* @return void
*/
function _detectCallback($request) {
return $request->return == true;
}
}