Add echo tests

This commit is contained in:
Francisco Baio Dias 2016-03-15 15:27:22 +00:00
parent 19bbe473d9
commit 3b81221566
2 changed files with 43 additions and 2 deletions

View File

@ -17,10 +17,10 @@ function WebSockets () {
options = {}
}
options.ready = options.ready || function noop () {}
options.connect = options.connect || function noop () {}
const maOpts = multiaddr.toOptions()
const conn = new SWS('ws://' + maOpts.host + ':' + maOpts.port)
conn.on('ready', options.ready)
conn.on('connect', options.connect)
conn.getObservedAddrs = () => {
return [multiaddr]
}

View File

@ -63,4 +63,45 @@ describe('libp2p-websockets', function () {
expect(valid[0]).to.deep.equal(mh3)
done()
})
it('echo', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
ws.createListener(mh, (conn) => {
conn.pipe(conn)
}, () => {
const conn = ws.dial(mh)
const message = 'Hello World!'
conn.write(message)
conn.on('data', (data) => {
expect(data.toString()).to.equal(message)
conn.end()
ws.close(() => {
done()
})
})
})
})
it('echo with connect event and send', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
ws.createListener(mh, (conn) => {
conn.pipe(conn)
}, () => {
const message = 'Hello World!'
const conn = ws.dial(mh, {
connect: () => {
conn.send(message)
}
})
conn.on('data', (data) => {
expect(data.toString()).to.equal(message)
conn.end()
ws.close(() => {
done()
})
})
})
})
})