Skip to content
Permalink
Browse files

Allow string payloads for GET requests.

Allow pre-built query strings in GET requests.

Refs #6540
  • Loading branch information...
markstory committed May 13, 2015
1 parent 9ab2b4d commit 2b6648164cdb817c775c53021387d67d229dafbf
Showing with 34 additions and 3 deletions.
  1. +4 −3 src/Network/Http/Client.php
  2. +30 −0 tests/TestCase/Network/Http/ClientTest.php
@@ -192,7 +192,7 @@ public function cookies()
* @param array $options Additional options for the request.
* @return \Cake\Network\Http\Response
*/
public function get($url, array $data = [], array $options = [])
public function get($url, $data = [], array $options = [])
{
$options = $this->_mergeOptions($options);
$body = [];
@@ -369,7 +369,7 @@ public function send(Request $request, $options = [])
* Generate a URL based on the scoped client options.
*
* @param string $url Either a full URL or just the path.
* @param array $query The query data for the URL.
* @param string|array $query The query data for the URL.
* @param array $options The config options stored with Client::config()
* @return string A complete url with scheme, port, host, path.
*/
@@ -380,7 +380,8 @@ public function buildUrl($url, $query = [], $options = [])
}
if ($query) {
$q = (strpos($url, '?') === false) ? '?' : '&';
$url .= $q . http_build_query($query);
$url .= $q;
$url .= is_string($query) ? $query : http_build_query($query);
}
if (preg_match('#^https?://#', $url)) {
return $url;
@@ -217,6 +217,36 @@ public function testGetQuerystring()
$this->assertSame($result, $response);
}
/**
* test get request with string of query data.
*
* @return void
*/
public function testGetQuerystringString()
{
$response = new Response();
$mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']);
$mock->expects($this->once())
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_url', 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3')
))
->will($this->returnValue([$response]));
$http = new Client([
'host' => 'cakephp.org',
'adapter' => $mock
]);
$data = [
'q' => 'hi there',
'Category' => ['id' => [2, 3]]
];
$result = $http->get('/search', http_build_query($data));
$this->assertSame($result, $response);
}
/**
* Test a GET with a request body. Services like
* elasticsearch use this feature.

0 comments on commit 2b66481

Please sign in to comment.
You can’t perform that action at this time.