Browse Source

Better simulation of GET requests whenusing http method override

Jose Lorenzo Rodriguez 10 years ago
parent
commit
d08be3bba4
2 changed files with 36 additions and 0 deletions
  1. 10 0
      src/Network/Request.php
  2. 26 0
      tests/TestCase/Network/RequestTest.php

+ 10 - 0
src/Network/Request.php

@@ -275,6 +275,8 @@ class Request implements ArrayAccess
     protected function _processPost($data)
     {
         $method = $this->env('REQUEST_METHOD');
+        $override = false;
+
         if (in_array($method, ['PUT', 'DELETE', 'PATCH']) &&
             strpos($this->contentType(), 'application/x-www-form-urlencoded') === 0
         ) {
@@ -283,12 +285,20 @@ class Request implements ArrayAccess
         }
         if ($this->env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
             $data['_method'] = $this->env('HTTP_X_HTTP_METHOD_OVERRIDE');
+            $override = true;
         }
         $this->_environment['ORIGINAL_REQUEST_METHOD'] = $method;
         if (isset($data['_method'])) {
             $this->_environment['REQUEST_METHOD'] = $data['_method'];
             unset($data['_method']);
+            $override = true;
+        }
+
+        $method = $this->_environment['REQUEST_METHOD'];
+        if ($override && !in_array($method, ['PUT', 'POST', 'DELETE', 'PATCH'])) {
+            $data = [];
         }
+
         return $data;
     }
 

+ 26 - 0
tests/TestCase/Network/RequestTest.php

@@ -2463,6 +2463,32 @@ XML;
     }
 
     /**
+     * Tests that overriding the method to GET will clean all request
+     * data, to better simulate a GET request.
+     *
+     * @return void
+     */
+    public function testMethodOverrideEmptyData()
+    {
+        $post = ['_method' => 'GET', 'foo' => 'bar'];
+        $request = new Request([
+            'post' => $post,
+            'environment' => ['REQUEST_METHOD' => 'POST']
+        ]);
+        $this->assertEmpty($request->data);
+
+        $post = ['_method' => 'GET', 'foo' => 'bar'];
+        $request = new Request([
+            'post' => ['foo' => 'bar'],
+            'environment' => [
+                'REQUEST_METHOD' => 'POST',
+                'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET'
+            ]
+        ]);
+        $this->assertEmpty($request->data);
+    }
+
+    /**
      * loadEnvironment method
      *
      * @param array $env