forked from Textalk/jquery.jsonrpcclient.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.jsonrpcclient.test.js
More file actions
449 lines (383 loc) · 12.6 KB
/
jquery.jsonrpcclient.test.js
File metadata and controls
449 lines (383 loc) · 12.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
TestCase(
"JsonRpcClientTest", {
testNew: function() {
var test = new $.JsonRpcClient({ ajaxUrl: '/foobar1' });
assertObject('test should be a new object', test);
}
}
);
// Adding a function to replace jQuery.ajax with for callbacks.
// It acts as a HTTP JSON-RPC 2.0 backend that returns the whole request as result for a request,
// and nothing for a notify.
function mock_jquery_ajax(params) {
var data = $.parseJSON(params.data);
// Is this a batch call? Mocking a little differently.
if (typeof data === 'object' && Array.isArray(data)) {
var response = [];
for (var i = 0; i < data.length; i++) {
var subdata = data[i];
// Fake a json-rpc error if the method is make_error.
if (subdata.method === 'make_error') {
response.push({
jsonrpc : '2.0',
id : subdata.id,
error: {
code : -42,
message : 'make_error made an error.',
data : subdata.params
}
});
}
else if ('id' in subdata) {
response.push({
jsonrpc : '2.0',
id : subdata.id,
result : subdata.params
});
}
}
params.success(response);
}
// Fake a json-rpc error if the method is make_error.
if (data.method === 'make_error') {
// The AJAX call is successfull, it's up to JsonRpcClient to parse this as an error.
params.success({
jsonrpc: '2.0',
id: params.id,
error: {
code : -42,
message : 'make_error made an error.',
data : params
}
});
}
// Call the success-callback if there is an id.
if ('id' in data) {
params.success({
jsonrpc : '2.0',
id : data.id,
result : params
});
}
}
AsyncTestCase(
'JsonRpcClientAsyncTest', {
testBadBackend: function(queue) {
// Setup a test client with a bad backend
var test = new $.JsonRpcClient({ ajaxUrl: '/foobar2' });
var error_cb_called = false;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
var error_cb = callbacks.add(
function(event, jqXHR, ajaxOptions) {
error_cb_called = true;
}
);
test.call('foo', [], error_cb, error_cb);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertTrue(
'The error_cb should be triggered.',
error_cb_called
);
}
);
},
testBatchErrorInHttp: function(queue) {
// Test that a non-existing backend gives the overall error_cb, and that the individual
// callbacks are not called.
var test = new $.JsonRpcClient({ ajaxUrl: '/foobar3' });
var main_error_cb_called = false;
var other_cb_called = false;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
var main_error_cb = callbacks.add(
function(event, jqXHR, ajaxOptions) {
main_error_cb_called = true;
}
);
var other_cb = function() { other_cb_called = true; };
test.batch(
function(batch) {
batch.call('foo', [], other_cb, other_cb);
batch.call('bar', [], other_cb, other_cb);
batch.call('baz', [], other_cb, other_cb);
},
other_cb, main_error_cb
);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertTrue(
'The main error_cb should be triggered.',
main_error_cb_called
);
assertFalse(
'No other callback should be triggered.',
other_cb_called
);
}
);
},
testBatchHttp: function(queue) {
// Test that both calls in the batch are given their respective data no matter the order of
// the results, with call 3 getting an error.
var test = new $.JsonRpcClient({ ajaxUrl: '/backend/Article/17094007' });
// Save the normal jQuery.ajax.
var saved_jquery_ajax = jQuery.ajax;
var all_done_cb_called = false;
var call1_data = undefined;
var call2_data = undefined;
var call3_errorcb_data = undefined;
var other_cb_called = false;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
// Replace jQuery.ajax with a mocked function.
jQuery.ajax = callbacks.add(mock_jquery_ajax);
//
var all_done_cb = callbacks.add(
function(given_result) {
all_done_cb_called = true;
}
);
var call1_cb = callbacks.add(
function(given_result) {
call1_data = given_result;
}
);
var call2_cb = callbacks.add(
function(given_result) {
call2_data = given_result;
}
);
var call3_errorcb = callbacks.add(
function(given_result) {
call3_errorcb_data = given_result;
}
);
var other_cb = function() { other_cb_called = true; };
test.batch(
function(batch) {
batch.call('foo', [ 1 ], call1_cb, other_cb);
batch.call('bar', [ 2 ], call2_cb, other_cb);
batch.call('make_error', [ 3 ], other_cb, call3_errorcb);
},
all_done_cb, other_cb
);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertTrue(
'all_done_cb should be called.',
all_done_cb_called
);
assertEquals(
'call1 should get the parameter list [ 1 ]',
[ 1 ],
call1_data
);
assertEquals(
'call2 should get the parameter list [ 2 ]',
[ 2 ],
call2_data
);
assertEquals(
'call3_errorcb should be given code -42.',
-42,
call3_errorcb_data.code
);
assertFalse(
'No other callback should be triggered.',
other_cb_called
);
}
);
},
testCall: function(queue) {
// Test a normal call procedure.
// Save the normal jQuery.ajax.
var saved_jquery_ajax = jQuery.ajax;
var given_url = '/bazz';
var test = new $.JsonRpcClient({ ajaxUrl: given_url });
var result = null;
var error_cb_called = false;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
// Replace jQuery.ajax with a mocked function.
jQuery.ajax = callbacks.add(mock_jquery_ajax);
// Add a success_cb that should be called with the whole request as result.
var success_cb = callbacks.add(
function(given_result) {
result = given_result;
}
);
// An error_cb that should NOT be called.
var error_cb = function(error) {
error_cb_called = true;
};
test.call('foo', [], success_cb, error_cb);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertFalse(
'Error callback should not be triggered.',
error_cb_called
);
assertEquals('Url should be same.', result.url, given_url);
// Restore jQuery.ajax
jQuery.ajax = saved_jquery_ajax;
}
);
},
testNotify: function(queue) {
// Test a normal notify procedure.
// Save the normal jQuery.ajax.
var saved_jquery_ajax = jQuery.ajax;
var given_url = '/bront';
var test = new $.JsonRpcClient({ ajaxUrl: given_url });
var id = null;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
var catch_cb = function(given_result) {
var data = $.parseJSON(given_result.result.data);
id = data.id;
};
// Replace jQuery.ajax with a mocked function.
jQuery.ajax = callbacks.add(
function(params) {
// Override success and set the catch_cb to catch request.
params.success = catch_cb;
mock_jquery_ajax(params);
}
);
test.notify('foo', []);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertNull('ID should not be set and cb would not be called.', id);
// Restore jQuery.ajax
jQuery.ajax = saved_jquery_ajax;
}
);
},
testNoWebSocket: function(queue) {
// Test that HTTP will be used when both ws and http is given and there is no
// window.WebSocket.
// Save the normal jQuery.ajax.
var saved_jquery_ajax = jQuery.ajax;
var given_http_url = '/foz';
var test = new $.JsonRpcClient({
ajaxUrl: given_http_url,
getSocket: function(onmessage_cb) { return null; }
});
var result = null;
var error_cb_called = false;
queue.call(
'Step 1: register ajax callback.',
function(callbacks) {
// Replace jQuery.ajax with a mocked function.
jQuery.ajax = callbacks.add(mock_jquery_ajax);
// Add a success_cb that should be called with the whole request as result.
var success_cb = callbacks.add(
function(given_result) {
result = given_result;
}
);
// An error_cb that should NOT be called.
var error_cb = function(error) {
error_cb_called = true;
};
test.call('foo', [], success_cb, error_cb);
}
);
queue.call(
'Step 2: check that callback has been called correctly.',
function() {
assertFalse(
'Error callback should not be triggered.',
error_cb_called
);
assertEquals('Url should be same.', result.url, given_http_url);
// Restore jQuery.ajax
jQuery.ajax = saved_jquery_ajax;
}
);
},
testDefaultGetsocket: function(queue) {
// Test the default ws_getsocket
if (!('WebSocket' in window)) {
// Skip test on browsers without WebSocket
assertTrue('Skipping WebSocket test in this browser.', true);
return;
}
var echo_data = null;
var other_cb_called = false;
var wanted_result = 'A valid result.';
var gotten_result = null;
queue.call(
'Setup ws-client and make call',
function(callbacks) {
// Since the echo-server at echo.websockets.org won't give a correct JSON-RPC response,
// we setup anonther onmessage catcher.
var other_onmessage = callbacks.add(
function(event) {
echo_data = event.data;
}
);
// The succes_cb, called bu the echo of our hand-crafter send below.
var success_cb = callbacks.add(
function(result) {
gotten_result = result;
}
);
var other_cb = function(data) {
other_cb_called = true;
};
var test = new $.JsonRpcClient({
socketUrl: 'ws://echo.websocket.org/',
onmessage: other_onmessage
});
// Send a request with the client. It will be echoed, and the echo will count as other
// message.
test.call('plebb', [ 'bar' ], success_cb, other_cb);
// Send the response via the client internal socket, to get the echo-server to send it
// back to us.
var old_onopen = test._ws_socket.onopen;
test._ws_socket.onopen = function(event) {
old_onopen(event); // chain in the already added onopen handler.
test._ws_socket.send($.toJSON({
jsonrpc: '2.0',
result: wanted_result,
id: test._current_id - 1 // match the last requests id
}));
};
}
);
queue.call(
'Assert that onmessage is called and nothing else.',
function() {
assertFalse('Other callback should not be called.', other_cb_called);
assertNotNull('Echo-server should return the request data.', echo_data);
assertEquals('The result should be parsed and send to success_cb.',
gotten_result, wanted_result);
}
);
}
}
);