Skip to content
Permalink
Browse files

Add support for request bodies in GET requests.

  • Loading branch information...
markstory committed Dec 27, 2012
1 parent e454238 commit 3921015bcdfde986b5dcba04242ca5229336d5d1
Showing with 40 additions and 1 deletion.
  1. +11 −1 lib/Cake/Network/Http/Client.php
  2. +29 −0 lib/Cake/Test/TestCase/Network/Http/ClientTest.php
@@ -95,18 +95,28 @@ public function config($config = null) {
/**
* Do a GET request.
*
* The $data argument supports a special `_content` key
* for providing a request body in a GET request. This is
* generally not used but services like ElasticSearch use
* this feature.
*
* @param string $url The url or path you want to request.
* @param array $data The query data you want to send.
* @param array $options Additional options for the request.
* @return Cake\Network\Http\Response
*/
public function get($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$body = [];
if (isset($data['_content'])) {
$body = $data['_content'];
unset($data['_content']);
}
$url = $this->buildUrl($url, $data, $options);
$request = $this->_createRequest(
Request::METHOD_GET,
$url,
[],
$body,
$options
);
return $this->send($request, $options);
@@ -228,4 +228,33 @@ public function testGetQuerystring() {
]);
$this->assertSame($result, $response);
}
/**
* Test a GET with a request body. Services like
* elasticsearch use this feature.
*
* @return void
*/
public function testGetWithContent() {
$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'),
$this->attributeEqualTo('_content', 'some data')
))
->will($this->returnValue($response));
$http = new Client([
'host' => 'cakephp.org',
'adapter' => $mock
]);
$result = $http->get('/search', [
'_content' => 'some data'
]);
$this->assertSame($result, $response);
}
}

0 comments on commit 3921015

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