forked from rongcloud/server-sdk-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.message.js
410 lines (348 loc) · 17.6 KB
/
test.message.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
410
var async = require( 'async' );
var parseString = require( 'xml2js' ).parseString;
var fs = require( 'fs' );
var should = require( 'should' );
var testConfig = require( './config' );
var rongSDK = require( '../index' );
var base64Voice = new Buffer( fs.readFileSync( './testvoice.amr' ) ).toString( 'base64' );
var base64Image = new Buffer( fs.readFileSync( './raindrop.jpg' ) ).toString( 'base64' );
var optionalArgs = [ 'push content', 'push data' ];
describe( 'Message Test', function() {
before( function( done ) {
// Init the SDK before testing.
rongSDK.init( testConfig.appKey, testConfig.appSecret );
done();
} );
after( function( done ) {
done();
} );
describe( 'Publish Message', function() {
it( 'Text message: should return OK', function( done ) {
var textMessageObject = { content : testConfig.message.textMsg };
// Testing for all the situations of arguments.
var args = [ testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:TxtMsg', JSON.stringify( textMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Image message: should return OK', function( done ) {
var imageMessageObject = { content : base64Image, imageUrl : 'http://lanceju-com.qiniudn.com/raindrop.jpg' };
var args = [ testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ImgMsg', JSON.stringify( imageMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Voice message: should return OK', function( done ) {
var voiceMessageObject = { content : base64Voice, duration : 4 };
var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:VcMsg', JSON.stringify( voiceMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Image Text message: should return OK', function( done ) {
var imageTextMessageObject = { title : 'hellotitle', content : 'hello', imageUrl : 'http://lanceju-com.qiniudn.com/raindrop.jpg', extra : 'image from a user' };
var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ImgTextMsg', JSON.stringify( imageTextMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Location message: should return OK', function( done ) {
var locationMessageObject = { content : 'You got a location message', latitude : 24.114, longitude : 334.221, poi : '北京市朝阳区北苑路北辰泰岳大厦', extra : 'The location of rong cloud' };
var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:LBSMsg', JSON.stringify( locationMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.private.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'ContactNtf message: should return OK', function( done ) {
var contactNtfMessageObject = { operation:"op1",sourceUserId:"24",targetUserId:"21",message:"haha",extra:"helloExtra"};
var args = [testConfig.message.fromUserId, testConfig.message.toUserId, 'RC:ContactNtf', JSON.stringify( contactNtfMessageObject ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.private.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
} );
describe( 'Publish Template Message', function() {
it( 'Send template message: should return OK', function( done ) {
var content = JSON.stringify( { content : "aa{c}{e}{d}", extra : "bb" } );
var values = [ { "{c}":"1","{d}":"2","{e}":"3"} ];
/*
rongSDK.message.private.publish_template( testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', content, values, [ 'push content for user' ], ['push data for user'], function( err, resultText ) {
return done();
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
done();
} );
*/
var args = [testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', content, values, [ 'push content for user' ], ['push data for user'] ];
var argsArray = [];
argsArray.push( args.concat() );
argsArray.push( args.concat( 'json' ) );
argsArray.push( args.concat( 'xml' ) );
async.each( argsArray, function( _args, cb ) {
rongSDK.message.private.publish_template.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Send system message: should return OK', function( done ) {
var args = [testConfig.message.fromUserId, [testConfig.message.toUserId], 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } )];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.system.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
it( 'Send group message before joining the group: should return error', function( done ) {
rongSDK.message.group.publish( testConfig.message.fromUserId, 'ProgrammerGroup1', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } ), null, null, function( err, resultText ) {
should.not.exists( err ); // FIXME should.exists( err );
// Joining the group
rongSDK.group.join( testConfig.message.fromUserId, 'ProgrammerGroup1', 'Programmers group', function( err, resultText ) {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
done();
} );
} );
} );
it( 'Send group message after joining the group: should return OK', function( done ) {
var args = [testConfig.message.fromUserId, 'ProgrammerGroup1', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } )];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.group.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
// Dismiss the group after testing.
rongSDK.group.quit( testConfig.message.fromUserId, 'ProgrammerGroup1', function( err, resultText ) {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
done();
} );
} );
} );
it( 'Send chatroom message: should return OK', function( done ) {
var args = [testConfig.message.fromUserId, 'my chatroom', 'RC:TxtMsg', JSON.stringify( { content : 'Hello, world!' } ) ];
var argsArray = [];
for( var i=0; i<3; ++i ) {
argsArray.push( args.concat( optionalArgs.slice( 0, i ) ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'json' ) );
argsArray.push( args.concat( optionalArgs.slice( 0, i ), 'xml' ) );
}
async.each( argsArray, function( _args, cb ) {
rongSDK.message.chatroom.publish.apply( this, _args.concat( function( err, resultText ) {
if( _args[ _args.length - 1 ] === 'xml' ) {
should.not.exists( err );
parseString( resultText, function( err, result ) {
parseInt( result.xml.code[0] ).should.equal( 200 );
cb();
} );
}
else {
should.not.exists( err );
var result = JSON.parse( resultText );
result.code.should.equal( 200 );
cb();
}
} ) );
}, function() {
done();
} );
} );
} );
// Since this API is a charing service, make sure your appKey&appSecret is from an charged account.
// describe( 'Broadcast Message', function() {
// it( 'Should return OK', function( done ) {
// rongSDK.message.broadcast( testConfig.message.fromUserId, 'RC:TxtMsg', testConfig.message.textMsg, function( err, resultText ) {
// should.not.exists( err );
// var result = JSON.parse( resultText );
// result.code.should.equal( 200 );
// done();
// } );
// } );
// } );
} );