1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Codeception\Test\Unit; |
4
|
|
|
use veejay\jsonrpc\tests\Api; |
5
|
|
|
use veejay\jsonrpc\tests\ProtectedHelper; |
6
|
|
|
use veejay\jsonrpc\tests\Server; |
7
|
|
|
|
8
|
|
|
class ServerTest extends Unit |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Json input and output. |
12
|
|
|
* @var array |
13
|
|
|
* @see https://www.jsonrpc.org/specification |
14
|
|
|
*/ |
15
|
|
|
protected $data = [ |
16
|
|
|
// Standard examples from specification |
17
|
|
|
'{"jsonrpc":"2.0","method":"withParams","params":[42,23],"id":1}' => '{"jsonrpc":"2.0","result":"withParams: 42, 23","id":1}', |
18
|
|
|
'{"jsonrpc":"2.0","method":"withParams","params":{"one":1,"two":2},"id":3}' => '{"jsonrpc":"2.0","result":"withParams: 1, 2","id":3}', |
19
|
|
|
'{"jsonrpc":"2.0","method":"withParams","params":[1,2,3,4,5]}' => '', |
20
|
|
|
'{"jsonrpc":"2.0","method":"notExists","id":"1"}' => '{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":"1"}', |
21
|
|
|
'{"jsonrpc":"2.0","method":"withoutParams","params":"bar","baz]' => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
22
|
|
|
'{"jsonrpc":"2.0","method":1,"params":"withoutParams"}' => '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}', |
23
|
|
|
'{"jsonrpc":"2.0","method":"withoutParams","params":[1,2,4],"id":"1"},{"jsonrpc":"2.0","method"' => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
24
|
|
|
'[]' => '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}', |
25
|
|
|
'[1]' => '[{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}]', |
26
|
|
|
'[1,2]' => '[{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null},{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}]', |
27
|
|
|
'[{"jsonrpc":"2.0","method":"withParams","params":[1,2],"id":"1"},{"jsonrpc":"2.0","method":"withParams","params":{"i":1}},{"foo":"boo"}]' => '[{"jsonrpc":"2.0","result":"withParams: 1, 2","id":"1"},{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":null}]', |
28
|
|
|
'[{"jsonrpc":"2.0","method":"withParams","params":[1,2]},{"jsonrpc":"2.0","method":"withoutParams"}]' => '', |
29
|
|
|
|
30
|
|
|
// Custom examples |
31
|
|
|
1 => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
32
|
|
|
true => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
33
|
|
|
false => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
34
|
|
|
'null' => '{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}', |
35
|
|
|
'[{"jsonrpc":"2.0","method":"withParams","params":[42, 23],"id":1}]' => '[{"jsonrpc":"2.0","result":"withParams: 42, 23","id":1}]', |
36
|
|
|
'{"jsonrpc":"1.0","method":"withoutParams","id":1}' => '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":1}', |
37
|
|
|
'{"method":"withoutParams","id":1}' => '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":1}', |
38
|
|
|
'{"jsonrpc":"2.0","method":["withoutParams"],"params":[42, 23],"id":1}' => '{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":1}', |
39
|
|
|
'{"jsonrpc":"2.0","method":"withParams","params":1,"id":1}' => '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params"},"id":1}', |
40
|
|
|
'{"jsonrpc":"2.0","method":"errorMethod","id":1}' => '{"jsonrpc":"2.0","error":{"code":-32603,"message":"Internal error"},"id":1}', |
41
|
|
|
'[{"jsonrpc":"2.0","method":"withoutParams","id":1},{"jsonrpc":"2.0","method":"withoutParams","id":1}]' => '{"jsonrpc":"2.0","error":{"code":-32001,"message":"Duplicated ID"},"id":null}', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
public function testRun() |
45
|
|
|
{ |
46
|
|
|
foreach ($this->data as $input => $output) { |
47
|
|
|
$server = new Server(new Api); |
48
|
|
|
$server->_input = $input; |
49
|
|
|
$result = $server->run(); |
50
|
|
|
$this->assertEquals($output, $result); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testLimit() |
55
|
|
|
{ |
56
|
|
|
$server = new Server(new Api, 1); |
57
|
|
|
ProtectedHelper::setProperty($server, 'limit', 2); |
58
|
|
|
$server->_input = '[{"jsonrpc":"2.0","method":"withoutParams","id":1},{"jsonrpc":"2.0","method":"withoutParams"},{"jsonrpc":"2.0","method":"withoutParams"}]'; |
59
|
|
|
$output = $server->run(); |
60
|
|
|
$this->assertEquals('{"jsonrpc":"2.0","error":{"code":-32000,"message":"Limit exceeded"},"id":null}', $output); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|