-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJsonRpcClientTest.java
More file actions
457 lines (373 loc) · 17.1 KB
/
JsonRpcClientTest.java
File metadata and controls
457 lines (373 loc) · 17.1 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
class JsonRpcClientTest {
private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();
// ---- Helpers ----
private record SocketPair(JsonRpcClient client, Socket serverSide,
ServerSocket serverSocket) implements AutoCloseable {
@Override
public void close() throws Exception {
client.close();
serverSide.close();
serverSocket.close();
}
}
private SocketPair createSocketPair() throws Exception {
var serverSocket = new ServerSocket(0);
var clientSocket = new Socket("localhost", serverSocket.getLocalPort());
var serverSide = serverSocket.accept();
var client = JsonRpcClient.fromSocket(clientSocket);
return new SocketPair(client, serverSide, serverSocket);
}
/** Write a raw JSON-RPC message (with Content-Length header) to a stream. */
private void writeRpcMessage(OutputStream out, String json) throws IOException {
byte[] content = json.getBytes(StandardCharsets.UTF_8);
String header = "Content-Length: " + content.length + "\r\n\r\n";
out.write(header.getBytes(StandardCharsets.UTF_8));
out.write(content);
out.flush();
}
/** Read a single JSON-RPC message (Content-Length framed) from a stream. */
private String readRpcMessage(InputStream in) throws IOException {
var headerLine = new StringBuilder();
int contentLength = -1;
boolean lastWasCR = false;
boolean inHeaders = true;
while (inHeaders) {
int b = in.read();
if (b == -1)
throw new IOException("EOF");
if (b == '\r') {
lastWasCR = true;
} else if (b == '\n') {
String line = headerLine.toString();
headerLine.setLength(0);
lastWasCR = false;
if (line.isEmpty()) {
inHeaders = false;
} else if (line.toLowerCase().startsWith("content-length:")) {
contentLength = Integer.parseInt(line.substring(15).trim());
}
} else {
if (lastWasCR) {
headerLine.append('\r');
lastWasCR = false;
}
headerLine.append((char) b);
}
}
byte[] buffer = new byte[contentLength];
int read = 0;
while (read < contentLength) {
int result = in.read(buffer, read, contentLength - read);
if (result == -1)
throw new IOException("EOF");
read += result;
}
return new String(buffer, StandardCharsets.UTF_8);
}
// ---- notify() ----
@Test
void testNotify() throws Exception {
try (var pair = createSocketPair()) {
pair.client.notify("test.method", Map.of("key", "value"));
String msg = readRpcMessage(pair.serverSide.getInputStream());
JsonNode node = MAPPER.readTree(msg);
assertEquals("2.0", node.get("jsonrpc").asText());
assertEquals("test.method", node.get("method").asText());
assertNull(node.get("id"), "Notification should not have an id");
assertEquals("value", node.get("params").get("key").asText());
}
}
// ---- isConnected() ----
@Test
void testIsConnectedWithSocket() throws Exception {
try (var pair = createSocketPair()) {
assertTrue(pair.client.isConnected());
}
}
@Test
void testIsConnectedWithSocketClosed() throws Exception {
var pair = createSocketPair();
pair.client.close();
assertFalse(pair.client.isConnected());
pair.serverSide.close();
pair.serverSocket.close();
}
private static Process startBlockingProcess() throws IOException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
return (isWindows ? new ProcessBuilder("cmd", "/c", "more") : new ProcessBuilder("cat")).start();
}
@Test
void testIsConnectedWithProcess() throws Exception {
Process proc = startBlockingProcess();
try (var client = JsonRpcClient.fromProcess(proc)) {
assertTrue(client.isConnected());
}
}
@Test
void testIsConnectedWithProcessDead() throws Exception {
Process proc = startBlockingProcess();
var client = JsonRpcClient.fromProcess(proc);
proc.destroy();
proc.waitFor(5, TimeUnit.SECONDS);
assertFalse(client.isConnected());
client.close();
}
// ---- getProcess() ----
@Test
void testGetProcessReturnsProcess() throws Exception {
Process proc = startBlockingProcess();
try (var client = JsonRpcClient.fromProcess(proc)) {
assertSame(proc, client.getProcess());
}
}
@Test
void testGetProcessNullForSocket() throws Exception {
try (var pair = createSocketPair()) {
assertNull(pair.client.getProcess());
}
}
// ---- invoke() edge cases ----
@Test
void testInvokeWithVoidPrimitive() throws Exception {
try (var pair = createSocketPair()) {
CompletableFuture<Void> future = pair.client.invoke("test", Map.of(), void.class);
String request = readRpcMessage(pair.serverSide.getInputStream());
long id = MAPPER.readTree(request).get("id").asLong();
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"result\":{\"any\":\"thing\"}}");
assertNull(future.get(5, TimeUnit.SECONDS));
}
}
@Test
void testInvokeWithSendFailure() throws Exception {
var serverSocket = new ServerSocket(0);
var clientSocket = new Socket("localhost", serverSocket.getLocalPort());
var serverSide = serverSocket.accept();
var client = JsonRpcClient.fromSocket(clientSocket);
// Close the client socket so write will fail
clientSocket.close();
Thread.sleep(100);
CompletableFuture<?> future = client.invoke("test", Map.of(), JsonNode.class);
var ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
assertInstanceOf(IOException.class, ex.getCause());
client.close();
serverSide.close();
serverSocket.close();
}
@Test
void testInvokeWithDeserializationError() throws Exception {
try (var pair = createSocketPair()) {
// Integer cannot be deserialized from a JSON object
CompletableFuture<Integer> future = pair.client.invoke("test", Map.of(), Integer.class);
String request = readRpcMessage(pair.serverSide.getInputStream());
long id = MAPPER.readTree(request).get("id").asLong();
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"result\":{\"complex\":\"object\"}}");
var ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
// CompletableFuture unwraps CompletionException, so cause is the
// underlying JsonProcessingException
assertInstanceOf(com.fasterxml.jackson.core.JsonProcessingException.class, ex.getCause());
}
}
// ---- handleMessage: response handling ----
@Test
void testResponseWithUnknownId() throws Exception {
try (var pair = createSocketPair()) {
// Response for an id that has no pending request - silently ignored
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":99999,\"result\":{\"ok\":true}}");
Thread.sleep(200);
// No exception, just silently dropped
}
}
@Test
void testErrorResponseWithoutMessage() throws Exception {
try (var pair = createSocketPair()) {
CompletableFuture<?> future = pair.client.invoke("test", Map.of(), JsonNode.class);
String request = readRpcMessage(pair.serverSide.getInputStream());
long id = MAPPER.readTree(request).get("id").asLong();
// Error with code but no message field
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"error\":{\"code\":-32600}}");
var ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
var rpcEx = assertInstanceOf(JsonRpcException.class, ex.getCause());
assertEquals("Unknown error", rpcEx.getMessage());
assertEquals(-32600, rpcEx.getCode());
}
}
@Test
void testErrorResponseWithoutCode() throws Exception {
try (var pair = createSocketPair()) {
CompletableFuture<?> future = pair.client.invoke("test", Map.of(), JsonNode.class);
String request = readRpcMessage(pair.serverSide.getInputStream());
long id = MAPPER.readTree(request).get("id").asLong();
// Error with message but no code field
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"error\":{\"message\":\"bad request\"}}");
var ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
var rpcEx = assertInstanceOf(JsonRpcException.class, ex.getCause());
assertEquals("bad request", rpcEx.getMessage());
assertEquals(-1, rpcEx.getCode());
}
}
// ---- handleMessage: server method calls ----
@Test
void testNoHandlerForNotification() throws Exception {
try (var pair = createSocketPair()) {
// Notification (no id) for unregistered method -- silently logged
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"method\":\"unknown.method\",\"params\":{}}");
Thread.sleep(200);
}
}
@Test
void testNoHandlerForRequestSendsErrorResponse() throws Exception {
try (var pair = createSocketPair()) {
// Request (with id) for unregistered method -> -32601 Method not found
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":42,\"method\":\"unknown.method\",\"params\":{}}");
String response = readRpcMessage(pair.serverSide.getInputStream());
JsonNode node = MAPPER.readTree(response);
assertEquals("2.0", node.get("jsonrpc").asText());
assertTrue(node.has("error"));
assertEquals(-32601, node.get("error").get("code").asInt());
assertTrue(node.get("error").get("message").asText().contains("Method not found"));
}
}
@Test
void testHandlerThrowsExceptionWithId() throws Exception {
try (var pair = createSocketPair()) {
pair.client.registerMethodHandler("fail.method", (id, params) -> {
throw new RuntimeException("handler error");
});
// Request with id - handler throws -> -32603 Internal error
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":7,\"method\":\"fail.method\",\"params\":{}}");
String response = readRpcMessage(pair.serverSide.getInputStream());
JsonNode node = MAPPER.readTree(response);
assertTrue(node.has("error"));
assertEquals(-32603, node.get("error").get("code").asInt());
assertEquals("handler error", node.get("error").get("message").asText());
}
}
@Test
void testHandlerThrowsExceptionWithoutId() throws Exception {
try (var pair = createSocketPair()) {
pair.client.registerMethodHandler("fail.notify", (id, params) -> {
throw new RuntimeException("notify error");
});
// Notification (no id) - handler throws -> just logged, no error response
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"method\":\"fail.notify\",\"params\":{}}");
Thread.sleep(200);
// Should not crash
}
}
@Test
void testMethodCallWithNullId() throws Exception {
try (var pair = createSocketPair()) {
var received = new AtomicReference<String>();
pair.client.registerMethodHandler("test.null.id", (id, params) -> {
received.set(id);
});
// Explicit null id - should be treated as notification
writeRpcMessage(pair.serverSide.getOutputStream(),
"{\"jsonrpc\":\"2.0\",\"id\":null,\"method\":\"test.null.id\",\"params\":{}}");
Thread.sleep(200);
assertNull(received.get());
}
}
// ---- handleMessage: edge cases ----
@Test
void testInvalidJson() throws Exception {
try (var pair = createSocketPair()) {
writeRpcMessage(pair.serverSide.getOutputStream(), "not valid json {{{");
Thread.sleep(200);
// Should not crash, error is logged
}
}
@Test
void testMessageWithNeitherResponseNorMethod() throws Exception {
try (var pair = createSocketPair()) {
// JSON object with id but no result/error/method - silently ignored
writeRpcMessage(pair.serverSide.getOutputStream(), "{\"jsonrpc\":\"2.0\",\"id\":1}");
Thread.sleep(200);
}
}
// ---- reader: header parsing edge cases ----
@Test
void testReaderWithUnknownHeader() throws Exception {
try (var pair = createSocketPair()) {
var received = new CompletableFuture<JsonNode>();
pair.client.registerMethodHandler("test.header", (id, params) -> {
received.complete(params);
});
// Send a message with an extra header before Content-Length
var out = pair.serverSide.getOutputStream();
String json = "{\"jsonrpc\":\"2.0\",\"method\":\"test.header\",\"params\":{\"ok\":true}}";
byte[] content = json.getBytes(StandardCharsets.UTF_8);
String msg = "X-Custom-Header: value\r\nContent-Length: " + content.length + "\r\n\r\n";
out.write(msg.getBytes(StandardCharsets.UTF_8));
out.write(content);
out.flush();
JsonNode params = received.get(5, TimeUnit.SECONDS);
assertTrue(params.get("ok").asBoolean());
}
}
@Test
void testReaderWithMissingContentLength() throws Exception {
try (var pair = createSocketPair()) {
var received = new CompletableFuture<JsonNode>();
pair.client.registerMethodHandler("test.after", (id, params) -> {
received.complete(params);
});
var out = pair.serverSide.getOutputStream();
// First: send a message with no Content-Length header (just blank line) -
// should skip
out.write("X-Only-Header: no-length\r\n\r\n".getBytes(StandardCharsets.UTF_8));
out.flush();
// Then: send a proper message that should be received
String json = "{\"jsonrpc\":\"2.0\",\"method\":\"test.after\",\"params\":{\"ok\":true}}";
byte[] content = json.getBytes(StandardCharsets.UTF_8);
String proper = "Content-Length: " + content.length + "\r\n\r\n";
out.write(proper.getBytes(StandardCharsets.UTF_8));
out.write(content);
out.flush();
JsonNode params = received.get(5, TimeUnit.SECONDS);
assertTrue(params.get("ok").asBoolean());
}
}
// ---- close() ----
@Test
void testCloseWithPendingRequests() throws Exception {
var pair = createSocketPair();
CompletableFuture<?> future = pair.client.invoke("test", Map.of(), JsonNode.class);
// Read the outgoing request to avoid blocking
readRpcMessage(pair.serverSide.getInputStream());
// Close without responding - should cancel pending request
pair.client.close();
var ex = assertThrows(ExecutionException.class, () -> future.get(5, TimeUnit.SECONDS));
assertInstanceOf(IOException.class, ex.getCause());
pair.serverSide.close();
pair.serverSocket.close();
}
}