Permalink
Browse files

Adding PATCH to list of HTTP methods with bodies.

  • Loading branch information...
1 parent 7f7108f commit 393714b60d199ca2cf96fe37ffbb6d8adc14923c @nateabele nateabele committed Nov 27, 2012
Showing with 27 additions and 15 deletions.
  1. +6 −7 action/Request.php
  2. +21 −8 tests/cases/action/RequestTest.php
View
@@ -184,14 +184,13 @@ protected function _init() {
}
$type = $this->type($this->_config['type'] ?: $this->env('CONTENT_TYPE'));
$this->method = $method = strtoupper($this->_env['REQUEST_METHOD']);
+ $hasBody = in_array($method, array('POST', 'PUT', 'PATCH'));
- if (!$this->data && ($method == 'POST' || $method == 'PUT')) {
- if ($type !== 'html') {
- $this->_stream = $this->_stream ?: fopen('php://input', 'r');
- $media = $this->_classes['media'];
- $this->data = (array) $media::decode($type, stream_get_contents($this->_stream));
- fclose($this->_stream);
- }
+ if (!$this->data && $hasBody && $type !== 'html') {
+ $this->_stream = $this->_stream ?: fopen('php://input', 'r');
+ $media = $this->_classes['media'];
+ $this->data = (array) $media::decode($type, stream_get_contents($this->_stream));
+ fclose($this->_stream);
}
$this->data = Set::merge((array) $this->data, $this->_parseFiles());
}
@@ -889,14 +889,27 @@ public function testRequestTypeFromParams() {
}
public function testAutomaticContentDecoding() {
- $stream = fopen('php://temp', 'r+');
- fwrite($stream, '{ "foo": "bar" }');
- rewind($stream);
- $request = new Request(compact('stream') + array('env' => array(
- 'CONTENT_TYPE' => 'application/json; charset=UTF-8',
- 'REQUEST_METHOD' => 'POST'
- )));
- $this->assertEqual(array('foo' => 'bar'), $request->data);
+ foreach (array('POST', 'PUT', 'PATCH') as $method) {
+ $stream = fopen('php://temp', 'r+');
+ fwrite($stream, '{ "foo": "bar" }');
+ rewind($stream);
+ $request = new Request(compact('stream') + array('env' => array(
+ 'CONTENT_TYPE' => 'application/json; charset=UTF-8',
+ 'REQUEST_METHOD' => $method
+ )));
+ $this->assertEqual(array('foo' => 'bar'), $request->data);
+ }
+
+ foreach (array('GET', 'HEAD', 'OPTIONS', 'DELETE') as $method) {
+ $stream = fopen('php://temp', 'r+');
+ fwrite($stream, '{ "foo": "bar" }');
+ rewind($stream);
+ $request = new Request(compact('stream') + array('env' => array(
+ 'CONTENT_TYPE' => 'application/json; charset=UTF-8',
+ 'REQUEST_METHOD' => $method
+ )));
+ $this->assertFalse($request->data);
+ }
}
public function testRequestTypeFromHeader() {

0 comments on commit 393714b

Please sign in to comment.