-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
115 lines (95 loc) · 2.97 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const tap = require('tap')
const secureRPC = require('.')
const net = require('net')
tap.test('api exports', async t => {
t.equal(typeof secureRPC, 'function', 'main export is the rpc function')
t.equal(typeof secureRPC.peer, 'function', 'peer export is included')
})
tap.test('server ends', t => {
const clientSock = new net.Socket()
let serverRPC
const server = net.createServer((socket) => {
const rpc = secureRPC(socket, false)
serverRPC = rpc
rpc.sec.on('error', (err) => {
t.fail(err, 'No errors fro the server allowed')
})
rpc.command('echo', (req) => {
t.pass(req.arguments, 'received an echo command')
return req.arguments
})
})
server.on('error', (err) => {
t.fail(err, 'Server should not emit an error')
})
server.listen(8124, () => {
t.pass('server started')
clientSock.connect(8124, () => {
const clientRpc = secureRPC(clientSock, true)
clientRpc.sec.on('error', (err) => {
t.fail(err, 'client should not error')
})
clientSock.on('close', () => {
t.pass('client socket closed')
server.close((err) => {
t.error(err, 'no error when closing the server')
t.end('client is closed')
})
})
clientRpc.call('echo', 'hello world', (err, res) => {
t.error(err, 'no error returned')
t.deepEqual(res, ['hello world'], 'echo server works')
// server closes connection
setTimeout(() => { // Not sure why this is needed.
serverRPC.sec.end(() => {
t.pass('server ended client session')
})
}, 100)
})
})
})
})
tap.test('client ends', t => {
const clientSock = new net.Socket()
const server = net.createServer((socket) => {
const rpc = secureRPC(socket, false)
rpc.sec.on('error', (err) => {
t.fail(err, 'No errors fro the server allowed')
})
rpc.command('echo', (req) => {
t.pass(req.arguments, 'received an echo command')
return req.arguments
})
clientSock.on('close', () => {
server.close((err) => {
t.error(err, 'no error when closing the server')
t.end('server is closed')
})
})
})
server.on('error', (err) => {
t.fail(err, 'Server should not emit an error')
})
server.listen(8124, () => {
t.pass('server started')
clientSock.connect(8124, () => {
const clientRpc = secureRPC(clientSock, true)
clientRpc.sec.on('error', (err) => {
t.fail(err, 'client should not error')
})
clientSock.on('close', () => {
t.pass('client socket closed')
})
clientRpc.call('echo', 'hello world', (err, res) => {
t.error(err, 'no error returned')
t.deepEqual(res, ['hello world'], 'echo server works')
// server closes connection
setTimeout(() => { // Not sure why this is needed.
clientRpc.sec.end(() => {
t.pass('client ended client session')
})
}, 100)
})
})
})
})