Skip to content

Commit

Permalink
Better method overriding emulation for GET
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 19, 2016
1 parent b4960c7 commit bd53ef0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Cake/Network/CakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ protected function _processPost() {
if (ini_get('magic_quotes_gpc') === '1') {
$this->data = stripslashes_deep($this->data);
}

$override = false;
if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
$this->data['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE');
$override = true;
}

$isArray = is_array($this->data);
if ($isArray && isset($this->data['_method'])) {
if (!empty($_SERVER)) {
Expand All @@ -184,7 +188,13 @@ protected function _processPost() {
$_ENV['REQUEST_METHOD'] = $this->data['_method'];
}
unset($this->data['_method']);
$override = true;
}

if ($override) {
$this->data = array();
}

if ($isArray && isset($this->data['data'])) {
$data = $this->data['data'];
if (count($this->data) <= 1) {
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2444,6 +2444,25 @@ public function testAllowMethodException() {
$request->allowMethod('POST');
}

/**
* Tests that overriding the method to GET will clean all request
* data, to better simulate a GET request.
*
* @return void
*/
public function testMethodOverrideEmptyData()
{
$_POST= array('_method' => 'GET', 'foo' => 'bar');
$_SERVER['REQUEST_METHOD'] = 'PUT';
$request = new CakeRequest('/posts/edit/1');
$this->assertEmpty($request->data);

$_POST= array('foo' => 'bar');
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'GET';
$request = new CakeRequest('/posts/edit/1');
$this->assertEmpty($request->data);
}

/**
* loadEnvironment method
*
Expand Down

0 comments on commit bd53ef0

Please sign in to comment.