Skip to content

Commit c27e35d

Browse files
authored
Fixed 500 caused by empty body. (#1964)
1 parent fae392d commit c27e35d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Server/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ protected static function normalizeParsedBody(array $data = [], ?RequestInterfac
480480

481481
try {
482482
$parser = static::getParser();
483-
if ($parser->has($contentType)) {
484-
$data = $parser->parse($request->getBody()->getContents(), $contentType);
483+
if ($parser->has($contentType) && $content = $request->getBody()->getContents()) {
484+
$data = $parser->parse($content, $contentType);
485485
}
486486
} catch (\InvalidArgumentException $exception) {
487487
throw new BadRequestHttpException($exception->getMessage());

tests/ServerRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ public function testXmlNormalizeParsedBodyException()
9393
$this->assertSame([], RequestStub::normalizeParsedBody($json, $request));
9494
}
9595

96+
public function testNormalizeEmptyBody()
97+
{
98+
$this->getContainer();
99+
100+
$json = ['name' => 'Hyperf'];
101+
$request = Mockery::mock(RequestInterface::class);
102+
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
103+
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
104+
$this->assertSame($json, RequestStub::normalizeParsedBody($json, $request));
105+
106+
$request = Mockery::mock(RequestInterface::class);
107+
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
108+
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
109+
$this->assertSame([], RequestStub::normalizeParsedBody([], $request));
110+
}
111+
96112
public function testNormalizeParsedBodyInvalidContentType()
97113
{
98114
$this->getContainer();

0 commit comments

Comments
 (0)