From 45ef895c9669940680d6545deae51fe158c51bb4 Mon Sep 17 00:00:00 2001 From: dmazurek Date: Thu, 22 Feb 2018 23:04:45 +0100 Subject: [PATCH 1/2] Handle parse error correctly, replace JsonRpcServer param by JsonRpcInput --- examples/server.php | 2 +- src/handler/JsonRpcRequestHandler.php | 2 + src/handler/MethodCallableHandler.php | 9 ++++ src/response/FailedResponse.php | 2 +- src/server/JsonRpcServer.php | 19 ++++--- .../JsonRpcServerIntegrationTest.php | 2 +- .../handler/MethodCallableHandlerTest.php | 13 +++++ tests/unit/server/JsonRpcServerTest.php | 51 +++++++++++++++++-- 8 files changed, 88 insertions(+), 12 deletions(-) diff --git a/examples/server.php b/examples/server.php index f1a5136..375f8e3 100644 --- a/examples/server.php +++ b/examples/server.php @@ -31,6 +31,6 @@ new JsonRpcRequestBuilder(new JsonSerializer()) ); -$response = $server->run($input->getRequest()); +$response = $server->run($input); echo json_encode($response->serialize()); diff --git a/src/handler/JsonRpcRequestHandler.php b/src/handler/JsonRpcRequestHandler.php index 398a63b..5ee1f6c 100644 --- a/src/handler/JsonRpcRequestHandler.php +++ b/src/handler/JsonRpcRequestHandler.php @@ -4,6 +4,7 @@ namespace DawidMazurek\JsonRpc\handler; +use DawidMazurek\JsonRpc\exception\JsonRpcException; use DawidMazurek\JsonRpc\request\JsonRpcRequest; use DawidMazurek\JsonRpc\response\JsonRpcResponse; @@ -11,4 +12,5 @@ interface JsonRpcRequestHandler { public function registerForMethod(string $method, callable $callback); public function handle(JsonRpcRequest $request): JsonRpcResponse; + public function handleError(JsonRpcException $exception): JsonRpcResponse; } diff --git a/src/handler/MethodCallableHandler.php b/src/handler/MethodCallableHandler.php index 487f5ca..1bbc224 100644 --- a/src/handler/MethodCallableHandler.php +++ b/src/handler/MethodCallableHandler.php @@ -36,6 +36,15 @@ public function handle(JsonRpcRequest $request): JsonRpcResponse } } + public function handleError(JsonRpcException $exception): JsonRpcResponse + { + return new FailedResponse( + "2.0", + $exception->getCode(), + $exception->getMessage() + ); + } + private function createErrorResponse(JsonRpcRequest $request, JsonRpcException $exception): JsonRpcResponse { if ($request instanceof Request) { diff --git a/src/response/FailedResponse.php b/src/response/FailedResponse.php index 2ccfed1..661205c 100644 --- a/src/response/FailedResponse.php +++ b/src/response/FailedResponse.php @@ -11,7 +11,7 @@ class FailedResponse implements JsonRpcResponse private $message; private $requestId; - public function __construct(string $apiVersion, int $errorCode, string $message, int $requestId) + public function __construct(string $apiVersion, int $errorCode, string $message, int $requestId = null) { $this->apiVersion = $apiVersion; $this->errorCode = $errorCode; diff --git a/src/server/JsonRpcServer.php b/src/server/JsonRpcServer.php index 2175965..39929f4 100644 --- a/src/server/JsonRpcServer.php +++ b/src/server/JsonRpcServer.php @@ -4,8 +4,9 @@ namespace DawidMazurek\JsonRpc\server; +use DawidMazurek\JsonRpc\exception\JsonRpcException; use DawidMazurek\JsonRpc\handler\JsonRpcRequestHandler; -use DawidMazurek\JsonRpc\request\JsonRpcRequestAggregate; +use DawidMazurek\JsonRpc\io\JsonRpcInput; use DawidMazurek\JsonRpc\response\JsonRpcResponseAggregate; class JsonRpcServer @@ -17,13 +18,19 @@ public function __construct(JsonRpcRequestHandler $handler) $this->handler = $handler; } - public function run(JsonRpcRequestAggregate $requests): JsonRpcResponseAggregate + public function run(JsonRpcInput $input): JsonRpcResponseAggregate { - $responseAggregate = new JsonRpcResponseAggregate(); - - foreach ($requests->getAll() as $request) { + try { + $responseAggregate = new JsonRpcResponseAggregate(); + + foreach ($input->getRequest()->getAll() as $request) { + $responseAggregate->addResponse( + $this->handler->handle($request) + ); + } + } catch (JsonRpcException $exception) { $responseAggregate->addResponse( - $this->handler->handle($request) + $this->handler->handleError($exception) ); } diff --git a/tests/integration/JsonRpcServerIntegrationTest.php b/tests/integration/JsonRpcServerIntegrationTest.php index dc7f59b..7394c63 100644 --- a/tests/integration/JsonRpcServerIntegrationTest.php +++ b/tests/integration/JsonRpcServerIntegrationTest.php @@ -76,6 +76,6 @@ private function runWithCustomPayload(string $json): JsonRpcResponseAggregate new JsonRpcRequestBuilder(new JsonSerializer()) ); - return $server->run($input->getRequest()); + return $server->run($input); } } diff --git a/tests/unit/handler/MethodCallableHandlerTest.php b/tests/unit/handler/MethodCallableHandlerTest.php index 46f0204..8f0ec2a 100644 --- a/tests/unit/handler/MethodCallableHandlerTest.php +++ b/tests/unit/handler/MethodCallableHandlerTest.php @@ -4,6 +4,7 @@ namespace DawidMazurek\JsonRpc\handler; +use DawidMazurek\JsonRpc\exception\ParseError; use DawidMazurek\JsonRpc\request\JsonRpcRequest; use DawidMazurek\JsonRpc\request\Notification; use DawidMazurek\JsonRpc\request\Request; @@ -97,4 +98,16 @@ public function returnsErrorResponseForNotificationRequest() $response = $handler->handle($request); $this->assertInstanceOf(NotificationResponse::class, $response); } + + /** + * @test + */ + public function returnsFailedResponseWhenHandlingError() + { + $exception = new ParseError(); + + $handler = new MethodCallableHandler(); + $response = $handler->handleError($exception); + $this->assertInstanceOf(FailedResponse::class, $response); + } } diff --git a/tests/unit/server/JsonRpcServerTest.php b/tests/unit/server/JsonRpcServerTest.php index bb593c8..ef50f6f 100644 --- a/tests/unit/server/JsonRpcServerTest.php +++ b/tests/unit/server/JsonRpcServerTest.php @@ -4,9 +4,14 @@ namespace DawidMazurek\JsonRpc\server; +use DawidMazurek\JsonRpc\exception\ParseError; use DawidMazurek\JsonRpc\handler\JsonRpcRequestHandler; +use DawidMazurek\JsonRpc\io\JsonRpcInput; +use DawidMazurek\JsonRpc\request\JsonRpcRequest; use DawidMazurek\JsonRpc\request\JsonRpcRequestAggregate; use DawidMazurek\JsonRpc\request\Request; +use DawidMazurek\JsonRpc\response\FailedResponse; +use DawidMazurek\JsonRpc\response\JsonRpcResponse; use PHPUnit\Framework\TestCase; class JsonRpcServerTest extends TestCase @@ -42,8 +47,13 @@ public function handlesRequest() 1 )); + $input = $this->createMock(JsonRpcInput::class); + $input->expects($this->once()) + ->method('getRequest') + ->willReturn($this->requests); + $server = new JsonRpcServer($this->requestHandler); - $server->run($this->requests); + $server->run($input); } @@ -68,8 +78,13 @@ public function handlesMultipleRequests() 2 )); + $input = $this->createMock(JsonRpcInput::class); + $input->expects($this->once()) + ->method('getRequest') + ->willReturn($this->requests); + $server = new JsonRpcServer($this->requestHandler); - $server->run($this->requests); + $server->run($input); } /** @@ -77,9 +92,39 @@ public function handlesMultipleRequests() */ public function returnsEmptyResponseForEmptyInput() { + $input = $this->createMock(JsonRpcInput::class); + $input->expects($this->once()) + ->method('getRequest') + ->willReturn($this->requests); + $server = new JsonRpcServer($this->requestHandler); $this->assertEmpty( - $server->run($this->requests)->getAll() + $server->run($input)->getAll() + ); + } + + /** + * @test + */ + public function handlesExceptionWhileHandlingRequest() + { + $input = $this->createMock(JsonRpcInput::class); + $input->expects($this->once()) + ->method('getRequest') + ->willThrowException(new ParseError()); + + $server = new JsonRpcServer($this->requestHandler); + + $response = $server->run($input)->getAll(); + //var_dump($response); exit; + $this->assertContainsOnlyInstancesOf( + JsonRpcResponse::class, + $response + ); + + $this->assertEquals( + 1, + count($response) ); } } From f18f40d7a8f6f8cf28ea519ed135a4acec4b433e Mon Sep 17 00:00:00 2001 From: dmazurek Date: Thu, 22 Feb 2018 23:11:34 +0100 Subject: [PATCH 2/2] remove commented code --- tests/unit/server/JsonRpcServerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/server/JsonRpcServerTest.php b/tests/unit/server/JsonRpcServerTest.php index ef50f6f..73ba450 100644 --- a/tests/unit/server/JsonRpcServerTest.php +++ b/tests/unit/server/JsonRpcServerTest.php @@ -116,7 +116,7 @@ public function handlesExceptionWhileHandlingRequest() $server = new JsonRpcServer($this->requestHandler); $response = $server->run($input)->getAll(); - //var_dump($response); exit; + $this->assertContainsOnlyInstancesOf( JsonRpcResponse::class, $response