diff --git a/README.md b/README.md index b1269c9..f14dbd8 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,6 @@ Json-Rpc is licensed under the MIT License - see the `LICENSE` file for details [composer]: http://getcomposer.org [download]: https://github.com/johnstevenson/json-rpc/downloads [wiki]:https://github.com/johnstevenson/json-rpc/wiki/Home - [client]:https://github.com/johnstevenson/json-rpc/wiki/Client-Usage - [server]:https://github.com/johnstevenson/json-rpc/wiki/Server-Usage - [advanced]:https://github.com/johnstevenson/json-rpc/wiki/Advanced-Functionality + [client]:https://github.com/johnstevenson/json-rpc/wiki/Client-usage + [server]:https://github.com/johnstevenson/json-rpc/wiki/Server-usage + [advanced]:https://github.com/johnstevenson/json-rpc/wiki/Advanced-functionality diff --git a/src/JsonRpc/Client.php b/src/JsonRpc/Client.php index 2ab1f4f..b30d873 100644 --- a/src/JsonRpc/Client.php +++ b/src/JsonRpc/Client.php @@ -59,6 +59,7 @@ class Client private $requests = array(); private $multi = false; private $notifications = 0; + private $headers = array(); const ERR_RPC_RESPONSE = 'Invalid Response'; @@ -78,6 +79,12 @@ public function __construct($url, $transport = null) } + public function setHeader($header) + { + $this->headers[] = $header; + } + + public function setTransport($transport) { $this->transport = $transport; @@ -169,7 +176,7 @@ private function send() try { - if ($res = $this->transport->send('POST', $this->url, $data)) + if ($res = $this->transport->send('POST', $this->url, $data, $this->headers)) { $this->output = $this->transport->output; $res = $this->checkResult(); diff --git a/src/JsonRpc/Server.php b/src/JsonRpc/Server.php index 40897dc..7761856 100644 --- a/src/JsonRpc/Server.php +++ b/src/JsonRpc/Server.php @@ -19,17 +19,13 @@ class Server private $refClass = null; - public function __construct($methodHandler, $otherHandlers = array()) + public function __construct($methodHandler, $transport = null) { ini_set('display_errors', '0'); $this->handler = $methodHandler; - - $other = (array) $otherHandlers; - - $this->transport = !empty($other['transport']) ? $other['transport'] : null; - $this->logger = !empty($other['logger']) ? $other['logger'] : null; + $this->transport = $transport; if (!$this->transport) { @@ -388,7 +384,7 @@ private function logError($message) $params = array( 500, - $msg + $message ); $result = call_user_func_array($callback, $params); @@ -396,13 +392,13 @@ private function logError($message) } else { - error_log($msg); + error_log($message); } } catch (\Exception $e) { - error_log($msg); + error_log($e->__toString()); } } diff --git a/src/JsonRpc/Transport/BasicServer.php b/src/JsonRpc/Transport/BasicServer.php index 4e3b136..db350fd 100644 --- a/src/JsonRpc/Transport/BasicServer.php +++ b/src/JsonRpc/Transport/BasicServer.php @@ -11,9 +11,9 @@ public function receive() } - public function reply($json) + public function reply($data) { - echo $json; + echo $data; exit; } diff --git a/tests/ServerTests/ServerErrorTest.php b/tests/ServerTests/ServerErrorTest.php index fa9c6ad..761913b 100644 --- a/tests/ServerTests/ServerErrorTest.php +++ b/tests/ServerTests/ServerErrorTest.php @@ -35,5 +35,26 @@ public function testNotifyWrongMethod() $this->assertEquals($expects, $json); } + public function testExceptionWithLogger() + { + + $this->methods = 'MethodsException'; + parent::setUp(); + + $logger = new ServerLogger(); + $this->server->setLogger($logger); + + $data = '{"jsonrpc": "2.0", "method": "divide", "params": [42, 0], "id": 1}'; + $expects = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "Internal error"}, "id": 1}'; + $json = $this->getResponseJson($data, $expects); + $this->assertEquals($expects, $json); + + $expects = 500; + $this->assertEquals($expects, $logger->level); + + $expects = 'divide'; + $this->assertContains($expects, $logger->message); + + } } diff --git a/tests/helpers.php b/tests/helpers.php index 344aa71..4f7c72e 100644 --- a/tests/helpers.php +++ b/tests/helpers.php @@ -146,6 +146,37 @@ public static function ping($msg, $user) } +class MethodsException +{ + + public static function divide($dividend, $divisor, $int = false) + { + throw new Exception(__FUNCTION__); + } + + public static function ping($msg, $user) + { + throw new Exception(__FUNCTION__); + } + +} + +class ServerLogger +{ + + public $level; + public $message; + + public function addRecord($level, $message, array $context = array()) + { + $this->level = $level; + $this->message = $message; + } + +} + + + /** * Dummy functions for testing */