From 1b57f9eb2b32589b23c409e8b8e63df3574ffbe6 Mon Sep 17 00:00:00 2001 From: KnifeLemon Date: Mon, 3 Feb 2025 23:08:24 +0900 Subject: [PATCH 1/2] Added PUT, PATCH, DELETE methods for data --- flight/net/Request.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/flight/net/Request.php b/flight/net/Request.php index 9cdc64b..02e012f 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -213,6 +213,16 @@ class Request } } + // Check PUT, PATCH, DELETE for data + if ($this->method === 'PUT' || $this->method === 'DELETE' || $this->method === 'PATCH') { + $body = $this->getBody(); + if ($body !== '') { + $data = []; + parse_str($body, $data); + $this->data->setData($data); + } + } + return $this; } From ce088c86dce8ec76bd37747f0e2dd25e41f53ff8 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Wed, 5 Feb 2025 22:58:17 -0700 Subject: [PATCH 2/2] Fix Unit test errors, correct logic --- flight/net/Request.php | 6 ++---- tests/RequestTest.php | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/flight/net/Request.php b/flight/net/Request.php index 02e012f..f88c770 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -211,10 +211,8 @@ class Request $this->data->setData($data); } } - } - - // Check PUT, PATCH, DELETE for data - if ($this->method === 'PUT' || $this->method === 'DELETE' || $this->method === 'PATCH') { + // 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 = []; 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';