Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from xicombd/echo-tests
Browse files Browse the repository at this point in the history
Add echo tests
  • Loading branch information
daviddias committed Mar 15, 2016
2 parents 4dcc77f + 3b81221 commit 97cea9d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
41 changes: 41 additions & 0 deletions tests/libp2p-websockets-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
})
})

0 comments on commit 97cea9d

Please sign in to comment.