From 3b812215668099b3e495b4a57d3b1be801dc398f Mon Sep 17 00:00:00 2001 From: Francisco Baio Dias Date: Tue, 15 Mar 2016 15:27:22 +0000 Subject: [PATCH] Add echo tests --- src/index.js | 4 ++-- tests/libp2p-websockets-test.js | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index f93b17f..b215bb7 100644 --- a/src/index.js +++ b/src/index.js @@ -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] } diff --git a/tests/libp2p-websockets-test.js b/tests/libp2p-websockets-test.js index f75d14a..59f347f 100644 --- a/tests/libp2p-websockets-test.js +++ b/tests/libp2p-websockets-test.js @@ -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() + }) + }) + }) + }) })