2016-03-14 20:25:00 +00:00
|
|
|
/* eslint-env mocha */
|
2016-03-23 16:23:10 +01:00
|
|
|
'use strict'
|
2016-03-14 20:25:00 +00:00
|
|
|
|
|
|
|
const expect = require('chai').expect
|
|
|
|
const multiaddr = require('multiaddr')
|
2016-03-23 16:23:10 +01:00
|
|
|
const WSlibp2p = require('../src')
|
2016-03-14 20:25:00 +00:00
|
|
|
|
|
|
|
describe('libp2p-websockets', function () {
|
|
|
|
this.timeout(10000)
|
|
|
|
var ws
|
|
|
|
|
|
|
|
it('create', (done) => {
|
|
|
|
ws = new WSlibp2p()
|
|
|
|
expect(ws).to.exist
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('listen and dial', (done) => {
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
2016-03-14 20:25:00 +00:00
|
|
|
ws.createListener(mh, (socket) => {
|
|
|
|
expect(socket).to.exist
|
|
|
|
socket.end()
|
2016-05-08 22:58:38 +02:00
|
|
|
ws.close(done)
|
2016-03-14 20:25:00 +00:00
|
|
|
}, () => {
|
|
|
|
const conn = ws.dial(mh)
|
|
|
|
conn.end()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('listen on several', (done) => {
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
|
|
|
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
|
2016-03-14 20:25:00 +00:00
|
|
|
const ws = new WSlibp2p()
|
|
|
|
|
|
|
|
ws.createListener([mh1, mh2], (socket) => {}, () => {
|
|
|
|
ws.close(done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('get observed addrs', (done) => {
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
2016-03-14 20:25:00 +00:00
|
|
|
ws.createListener(mh, (socket) => {
|
|
|
|
expect(socket).to.exist
|
|
|
|
socket.end()
|
|
|
|
expect(socket.getObservedAddrs()).to.deep.equal([])
|
2016-05-08 22:58:38 +02:00
|
|
|
ws.close(done)
|
2016-03-14 20:25:00 +00:00
|
|
|
}, () => {
|
|
|
|
const conn = ws.dial(mh)
|
|
|
|
conn.end()
|
|
|
|
})
|
|
|
|
})
|
2016-03-14 21:19:42 +00:00
|
|
|
|
|
|
|
it('filter', (done) => {
|
|
|
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
|
|
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
2016-03-14 21:19:42 +00:00
|
|
|
|
|
|
|
const valid = ws.filter([mh1, mh2, mh3])
|
|
|
|
expect(valid.length).to.equal(1)
|
|
|
|
expect(valid[0]).to.deep.equal(mh3)
|
|
|
|
done()
|
|
|
|
})
|
2016-03-15 15:27:22 +00:00
|
|
|
|
|
|
|
it('echo', (done) => {
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
2016-03-15 15:27:22 +00:00
|
|
|
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()
|
2016-05-08 22:58:38 +02:00
|
|
|
ws.close(done)
|
2016-03-15 15:27:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('echo with connect event and send', (done) => {
|
2016-05-17 22:37:03 +01:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
2016-03-15 15:27:22 +00:00
|
|
|
ws.createListener(mh, (conn) => {
|
|
|
|
conn.pipe(conn)
|
|
|
|
}, () => {
|
|
|
|
const message = 'Hello World!'
|
|
|
|
|
|
|
|
const conn = ws.dial(mh, {
|
2016-03-20 17:25:17 +00:00
|
|
|
ready: () => {
|
2016-03-15 15:27:22 +00:00
|
|
|
conn.send(message)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
conn.on('data', (data) => {
|
|
|
|
expect(data.toString()).to.equal(message)
|
|
|
|
conn.end()
|
2016-05-08 22:58:38 +02:00
|
|
|
ws.close(done)
|
2016-03-15 15:27:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-14 20:25:00 +00:00
|
|
|
})
|