From b509bdb04bb19914c3bee8baafa64059e2f80d43 Mon Sep 17 00:00:00 2001 From: AD7six Date: Wed, 6 Jun 2012 22:58:27 +0200 Subject: [PATCH] Automatically handle PUT requests make the following "just work" curl -X PUT -d foo=bar http://... --- lib/Cake/Network/CakeRequest.php | 9 +- .../Test/Case/Network/CakeRequestTest.php | 103 +++++++++++++++++- 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 62b1cf291..5c80e93d8 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -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); } diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index b6cb118bc..e0ad0b59f 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -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 *