-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathServerTest.php
304 lines (220 loc) · 9.01 KB
/
ServerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* Copyright (C) 2015 Datto, Inc.
*
* This file is part of PHP JSON-RPC.
*
* PHP JSON-RPC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 3,
* as published by the Free Software Foundation.
*
* PHP JSON-RPC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PHP JSON-RPC. If not, see <http://www.gnu.org/licenses/>.
*
* @author Spencer Mortensen <[email protected]>
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0
* @copyright 2015 Datto, Inc.
*/
namespace Datto\JsonRpc\Tests;
use PHPUnit\Framework\TestCase;
use Datto\JsonRpc\Server;
class ServerTest extends TestCase
{
public function testArgumentsPositionalA()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "subtract", "params": [3, 2]}';
$output = '{"jsonrpc": "2.0", "id": 1, "result": 1}';
$this->compare($input, $output);
}
public function testArgumentsPositionalB()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "subtract", "params": [2, 3]}';
$output = '{"jsonrpc": "2.0", "id": 1, "result": -1}';
$this->compare($input, $output);
}
public function testArgumentsNamedA()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "subtract", "params": {"minuend": 3, "subtrahend": 2}}';
$output = '{"jsonrpc": "2.0", "id": 1, "result": 1}';
$this->compare($input, $output);
}
public function testArgumentsInvalid()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "subtract", "params": []}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32602, "message": "Invalid params"}}';
$this->compare($input, $output);
}
public function testArgumentsNamedB()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "subtract", "params": {"subtrahend": 2, "minuend": 3}}';
$output = '{"jsonrpc": "2.0", "id": 1, "result": 1}';
$this->compare($input, $output);
}
public function testNotificationArguments()
{
$input = '{"jsonrpc": "2.0", "method": "subtract", "params": [3, 2]}';
$output = 'null';
$this->compare($input, $output);
}
public function testNotification()
{
$input = '{"jsonrpc": "2.0", "method": "subtract"}';
$output = 'null';
$this->compare($input, $output);
}
public function testUndefinedMethod()
{
$input ='{"jsonrpc": "2.0", "id": 1, "method": "undefined"}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32601, "message": "Method not found"}}';
$this->compare($input, $output);
}
public function testInvalidJson()
{
$input = '{"jsonrpc": "2.0", "method": "foobar", "params": "bar", "baz]';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": "Parse error"}}';
$this->compare($input, $output);
}
public function testMissingJsonrpcVersion()
{
$input = '{"method": "subtract", "params": [3, 2], "id": 1}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testInvalidJsonrpcVersion()
{
$input = '{"jsonrpc": "2.1", "id": 1, "method": "subtract", "params": [3, 2]}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testInvalidMethod()
{
$input = '{"jsonrpc": "2.0", "method": 1, "params": [1, 2]}';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testInvalidParams()
{
$input = '{"jsonrpc": "2.0", "method": "foobar", "params": "bar"}';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testImplementationError()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "implementation error"}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32099, "message": "Server error"}}';
$this->compare($input, $output);
}
public function testImplementationErrorData()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "implementation error", "params": ["details"]}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32099, "message": "Server error", "data": "details"}}';
$this->compare($input, $output);
}
public function testImplementationErrorInvalid()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "invalid implementation error"}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": -32099, "message": "Server error"}}';
$this->compare($input, $output);
}
public function testApplicationError()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "application error"}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": 1, "message": "Application error"}}';
$this->compare($input, $output);
}
public function testApplicationErrorData()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "application error", "params": ["details"]}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": 1, "message": "Application error", "data": "details"}}';
$this->compare($input, $output);
}
public function testApplicationErrorInvalid()
{
$input = '{"jsonrpc": "2.0", "id": 1, "method": "invalid application error"}';
$output = '{"jsonrpc": "2.0", "id": 1, "error": {"code": 1, "message": ""}}';
$this->compare($input, $output);
}
public function testInvalidId()
{
$input = '{"jsonrpc": "2.0", "method": "foobar", "params": [1, 2], "id": [1]}';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testBatchInvalidJson()
{
$input = ' [
{"jsonrpc": "2.0", "method": "subtract", "params": [1, 2, 4], "id": "1"},
{"jsonrpc": "2.0", "method"
]';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": "Parse error"}}';
$this->compare($input, $output);
}
public function testBatchEmpty()
{
$input = '[
]';
$output = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}';
$this->compare($input, $output);
}
public function testBatchInvalidElement()
{
$input = '[
1
]';
$output = '[
{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}
]';
$this->compare($input, $output);
}
public function testBatchInvalidElements()
{
$input = '[
1,
2,
3
]';
$output = '[
{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}},
{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}},
{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}}
]';
$this->compare($input, $output);
}
public function testBatch()
{
$input = '[
{"jsonrpc": "2.0", "method": "subtract", "params": [1, -1], "id": "1"},
{"jsonrpc": "2.0", "method": "subtract", "params": [1, -1]},
{"foo": "boo"},
{"jsonrpc": "2.0", "method": "undefined", "params": {"name": "myself"}, "id": "5"}
]';
$output = '[
{"jsonrpc": "2.0", "id": "1", "result": 2},
{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid Request"}},
{"jsonrpc": "2.0", "id": "5", "error": {"code": -32601, "message": "Method not found"}}
]';
$this->compare($input, $output);
}
public function testBatchNotifications()
{
$input = '[
{"jsonrpc": "2.0", "method": "subtract", "params": [4, 2]},
{"jsonrpc": "2.0", "method": "subtract", "params": [3, 7]}
]';
$output = 'null';
$this->compare($input, $output);
}
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->assertSame($expectedOutput, $actualOutput);
}
}