Пример #1
0
 private function call($method)
 {
     return JSON::decode($this->server->handle(Request::createRPC($method))->getContent());
 }
Пример #2
0
    public function testNotificationInBatchRequest()
    {
        $server = new Server();
        $server->expose('Sample', 'PG\\JsonRpc\\tests\\sample\\Sample');
        $body = <<<JSON
[
{"jsonrpc": "2.0", "method": "Sample.divide", "params": [10, 0], "id":1},
{"jsonrpc": "2.0", "method": "Sample.divide", "params": [10, 5], "id":2},
{"jsonrpc": "2.0", "method": "Sample.divide", "params": [10, 5]},
{"jsonrpc": "2.0", "method": "Sample.divide", "params": [10, 0]},
{"jsonrpc": "2.0", "method": "Sample.divide", "params": [10, 5], "id":3}
]
JSON;
        $expected = <<<JSON
[{"id":1,"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params"}},{"jsonrpc":"2.0","result":2,"id":2},{"jsonrpc":"2.0","result":2,"id":3}]
JSON;
        $request = Request::create('', 'POST', array(), array(), array(), array(), $body);
        $response = $server->handle($request);
        $this->assertJSONEquals($response->getContent(), $expected);
    }
Пример #3
0
 public function testExtractNonArrayBody()
 {
     $request = Request::create('', 'POST', array(), array(), array(), array(), '"hallo"');
     $this->setExpectedException('PG\\JsonRpc\\Exception\\InvalidRequest');
     $request->extract();
 }
Пример #4
0
 /**
  * @covers \PG\JsonRpc\Server::handle
  */
 public function testIdIsPreserved()
 {
     $this->server->expose('Sample', 'PG\\JsonRpc\\tests\\sample\\Sample');
     $request = Request::create('', 'POST', array(), array(), array(), array(), '[{"jsonrpc":"2.0","id":1,"method":"Sample.divide","params": [10, 5]},
          {"jsonrpc":"2.0","id":"fourteen","method":"Sample.divide","params": [11, 5]}]');
     $response = $this->server->handle($request);
     $result = JSON::decode($response->getContent());
     $this->assertEquals(array(array('jsonrpc' => '2.0', 'id' => 1, 'result' => 10 / 5), array('jsonrpc' => '2.0', 'id' => 'fourteen', 'result' => 11 / 5)), $result);
 }
Пример #5
0
 /**
  * Handles a given Request object and returns
  * a Response object which can then be sent to the
  * client.
  *
  * @param Request $request
  * @return Response
  */
 public function handle(Request $request)
 {
     $calls = $request->extract();
     $response = new Response();
     $results = array();
     foreach ($calls as $c) {
         try {
             $call = new Call($c, $this->exposed, $this);
             $results[] = $call->execute();
         } catch (AbstractException $e) {
             if (isset($c['id'])) {
                 $e->setId($c['id']);
             }
             $results[] = $e;
         }
     }
     if (!$request->isBatch()) {
         $response->setResult($results[0]);
     } else {
         $response->setResult(new BatchResult($this, $results));
     }
     return $response;
 }