summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-05-28 00:42:05 +0200
committerErik Larkö <erik@mullvad.net>2017-05-28 17:15:08 +0200
commit7daa29b0b7d6fca52515bcbe9b43be5e6b63279d (patch)
tree21bd9b9bc28aa62f32024c321395700d78435c73
parenta99e3585e0f06657d0813ec65d8bab7423379d2d (diff)
downloadmullvadvpn-7daa29b0b7d6fca52515bcbe9b43be5e6b63279d.tar.xz
mullvadvpn-7daa29b0b7d6fca52515bcbe9b43be5e6b63279d.zip
JSONRPC communication tests
-rw-r--r--app/lib/jsonrpc-ws-ipc.js11
-rw-r--r--test/ipc.spec.js32
2 files changed, 41 insertions, 2 deletions
diff --git a/app/lib/jsonrpc-ws-ipc.js b/app/lib/jsonrpc-ws-ipc.js
index a327696e27..f2b54b38da 100644
--- a/app/lib/jsonrpc-ws-ipc.js
+++ b/app/lib/jsonrpc-ws-ipc.js
@@ -38,6 +38,13 @@ export type JsonRpcSuccess = {
}
export type JsonRpcMessage = JsonRpcError | JsonRpcNotification | JsonRpcSuccess;
+export class TimeOutError extends Error {
+ constructor() {
+ super('Request timed out');
+ this.name = 'TimeOutError';
+ }
+}
+
const DEFAULT_TIMEOUT_MILLIS = 750;
export default class Ipc {
@@ -119,7 +126,7 @@ export default class Ipc {
return;
}
- request.reject('The request timed out');
+ request.reject(new TimeOutError());
}
_onMessage(message: string) {
@@ -160,7 +167,7 @@ export default class Ipc {
clearTimeout(request.timeout);
if (message.type === 'error') {
- request.reject(message.payload.error.message);
+ request.reject(message.payload.error);
} else {
const reply = message.payload.result;
request.resolve(reply);
diff --git a/test/ipc.spec.js b/test/ipc.spec.js
index afac3ad704..07421ce839 100644
--- a/test/ipc.spec.js
+++ b/test/ipc.spec.js
@@ -3,6 +3,7 @@
import Ipc from '../app/lib/jsonrpc-ws-ipc.js';
import jsonrpc from 'jsonrpc-lite';
import { expect } from 'chai';
+import assert from 'assert';
import type { JsonRpcMessage } from '../app/lib/jsonrpc-ws-ipc.js';
describe('The IPC server', () => {
@@ -26,6 +27,37 @@ describe('The IPC server', () => {
return p;
});
+
+ it('should reject failed jsonrpc requests', () => {
+ const { ws, ipc } = setupIpc();
+ ws.on('WHAT_IS_THIS', (msg) => {
+ ws.replyFail(msg.id, 'Method not found', -32601);
+ });
+
+ return ipc.send('WHAT_IS_THIS')
+ .catch((e) => {
+ expect(e.code).to.equal(-32601);
+ expect(e.message).to.contain('Method not found');
+ });
+ });
+
+ it('should route reply to correct promise', () => {
+ const { ws, ipc } = setupIpc();
+
+ ws.on('a message', (msg) => ws.replyOk(msg.id, 'a reply'));
+
+ const decoy = ipc.send('a decoy')
+ .then(() => assert(false, 'Should not be called'))
+ .catch(e => {
+ if (e.name !== 'TimeOutError') {
+ throw e;
+ }
+ });
+ const message = ipc.send('a message')
+ .then((reply) => expect(reply).to.equal('a reply'));
+
+ return Promise.all([message, decoy]);
+ });
});
function mockWebsocket() {