mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-23 05:41:47 +00:00
follow new transport and connection spec
This commit is contained in:
344
test/node.js
344
test/node.js
@ -3,98 +3,290 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
const multiaddr = require('multiaddr')
|
||||
const WSlibp2p = require('../src')
|
||||
|
||||
describe('libp2p-websockets', function () {
|
||||
this.timeout(10000)
|
||||
var ws
|
||||
const WS = require('../src')
|
||||
|
||||
describe('instantiate the transport', () => {
|
||||
it('create', (done) => {
|
||||
ws = new WSlibp2p()
|
||||
const ws = new WS()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
|
||||
it('listen and dial', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
ws.createListener(mh, (socket) => {
|
||||
expect(socket).to.exist
|
||||
socket.end()
|
||||
ws.close(done)
|
||||
}, () => {
|
||||
const conn = ws.dial(mh)
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('listen on several', (done) => {
|
||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
|
||||
const ws = new WSlibp2p()
|
||||
|
||||
ws.createListener([mh1, mh2], (socket) => {}, () => {
|
||||
ws.close(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('get observed addrs', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
ws.createListener(mh, (socket) => {
|
||||
expect(socket).to.exist
|
||||
socket.end()
|
||||
expect(socket.getObservedAddrs()).to.deep.equal([])
|
||||
ws.close(done)
|
||||
}, () => {
|
||||
const conn = ws.dial(mh)
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('filter', (done) => {
|
||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
||||
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
|
||||
const valid = ws.filter([mh1, mh2, mh3])
|
||||
expect(valid.length).to.equal(1)
|
||||
expect(valid[0]).to.deep.equal(mh3)
|
||||
it('create without new', (done) => {
|
||||
const ws = WS()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('echo', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
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)
|
||||
describe('listen', () => {
|
||||
let ws
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new WS()
|
||||
})
|
||||
|
||||
it('listen, check for callback', (done) => {
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
|
||||
it('listen, check for listening event', (done) => {
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.on('listening', () => {
|
||||
listener.close(done)
|
||||
})
|
||||
listener.listen(ma)
|
||||
})
|
||||
|
||||
it('listen, check for the close event', (done) => {
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.on('listening', () => {
|
||||
listener.on('close', done)
|
||||
listener.close()
|
||||
})
|
||||
listener.listen(ma)
|
||||
})
|
||||
|
||||
it('listen on addr with /ipfs/QmHASH', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
|
||||
it.skip('close listener with connections, through timeout', (done) => {
|
||||
// TODO `ws` closes all anyway, we need to make it not close
|
||||
// first - https://github.com/diasdavid/simple-websocket-server
|
||||
})
|
||||
|
||||
it.skip('listen on port 0', (done) => {
|
||||
// TODO port 0 not supported yet
|
||||
})
|
||||
it.skip('listen on IPv6 addr', (done) => {
|
||||
// TODO IPv6 not supported yet
|
||||
})
|
||||
|
||||
it.skip('listen on any Interface', (done) => {
|
||||
// TODO 0.0.0.0 not supported yet
|
||||
})
|
||||
|
||||
it('getAddrs', (done) => {
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.listen(ma, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('echo with connect event and send', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
ws.createListener(mh, (conn) => {
|
||||
conn.pipe(conn)
|
||||
}, () => {
|
||||
const message = 'Hello World!'
|
||||
it.skip('getAddrs on port 0 listen', (done) => {
|
||||
// TODO port 0 not supported yet
|
||||
})
|
||||
|
||||
const conn = ws.dial(mh, {
|
||||
ready: () => {
|
||||
conn.send(message)
|
||||
}
|
||||
})
|
||||
it.skip('getAddrs from listening on 0.0.0.0', (done) => {
|
||||
// TODO 0.0.0.0 not supported yet
|
||||
})
|
||||
|
||||
conn.on('data', (data) => {
|
||||
expect(data.toString()).to.equal(message)
|
||||
conn.end()
|
||||
ws.close(done)
|
||||
it.skip('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
|
||||
// TODO 0.0.0.0 or port 0 not supported yet
|
||||
})
|
||||
|
||||
it('getAddrs preserves IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
const listener = ws.createListener((conn) => {})
|
||||
listener.listen(ma, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('dial', () => {
|
||||
let ws
|
||||
let listener
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
listener = ws.createListener((conn) => {
|
||||
conn.pipe(conn)
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
})
|
||||
|
||||
afterEach((done) => {
|
||||
listener.close(done)
|
||||
})
|
||||
|
||||
it('dial on IPv4', (done) => {
|
||||
const conn = ws.dial(ma)
|
||||
conn.write('hey')
|
||||
conn.end()
|
||||
conn.on('data', (chunk) => {
|
||||
expect(chunk.toString()).to.equal('hey')
|
||||
})
|
||||
conn.on('end', done)
|
||||
})
|
||||
|
||||
it.skip('dial on IPv6', (done) => {
|
||||
// TODO IPv6 not supported yet
|
||||
})
|
||||
|
||||
it('dial on IPv4 with IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = ws.dial(ma)
|
||||
conn.write('hey')
|
||||
conn.end()
|
||||
conn.on('data', (chunk) => {
|
||||
expect(chunk.toString()).to.equal('hey')
|
||||
})
|
||||
conn.on('end', done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('filter addrs', () => {
|
||||
let ws
|
||||
|
||||
before(() => {
|
||||
ws = new WS()
|
||||
})
|
||||
|
||||
it('filter valid addrs for this transport', (done) => {
|
||||
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
||||
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
const mh4 = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
const valid = ws.filter([mh1, mh2, mh3, mh4])
|
||||
expect(valid.length).to.equal(2)
|
||||
expect(valid[0]).to.deep.equal(mh3)
|
||||
expect(valid[1]).to.deep.equal(mh4)
|
||||
done()
|
||||
})
|
||||
|
||||
it('filter a single addr for this transport', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
const valid = ws.filter(ma)
|
||||
expect(valid.length).to.equal(1)
|
||||
expect(valid[0]).to.deep.equal(ma)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('valid Connection', () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
|
||||
it('get observed addrs', (done) => {
|
||||
let dialerObsAddrs
|
||||
let listenerObsAddrs
|
||||
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist
|
||||
|
||||
conn.getObservedAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist
|
||||
dialerObsAddrs = addrs
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
conn.on('end', () => {
|
||||
conn.getObservedAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist
|
||||
listenerObsAddrs = addrs
|
||||
|
||||
listener.close(() => {
|
||||
expect(listenerObsAddrs[0]).to.deep.equal(ma)
|
||||
expect(dialerObsAddrs.length).to.equal(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
conn.resume()
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('get Peer Info', (done) => {
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist
|
||||
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.exist
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
conn.on('end', () => {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.exit
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
conn.resume()
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
it('set Peer Info', (done) => {
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist
|
||||
conn.setPeerInfo('a')
|
||||
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.not.exist
|
||||
expect(peerInfo).to.equal('a')
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
conn.setPeerInfo('b')
|
||||
|
||||
conn.on('end', () => {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.not.exist
|
||||
expect(peerInfo).to.equal('b')
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
conn.resume()
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe.skip('turbolence', () => {
|
||||
it('dialer - emits error on the other end is terminated abruptly', (done) => {})
|
||||
it('listener - emits error on the other end is terminated abruptly', (done) => {})
|
||||
})
|
||||
|
Reference in New Issue
Block a user