summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-05-28 00:41:16 +0200
committerErik Larkö <erik@mullvad.net>2017-05-28 12:43:09 +0200
commit9f68855437a52da5586d146a2a1f8dda056c8f91 (patch)
tree049e9bcb8fca6664ef181a2462c58becb46cc573 /test
parent3632bdf60ec8021c9d41e40d72a1a37edee5a658 (diff)
downloadmullvadvpn-9f68855437a52da5586d146a2a1f8dda056c8f91.tar.xz
mullvadvpn-9f68855437a52da5586d146a2a1f8dda056c8f91.zip
Send on connect test
Diffstat (limited to 'test')
-rw-r--r--test/ipc.spec.js76
1 files changed, 76 insertions, 0 deletions
diff --git a/test/ipc.spec.js b/test/ipc.spec.js
new file mode 100644
index 0000000000..afac3ad704
--- /dev/null
+++ b/test/ipc.spec.js
@@ -0,0 +1,76 @@
+// @flow
+
+import Ipc from '../app/lib/jsonrpc-ws-ipc.js';
+import jsonrpc from 'jsonrpc-lite';
+import { expect } from 'chai';
+import type { JsonRpcMessage } from '../app/lib/jsonrpc-ws-ipc.js';
+
+describe('The IPC server', () => {
+
+ it('should send as soon as the websocket connects', () => {
+ const { ws, ipc } = setupIpc();
+ ws.close();
+
+ let sent = false;
+ const p = ipc.send('hello')
+ .then(() => {
+ expect(sent).to.be.true;
+ });
+
+ ws.on('hello', (msg) => {
+ sent = true;
+
+ ws.replyOk(msg.id);
+ });
+ ws.acceptConnection();
+
+ return p;
+ });
+});
+
+function mockWebsocket() {
+ const ws : any = {
+ listeners: {},
+ readyState: 1,
+ };
+
+ ws.on = (event, listener) => ws.listeners[event] = listener;
+ ws.send = (data) => {
+ const listener = ws.listeners[data.method];
+ if (listener) {
+ listener(data);
+ }
+ };
+
+ ws.factory = () => ws;
+
+ ws.acceptConnection = () => {
+ ws.readyState = 1;
+ ws.onopen();
+ };
+ ws.close = () => {
+ ws.readyState = 3;
+ ws.onclose();
+ };
+
+ ws.reply = (msg: JsonRpcMessage) => {
+ ws.onmessage({data: JSON.stringify(msg)});
+ };
+ ws.replyOk = (id: string, msg) => {
+ ws.reply(jsonrpc.success(id, msg || ''));
+ };
+ ws.replyFail = (id: string, msg: string, code: number) => {
+ ws.reply(jsonrpc.error(id, new jsonrpc.JsonRpcError(msg, code)));
+ };
+
+ return ws;
+}
+
+function setupIpc() {
+ const ws = mockWebsocket();
+ return {
+ ws: ws,
+ ipc: new Ipc('1.2.3.4', ws.factory),
+ };
+}
+