forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 2
/
unit-test.html
346 lines (293 loc) · 12.6 KB
/
unit-test.html
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!doctype html>
<html>
<head>
<!-- Hey, let's be friends! twitter.com/pubnub -->
<title>PubNub JavaScript Unit Test</title>
<meta http-equiv=content-type content=text/html;charset=utf-8>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
<style type=text/css>
#stop-test,
#finished-fail,
#finished-success,
.tpl {
display: none
}
</style>
</head>
<body>
<div class=container>
<!-- NOTICE / TITLE -->
<div class="alert alert-block">
<a class="close" href="#">×</a>
<p><strong>PubNub Unit Tests</strong> for JavaScript on Mobile and Desktop Web Browser</p>
</div>
<!-- BUTTON PANNEL -->
<div class=well>
<button id=start-test class='btn info'>Start Test</button>
<button id=stop-test class='btn danger'>STOP</button>
<span id=finished-success class='label label-success'>100% Successful</span>
<span id=finished-fail class='label label-important'>Finished With Errors</span>
<span
id=test-status
class=label
template='Result: {pass}/{fail} (pass/fail) - Total: {total}'
>...</span>
</div>
<!-- OUTPUT -->
<table id=unit-test-out class="zebra-striped">
</table>
<!-- TEMPLATES -->
<script type=text/template id=test_template class=tpl>
<td><span class="label label-{result}">{display}</span></td>
<td>{message}</td>
</script>
<div id=pubnub pub-key=demo sub-key=demo origin=pubsub.pubnub.com></div>
<script src=../pubnub.min.js></script>
<script src=../pubnub-crypto.min.js></script>
<script>(function () {
function test(t, msg) {
if (!test.run) return;
var entry = p.create('tr');
entry.innerHTML = p.supplant(test_tpl, {
result: t ? 'success' : 'important',
display: t ? 'pass' : 'fail',
message: msg
});
t ? test.pass++ : test.fail++;
test.done++;
out.insertBefore(entry, out.firstChild);
console.log(t, msg);
status_area.innerHTML = p.supplant(status_tpl, {
pass: test.pass + '',
fail: test.fail + '',
total: test.done + ''
});
if (test.done === test.plan) {
stop_test();
test.fail ||
p.css(p.$('finished-success'), { display: 'inline-block' }) &&
p.css(p.$('finished-fail'), { display: 'inline-block' });
}
}
var p = PUBNUB
, many_con_tst = 2
, channel = 'pn-javascript-unit-test'
, out = p.$('unit-test-out')
, test_tpl = p.$('test_template').innerHTML
, start_button = p.$('start-test')
, stop_button = p.$('stop-test')
, status_area = p.$('test-status')
, status_tpl = p.attr(status_area, 'template');
/* ======================================================================
Stop Test
====================================================================== */
p.bind('mousedown,touchstart', stop_button, stop_test);
function stop_test() {
//p.css(start_button, { display: 'inline-block' });
p.css(stop_button, { display: 'none' });
test.run = 0;
}
/* ======================================================================
Start Test
====================================================================== */
p.bind('mousedown,touchstart', start_button, start_test);
function start_test() {
test.plan = 43 + many_con_tst * 3; // # of tests
test.done = 0; // 0 tests done so far
test.pass = 0; // 0 passes so far
test.fail = 0; // 0 fails so far
test.run = 1; // continue running?
p.css(stop_button, { display: 'inline-block' });
p.css(start_button, { display: 'none' });
p.css(p.$('finished-fail'), { display: 'none' });
p.css(p.$('finished-success'), { display: 'none' });
test(1, 'Ready to Test');
test(PUBNUB, 'PubNub Lib Exists');
// Encryption Unit Tests
test(PUBNUB.secure && true, "PubNub AES Lib Exists");
var aes = PUBNUB.secure({
publish_key: "demo",
subscribe_key: "demo",
cipher_key: "enigma"
});
test(aes, "AES PubNub Object Instantiated");
encryptionTests();
p.time(function (time) {
test(time, 'TimeToken');
test(time > 0, 'Time Value');
});
p.uuid(function (uuid) {
test(uuid, 'UUID Response');
test(uuid.length > 10, 'UUID Long Enough');
});
function publish_test(con) {
con || test(1, 'Connection Established');
p.publish({
channel: channel,
message: { test: "test" },
callback: function (response) {
test(response[0], 'Successful Publish');
test(response[1], 'Success With Demo');
setTimeout(history_test, 2500);
}
});
}
publish_test(1);
function history_test() {
p.history({
limit: 1,
channel: channel,
callback: function (data) {
test(data, 'History Response');
test(data[0][0].test === "test", 'History Content');
}
});
}
p.subscribe({
channel: channel,
connect: publish_test,
presence: function (message, envelope, channel) {
console.log(message, envelope, channel);
},
callback: function (message, envelope, channel) {
p.unsubscribe({ channel: channel });
test(message, 'Subscribe Message');
test(message.test === "test", 'Subscribe Message Data');
test(envelope[1], 'TimeToken Returned: ' + envelope[1]);
}
});
/* ------------------------------------------------------------------
--- MANY CONNECTIONS TESTS
------------------------------------------------------------------ */
p.map(Array(many_con_tst).join('-').split('-'), function (_, conn) {
var chan = 'many-conn-test-' + conn;
test(chan, 'Many Connections: ' + conn + ' Connecting...');
p.subscribe({
channel: chan,
presence: function (message, envelope, channel) {
console.log(message, envelope, channel);
},
connect: function (chan) {
test(chan, 'Many Connections: ' + chan + ' Connected!');
p.publish({ channel: chan, message: chan });
},
callback: function (message, envelope, channel) {
p.unsubscribe({ channel: channel });
test(
channel === message,
'Many Connections: ' + channel +
' Received - ' + message
);
}
});
});
/* ------------------------------------------------------------------
--- TESTING NEW CONNECTION RESTORE FEATURE
------------------------------------------------------------------ */
//return;
var restore_channel = 'restore-channel' + Math.random()
, p2 = PUBNUB.init({
publish_key : 'demo',
subscribe_key : 'demo'
});
p2.subscribe({
restore: true,
channel: restore_channel,
presence: function (message, envelope, channel) {
console.log(message, envelope, channel);
},
callback: function () {
},
connect: function () {
p2.unsubscribe({ channel: restore_channel });
// Send Message While Not Connected
p2.publish({
channel: restore_channel,
message: 'test',
callback: function () {
p2.subscribe({
restore: true,
channel: restore_channel,
callback: function (message, stack) {
p2.unsubscribe({ channel: restore_channel });
test(
message === "test",
'Subscribe Restore'
);
}
});
}
});
}
});
function encryptionTests() {
var test_plain_string_1 = "Pubnub Messaging API 1";
var test_plain_string_2 = "Pubnub Messaging API 2";
var test_plain_object_1 = {"foo": {"bar": "foobar"}};
var test_plain_object_2 = {"this stuff": {"can get": "complicated!"}};
var test_plain_unicode_1 = '漢語'
var test_cipher_string_1 = "f42pIQcWZ9zbTbH8cyLwByD/GsviOE0vcREIEVPARR0=";
var test_cipher_string_2 = "f42pIQcWZ9zbTbH8cyLwB/tdvRxjFLOYcBNMVKeHS54=";
var test_cipher_object_1 = "GsvkCYZoYylL5a7/DKhysDjNbwn+BtBtHj2CvzC4Y4g=";
var test_cipher_object_2 = "zMqH/RTPlC8yrAZ2UhpEgLKUVzkMI2cikiaVg30AyUu7B6J0FLqCazRzDOmrsFsF";
var test_cipher_unicode_1 = "WvztVJ5SPNOcwrKsDrGlWQ==";
test(aes.raw_encrypt(test_plain_string_1) == test_cipher_string_1, "AES String Encryption Test 1");
test(aes.raw_encrypt(test_plain_string_2) == test_cipher_string_2, "AES String Encryption Test 2");
test(aes.raw_encrypt(test_plain_object_1) == test_cipher_object_1, "AES Object Encryption Test 1");
test(aes.raw_encrypt(test_plain_object_2) == test_cipher_object_2, "AES Object Encryption Test 2");
test(aes.raw_encrypt(test_plain_unicode_1) == test_cipher_unicode_1, "AES Unicode Encryption Test 1");
test(aes.raw_decrypt(test_cipher_string_1) == test_plain_string_1, "AES String Decryption Test 1");
test(aes.raw_decrypt(test_cipher_string_2) == test_plain_string_2, "AES String Decryption Test 2");
test(JSON.stringify(aes.raw_decrypt(test_cipher_object_1)) == JSON.stringify(test_plain_object_1), "AES Object Decryption Test 1");
test(JSON.stringify(aes.raw_decrypt(test_cipher_object_2)) == JSON.stringify(test_plain_object_2), "AES Object Decryption Test 2");
test(aes.raw_decrypt(test_cipher_unicode_1) == test_plain_unicode_1, "AES Unicode Decryption Test 1");
var s = PUBNUB.secure({"cipher_key": "enigma", "subscribe_key": "demo", "publish_key": "demo"})
aes_channel = "hello_world";
function aes_publish_test() {
test(1, 'Beginning AES Publish / Subscribe / History Tests');
function aes_history_test() {
// Switchup JSONP
PUBNUB.init({
jsonp : true,
publish_key : 'demo',
subscribe_key : 'demo'
});
s.history({
limit: 1,
reverse: false,
channel: aes_channel,
callback: function (data) {
test(data, 'AES History Response');
test(data[0][0].test === "test", 'AES History Content');
}
});
}
s.publish({
channel: aes_channel,
message: { test: "test" },
callback: function (response) {
test(response[0], 'AES Successful Publish ' + response[0]);
test(response[1], 'AES Success With Demo ' + response[1]);
setTimeout(aes_history_test, 2500);
}
});
}
aes_publish_test();
s.subscribe({
channel: aes_channel,
connect: aes_publish_test,
presence: function (message, envelope, aes_channel) {
},
callback: function (message, envelope, aes_channel) {
test(message, 'AES Subscribe Message');
test(message.test === "test", 'AES Subscribe Message Data');
test(envelope[1], 'AES TimeToken Returned: ' + envelope[1]);
}
});
}
}
start_test();
})();</script>
</div>
</body>
</html>