Skip to content

Commit

Permalink
Merge pull request #10 from DawidMazurek/jsonrpcserver-JsonRpcInput
Browse files Browse the repository at this point in the history
Handle parse error correctly, replace JsonRpcServer param by JsonRpcInput
  • Loading branch information
DawidMazurek authored Feb 22, 2018
2 parents f8410f3 + f18f40d commit 9a25b08
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
new JsonRpcRequestBuilder(new JsonSerializer())
);

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

echo json_encode($response->serialize());
2 changes: 2 additions & 0 deletions src/handler/JsonRpcRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace DawidMazurek\JsonRpc\handler;

use DawidMazurek\JsonRpc\exception\JsonRpcException;
use DawidMazurek\JsonRpc\request\JsonRpcRequest;
use DawidMazurek\JsonRpc\response\JsonRpcResponse;

interface JsonRpcRequestHandler
{
public function registerForMethod(string $method, callable $callback);
public function handle(JsonRpcRequest $request): JsonRpcResponse;
public function handleError(JsonRpcException $exception): JsonRpcResponse;
}
9 changes: 9 additions & 0 deletions src/handler/MethodCallableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/response/FailedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 13 additions & 6 deletions src/server/JsonRpcServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/JsonRpcServerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ private function runWithCustomPayload(string $json): JsonRpcResponseAggregate
new JsonRpcRequestBuilder(new JsonSerializer())
);

return $server->run($input->getRequest());
return $server->run($input);
}
}
13 changes: 13 additions & 0 deletions tests/unit/handler/MethodCallableHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
51 changes: 48 additions & 3 deletions tests/unit/server/JsonRpcServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}


Expand All @@ -68,18 +78,53 @@ 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);
}

/**
* @test
*/
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();

$this->assertContainsOnlyInstancesOf(
JsonRpcResponse::class,
$response
);

$this->assertEquals(
1,
count($response)
);
}
}

0 comments on commit 9a25b08

Please sign in to comment.