Skip to content

Commit

Permalink
Wire up the other HTTP methods in a basic fashion.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 28, 2012
1 parent 5b8e16f commit f3af8bd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
36 changes: 27 additions & 9 deletions lib/Cake/Network/Http/Client.php
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
47 changes: 18 additions & 29 deletions lib/Cake/Test/TestCase/Network/Http/ClientTest.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -262,20 +236,35 @@ 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']);
$mock->expects($this->once())
->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));
Expand All @@ -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);
}

Expand Down

0 comments on commit f3af8bd

Please sign in to comment.