-
Notifications
You must be signed in to change notification settings - Fork 371
/
tcp-test.js
82 lines (72 loc) · 2.19 KB
/
tcp-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
'use strict'
/* global describe, it, beforeEach, afterEach */
var assert = require('assert')
var xmpp = require('../index')
var C2S_PORT = 45552
describe('TCP client/server', function () {
var sv = null
var cl = null
var svcl = null
beforeEach(function (done) {
sv = new xmpp.server.C2S.TCPServer({port: C2S_PORT, autostart: false})
sv.on('connection', function (conn) {
svcl = conn
// Always authenticate
conn.once('authenticate', function (opts, cb) {
cb(null, opts)
})
})
sv.listen(function (err) {
if (err) return done(err)
cl = new xmpp.Client({
jid: 'test@localhost',
password: 'test',
host: 'localhost',
port: C2S_PORT
})
cl.on('online', function () {
done()
})
})
})
afterEach(function (done) {
if (cl) cl.end()
sv.shutdown(done)
})
describe('client', function () {
it('should go online', function () {
assert.ok(svcl.authenticated, 'Client should have authenticated')
})
it('should send a stanza', function (done) {
var message = new xmpp.Message({to: 'foo@bar.org'})
.c('body').t('Hello')
svcl.once('stanza', function (stanza) {
assert.ok(stanza.is('message'), 'Message stanza')
assert.equal(stanza.attrs.to, 'foo@bar.org')
assert.equal(stanza.getChildText('body'), 'Hello')
done()
})
cl.send(message)
})
it('should receive a stanza', function (done) {
cl.once('stanza', function (stanza) {
assert.ok(stanza.is('message'), 'Message stanza')
assert.equal(stanza.attrs.to, 'bar@bar.org')
assert.equal(stanza.getChildText('body'), 'Hello back')
done()
})
svcl.send(new xmpp.Message({ to: 'bar@bar.org' })
.c('body')
.t('Hello back'))
})
it('Can send plain text stanza', function (done) {
svcl.once('stanza', function (stanza) {
assert.ok(stanza.is('message'), 'Message stanza')
assert.equal(stanza.attrs.to, 'foo@bar.org')
assert.equal(stanza.getChildText('body'), 'Hello')
done()
})
cl.send('<message to="foo@bar.org"><body>Hello</body></message>')
})
})
})