diff --git a/flight/net/Request.php b/flight/net/Request.php index 9cdc64b..f88c770 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -211,6 +211,14 @@ class Request $this->data->setData($data); } } + // Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data + } else if (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) { + $body = $this->getBody(); + if ($body !== '') { + $data = []; + parse_str($body, $data); + $this->data->setData($data); + } } return $this; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 9b4c234..03acd5a 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -162,7 +162,8 @@ class RequestTest extends TestCase 'url' => '/vagrant/public/flightphp', 'base' => '/vagrant/public', 'query' => new Collection(), - 'type' => '' + 'type' => '', + 'method' => 'GET' ]); $this->assertEquals('/flightphp', $request->url); } @@ -172,7 +173,8 @@ class RequestTest extends TestCase $request = new Request([ 'url' => '', 'base' => '/vagrant/public', - 'type' => '' + 'type' => '', + 'method' => 'GET' ]); $this->assertEquals('/', $request->url); } @@ -183,7 +185,6 @@ class RequestTest extends TestCase $tmpfile = tmpfile(); $stream_path = stream_get_meta_data($tmpfile)['uri']; file_put_contents($stream_path, '{"foo":"bar"}'); - $_SERVER['REQUEST_METHOD'] = 'POST'; $request = new Request([ 'url' => '/something/fancy', 'base' => '/vagrant/public', @@ -191,12 +192,36 @@ class RequestTest extends TestCase 'length' => 13, 'data' => new Collection(), 'query' => new Collection(), - 'stream_path' => $stream_path + 'stream_path' => $stream_path, + 'method' => 'POST' ]); $this->assertEquals([ 'foo' => 'bar' ], $request->data->getData()); $this->assertEquals('{"foo":"bar"}', $request->getBody()); } + public function testInitWithFormBody() + { + // create dummy file to pull request body from + $tmpfile = tmpfile(); + $stream_path = stream_get_meta_data($tmpfile)['uri']; + file_put_contents($stream_path, 'foo=bar&baz=qux'); + $request = new Request([ + 'url' => '/something/fancy', + 'base' => '/vagrant/public', + 'type' => 'application/x-www-form-urlencoded', + 'length' => 15, + 'data' => new Collection(), + 'query' => new Collection(), + 'stream_path' => $stream_path, + 'method' => 'PATCH' + ]); + $this->assertEquals([ + 'foo' => 'bar', + 'baz' => 'qux' + ], $request->data->getData()); + $this->assertEquals('foo=bar&baz=qux', $request->getBody()); + } + public function testGetHeader() { $_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value';