From 39ba9606f97112e9caa5e363f0809c52d3472a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 1 Nov 2023 11:22:10 +0800 Subject: [PATCH] Optimized code that you can get `request` from `BadRequestHttpException`. (#6247) --- src/Exception/BadRequestHttpException.php | 14 +++++++++++++- src/Server/Request.php | 7 ++++--- tests/ServerRequestTest.php | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Exception/BadRequestHttpException.php b/src/Exception/BadRequestHttpException.php index 5c6a0a2..cbd13c3 100644 --- a/src/Exception/BadRequestHttpException.php +++ b/src/Exception/BadRequestHttpException.php @@ -11,12 +11,24 @@ */ namespace Hyperf\HttpMessage\Exception; +use Psr\Http\Message\ServerRequestInterface; use Throwable; class BadRequestHttpException extends HttpException { - public function __construct($message = null, $code = 0, Throwable $previous = null) + public function __construct($message = null, $code = 0, Throwable $previous = null, protected ?ServerRequestInterface $request = null) { parent::__construct(400, $message, $code, $previous); } + + public function setRequest(?ServerRequestInterface $request): static + { + $this->request = $request; + return $this; + } + + public function getRequest(): ?ServerRequestInterface + { + return $this->request; + } } diff --git a/src/Server/Request.php b/src/Server/Request.php index b618a32..7da6ca2 100644 --- a/src/Server/Request.php +++ b/src/Server/Request.php @@ -18,7 +18,6 @@ use Hyperf\HttpMessage\Upload\UploadedFile; use Hyperf\HttpMessage\Uri\Uri; use InvalidArgumentException; -use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\UriInterface; @@ -464,7 +463,7 @@ public function unsetAttribute(string $name): static return $this; } - protected static function normalizeParsedBody(array $data = [], ?RequestInterface $request = null): array + protected static function normalizeParsedBody(array $data = [], ?ServerRequestInterface $request = null): array { if (! $request) { return $data; @@ -484,7 +483,9 @@ protected static function normalizeParsedBody(array $data = [], ?RequestInterfac $data = $parser->parse($content, $contentType); } } catch (InvalidArgumentException $exception) { - throw new BadRequestHttpException($exception->getMessage()); + throw new BadRequestHttpException($exception->getMessage(), request: $request); + } catch (BadRequestHttpException $exception) { + throw $exception->setRequest($request); } return $data; diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index 7c8e024..e55a276 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -27,7 +27,7 @@ use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; -use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ServerRequestInterface; use ReflectionClass; use Swoole\Http\Request as SwooleRequest; @@ -55,19 +55,19 @@ public function testNormalizeParsedBody() $data = ['id' => 1]; $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn(''); $this->assertSame($data, RequestStub::normalizeParsedBody($data)); $this->assertSame($data, RequestStub::normalizeParsedBody($data, $request)); - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/xml; charset=utf-8'); $request->shouldReceive('getBody')->andReturn(new SwooleStream(Xml::toXml($json))); $this->assertSame($json, RequestStub::normalizeParsedBody($json, $request)); - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8'); $request->shouldReceive('getBody')->andReturn(new SwooleStream(Json::encode($json))); $this->assertSame($json, RequestStub::normalizeParsedBody($data, $request)); @@ -80,7 +80,7 @@ public function testNormalizeParsedBodyException() $this->getContainer(); $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8'); $request->shouldReceive('getBody')->andReturn(new SwooleStream('xxxx')); $this->assertSame([], RequestStub::normalizeParsedBody($json, $request)); @@ -93,7 +93,7 @@ public function testXmlNormalizeParsedBodyException() $this->getContainer(); $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/xml; charset=utf-8'); $request->shouldReceive('getBody')->andReturn(new SwooleStream('xxxx')); $this->assertSame([], RequestStub::normalizeParsedBody($json, $request)); @@ -104,12 +104,12 @@ public function testNormalizeEmptyBody() $this->getContainer(); $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::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 = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8'); $request->shouldReceive('getBody')->andReturn(new SwooleStream('')); $this->assertSame([], RequestStub::normalizeParsedBody([], $request)); @@ -122,7 +122,7 @@ public function testNormalizeParsedBodyInvalidContentType() $data = ['id' => 1]; $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/JSON'); $request->shouldReceive('getBody')->andReturn(new SwooleStream(json_encode($json))); $this->assertSame($json, RequestStub::normalizeParsedBody($data, $request)); @@ -136,7 +136,7 @@ public function testOverrideRequestParser() RequestStub::setParser(new ParserStub()); $json = ['name' => 'Hyperf']; - $request = Mockery::mock(RequestInterface::class); + $request = Mockery::mock(ServerRequestInterface::class); $request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/JSON'); $request->shouldReceive('getBody')->andReturn(new SwooleStream(json_encode($json))); $this->assertSame(['mock' => true], RequestStub::normalizeParsedBody([], $request));