Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Read $_COOKIE superglobal into the request object, add tests, and fix request query config issue. #860

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+58 −3
Split
View
@@ -175,6 +175,12 @@ protected function _init() {
if (isset($_POST)) {
$this->data += $_POST;
}
+ if (!empty($this->_config['cookies'])) {
+ $this->cookies = $this->_config['cookies'];
+ }
+ if (isset($_COOKIE)) {
+ $this->cookies += $_COOKIE;
+ }
if (isset($this->data['_method'])) {
$this->_env['HTTP_X_HTTP_METHOD_OVERRIDE'] = strtoupper($this->data['_method']);
unset($this->data['_method']);
View
@@ -83,7 +83,7 @@ public function __construct(array $config = array()) {
'auth' => null
);
$config += $defaults;
- parent::__construct($config);
+
foreach (array_intersect_key(array_filter($config), $defaults) as $key => $value) {
$this->{$key} = $value;
}
@@ -92,6 +92,8 @@ public function __construct(array $config = array()) {
}
$this->path = str_replace('//', '/', "/{$this->path}");
$this->protocol = $this->protocol ?: "HTTP/{$this->version}";
+
+ parent::__construct($config);
}
/**
View
@@ -84,6 +84,16 @@ public function __construct(array $config = array()) {
'followLocation' => true
);
parent::__construct($config + $defaults);
+ }
+
+ /**
+ * Initialize request object method, content type, and headers
+ *
+ * @return void
+ */
+ protected function _init() {
+ parent::_init();
+
$this->method = $this->method ?: $this->_config['method'];
$this->headers = array(
@@ -19,15 +19,23 @@ class RequestTest extends \lithium\test\Unit {
protected $_get = array();
+ protected $_post = array();
+
+ protected $_cookie = array();
+
public function setUp() {
$this->request = new Request(array('init' => false));
$this->_get = $_GET;
- unset($_GET);
+ $this->_post = $_POST;
+ $this->_cookie = $_COOKIE;
+ unset($_GET, $_POST, $_COOKIE);
}
public function tearDown() {
unset($this->request);
$_GET = $this->_get;
+ $_POST = $this->_post;
+ $_COOKIE = $this->_cookie;
}
public function testInitData() {
@@ -871,6 +879,13 @@ public function testDataFromConstructor() {
$expected = array('name' => 'bob');
$result = $request->data;
$this->assertEqual($expected, $result);
+
+ $_POST['organization'] = 'Union of Rad';
+ $request = new Request(array('data' => array('name' => 'bob')));
+
+ $expected = array('name' => 'bob', 'organization' => 'Union of Rad');
+ $result = $request->data;
+ $this->assertEqual($expected, $result);
}
public function testQueryFromConstructor() {
@@ -879,6 +894,28 @@ public function testQueryFromConstructor() {
$expected = array('page' => 1);
$result = $request->query;
$this->assertEqual($expected, $result);
+
+ $_GET = array('limit' => 10);
+ $request = new Request(array('query' => array('page' => 1)));
+
+ $expected = array('page' => 1, 'limit' => 10);
+ $result = $request->query;
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testCookiesFromConstructor() {
+ $request = new Request(array('cookies' => array('sid' => 12345)));
+
+ $expected = array('sid' => 12345);
+ $result = $request->cookies;
+ $this->assertEqual($expected, $result);
+
+ $_COOKIE['token'] = 'foo';
+ $request = new Request(array('cookies' => array('sid' => 12345)));
+
+ $expected = array('sid' => 12345, 'token' => 'foo');
+ $result = $request->cookies;
+ $this->assertEqual($expected, $result);
}
public function testMethodOverrideFromData() {
@@ -15,7 +15,7 @@ class RequestTest extends \lithium\test\Unit {
public $request = null;
public function setUp() {
- $this->request = new Request(array('init' => false));
+ $this->request = new Request();
}
public function testConstruct() {