|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-import { describe, it } from "mocha";
|
|
|
1
|
+import { describe, it, Func } from "mocha";
|
|
2
|
2
|
|
|
3
|
3
|
import { RPCServer, RPCSocket, SubscriptionResponse, makeSubResponse } from '../Index'
|
|
4
|
4
|
import * as uuidv4 from "uuid/v4"
|
|
|
@@ -226,18 +226,35 @@ describe('It should do unhook', () => {
|
|
226
|
226
|
})
|
|
227
|
227
|
|
|
228
|
228
|
|
|
229
|
|
-type SesameTestIfc = { test: { checkCandy: ()=>Promise<string>} }
|
|
|
229
|
+type SesameTestIfc = {
|
|
|
230
|
+ test: {
|
|
|
231
|
+ checkCandy: ()=>Promise<string>
|
|
|
232
|
+ subscribe: (callback) => Promise<SubscriptionResponse<{ topic: string; }>>
|
|
|
233
|
+ }
|
|
|
234
|
+}
|
|
230
|
235
|
|
|
231
|
236
|
describe('Sesame should unlock the socket', () => {
|
|
232
|
237
|
let candy = "OK"
|
|
233
|
238
|
let client: RPCSocket & SesameTestIfc
|
|
234
|
|
- let server: RPCServer<{topic: string}, SesameTestIfc>
|
|
|
239
|
+ let server: RPCServer
|
|
|
240
|
+ let cb = (...args) => {}
|
|
235
|
241
|
|
|
236
|
242
|
before(async() => {
|
|
237
|
|
- server = new RPCServer<{ topic: string }, SesameTestIfc>(20004, [{
|
|
|
243
|
+ server = new RPCServer(20004, [{
|
|
238
|
244
|
name: "test",
|
|
239
|
245
|
exportRPCs: () => [
|
|
240
|
|
- async function checkCandy():Promise<string> { return candy },
|
|
|
246
|
+ {
|
|
|
247
|
+ name: 'subscribe',
|
|
|
248
|
+ hook: async(callback) => {
|
|
|
249
|
+ cb = callback
|
|
|
250
|
+ return <SubscriptionResponse>{
|
|
|
251
|
+ result: "Success",
|
|
|
252
|
+ uuid: uuidv4(),
|
|
|
253
|
+ topic: 'test'
|
|
|
254
|
+ }
|
|
|
255
|
+ }
|
|
|
256
|
+ },
|
|
|
257
|
+ async function checkCandy():Promise<string> { cb(candy); cb=()=>{}; return candy },
|
|
241
|
258
|
]}
|
|
242
|
259
|
],{
|
|
243
|
260
|
sesame: (_sesame) => _sesame === 'sesame!'
|
|
|
@@ -251,6 +268,10 @@ describe('Sesame should unlock the socket', () => {
|
|
251
|
268
|
server.destroy()
|
|
252
|
269
|
})
|
|
253
|
270
|
|
|
|
271
|
+ it('should work with sesame', (done) => {
|
|
|
272
|
+ client.test.checkCandy().then(c => done())
|
|
|
273
|
+ })
|
|
|
274
|
+
|
|
254
|
275
|
it('should not work without sesame', (done) => {
|
|
255
|
276
|
const sock = new RPCSocket(20004, "localhost")
|
|
256
|
277
|
sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
|
|
|
@@ -264,7 +285,35 @@ describe('Sesame should unlock the socket', () => {
|
|
264
|
285
|
})
|
|
265
|
286
|
})
|
|
266
|
287
|
|
|
267
|
|
- it('should work with sesame', (done) => {
|
|
268
|
|
- client.test.checkCandy().then(c => done())
|
|
|
288
|
+ it('callback should work with sesame', (done) => {
|
|
|
289
|
+ client.test.subscribe((c) => {
|
|
|
290
|
+ if(c === candy){
|
|
|
291
|
+ done()
|
|
|
292
|
+ }
|
|
|
293
|
+ }).then(d => {
|
|
|
294
|
+ if(d.result !== 'Success')
|
|
|
295
|
+ done('expected valid response')
|
|
|
296
|
+
|
|
|
297
|
+ client.test.checkCandy()
|
|
|
298
|
+ })
|
|
|
299
|
+ })
|
|
|
300
|
+
|
|
|
301
|
+ it('callback should not work without sesame', (done) => {
|
|
|
302
|
+ const sock = new RPCSocket(20004, "localhost")
|
|
|
303
|
+ sock.connect<SesameTestIfc>( /* no sesame */).then(async (c) => {
|
|
|
304
|
+ c.test.subscribe((c) => {
|
|
|
305
|
+ console.log("CALLBACK TRIGGERED UNEXPECTED");
|
|
|
306
|
+
|
|
|
307
|
+ if(c === candy)
|
|
|
308
|
+ done("super not")
|
|
|
309
|
+ }).then(async d => {
|
|
|
310
|
+ await client.test.checkCandy()
|
|
|
311
|
+ if(d == null){
|
|
|
312
|
+ done()
|
|
|
313
|
+ }else
|
|
|
314
|
+ done('unexpected valid response '+(d) )
|
|
|
315
|
+ c.destroy()
|
|
|
316
|
+ })
|
|
|
317
|
+ })
|
|
269
|
318
|
})
|
|
270
|
|
-})
|
|
|
319
|
+})
|