Browse Source

Fix notice error when parsing input data.

Prevent error in CakeRequest from parsing input data in
PUT and DELETE requests.

Fixes #3002

Signed-off-by: mark_story <mark@mark-story.com>
Rodrigo Moyle 13 years ago
parent
commit
e10f6f57a3
2 changed files with 19 additions and 3 deletions
  1. 3 3
      lib/Cake/Network/CakeRequest.php
  2. 16 0
      lib/Cake/Test/Case/Network/CakeRequestTest.php

+ 3 - 3
lib/Cake/Network/CakeRequest.php

@@ -175,7 +175,8 @@ class CakeRequest implements ArrayAccess {
 		if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
 			$this->data['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE');
 		}
-		if (isset($this->data['_method'])) {
+		$isArray = is_array($this->data);
+		if ($isArray && isset($this->data['_method'])) {
 			if (!empty($_SERVER)) {
 				$_SERVER['REQUEST_METHOD'] = $this->data['_method'];
 			} else {
@@ -183,8 +184,7 @@ class CakeRequest implements ArrayAccess {
 			}
 			unset($this->data['_method']);
 		}
-
-		if (isset($this->data['data'])) {
+		if ($isArray && isset($this->data['data'])) {
 			$data = $this->data['data'];
 			if (count($this->data) <= 1) {
 				$this->data = $data;

+ 16 - 0
lib/Cake/Test/Case/Network/CakeRequestTest.php

@@ -301,6 +301,22 @@ class CakeRequestTest extends CakeTestCase {
 	}
 
 /**
+ * test parsing json PUT data into the object.
+ *
+ * @return void
+ */
+	public function testPutParsingJSON() {
+		$_SERVER['REQUEST_METHOD'] = 'PUT';
+		$_SERVER['CONTENT_TYPE'] = 'application/json';
+
+		$request = $this->getMock('TestCakeRequest', array('_readInput'));
+		$request->expects($this->at(0))->method('_readInput')
+			->will($this->returnValue('{Article":["title"]}'));
+		$request->reConstruct();
+		$this->assertEquals('{Article":["title"]}', $request->data);
+	}
+
+/**
  * test parsing of FILES array
  *
  * @return void