From 50fc5e8792a7870268f2e4c6fddc3cf4c432fe3c Mon Sep 17 00:00:00 2001
From: Thomas Tanghus <thomas@tanghus.net>
Date: Fri, 27 Sep 2013 14:36:19 +0200
Subject: [PATCH] Check if accessor matched request method.

It's easier to find errors in the code if an exception is thrown.
---
 lib/appframework/http/request.php           |  3 +++
 tests/lib/appframework/http/RequestTest.php | 27 +++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/lib/appframework/http/request.php b/lib/appframework/http/request.php
index 34605acdfe..5a86066b48 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 0371c870cf..ff4a8357f0 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']);
+	}
+
 }
-- 
GitLab