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

Add echo tests #2

Merged
merged 1 commit into from
Mar 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
})
})
})