minor update to Request::to() and Request::queryString() #854

Merged
merged 2 commits into from Mar 17, 2013
Jump to file or symbol
Failed to load files and symbols.
+64 −18
Split
View
@@ -121,21 +121,20 @@ public function body($data = null, $options = array()) {
*/
public function queryString($params = array(), $format = null) {
$result = array();
+ $query = array();
- foreach (array_filter(array($this->query, $params)) as $query) {
- if (is_string($query)) {
- $result[] = $query;
- continue;
- }
- $query = array_filter($query);
-
- if (!$format) {
- $result[] = http_build_query($query);
+ foreach (array_filter(array($this->query, $params)) as $querySet) {
+ if (is_string($querySet)) {
+ $result[] = $querySet;
continue;
}
+ $query = array_merge($query, $querySet);
+ }
+ $query = array_filter($query);
+
+ if ($format) {
$q = null;
-
- foreach ($params as $key => $value) {
+ foreach ($query as $key => $value) {
if (!is_array($value)) {
$q .= String::insert($format, array(
'key' => urlencode($key),
@@ -151,7 +150,10 @@ public function queryString($params = array(), $format = null) {
}
}
$result[] = substr($q, 0, -1);
+ } else {
+ $result[] = http_build_query($query);
}
+
$result = array_filter($result);
return $result ? "?" . join("&", $result) : null;
}
@@ -172,11 +174,9 @@ public function queryString($params = array(), $format = null) {
* - `'method'` _string_: If applicable, the HTTP method to use in the request.
* Mainly applies to the `'context'` format.
* - `'host'` _string_: The host name the request is pointing at.
- * - `'port'` _string_: The host port, if any. If specified, should be prefixed
- * with `':'`.
+ * - `'port'` _string_: The host port, if any.
* - `'path'` _string_: The URL path.
- * - `'query'` _mixed_: The query string of the URL as a string or array. If passed
- * as a string, should be prefixed with `'?'`.
+ * - `'query'` _mixed_: The query string of the URL as a string or array.
* - `'auth'` _string_: Authentication information. See the constructor for
* details.
* - `'content'` _string_: The body of the request.
@@ -189,7 +189,7 @@ public function to($format, array $options = array()) {
'method' => $this->method,
'scheme' => $this->scheme,
'host' => $this->host,
- 'port' => $this->port ? ":{$this->port}" : '',
+ 'port' => $this->port,
'path' => $this->path,
'query' => null,
'auth' => $this->auth,
@@ -205,6 +205,14 @@ public function to($format, array $options = array()) {
);
$options += $defaults;
+ if (is_string($options['query'])) {
+ $options['query'] = "?" . $options['query'];
+ } else if ($options['query']) {
+ $options['query'] = "?" . http_build_query($options['query']);
+ } else if ($options['query'] === null) {
+ $options['query'] = $this->queryString();
+ }
+
if ($options['auth']) {
$data = array();
@@ -221,7 +229,7 @@ public function to($format, array $options = array()) {
switch ($format) {
case 'url':
- $options['query'] = $this->queryString($options['query']);
+ $options['port'] = $options['port'] ? ":{$options['port']}" : '';
$options['path'] = str_replace('//', '/', $options['path']);
return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options);
case 'context':
@@ -237,7 +245,7 @@ public function to($format, array $options = array()) {
);
return array('http' => array_diff_key($options, $defaults) + $base);
case 'string':
- $path = str_replace('//', '/', $this->path) . $this->queryString($options['query']);
+ $path = str_replace('//', '/', $this->path) . $options['query'];
$status = "{$this->method} {$path} {$this->protocol}";
return join("\r\n", array($status, join("\r\n", $this->headers()), "", $body));
default:
@@ -111,6 +111,17 @@ public function testQueryStringSetup() {
$this->assertEqual($expected, $result);
}
+ public function testQueryStringMerge() {
+ $expected = "?param=foo";
+ $this->request->query = array('param' => 'value');
+ $result = $this->request->queryString(array('param' => 'foo'));
+ $this->assertEqual($expected, $result);
+
+ $expected = "?param=foo&param2=bar";
+ $result = $this->request->queryString(array('param' => 'foo', 'param2' => 'bar'));
+ $this->assertEqual($expected, $result);
+ }
+
public function testToString() {
$expected = join("\r\n", array(
'GET / HTTP/1.1',
@@ -237,6 +248,25 @@ public function testToUrl() {
$this->assertEqual($expected, $result);
}
+ public function testToUrlOverride() {
+ $request = new Request(array(
+ 'scheme' => 'http',
+ 'host' => 'localhost',
+ 'port' => 80,
+ 'query' => array('foo' => 'bar', 'bin' => 'baz')
+ ));
+
+ $result = $request->to('url', array(
+ 'scheme' => 'https',
+ 'host' => 'lithium.com',
+ 'port' => 443,
+ 'query' => array('foo' => 'you')
+ ));
+ $expected = 'https://lithium.com:443/?foo=you';
+
+ $this->assertEqual($expected, $result);
+ }
+
public function testToContext() {
$expected = array('http' => array(
'method' => 'GET',
@@ -306,6 +336,14 @@ public function testParseUrlToConfig() {
$expected = $url;
$result = $request->to('url');
$this->assertEqual($expected, $result);
+
+ $url = "http://localhost:80/path/one.php?param=1&param=2";
+ $config = parse_url($url);
+ $request = new Request($config);
+
+ $expected = $url;
+ $result = $request->to('url');
+ $this->assertEqual($expected, $result);
}
public function testQueryParamsConstructed() {