Skip to content

Commit

Permalink
Add basic test for POST.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 28, 2012
1 parent 3921015 commit 5b8e16f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/Cake/Network/Http/Client.php
Expand Up @@ -131,6 +131,15 @@ public function get($url, $data = [], $options = []) {
* @return Cake\Network\Http\Response
*/
public function post($url, $data = [], $options = []) {
$options = $this->_mergeOptions($options);
$url = $this->buildUrl($url, [], $options);
$request = $this->_createRequest(
Request::METHOD_POST,
$url,
$data,
$options
);
return $this->send($request, $options);
}

/**
Expand Down Expand Up @@ -166,8 +175,13 @@ public function patch($url, $data = [], $options = []) {
public function delete($url, $data = [], $options = []) {
}

protected function _mergeOptions($options)
{
/**
* Does a recursive merge of the parameter with the scope config.
*
* @param array $options Options to merge.
* @return array Options merged with set config.
*/
protected function _mergeOptions($options) {
return Hash::merge($this->_config, $options);
}

Expand Down
31 changes: 31 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/ClientTest.php
Expand Up @@ -153,6 +153,7 @@ public function testGetSimple() {
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/test.html')
))
->will($this->returnValue($response));
Expand Down Expand Up @@ -187,6 +188,7 @@ public function testGetSimpleWithHeadersAndCookies() {
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/test.html'),
$this->attributeEqualTo('_headers', $headers),
$this->attributeEqualTo('_cookies', $cookies)
Expand Down Expand Up @@ -214,6 +216,7 @@ public function testGetQuerystring() {
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$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));
Expand Down Expand Up @@ -243,6 +246,7 @@ public function testGetWithContent() {
->method('send')
->with($this->logicalAnd(
$this->isInstanceOf('Cake\Network\Http\Request'),
$this->attributeEqualTo('_method', Request::METHOD_GET),
$this->attributeEqualTo('_url', 'http://cakephp.org/search'),
$this->attributeEqualTo('_content', 'some data')
))
Expand All @@ -257,4 +261,31 @@ public function testGetWithContent() {
]);
$this->assertSame($result, $response);
}

/**
* test simple POST request.
*
* @return void
*/
public function testPostSimple() {
$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('_method', Request::METHOD_POST),
$this->attributeEqualTo('_url', 'http://cakephp.org/projects/add')
))
->will($this->returnValue($response));

$http = new Client([
'host' => 'cakephp.org',
'adapter' => $mock
]);
$result = $http->post('/projects/add');
$this->assertSame($result, $response);
}

}

0 comments on commit 5b8e16f

Please sign in to comment.