Current Behavior
When performing a non-JSON request with the integration testing package, $request->getParsedBody() is null whereas it's [] (empty array) when used via the web. 500 errors are thrown from Flarum code because Flarum itself cannot handle null values in some contexts.
Steps to Reproduce
Write integration test:
<?php
namespace Kilowhat\Audit\Tests\integration;
use Carbon\Carbon;
class NullParsedBodyTest extends \Flarum\Testing\integration\TestCase
{
public function setUp(): void
{
parent::setUp();
$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => 'A', 'created_at' => Carbon::now(), 'user_id' => 1],
],
]);
}
public function test()
{
$response = $this->send($this->request('DELETE', '/api/discussions/1', [
'authenticatedAs' => 1,
]));
$this->assertEquals(204, $response->getStatusCode());
}
}
Output: Failed asserting that 500 matches expected 204.
Expected Behavior
It should be possible to make a DELETE request to /api/discussions/<id> without specifying a content type in the integration test, just like it's possible to do from the web.
In the testing package we build the request from scratch so we never default to $_POST for parsedBody.
A workaround for the test package is to provide 'json' => [], in the request options, this forces Flarum to parse the body as JSON and default to an empty array.
Bug Report
Current Behavior When performing a non-JSON request with the integration testing package,
$request->getParsedBody()
isnull
whereas it's[]
(empty array) when used via the web. 500 errors are thrown from Flarum code because Flarum itself cannot handlenull
values in some contexts.Steps to Reproduce Write integration test:
Output:
Failed asserting that 500 matches expected 204.
Error log:
Expected Behavior It should be possible to make a DELETE request to
/api/discussions/<id>
without specifying a content type in the integration test, just like it's possible to do from the web.Environment
Possible Solution More parameters should be passed to the
ServerRequest
constructor in https://github.com/flarum/testing/blob/main/src/integration/TestCase.php#L267 to provide a default$parsedBody
value of[]
instead ofnull
.I think it would also make sense to either allow
null
in https://github.com/flarum/core/blob/master/src/Discussion/Command/DeleteDiscussion.php#L44 or add?? []
in https://github.com/flarum/core/blob/master/src/Api/Controller/DeleteDiscussionController.php#L43 to make sure anull
parsed body doesn't crash the request with a 500 error, since an extension could also alter theparsedBody
attribute. The same probably also applies to otherDELETE
endpoints where we pass theparsedBody
value directly to anarray
type-hint.Same issue affects https://github.com/flarum/flags/blob/master/src/Api/Controller/DeleteFlagsController.php#L40
Additional Context
We default to an empty array in https://github.com/flarum/core/blob/master/src/Http/Middleware/ParseJsonBody.php but the reason an empty array is also present for non-JSON requests is because inside of Lamina's
ServerRequestFactory
class the default value forparsedBody
is$body ?: $_POST
(line 74).In the testing package we build the request from scratch so we never default to
$_POST
forparsedBody
.A workaround for the test package is to provide
'json' => [],
in the request options, this forces Flarum to parse the body as JSON and default to an empty array.