Skip to content

Commit 9a25b08

Browse files
authored
Merge pull request #10 from DawidMazurek/jsonrpcserver-JsonRpcInput
Handle parse error correctly, replace JsonRpcServer param by JsonRpcInput
2 parents f8410f3 + f18f40d commit 9a25b08

File tree

8 files changed

+88
-12
lines changed

8 files changed

+88
-12
lines changed

examples/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
new JsonRpcRequestBuilder(new JsonSerializer())
3232
);
3333

34-
$response = $server->run($input->getRequest());
34+
$response = $server->run($input);
3535

3636
echo json_encode($response->serialize());

src/handler/JsonRpcRequestHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace DawidMazurek\JsonRpc\handler;
66

7+
use DawidMazurek\JsonRpc\exception\JsonRpcException;
78
use DawidMazurek\JsonRpc\request\JsonRpcRequest;
89
use DawidMazurek\JsonRpc\response\JsonRpcResponse;
910

1011
interface JsonRpcRequestHandler
1112
{
1213
public function registerForMethod(string $method, callable $callback);
1314
public function handle(JsonRpcRequest $request): JsonRpcResponse;
15+
public function handleError(JsonRpcException $exception): JsonRpcResponse;
1416
}

src/handler/MethodCallableHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public function handle(JsonRpcRequest $request): JsonRpcResponse
3636
}
3737
}
3838

39+
public function handleError(JsonRpcException $exception): JsonRpcResponse
40+
{
41+
return new FailedResponse(
42+
"2.0",
43+
$exception->getCode(),
44+
$exception->getMessage()
45+
);
46+
}
47+
3948
private function createErrorResponse(JsonRpcRequest $request, JsonRpcException $exception): JsonRpcResponse
4049
{
4150
if ($request instanceof Request) {

src/response/FailedResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FailedResponse implements JsonRpcResponse
1111
private $message;
1212
private $requestId;
1313

14-
public function __construct(string $apiVersion, int $errorCode, string $message, int $requestId)
14+
public function __construct(string $apiVersion, int $errorCode, string $message, int $requestId = null)
1515
{
1616
$this->apiVersion = $apiVersion;
1717
$this->errorCode = $errorCode;

src/server/JsonRpcServer.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace DawidMazurek\JsonRpc\server;
66

7+
use DawidMazurek\JsonRpc\exception\JsonRpcException;
78
use DawidMazurek\JsonRpc\handler\JsonRpcRequestHandler;
8-
use DawidMazurek\JsonRpc\request\JsonRpcRequestAggregate;
9+
use DawidMazurek\JsonRpc\io\JsonRpcInput;
910
use DawidMazurek\JsonRpc\response\JsonRpcResponseAggregate;
1011

1112
class JsonRpcServer
@@ -17,13 +18,19 @@ public function __construct(JsonRpcRequestHandler $handler)
1718
$this->handler = $handler;
1819
}
1920

20-
public function run(JsonRpcRequestAggregate $requests): JsonRpcResponseAggregate
21+
public function run(JsonRpcInput $input): JsonRpcResponseAggregate
2122
{
22-
$responseAggregate = new JsonRpcResponseAggregate();
23-
24-
foreach ($requests->getAll() as $request) {
23+
try {
24+
$responseAggregate = new JsonRpcResponseAggregate();
25+
26+
foreach ($input->getRequest()->getAll() as $request) {
27+
$responseAggregate->addResponse(
28+
$this->handler->handle($request)
29+
);
30+
}
31+
} catch (JsonRpcException $exception) {
2532
$responseAggregate->addResponse(
26-
$this->handler->handle($request)
33+
$this->handler->handleError($exception)
2734
);
2835
}
2936

tests/integration/JsonRpcServerIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ private function runWithCustomPayload(string $json): JsonRpcResponseAggregate
7676
new JsonRpcRequestBuilder(new JsonSerializer())
7777
);
7878

79-
return $server->run($input->getRequest());
79+
return $server->run($input);
8080
}
8181
}

tests/unit/handler/MethodCallableHandlerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DawidMazurek\JsonRpc\handler;
66

7+
use DawidMazurek\JsonRpc\exception\ParseError;
78
use DawidMazurek\JsonRpc\request\JsonRpcRequest;
89
use DawidMazurek\JsonRpc\request\Notification;
910
use DawidMazurek\JsonRpc\request\Request;
@@ -97,4 +98,16 @@ public function returnsErrorResponseForNotificationRequest()
9798
$response = $handler->handle($request);
9899
$this->assertInstanceOf(NotificationResponse::class, $response);
99100
}
101+
102+
/**
103+
* @test
104+
*/
105+
public function returnsFailedResponseWhenHandlingError()
106+
{
107+
$exception = new ParseError();
108+
109+
$handler = new MethodCallableHandler();
110+
$response = $handler->handleError($exception);
111+
$this->assertInstanceOf(FailedResponse::class, $response);
112+
}
100113
}

