From f3af8bd7d7eca7c416756d7058ff4f1459d4adc3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 27 Dec 2012 23:14:41 -0500 Subject: [PATCH] Wire up the other HTTP methods in a basic fashion. --- lib/Cake/Network/Http/Client.php | 36 ++++++++++---- .../Test/TestCase/Network/Http/ClientTest.php | 47 +++++++------------ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/lib/Cake/Network/Http/Client.php b/lib/Cake/Network/Http/Client.php index 6db12a45954..809cef37572 100644 --- a/lib/Cake/Network/Http/Client.php +++ b/lib/Cake/Network/Http/Client.php @@ -113,13 +113,12 @@ public function get($url, $data = [], $options = []) { unset($data['_content']); } $url = $this->buildUrl($url, $data, $options); - $request = $this->_createRequest( + return $this->_doRequest( Request::METHOD_GET, $url, $body, $options ); - return $this->send($request, $options); } /** @@ -133,13 +132,7 @@ public function get($url, $data = [], $options = []) { 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); + return $this->_doRequest(Request::METHOD_POST, $url, $data, $options); } /** @@ -151,6 +144,9 @@ public function post($url, $data = [], $options = []) { * @return Cake\Network\Http\Response */ public function put($url, $data = [], $options = []) { + $options = $this->_mergeOptions($options); + $url = $this->buildUrl($url, [], $options); + return $this->_doRequest(Request::METHOD_PUT, $url, $data, $options); } /** @@ -162,6 +158,9 @@ public function put($url, $data = [], $options = []) { * @return Cake\Network\Http\Response */ public function patch($url, $data = [], $options = []) { + $options = $this->_mergeOptions($options); + $url = $this->buildUrl($url, [], $options); + return $this->_doRequest(Request::METHOD_PATCH, $url, $data, $options); } /** @@ -173,6 +172,25 @@ public function patch($url, $data = [], $options = []) { * @return Cake\Network\Http\Response */ public function delete($url, $data = [], $options = []) { + $options = $this->_mergeOptions($options); + $url = $this->buildUrl($url, [], $options); + return $this->_doRequest(Request::METHOD_DELETE, $url, $data, $options); + } + +/** + * Helper method for doing non-GET requests. + * + * @param string $method HTTP method. + * @param string $url URL to request. + */ + protected function _doRequest($method, $url, $data, $options) { + $request = $this->_createRequest( + $method, + $url, + $data, + $options + ); + return $this->send($request, $options); } /** diff --git a/lib/Cake/Test/TestCase/Network/Http/ClientTest.php b/lib/Cake/Test/TestCase/Network/Http/ClientTest.php index b97a63c3479..6d08aa247e1 100644 --- a/lib/Cake/Test/TestCase/Network/Http/ClientTest.php +++ b/lib/Cake/Test/TestCase/Network/Http/ClientTest.php @@ -140,32 +140,6 @@ public function testBuildUrl($expected, $url, $query, $opts) { $this->assertEquals($expected, $result); } -/** - * test simple get request. - * - * @return void - */ - public function testGetSimple() { - $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_GET), - $this->attributeEqualTo('_url', 'http://cakephp.org/test.html') - )) - ->will($this->returnValue($response)); - - $http = new Client([ - 'host' => 'cakephp.org', - 'adapter' => $mock - ]); - $result = $http->get('/test.html'); - $this->assertSame($result, $response); - } - /** * test simple get request with headers & cookies. * @@ -262,12 +236,27 @@ public function testGetWithContent() { $this->assertSame($result, $response); } +/** + * Return a list of HTTP methods. + * + * @return array + */ + public static function methodProvider() { + return [ + [Request::METHOD_GET], + [Request::METHOD_POST], + [Request::METHOD_PUT], + [Request::METHOD_DELETE], + [Request::METHOD_PATCH], + ]; + } /** * test simple POST request. * + * @dataProvider methodProvider * @return void */ - public function testPostSimple() { + public function testMethodsSimple($method) { $response = new Response(); $mock = $this->getMock('Cake\Network\Http\Adapter\Stream', ['send']); @@ -275,7 +264,7 @@ public function testPostSimple() { ->method('send') ->with($this->logicalAnd( $this->isInstanceOf('Cake\Network\Http\Request'), - $this->attributeEqualTo('_method', Request::METHOD_POST), + $this->attributeEqualTo('_method', $method), $this->attributeEqualTo('_url', 'http://cakephp.org/projects/add') )) ->will($this->returnValue($response)); @@ -284,7 +273,7 @@ public function testPostSimple() { 'host' => 'cakephp.org', 'adapter' => $mock ]); - $result = $http->post('/projects/add'); + $result = $http->{$method}('/projects/add'); $this->assertSame($result, $response); }