-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime.test.js
99 lines (92 loc) · 2.98 KB
/
realtime.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
var test = require('tape')
var Realtime = require('./realtime.js')
function makeMockServer(script) {
var i = -1
return (method, url, body, cb) => {
i++
script[i](method, url, body, cb)
}
}
test('realtime', async (t) => {
var callapi = makeMockServer([
async (method, url, body, cb) => {
// must first call sub and must attach user-mask in the query
t.equal(method, 'post')
t.true(url.indexOf('subs?') !== -1)
t.true(url.indexOf('user-mask=mask123') !== -1)
cb(`{"initial_token": "token0"}`, 200)
},
async (method, url, body, cb) => {
// start the poll and must attach initial token in the query
t.equal(method, 'get')
t.true(url.indexOf('poll?') !== -1)
t.true(url.indexOf('token=token0') !== -1)
cb(`{"events": [], "sequential_token": "token1"}`, 200)
},
async (method, url, body, cb) => {
// next poll must attach last received token
t.equal(method, 'get')
t.true(url.indexOf('poll?') !== -1)
t.true(url.indexOf('token=token1') !== -1)
realtime.stop()
cb(`{"events": [], "token": "-"}`, 200)
},
])
var realtime = new Realtime('', {user_mask: 'mask123'}, callapi)
var err = await realtime.subscribe(['message_pong'])
t.true(Object.keys(err).length === 0)
})
test('realtime subs when server down and then backup', async (t) => {
var callapi = makeMockServer([
async (method, url, body, cb) => {
// must first call sub and must attach user-mask in the query
t.equal(method, 'post')
t.true(url.indexOf('subs?') !== -1)
t.true(url.indexOf('user-mask=mask123') !== -1)
cb(`502 too busy`, 502)
},
async (method, url, body, cb) => {
// should retry
t.equal(method, 'post')
t.true(url.indexOf('subs?') !== -1)
t.true(url.indexOf('user-mask=mask123') !== -1)
cb(`502 still busy`, 502)
},
async (method, url, body, cb) => {
// should retry
t.equal(method, 'post')
t.true(url.indexOf('subs?') !== -1)
t.true(url.indexOf('user-mask=mask123') !== -1)
cb(`{"initial_token": "token0"}`, 200)
},
async (method, url, body, cb) => {
t.equal(method, 'get')
t.true(url.indexOf('poll?') !== -1)
t.true(url.indexOf('token=token0') !== -1)
realtime.stop()
cb(`{"events": [], "token": "-"}`, 200)
},
])
var realtime = new Realtime('', {user_mask: 'mask123'}, callapi)
var err = await realtime.subscribe(['message_pong'])
t.true(Object.keys(err).length === 0)
})
test('realtime must stop if there is something wrong with our credential or our code', async (t) => {
var subcode
var callapi = makeMockServer([
async (method, url, bkody, cb) => {
// must first call sub and must attach user-mask in the query
t.equal(method, 'post')
t.true(url.indexOf('subs?') !== -1)
t.true(url.indexOf('user-mask=mask123') !== -1)
cb(`400 invalid mask`, 400)
realtime.stop()
},
])
var realtime = new Realtime('', {user_mask: 'mask123'}, callapi)
realtime.onInterrupted((_, _1, code) => {
subcode = code
})
var err = await realtime.subscribe(['message_pong'])
t.false(Object.keys(err).length === 0)
})