summaryrefslogtreecommitdiff
path: root/libs/jsonrpc/tests/Response
diff options
context:
space:
mode:
Diffstat (limited to 'libs/jsonrpc/tests/Response')
-rw-r--r--libs/jsonrpc/tests/Response/HeaderMockTest.php25
-rw-r--r--libs/jsonrpc/tests/Response/ResponseBuilderTest.php151
-rw-r--r--libs/jsonrpc/tests/Response/ResponseParserTest.php100
3 files changed, 276 insertions, 0 deletions
diff --git a/libs/jsonrpc/tests/Response/HeaderMockTest.php b/libs/jsonrpc/tests/Response/HeaderMockTest.php
new file mode 100644
index 00000000..cbeb7388
--- /dev/null
+++ b/libs/jsonrpc/tests/Response/HeaderMockTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace JsonRPC\Response;
+
+use PHPUnit_Framework_TestCase;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+function header($value)
+{
+ HeaderMockTest::$functions->header($value);
+}
+
+abstract class HeaderMockTest extends PHPUnit_Framework_TestCase
+{
+ public static $functions;
+
+ public function setUp()
+ {
+ self::$functions = $this
+ ->getMockBuilder('stdClass')
+ ->setMethods(array('header'))
+ ->getMock();
+ }
+}
diff --git a/libs/jsonrpc/tests/Response/ResponseBuilderTest.php b/libs/jsonrpc/tests/Response/ResponseBuilderTest.php
new file mode 100644
index 00000000..e2dcb2a0
--- /dev/null
+++ b/libs/jsonrpc/tests/Response/ResponseBuilderTest.php
@@ -0,0 +1,151 @@
+<?php
+
+use JsonRPC\Exception\AccessDeniedException;
+use JsonRPC\Exception\AuthenticationFailureException;
+use JsonRPC\Exception\InvalidJsonFormatException;
+use JsonRPC\Exception\InvalidJsonRpcFormatException;
+use JsonRPC\Exception\ResponseEncodingFailureException;
+use JsonRPC\Exception\ResponseException;
+use JsonRPC\Response\ResponseBuilder;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class ResponseBuilderTest extends PHPUnit_Framework_TestCase
+{
+ public function testBuildResponse()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","result":"test","id":123}', $response);
+ }
+
+ public function testBuildResponseWithError()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withError(42, 'Test', 'More info')
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":42,"message":"Test","data":"More info"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new Exception('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":0,"message":"Test"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithResponseException()
+ {
+ $exception = new ResponseException('Error', 42);
+ $exception->setData('Data');
+
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException($exception)
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":42,"message":"Error","data":"Data"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithAccessDeniedException()
+ {
+ $responseBuilder = ResponseBuilder::create();
+ $response = $responseBuilder
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new AccessDeniedException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":403,"message":"Forbidden"},"id":123}', $response);
+ $this->assertEquals('HTTP/1.0 403 Forbidden', $responseBuilder->getStatus());
+
+ $this->assertEquals(
+ array('Content-Type' => 'application/json'),
+ $responseBuilder->getHeaders()
+ );
+ }
+
+ public function testBuildResponseWithAuthenticationFailureException()
+ {
+ $responseBuilder = ResponseBuilder::create();
+ $response = $responseBuilder
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new AuthenticationFailureException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":401,"message":"Unauthorized"},"id":123}', $response);
+ $this->assertEquals('HTTP/1.0 401 Unauthorized', $responseBuilder->getStatus());
+
+ $this->assertEquals(
+ array('Content-Type' => 'application/json', 'WWW-Authenticate' => 'Basic realm="JsonRPC"'),
+ $responseBuilder->getHeaders()
+ );
+ }
+
+ public function testBuildResponseWithResponseEncodingFailureException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new ResponseEncodingFailureException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error","data":"Test"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithInvalidArgumentException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new InvalidArgumentException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params","data":"Test"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithBadFunctionCallException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new BadFunctionCallException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":123}', $response);
+ }
+
+ public function testBuildResponseWithInvalidJsonRpcFormatException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new InvalidJsonRpcFormatException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":null}', $response);
+ }
+
+ public function testBuildResponseWithInvalidJsonFormatException()
+ {
+ $response = ResponseBuilder::create()
+ ->withId(123)
+ ->withResult('test')
+ ->withException(new InvalidJsonFormatException('Test'))
+ ->build();
+
+ $this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', $response);
+ }
+}
diff --git a/libs/jsonrpc/tests/Response/ResponseParserTest.php b/libs/jsonrpc/tests/Response/ResponseParserTest.php
new file mode 100644
index 00000000..f195014f
--- /dev/null
+++ b/libs/jsonrpc/tests/Response/ResponseParserTest.php
@@ -0,0 +1,100 @@
+<?php
+
+use JsonRPC\Response\ResponseParser;
+
+require_once __DIR__.'/../../../../vendor/autoload.php';
+
+class ResponseParserTest extends PHPUnit_Framework_TestCase
+{
+ public function testSingleRequest()
+ {
+ $result = ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "result": "foobar", "id": "1"}', true))
+ ->parse();
+
+ $this->assertEquals('foobar', $result);
+ }
+
+ public function testWithBadJsonFormat()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonFormatException');
+
+ ResponseParser::create()
+ ->withPayload('foobar')
+ ->parse();
+ }
+
+ public function testWithBadProcedure()
+ {
+ $this->setExpectedException('BadFunctionCallException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}', true))
+ ->parse();
+ }
+
+ public function testWithInvalidArgs()
+ {
+ $this->setExpectedException('InvalidArgumentException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": "1"}', true))
+ ->parse();
+ }
+
+ public function testWithInvalidRequest()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonRpcFormatException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}', true))
+ ->parse();
+ }
+
+ public function testWithParseError()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\InvalidJsonFormatException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}', true))
+ ->parse();
+ }
+
+ public function testWithOtherError()
+ {
+ $this->setExpectedException('\JsonRPC\Exception\ResponseException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode('{"jsonrpc": "2.0", "error": {"code": 42, "message": "Something", "data": "foobar"}, "id": null}', true))
+ ->parse();
+ }
+
+ public function testBatch()
+ {
+ $payload = '[
+ {"jsonrpc": "2.0", "result": 7, "id": "1"},
+ {"jsonrpc": "2.0", "result": 19, "id": "2"}
+ ]';
+
+ $result = ResponseParser::create()
+ ->withPayload(json_decode($payload, true))
+ ->parse();
+
+ $this->assertEquals(array(7, 19), $result);
+ }
+
+ public function testBatchWithError()
+ {
+ $payload = '[
+ {"jsonrpc": "2.0", "result": 7, "id": "1"},
+ {"jsonrpc": "2.0", "result": 19, "id": "2"},
+ {"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": "1"}
+ ]';
+
+ $this->setExpectedException('InvalidArgumentException');
+
+ ResponseParser::create()
+ ->withPayload(json_decode($payload, true))
+ ->parse();
+ }
+}