Fixed 500 caused by empty body. (#1964)

This commit is contained in:
李铭昕 2020-06-23 10:44:43 +08:00 committed by GitHub
parent c67160fe96
commit 33bcb55969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -1,8 +1,12 @@
# v2.0.1 - TBD
## Fixed
- [#1964](https://github.com/hyperf/hyperf/pull/1964) Fixed http status code 500 caused by empty body.
## Optimized
[#1959](https://github.com/hyperf/hyperf/pull/1959) Make ClassLoader easier to be extended.
- [#1959](https://github.com/hyperf/hyperf/pull/1959) Make ClassLoader easier to be extended.
# v2.0.0 - 2020-06-22

View File

@ -480,8 +480,8 @@ class Request extends \Hyperf\HttpMessage\Base\Request implements ServerRequestI
try {
$parser = static::getParser();
if ($parser->has($contentType)) {
$data = $parser->parse($request->getBody()->getContents(), $contentType);
if ($parser->has($contentType) && $content = $request->getBody()->getContents()) {
$data = $parser->parse($content, $contentType);
}
} catch (\InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());

View File

@ -93,6 +93,22 @@ class ServerRequestTest extends TestCase
$this->assertSame([], RequestStub::normalizeParsedBody($json, $request));
}
public function testNormalizeEmptyBody()
{
$this->getContainer();
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(RequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
$this->assertSame($json, RequestStub::normalizeParsedBody($json, $request));
$request = Mockery::mock(RequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
$this->assertSame([], RequestStub::normalizeParsedBody([], $request));
}
public function testNormalizeParsedBodyInvalidContentType()
{
$this->getContainer();