Automatically handle PUT requests

make the following "just work"

    curl -X PUT -d foo=bar http://...
This commit is contained in:
AD7six 2012-06-06 22:58:27 +02:00
parent d1475b1fd3
commit b509bdb04b
2 changed files with 110 additions and 2 deletions

View file

@ -161,7 +161,14 @@ class CakeRequest implements ArrayAccess {
* @return void
*/
protected function _processPost() {
$this->data = $_POST;
if ($_POST) {
$this->data = $_POST;
} elseif ($this->is('put')) {
$this->data = $this->_readInput();
if (env('CONTENT_TYPE') === 'application/x-www-form-urlencoded') {
parse_str($this->data, $this->data);
}
}
if (ini_get('magic_quotes_gpc') === '1') {
$this->data = stripslashes_deep($this->data);
}

View file

@ -21,6 +21,34 @@ App::uses('Dispatcher', 'Routing');
App::uses('Xml', 'Utility');
App::uses('CakeRequest', 'Network');
class TestCakeRequest extends CakeRequest {
public $input;
public function reConstruct($url = 'some/path', $parseEnvironment = true) {
$this->_base();
if (empty($url)) {
$url = $this->_url();
}
if ($url[0] == '/') {
$url = substr($url, 1);
}
$this->url = $url;
if ($parseEnvironment) {
$this->_processPost();
$this->_processGet();
$this->_processFiles();
}
$this->here = $this->base . '/' . $this->url;
}
protected function _readInput() {
return $this->input;
}
}
class CakeRequestTest extends CakeTestCase {
/**
@ -41,7 +69,7 @@ class CakeRequestTest extends CakeTestCase {
}
/**
* tearDown-
* tearDown
*
* @return void
*/
@ -204,6 +232,79 @@ class CakeRequestTest extends CakeTestCase {
$this->assertEquals($_POST, $request->data);
}
/**
* test parsing PUT data into the object.
*
* @return void
*/
public function testPutParsing() {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$data = array('data' => array(
'Article' => array('title')
));
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('data[Article][]=title'));
$request->reConstruct();
$this->assertEquals($data['data'], $request->data);
$data = array('one' => 1, 'two' => 'three');
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('one=1&two=three'));
$request->reConstruct();
$this->assertEquals($data, $request->data);
$data = array(
'data' => array(
'Article' => array('title' => 'Testing'),
),
'action' => 'update'
);
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('data[Article][title]=Testing&action=update'));
$request->reConstruct();
$expected = array(
'Article' => array('title' => 'Testing'),
'action' => 'update'
);
$this->assertEquals($expected, $request->data);
$data = array('data' => array(
'Article' => array('title'),
'Tag' => array('Tag' => array(1, 2))
));
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2'));
$request->reConstruct();
$this->assertEquals($data['data'], $request->data);
$data = array('data' => array(
'Article' => array('title' => 'some title'),
'Tag' => array('Tag' => array(1, 2))
));
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2'));
$request->reConstruct();
$this->assertEquals($data['data'], $request->data);
$data = array(
'a' => array(1, 2),
'b' => array(1, 2)
);
$request = $this->getMock('TestCakeRequest', array('_readInput'));
$request->expects($this->at(0))->method('_readInput')
->will($this->returnValue('a[]=1&a[]=2&b[]=1&b[]=2'));
$request->reConstruct();
$this->assertEquals($data, $request->data);
}
/**
* test parsing of FILES array
*