From: Thomas Tanghus Date: Fri, 27 Sep 2013 12:36:19 +0000 (+0200) Subject: Check if accessor matched request method. X-Git-Tag: v6.0.0alpha2~93^2~10 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bdad7697ac333ea442b79b5e0e00f90ad5398bc9;p=nextcloud-server.git Check if accessor matched request method. It's easier to find errors in the code if an exception is thrown. --- diff --git a/lib/private/appframework/http/request.php b/lib/private/appframework/http/request.php index 34605acdfea..5a86066b48b 100644 --- a/lib/private/appframework/http/request.php +++ b/lib/private/appframework/http/request.php @@ -152,6 +152,9 @@ class Request implements \ArrayAccess, \Countable, IRequest { switch($name) { case 'get': case 'post': + if($this->method !== strtoupper($name)) { + throw new \BadMethodCallException(sprintf('%s cannot be accessed in a %s request.', $name, $this->method)); + } case 'files': case 'server': case 'env': diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php index 0371c870cf2..ff4a8357f06 100644 --- a/tests/lib/appframework/http/RequestTest.php +++ b/tests/lib/appframework/http/RequestTest.php @@ -14,6 +14,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase { public function testRequestAccessors() { $vars = array( 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), + 'method' => 'GET', ); $request = new Request($vars); @@ -73,4 +74,30 @@ class RequestTest extends \PHPUnit_Framework_TestCase { $request->{'nickname'} = 'Janey'; } + /** + * @expectedException BadMethodCallException + */ + public function testGetTheMethodRight() { + $vars = array( + 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), + 'method' => 'GET', + ); + + $request = new Request($vars); + $result = $request->post; + } + + public function testTheMethodIsRight() { + $vars = array( + 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'), + 'method' => 'GET', + ); + + $request = new Request($vars); + $this->assertEquals('GET', $request->method); + $result = $request->get; + $this->assertEquals('John Q. Public', $result['name']); + $this->assertEquals('Joey', $result['nickname']); + } + }