]> source.dussan.org Git - nextcloud-server.git/commitdiff
Check if accessor matched request method.
authorThomas Tanghus <thomas@tanghus.net>
Fri, 27 Sep 2013 12:36:19 +0000 (14:36 +0200)
committerThomas Tanghus <thomas@tanghus.net>
Tue, 1 Oct 2013 18:13:13 +0000 (20:13 +0200)
It's easier to find errors in the code if an exception is thrown.

lib/private/appframework/http/request.php
tests/lib/appframework/http/RequestTest.php

index 34605acdfead8b7073f19702fd6965799e1b776c..5a86066b48b640d1eeac43672070c465b38df884 100644 (file)
@@ -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':
index 0371c870cf292682b70a35a35fac70d3020f2468..ff4a8357f06d9a8ede3ecd16b5a7bf6a896d8dbe 100644 (file)
@@ -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']);
+       }
+
 }