Skip to content

Commit

Permalink
Optimized code that you can get request from `BadRequestHttpExcepti…
Browse files Browse the repository at this point in the history
…on`. (#6247)
  • Loading branch information
limingxinleo authored Nov 1, 2023
1 parent 1024754 commit 39ba960
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
14 changes: 13 additions & 1 deletion src/Exception/BadRequestHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
7 changes: 4 additions & 3 deletions src/Server/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions tests/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit 39ba960

Please sign in to comment.