forked from villadora/node-hessian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.js
409 lines (326 loc) · 11.7 KB
/
test2.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
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
var assert = require('chai').assert,
hessian = require('../lib'),
Long = require('long'),
Proxy = hessian.Proxy;
require('es6-map-shim');
describe('de/serialize', function() {
var Reader = hessian.Reader2,
Writer = hessian.Writer2;
it('test de/serizlize', function() {
var writer = new Writer();
writer.write(null);
writer.write(true);
writer.write(false);
var buf = writer.getBuffer();
var reader = new Reader();
reader.appendData(buf);
assert.isNull(reader.read());
assert.isTrue(reader.read());
assert.isFalse(reader.read());
});
});
describe('hessian 2.0 test', function() {
this.timeout(10000);
var proxy;
before(function() {
proxy = new Proxy('http://hessian.caucho.com/test/test2');
proxy.on('call', function(data) {
// console.log('call: ', data);
});
proxy.on('reply', function(data) {
// console.log('reply:', data);
});
});
it('methodNull', function(done) {
proxy.invoke('methodNull', [], function(err, res) {
assert.isNull(res);
done(err);
});
});
function MAKE_ARGTEST(method, args, as) {
it(method, function(done) {
proxy.invoke(method, args, function(err, res) {
if (as) as(res);
else assert.isTrue(res);
done(err);
});
});
}
function MAKE_REPLYTEST(method, reply, as) {
it(method, function(done) {
proxy.invoke(method, [], function(err, res) {
if (as) as(res, reply);
else assert.strictEqual(res, reply);
done(err);
});
});
}
describe('test Null|True|False', function() {
MAKE_REPLYTEST('replyNull', null);
MAKE_REPLYTEST('replyTrue', true);
MAKE_REPLYTEST('replyFalse', false);
MAKE_ARGTEST('argNull', [null]);
MAKE_ARGTEST('argTrue', [true]);
MAKE_ARGTEST('argFalse', [false]);
});
describe('test Map', function() {
[0, 1, 2, 3].forEach(function(i) {
MAKE_REPLYTEST('replyTypedMap_' + i, null, function(res) {
assert.equal(res.__mapType__, 'java.util.Hashtable');
});
MAKE_REPLYTEST('replyUntypedMap_' + i, null, function(res) {
if (i < 3) {
if (res.keys().length) assert.lengthOf(res.keys(), i);
else assert.equal(res.size, i);
} else {
if (res.keys().length) assert.lengthOf(res.keys(), 1);
else assert.equal(res.size, 1);
}
});
});
MAKE_ARGTEST('argUntypedMap_0', [{}]);
MAKE_ARGTEST('argUntypedMap_1', [{
'a': 0
}]);
var map = new Map();
map.set(0, 'a');
map.set(1, 'b');
MAKE_ARGTEST('argUntypedMap_2', [map]);
map = new Map();
map.set(['a'], 0);
MAKE_ARGTEST('argUntypedMap_3', [map]);
var tmap = getHashtable();
MAKE_ARGTEST('argTypedMap_0', [tmap]);
tmap = getHashtable();
tmap.a = 0;
MAKE_ARGTEST('argTypedMap_1', [tmap]);
tmap = getHashtable(new Map());
tmap.set(0, 'a');
tmap.set(1, 'b');
MAKE_ARGTEST('argTypedMap_2', [tmap]);
tmap = getHashtable(new Map());
tmap.set(['a'], 0);
MAKE_ARGTEST('argTypedMap_3', [tmap]);
function getHashtable(map) {
var map = map || {};
map.__mapType__ = 'java.util.Hashtable';
return map;
}
});
describe('test List', function() {
var list = [];
[0, 1, 7, 8].forEach(function(len) {
var list = [];
for (var i = 0; i < len; ++i)
list.push((i + 1) + '');
MAKE_ARGTEST('argUntypedFixedList_' + len, [list]);
var typedList = list.concat();
typedList.__type__ = '[string';
MAKE_ARGTEST('argTypedFixedList_' + len, [typedList]);
});
});
describe('test Object', function() {
MAKE_ARGTEST('argObject_0', [{
__type__: 'com.caucho.hessian.test.A0'
}]);
MAKE_REPLYTEST('replyObject_0', null, function(res) {
assert.equal(res.__type__, 'com.caucho.hessian.test.A0');
});
var testObject = 'com.caucho.hessian.test.TestObject';
MAKE_ARGTEST('argObject_1', [{
__type__: testObject,
_value: 0
}]);
MAKE_REPLYTEST('replyObject_1', null, function(res) {
assert.equal(res.__type__, testObject);
assert.equal(res._value, 0);
});
MAKE_ARGTEST('argObject_2', [
[{
__type__: testObject,
_value: 0
}, {
__type__: testObject,
_value: 1
}]
]);
var obj = {
__type__: testObject,
_value: 0
};
MAKE_ARGTEST('argObject_2a', [
[obj, obj]
]);
MAKE_ARGTEST('argObject_2b', [
[obj, {
__type__: testObject,
_value: 0
}]
]);
var cons = {
__type__: 'com.caucho.hessian.test.TestCons',
_first: 'a',
_rest: null
};
cons._rest = cons;
MAKE_ARGTEST('argObject_3', [cons]);
});
describe('test Date', function() {
var dates = [new Date(0), new Date(Date.UTC(98, 4, 8, 9, 51, 31)), new Date(Date.UTC(98, 4, 8, 9, 51))];
for (var i = 0; i < dates.length; ++i) {
MAKE_ARGTEST('argDate_' + i, [dates[i]]);
MAKE_REPLYTEST('replyDate_' + i, dates[i], function(res, reply) {
assert.equal(res.getTime(), reply.getTime());
});
}
});
describe('test Binary', function() {
var buf1023, ss = [];
for (var i = 0; i < 16; i++) {
ss.push("" + parseInt(i / 10, 10) + (i % 10) + " 456789012345678901234567890123456789012345678901234567890123\n");
}
buf1023 = new Buffer(ss.join('').substring(0, 1023));
[new Buffer(0), new Buffer('012345678901234'),
new Buffer('0123456789012345'),
buf1023
].forEach(function(arg) {
var len = arg.length;
MAKE_ARGTEST('argBinary_' + len, [arg]);
MAKE_REPLYTEST('replyBinary_' + len, arg, function(res) {
assert.equal(res.length, len);
});
});
});
describe('test String', function() {
[0, 1, 31, 32, 1023, 1024, 65536].forEach(function(length) {
var str;
if (length <= 32) {
str = new Array(Math.ceil(length / 10)).join('0123456789');
var rest = length % 10;
for (var i = 0; i < rest; ++i) {
str += i + '';
}
} else if (length <= 1024) {
var ss = [];
for (var i = 0; i < 16; i++)
ss.push("" + parseInt(i / 10, 10) + (i % 10) + " 456789012345678901234567890123456789012345678901234567890123\n");
str = ss.join('').substring(0, length);
} else {
var ss = [];
for (var i = 0; i < 64 * 16; i++)
ss.push("" + parseInt(i / 100, 10) + (parseInt(i / 10, 10) % 10) + (i % 10) + " 56789012345678901234567890123456789012345678901234567890123\n");
str = ss.join('').substring(0, length);
}
MAKE_ARGTEST('argString_' + length, [str]);
MAKE_REPLYTEST('replyString_' + length, str);
});
});
describe('test Int', function() {
function MAKE_INT_TEST(val) {
var arg = parseInt(val, /^-?0x/.test(val) ? 16 : 10),
name = val;
if (arg < 0)
name = name.replace(/^./g, 'm');
assert.isNumber(arg);
MAKE_ARGTEST('argInt_' + name, [arg]);
MAKE_REPLYTEST('replyInt_' + name, arg);
}
MAKE_INT_TEST('0');
MAKE_INT_TEST('1');
MAKE_INT_TEST('47');
MAKE_INT_TEST('-16');
MAKE_INT_TEST('0x30');
MAKE_INT_TEST('0x7ff');
MAKE_INT_TEST('-17');
MAKE_INT_TEST('-0x800');
MAKE_INT_TEST('0x800');
MAKE_INT_TEST('0x3ffff');
MAKE_INT_TEST('-0x801');
MAKE_INT_TEST('-0x40000');
MAKE_INT_TEST('0x40000');
MAKE_INT_TEST('0x7fffffff');
MAKE_INT_TEST('-0x40001');
MAKE_INT_TEST('-0x80000000');
});
describe('test Long', function() {
function MAKE_ARGLONG_TEST(name, low, high) {
high = high || 0;
var arg;
if ((low > 0x7fffffff || low < 0) && high === 0) {
arg = Long.fromNumber(low);
} else {
arg = {
high: high & 0xffffffff,
low: low & 0xffffffff
};
}
var val;
if (arg.hasOwnProperty('high'))
val = new Long(arg.low, arg.high).toNumber();
else
val = arg.toNumber();
MAKE_ARGTEST('argLong_' + name, [arg]);
MAKE_REPLYTEST('replyLong_' + name, arg, function(res) {
if (res.hasOwnProperty('high'))
res = new Long(res.low, res.high).toNumber();
assert.equal(val, res);
});
}
MAKE_ARGLONG_TEST('0', 0);
MAKE_ARGLONG_TEST('1', 1);
MAKE_ARGLONG_TEST('15', 15);
MAKE_ARGLONG_TEST('m8', -8);
MAKE_ARGLONG_TEST('0x10', 0x10);
MAKE_ARGLONG_TEST('0x7ff', 0x7ff);
MAKE_ARGLONG_TEST('m9', -9);
MAKE_ARGLONG_TEST('m0x800', -0x800);
MAKE_ARGLONG_TEST('0x800', 0x800);
MAKE_ARGLONG_TEST('0x3ffff', 0x3ffff);
MAKE_ARGLONG_TEST('m0x801', -0x801);
MAKE_ARGLONG_TEST('m0x40000', -0x40000);
MAKE_ARGLONG_TEST('0x40000', 0x40000);
MAKE_ARGLONG_TEST('0x7fffffff', 0x7fffffff);
MAKE_ARGLONG_TEST('m0x40001', -0x40001);
MAKE_ARGLONG_TEST('m0x80000000', -0x80000000);
MAKE_ARGLONG_TEST('0x80000000', 0x80000000);
MAKE_ARGLONG_TEST('m0x80000001', ~0x80000000, ~0x0); // 2-complement
});
describe('test argDouble', function() {
function MAKE_ARGDBOULE_TEST(value) {
var arg = parseFloat(value),
name = value.replace(/\./g, '_');
if (arg < 0)
name = name.replace(/^./g, 'm');
var buf = new Buffer(8);
buf.writeDoubleBE(arg, 0);
assert.isNumber(arg);
// MAKE_ARGTEST('argDouble_' + name, [arg]);
var method = 'argDouble_' + name;
it(method, function(done) {
proxy.invoke(method, [{
val: arg,
type: 'double'
}], function(err, res) {
assert.isTrue(res);
done(err);
});
});
it('replyDouble_' + name, function(done) {
proxy.invoke('replyDouble_' + name, [], function(err, res) {
assert(res == arg);
done(err);
});
});
}
// MAKE_ARGDBOULE_TEST('0.0');
// MAKE_ARGDBOULE_TEST('1.0');
// MAKE_ARGDBOULE_TEST('2.0');
// MAKE_ARGDBOULE_TEST('127.0');
// MAKE_ARGDBOULE_TEST('-128.0');
// MAKE_ARGDBOULE_TEST('128.0');
// MAKE_ARGDBOULE_TEST('-129.0');
// MAKE_ARGDBOULE_TEST('32767.0');
// MAKE_ARGDBOULE_TEST('-32768.0');
MAKE_ARGDBOULE_TEST('3.14159');
});
});