summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2013-09-27 14:36:19 +0200
committerThomas Tanghus <thomas@tanghus.net>2013-09-27 14:36:19 +0200
commitd49e982ac8cb32830275e0086cc8e8e8a11a84b1 (patch)
treea3e43f335ee71facd87f57e922369bcf305f3f79
parent469b309b21b6634b275c485fb09aa5d8d69fe33c (diff)
downloadnextcloud-server-d49e982ac8cb32830275e0086cc8e8e8a11a84b1.tar.gz
nextcloud-server-d49e982ac8cb32830275e0086cc8e8e8a11a84b1.zip
Check if accessor matched request method.
It's easier to find errors in the code if an exception is thrown.
-rw-r--r--lib/appframework/http/request.php3
-rw-r--r--tests/lib/appframework/http/RequestTest.php27
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/appframework/http/request.php b/lib/appframework/http/request.php
index 34605acdfea..5a86066b48b 100644
--- a/lib/appframework/http/request.php
+++ b/lib/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']);
+ }
+
}