public function testSimpleEvaluatorWithValidNamedArguments() { $evaluator = new Evaluator(); $server = new Server($evaluator); $result = $server->reply('{"jsonrpc": "2.0", "method": "math/subtract", "params": {"a": 3, "b": 2}, "id": 1}'); $this->assertSame('{"jsonrpc":"2.0","id":1,"result":1}', $result); }
public function reply() { if (@$_SERVER['CONTENT_TYPE'] !== self::$CONTENT_TYPE) { self::errorInvalidContentType(); } $message = @file_get_contents('php://input'); if ($message === false) { self::errorInvalidBody(); } $server = new JsonRpc\Server($this->evaluator); $reply = $server->reply($message); if ($reply === null) { self::successNoContent(); } self::successContent($reply); }
/** * Reads the JSON request/payload from the POST request input and * echoes the response (if any). * * The actual processing is done in JsonRpc\Server's reply() method. * * @param string $message JSON-RPC request message string * @return null|string JSON-RPC response message string (or null if the request was a notification) */ public function reply($message) { $this->logger->log($this->level, 'Message received: ' . str_replace("\n", ' ', $message)); $reply = parent::reply($message); if ($reply !== null) { $this->logger->log($this->level, 'Sending reply: ' . str_replace("\n", ' ', $reply)); return $reply; } else { $this->logger->log($this->level, 'Completed notification. No reply.'); return null; } }
<?php require_once '../../vendor/autoload.php'; use Datto\JsonRpc; use Demo\Api\Evaluator\DevicesEvaluator; $evaluator = new DevicesEvaluator(); $server = new JsonRpc\Server($evaluator); header('Content-Type: application/json'); $message = file_get_contents('php://input'); echo $server->reply($message);
<?php use Datto\JsonRpc\Server; use Datto\JsonRpc\Examples\Api; require_once __DIR__ . '/../vendor/autoload.php'; $server = new Server(new Api()); $reply = $server->reply('{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}'); echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":3}
public function testIllegalArgument() { $server = new Server(new Validator\Evaluator(new Simple\Evaluator())); $result = $server->reply('{"jsonrpc": "2.0", "method": "math/subtract", "params": { "a": "INVALID", "b": 2 }, "id": 1}'); $this->assertSame('{"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"Invalid params"}}', $result); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Datto\JsonRpc\Tests\Example\Stateless\Translator; use Datto\JsonRpc\Server; $translator = new Translator(); $server = new Server($translator); $request = '{"jsonrpc":"2.0","id":1,"method":"Math\\/subtract","params":[5,3]}'; $reply = $server->reply($request); echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":2}
private function compare($input, $expectedJsonOutput) { $method = new Translator(); $server = new Server($method); $actualJsonOutput = $server->reply($input); $expectedOutput = json_decode($expectedJsonOutput, true); $actualOutput = json_decode($actualJsonOutput, true); $this->assertEquals($expectedOutput, $actualOutput); }
private function compare($input, $expectedJsonOutput) { $server = new Server(new Api()); $actualJsonOutput = $server->reply($input); $expectedOutput = json_decode($expectedJsonOutput, true); $actualOutput = json_decode($actualJsonOutput, true); $this->assertEquals($expectedOutput, $actualOutput); }
public function testSecondHandlerSucceeds() { $server = new Server(new Evaluator(new Simple\Evaluator(), new Authenticator(array(new NoLuckHandler(), new TokenHandler())))); $result = $server->reply('{"jsonrpc": "2.0", "method": "math/subtract", "params": { "token": "spooky password", "a": 3, "b": 2 }, "id": 1}'); $this->assertSame('{"jsonrpc":"2.0","id":1,"result":1}', $result); }