Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zendframework/zendframework#6492-support-delete-…
Browse files Browse the repository at this point in the history
…post-request-body' into develop

Close zendframework/zendframework#6492
Forward port zendframework/zendframework#6492
  • Loading branch information
Ocramius committed Nov 19, 2014
6 parents 4b05631 + 651043c + 94deaef + ffd4e9b + 2aef442 + 281d6f9 commit d87b53f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
16 changes: 13 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,19 @@ public function setMethod($method)
{
$method = $this->getRequest()->setMethod($method)->getMethod();

if (($method == Request::METHOD_POST || $method == Request::METHOD_PUT ||
$method == Request::METHOD_DELETE || $method == Request::METHOD_PATCH)
&& empty($this->encType)) {
if (empty($this->encType)
&& in_array(
$method,
array(
Request::METHOD_POST,
Request::METHOD_PUT,
Request::METHOD_DELETE,
Request::METHOD_PATCH,
Request::METHOD_OPTIONS,
),
true
)
) {
$this->setEncType(self::ENC_URLENCODED);
}

Expand Down
10 changes: 1 addition & 9 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
* Make sure POSTFIELDS is set after $curlMethod is set:
* @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
*/
if ($method == 'POST') {
if (in_array($method, array('POST', 'PATCH', 'DELETE', 'OPTIONS'), true)) {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
} elseif ($curlMethod == CURLOPT_UPLOAD) {
// this covers a PUT by file-handle:
Expand All @@ -385,14 +385,6 @@ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $bo
curl_setopt($this->curl, CURLOPT_INFILESIZE, $this->config['curloptions'][CURLOPT_INFILESIZE]);
unset($this->config['curloptions'][CURLOPT_INFILE]);
unset($this->config['curloptions'][CURLOPT_INFILESIZE]);
} elseif ($method == 'PUT') {
// This is a PUT by a setRawData string, not by file-handle
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
} elseif ($method == 'PATCH') {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
} elseif ($method == 'DELETE' && $body) {
// DELETE requests can also have a body
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
}

// set additional curl options
Expand Down
40 changes: 40 additions & 0 deletions test/Client/CommonHttpTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,46 @@ public function testPatchData($params)
$this->assertEquals(serialize($params), $res->getBody(), "PATCH data integrity test failed");
}

/**
* Test we can properly send DELETE parameters with
* application/x-www-form-urlencoded content type
*
* @dataProvider parameterArrayProvider
*/
public function testDeleteData($params)
{
$client = $this->client;
$client->setUri($this->baseuri . 'testDeleteData.php');

$client->setRawBody(serialize($params));

$client->setMethod('DELETE');
$this->assertEquals($client::ENC_URLENCODED, $this->client->getEncType());
$this->assertTrue($client->getRequest()->isDelete());
$res = $this->client->send();
$this->assertEquals(serialize($params), $res->getBody(), "DELETE data integrity test failed");
}

/**
* Test we can properly send OPTIONS parameters with
* application/x-www-form-urlencoded content type
*
* @dataProvider parameterArrayProvider
*/
public function testOptionsData($params)
{
$client = $this->client;
$client->setUri($this->baseuri . 'testOptionsData.php');

$client->setRawBody(serialize($params));

$client->setMethod('OPTIONS');
$this->assertEquals($client::ENC_URLENCODED, $this->client->getEncType());
$this->assertTrue($client->getRequest()->isOptions());
$res = $this->client->send();
$this->assertEquals(serialize($params), $res->getBody(), "OPTIONS data integrity test failed");
}

/**
* Test we can properly send POST parameters with
* multipart/form-data content type
Expand Down
10 changes: 10 additions & 0 deletions test/Client/_files/testDeleteData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

readfile("php://input");
10 changes: 10 additions & 0 deletions test/Client/_files/testOptionsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

readfile("php://input");
7 changes: 1 addition & 6 deletions test/Client/_files/testPatchData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

$putdata = fopen("php://input", "r");
while ($data = fread($putdata, 1024)) {
echo $data;
}

fclose($putdata);
readfile("php://input");

0 comments on commit d87b53f

Please sign in to comment.