tests/unit/server/JsonRpcServerTest.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
namespace DawidMazurek\JsonRpc\server;
66

7+
use DawidMazurek\JsonRpc\exception\ParseError;
78
use DawidMazurek\JsonRpc\handler\JsonRpcRequestHandler;
9+
use DawidMazurek\JsonRpc\io\JsonRpcInput;
10+
use DawidMazurek\JsonRpc\request\JsonRpcRequest;
811
use DawidMazurek\JsonRpc\request\JsonRpcRequestAggregate;
912
use DawidMazurek\JsonRpc\request\Request;
13+
use DawidMazurek\JsonRpc\response\FailedResponse;
14+
use DawidMazurek\JsonRpc\response\JsonRpcResponse;
1015
use PHPUnit\Framework\TestCase;
1116

1217
class JsonRpcServerTest extends TestCase
@@ -42,8 +47,13 @@ public function handlesRequest()
4247
1
4348
));
4449

50+
$input = $this->createMock(JsonRpcInput::class);
51+
$input->expects($this->once())
52+
->method('getRequest')
53+
->willReturn($this->requests);
54+
4555
$server = new JsonRpcServer($this->requestHandler);
46-
$server->run($this->requests);
56+
$server->run($input);
4757
}
4858

4959

@@ -68,18 +78,53 @@ public function handlesMultipleRequests()
6878
2
6979
));
7080

81+
$input = $this->createMock(JsonRpcInput::class);
82+
$input->expects($this->once())
83+
->method('getRequest')
84+
->willReturn($this->requests);
85+
7186
$server = new JsonRpcServer($this->requestHandler);
72-
$server->run($this->requests);
87+
$server->run($input);
7388
}
7489

7590
/**
7691
* @test
7792
*/
7893
public function returnsEmptyResponseForEmptyInput()
7994
{
95+
$input = $this->createMock(JsonRpcInput::class);
96+
$input->expects($this->once())
97+
->method('getRequest')
98+
->willReturn($this->requests);
99+
80100
$server = new JsonRpcServer($this->requestHandler);
81101
$this->assertEmpty(
82-
$server->run($this->requests)->getAll()
102+
$server->run($input)->getAll()
103+
);
104+
}
105+
106+
/**
107+
* @test
108+
*/
109+
public function handlesExceptionWhileHandlingRequest()
110+
{
111+
$input = $this->createMock(JsonRpcInput::class);
112+
$input->expects($this->once())
113+
->method('getRequest')
114+
->willThrowException(new ParseError());
115+
116+
$server = new JsonRpcServer($this->requestHandler);
117+
118+
$response = $server->run($input)->getAll();
119+
120+
$this->assertContainsOnlyInstancesOf(
121+
JsonRpcResponse::class,
122+
$response
123+
);
124+
125+
$this->assertEquals(
126+
1,
127+
count($response)
83128
);
84129
}
85130
}

0 commit comments

Comments
 (0)