add chai to tests
This commit is contained in:
+121
-182
@@ -8,6 +8,8 @@ import * as express from 'express';
|
||||
import * as fetch from 'node-fetch';
|
||||
import { PromiseIO } from "../src/PromiseIO/Server";
|
||||
import { PromiseIOClient } from "../src/PromiseIO/Client";
|
||||
import { assert, expect } from 'chai';
|
||||
var should = require('chai').should();
|
||||
|
||||
const noop = (...args) => { }
|
||||
|
||||
@@ -20,6 +22,11 @@ function makeServer(onCallback = noop, connectionHandler = noop, hookCloseHandle
|
||||
{
|
||||
name: 'echo',
|
||||
call: async (s: string) => s,
|
||||
}, {
|
||||
name: 'complexSignature',
|
||||
call: async ([a, b]) => {
|
||||
return [b, a]
|
||||
}
|
||||
}, {
|
||||
name: 'simpleSubscribe',
|
||||
hook: async (callback) => {
|
||||
@@ -140,34 +147,26 @@ describe('RPCServer', () => {
|
||||
done()
|
||||
})
|
||||
|
||||
it('should be able to use all kinds of RPC definitions', (done) => {
|
||||
client.connect().then(async () => {
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
it('should be able to use all kinds of RPC definitions', async () => {
|
||||
await client.connect()
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
|
||||
|
||||
if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
|
||||
done()
|
||||
} else {
|
||||
done(new Error("Bad response"))
|
||||
}
|
||||
})
|
||||
expect(r0).to.be.equal('Hello')
|
||||
expect(r1).to.be.equal('World')
|
||||
expect(r2.join('')).to.be.equal('RPC!')
|
||||
})
|
||||
|
||||
it('new RPCServer() should fail on bad RPC', (done) => {
|
||||
try {
|
||||
it('new RPCServer() should fail on unnamed RPC', async () => {
|
||||
expect(() => {
|
||||
const sv = new RPCServer([{
|
||||
name: 'bad',
|
||||
RPCs: () => [
|
||||
(aaa, bbb, ccc) => { return aaa + bbb + ccc }
|
||||
]
|
||||
}])
|
||||
sv.listen(20001)
|
||||
done(new Error("Didn't fail with bad RPC"))
|
||||
} catch (badRPCError) {
|
||||
done()
|
||||
}
|
||||
}).to.throw()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -228,31 +227,21 @@ describe('RPCServer with premade http server', () => {
|
||||
done()
|
||||
})
|
||||
|
||||
it('should serve REST', (done) => {
|
||||
fetch('http://localhost:8080/REST_ping').then(response => {
|
||||
response.text().then(text => {
|
||||
if (text === "REST_pong")
|
||||
done()
|
||||
else
|
||||
done(new Error("REST repsonse was " + text))
|
||||
})
|
||||
})
|
||||
it('should serve REST', async () => {
|
||||
const response = await fetch('http://localhost:8080/REST_ping')
|
||||
const text = await response.text()
|
||||
expect(text).to.be.equal("REST_pong")
|
||||
})
|
||||
|
||||
|
||||
it('should be able to use all kinds of RPC definitions', (done) => {
|
||||
client.connect().then(async () => {
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
|
||||
|
||||
if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
|
||||
done()
|
||||
} else {
|
||||
done(new Error("Bad response"))
|
||||
}
|
||||
})
|
||||
it('should be able to use all kinds of RPC definitions', async () => {
|
||||
await client.connect()
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
expect(r0).to.be.equal('Hello')
|
||||
expect(r1).to.be.equal('World')
|
||||
expect(r2.join('')).to.be.equal('RPC!')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -272,8 +261,8 @@ describe('should be able to attach to non-standard path', () => {
|
||||
}
|
||||
]
|
||||
}])
|
||||
server.listen(21003, {path: '/test'})
|
||||
client = new RPCSocket(21003, 'localhost', {path: '/test'})
|
||||
server.listen(21003, { path: '/test' })
|
||||
client = new RPCSocket(21003, 'localhost', { path: '/test' })
|
||||
done()
|
||||
})
|
||||
|
||||
@@ -284,19 +273,14 @@ describe('should be able to attach to non-standard path', () => {
|
||||
done()
|
||||
})
|
||||
|
||||
it('should be able to use all kinds of RPC definitions', (done) => {
|
||||
client.connect().then(async () => {
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
|
||||
|
||||
if (r0 === 'Hello' && r1 === 'World' && r2.join('') === 'RPC!') {
|
||||
done()
|
||||
} else {
|
||||
done(new Error("Bad response"))
|
||||
}
|
||||
})
|
||||
it('should be able to use all kinds of RPC definitions', async () => {
|
||||
await client.connect()
|
||||
const r0 = await client['HelloWorldRPCGroup'].echo('Hello')
|
||||
const r1 = await client['HelloWorldRPCGroup'].echof('World')
|
||||
const r2 = await client['HelloWorldRPCGroup'].echoExplicit('R', 'P', 'C!')
|
||||
expect(r0).to.be.equal('Hello')
|
||||
expect(r1).to.be.equal('World')
|
||||
expect(r2.join('')).to.be.equal('RPC!')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -366,22 +350,12 @@ describe('can attach multiple RPCServers to same http server', () => {
|
||||
done()
|
||||
})
|
||||
|
||||
it('both servers should answer', (done) => {
|
||||
client['HelloWorldRPCGroup'].echo("test").then(res => {
|
||||
if(res != "test"){
|
||||
done(new Error("response was "+res))
|
||||
}else{
|
||||
client2['Grp2'].test().then(res => {
|
||||
if(res != "/test"){
|
||||
done(new Error("response2 was "+res))
|
||||
}else{
|
||||
done()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
it('both servers should answer', async () => {
|
||||
const res = await client['HelloWorldRPCGroup'].echo("test")
|
||||
expect(res).to.equal('test')
|
||||
const res2 = await client2['Grp2'].test()
|
||||
expect(res2).to.equal('/test')
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("can attach second RPCServer if first is already running", () => {
|
||||
@@ -390,7 +364,7 @@ describe("can attach second RPCServer if first is already running", () => {
|
||||
{
|
||||
name: 'HelloWorldRPCGroup',
|
||||
RPCs: [
|
||||
function echo (x) { return x}, //named function variable
|
||||
function echo(x) { return x }, //named function variable
|
||||
function echof(x) { return x }, //named function
|
||||
{
|
||||
name: 'echoExplicit', //describing object
|
||||
@@ -409,7 +383,7 @@ describe("can attach second RPCServer if first is already running", () => {
|
||||
}
|
||||
]
|
||||
|
||||
it("attaches correctly", done => {
|
||||
it("attaches correctly", async () => {
|
||||
const expressServer = express()
|
||||
const httpServer = new http.Server(expressServer)
|
||||
|
||||
@@ -427,21 +401,15 @@ describe("can attach second RPCServer if first is already running", () => {
|
||||
path: "test"
|
||||
})
|
||||
|
||||
new RPCSocket(8080, 'localhost').connect().then(sock => {
|
||||
new RPCSocket(8080, 'localhost', { path: "test" }).connect().then(sock2 => {
|
||||
sock2.Grp2.test().then(resp => {
|
||||
if(resp === "/test")
|
||||
done()
|
||||
else
|
||||
done(new Error("response did not match"))
|
||||
const sock = await new RPCSocket(8080, 'localhost').connect()
|
||||
const sock2 = await new RPCSocket(8080, 'localhost', { path: "test" }).connect()
|
||||
const resp = await sock2.Grp2.test()
|
||||
expect(resp).to.be.equal("/test")
|
||||
|
||||
server.close()
|
||||
server2.close()
|
||||
sock.close()
|
||||
sock2.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
server.close()
|
||||
server2.close()
|
||||
sock.close()
|
||||
sock2.close()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -509,33 +477,19 @@ describe('RPCSocket', () => {
|
||||
})
|
||||
|
||||
|
||||
it('should have rpc echo', (done) => {
|
||||
client['test'].echo("x").then(x => {
|
||||
if (x === 'x')
|
||||
done()
|
||||
else
|
||||
done(new Error('echo RPC response did not match'))
|
||||
})
|
||||
it('should have rpc echo', async() => {
|
||||
const x = await client['test'].echo("x")
|
||||
expect(x).to.be.equal('x')
|
||||
})
|
||||
|
||||
it('should add up to 6', (done) => {
|
||||
client['test'].add(1, 2, 3).then(x => {
|
||||
if (x === 6)
|
||||
done()
|
||||
else
|
||||
done(new Error('add RPC response did not match'))
|
||||
})
|
||||
it('should add up to 6', async() => {
|
||||
const sum = await client['test'].add(1, 2, 3)
|
||||
expect(sum).to.be.equal(6)
|
||||
})
|
||||
|
||||
it('should subscribe with success', (done) => {
|
||||
client['test'].simpleSubscribe(console.log).then(res => {
|
||||
if (res.topic === 'test') {
|
||||
done()
|
||||
} else {
|
||||
console.error(res)
|
||||
done(new Error('Subscribe did not return success'))
|
||||
}
|
||||
})
|
||||
it('should subscribe with success', async () => {
|
||||
const res = await client['test'].simpleSubscribe(noop)
|
||||
expect(res.topic).to.be.equal('test')
|
||||
})
|
||||
|
||||
it('subscribe should call back', (done) => {
|
||||
@@ -616,38 +570,30 @@ describe('It should do unhook', () => {
|
||||
server.close()
|
||||
})
|
||||
|
||||
it('Subscribe with param', (done) => {
|
||||
client['test'].subscribeWithParam("OK", noop).then(async (res) => {
|
||||
if (res.uuid === candy) {
|
||||
done()
|
||||
} else
|
||||
done(new Error("Results did not match " + res.uuid))
|
||||
})
|
||||
it('Subscribe with param', async () => {
|
||||
const res = await client['test'].subscribeWithParam("OK", noop)
|
||||
expect(res.uuid).to.be.equal(candy)
|
||||
})
|
||||
|
||||
let run = 0
|
||||
const expected = [yesCandy, noCandy, noCandy, noCandy]
|
||||
|
||||
it('Unhook+unsubscribe should stop callbacks', (done) => {
|
||||
|
||||
client['test'].subscribe(function myCallback(c) {
|
||||
it('Unhook+unsubscribe should stop callbacks', async() => {
|
||||
await client['test'].subscribe(function myCallback(c) {
|
||||
if (run == 1)
|
||||
(myCallback as any).destroy()
|
||||
|
||||
if (c !== expected[run++]) {
|
||||
done(new Error(`Wrong candy '${c}' in iteration '${run - 1}'`))
|
||||
}
|
||||
}).then(async function (res) {
|
||||
const r1 = await client['test'].publish()
|
||||
const r3 = await client['test'].unsubscribe()
|
||||
const r2 = await client['test'].publish()
|
||||
const r4 = await client['test'].publish()
|
||||
|
||||
if (r1 === yesCandy && r3 === noCandy && r2 === noCandy && r4 === noCandy)
|
||||
done()
|
||||
else
|
||||
done(new Error("Results did not match: " + [r1, r2, r3, r4]))
|
||||
expect(c).to.equal(expected[run++])
|
||||
})
|
||||
|
||||
const r1 = await client['test'].publish()
|
||||
const r3 = await client['test'].unsubscribe()
|
||||
const r2 = await client['test'].publish()
|
||||
const r4 = await client['test'].publish()
|
||||
|
||||
expect(r1).to.be.equal(yesCandy)
|
||||
expect(r2).to.be.equal(noCandy)
|
||||
expect(r3).to.be.equal(noCandy)
|
||||
expect(r4).to.be.equal(noCandy)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -700,41 +646,33 @@ describe('Sesame should unlock the socket', () => {
|
||||
server.close()
|
||||
})
|
||||
|
||||
it('should work with sesame', (done) => {
|
||||
client.test.checkCandy().then(c => done())
|
||||
it('should work with sesame', async () => {
|
||||
const c = client.test.checkCandy()
|
||||
expect(c).to.exist
|
||||
})
|
||||
|
||||
it('should work with multiple params', (done) => {
|
||||
client.test['manyParams']('a', 'b', 'c', 'd').then(c => {
|
||||
if (c[0] == 'a' && c[1] === 'b' && c[2] === 'c' && c[3] === 'd')
|
||||
done()
|
||||
})
|
||||
it('should work with multiple params', async () => {
|
||||
const c = await client.test['manyParams']('a', 'b', 'c', 'd')
|
||||
expect(c[0]).to.be.equal('a')
|
||||
expect(c[1]).to.be.equal('b')
|
||||
expect(c[2]).to.be.equal('c')
|
||||
expect(c[3]).to.be.equal('d')
|
||||
})
|
||||
|
||||
it('should not work without sesame', (done) => {
|
||||
it('should not work without sesame', async () => {
|
||||
const sock = new RPCSocket(21004, "localhost")
|
||||
sock.connect().then(async (cli) => {
|
||||
if (!cli.test)
|
||||
done()
|
||||
else {
|
||||
done(new Error("Function supposed to be removed without sesame"))
|
||||
}
|
||||
cli.close()
|
||||
sock.close()
|
||||
})
|
||||
const cli = await sock.connect()
|
||||
expect(cli.test).to.not.exist
|
||||
cli.close()
|
||||
sock.close()
|
||||
})
|
||||
|
||||
it('should fail with wrong sesame', (done) => {
|
||||
it('should fail with wrong sesame', async () => {
|
||||
const sock = new RPCSocket(21004, "localhost")
|
||||
sock.connect('abasd').then(async (cli) => {
|
||||
if (!cli.test)
|
||||
done()
|
||||
else {
|
||||
done(new Error("Function supposed to be removed without sesame"))
|
||||
}
|
||||
cli.close()
|
||||
sock.close()
|
||||
})
|
||||
const cli = await sock.connect('iamwrong')
|
||||
expect(cli.test).to.not.exist
|
||||
cli.close()
|
||||
sock.close()
|
||||
})
|
||||
|
||||
it('callback should work with sesame', (done) => {
|
||||
@@ -775,21 +713,21 @@ describe('Error handling', () => {
|
||||
a: 'a',
|
||||
b: 'b'
|
||||
})
|
||||
.then(r => {
|
||||
if (r != null)
|
||||
done(new Error("UNEXPECTED RESULT " + r))
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e.message === errtxt)
|
||||
done()
|
||||
else
|
||||
done(e)
|
||||
})
|
||||
.finally(() => {
|
||||
cli.close()
|
||||
sock.close()
|
||||
server.close()
|
||||
})
|
||||
.then(r => {
|
||||
if (r != null)
|
||||
done(new Error("UNEXPECTED RESULT " + r))
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e.message === errtxt)
|
||||
done()
|
||||
else
|
||||
done(e)
|
||||
})
|
||||
.finally(() => {
|
||||
cli.close()
|
||||
sock.close()
|
||||
server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -926,6 +864,7 @@ describe("Class binding", () => {
|
||||
let serv: RPCServer<myExporterIfc>
|
||||
let sock: RPCSocket & myExporterIfc
|
||||
let allowed = true
|
||||
const SESAME = 'xyz'
|
||||
|
||||
class MyExporter implements RPCExporter<myExporterIfc>{
|
||||
name = "MyExporter" as "MyExporter"
|
||||
@@ -960,12 +899,12 @@ describe("Class binding", () => {
|
||||
if (exporter.name === 'MyExporter') {
|
||||
if (!allowed) return false
|
||||
allowed = false
|
||||
return sesame === 'xxx';
|
||||
return sesame === SESAME;
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
},
|
||||
sesame: "xxx"
|
||||
sesame: SESAME
|
||||
})
|
||||
serv.listen(21004)
|
||||
done()
|
||||
@@ -973,7 +912,7 @@ describe("Class binding", () => {
|
||||
|
||||
beforeEach((done) => {
|
||||
const s = new RPCSocket<myExporterIfc>(21004, 'localhost')
|
||||
s.connect("xxx").then(conn => {
|
||||
s.connect(SESAME).then(conn => {
|
||||
sock = conn
|
||||
done()
|
||||
})
|
||||
@@ -1049,9 +988,9 @@ describe("attaching handlers before connecting", () => {
|
||||
})
|
||||
|
||||
sock.connect().then(_ => {
|
||||
sock.call("unknownRPC123", "AAAAA").catch(e => { }).then(x => {
|
||||
console.log("X",x);
|
||||
|
||||
sock.call("unknownRPC123", "AAAAA").catch(e => { }).then(x => {
|
||||
console.log("X", x);
|
||||
|
||||
})
|
||||
}).catch(e => {
|
||||
console.log("unexpected connect catch clause");
|
||||
|
||||
Reference in New Issue
Block a user