Skip to content

Commit

Permalink
Allow string payloads for GET requests.
Browse files Browse the repository at this point in the history
Allow pre-built query strings in GET requests.

Refs #6540
  • Loading branch information
markstory committed May 13, 2015
1 parent 9ab2b4d commit 2b66481
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Network/Http/Client.php
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Network/Http/ClientTest.php
Expand Up @@ -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.
Expand Down

0 comments on commit 2b66481

Please sign in to comment.