Skip to content

Commit

Permalink
Better simulation of GET requests whenusing http method override
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 19, 2016
1 parent 507853a commit d08be3b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Network/Request.php
Expand Up @@ -275,6 +275,8 @@ protected function _setConfig($config)
protected function _processPost($data)
{
$method = $this->env('REQUEST_METHOD');
$override = false;

if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
strpos($this->contentType(), 'application/x-www-form-urlencoded') === 0
) {
Expand All @@ -283,12 +285,20 @@ protected function _processPost($data)
}
if ($this->env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
$data['_method'] = $this->env('HTTP_X_HTTP_METHOD_OVERRIDE');
$override = true;
}
$this->_environment['ORIGINAL_REQUEST_METHOD'] = $method;
if (isset($data['_method'])) {
$this->_environment['REQUEST_METHOD'] = $data['_method'];
unset($data['_method']);
$override = true;
}

$method = $this->_environment['REQUEST_METHOD'];
if ($override && !in_array($method, ['PUT', 'POST', 'DELETE', 'PATCH'])) {
$data = [];
}

return $data;
}

Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -2462,6 +2462,32 @@ public function testContentType()
$this->assertEquals('application/xml', $request->contentType(), 'prefer non http header.');
}

/**
* Tests that overriding the method to GET will clean all request
* data, to better simulate a GET request.
*
* @return void
*/
public function testMethodOverrideEmptyData()
{
$post = ['_method' => 'GET', 'foo' => 'bar'];
$request = new Request([
'post' => $post,
'environment' => ['REQUEST_METHOD' => 'POST']
]);
$this->assertEmpty($request->data);

$post = ['_method' => 'GET', 'foo' => 'bar'];
$request = new Request([
'post' => ['foo' => 'bar'],
'environment' => [
'REQUEST_METHOD' => 'POST',
'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET'
]
]);
$this->assertEmpty($request->data);
}

/**
* loadEnvironment method
*
Expand Down

0 comments on commit d08be3b

Please sign in to comment.