fix subscribe+sesame

This commit is contained in:
2020-01-18 20:54:34 +01:00
parent 367d42a965
commit ae843d341e
5 changed files with 90 additions and 33 deletions
+57 -8
View File
@@ -1,4 +1,4 @@
import { describe, it } from "mocha";
import { describe, it, Func } from "mocha";
import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../Index'
import * as uuidv4 from "uuid/v4"
@@ -226,18 +226,35 @@ describe('It should do unhook', () => {
})
type SesameTestIfc = { test: { checkCandy: ()=>Promise<string>} }
type SesameTestIfc = {
test: {
checkCandy: ()=>Promise<string>
subscribe: (callback) => Promise<SubscriptionResponse<{ topic: string; }>>
}
}
describe('Sesame should unlock the socket', () => {
let candy = "OK"
let client: RPCSocket & SesameTestIfc
let server: RPCServer<{topic: string}, SesameTestIfc>
let server: RPCServer
let cb = (...args) => {}
before(async() => {
server = new RPCServer<{ topic: string }, SesameTestIfc>(20004, [{
server = new RPCServer(20004, [{
name: "test",
exportRPCs: () => [
async function checkCandy():Promise<string> { return candy },
{
name: 'subscribe',
hook: async(callback) => {
cb = callback
return <SubscriptionResponse>{
result: "Success",
uuid: uuidv4(),
topic: 'test'
}
}
},
async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
]}
],{
sesame: (_sesame) => _sesame === 'sesame!'
@@ -251,6 +268,10 @@ describe('Sesame should unlock the socket', () => {
server.destroy()
})
it('should work with sesame', (done) => {
client.test.checkCandy().then(c => done())
})
it('should not work without sesame', (done) => {
const sock = new RPCSocket(20004, "localhost")
sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
@@ -264,7 +285,35 @@ describe('Sesame should unlock the socket', () => {
})
})
it('should work with sesame', (done) => {
client.test.checkCandy().then(c => done())
it('callback should work with sesame', (done) => {
client.test.subscribe((c) => {
if(c === candy){
done()
}
}).then(d => {
if(d.result !== 'Success')
done('expected valid response')
client.test.checkCandy()
})
})
})
it('callback should not work without sesame', (done) => {
const sock = new RPCSocket(20004, "localhost")
sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
c.test.subscribe((c) => {
console.log("CALLBACK TRIGGERED UNEXPECTED");
if(c === candy)
done("super not")
}).then(async d => {
await client.test.checkCandy()
if(d == null){
done()
}else
done('unexpected valid response '+(d) )
c.destroy()
})
})
})
})