79 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-03-15 18:59:32 +00:00
/* eslint-env mocha */
2016-03-23 16:23:10 +01:00
'use strict'
2016-03-15 18:59:32 +00:00
const { expect } = require('aegir/utils/chai')
2017-03-21 14:32:59 +00:00
2016-03-15 18:59:32 +00:00
const multiaddr = require('multiaddr')
const pipe = require('it-pipe')
const goodbye = require('it-goodbye')
const { collect, take } = require('streaming-iterables')
const uint8ArrayFromString = require('uint8arrays/from-string')
2016-08-11 14:50:44 +02:00
const WS = require('../src')
2016-03-15 18:59:32 +00:00
const mockUpgrader = {
upgradeInbound: maConn => maConn,
upgradeOutbound: maConn => maConn
}
describe('libp2p-websockets', () => {
2021-04-09 17:49:30 +03:00
const ma = new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
let ws
2016-08-11 14:50:44 +02:00
let conn
2016-03-15 18:59:32 +00:00
beforeEach(async () => {
ws = new WS({ upgrader: mockUpgrader })
conn = await ws.dial(ma)
2016-03-15 18:59:32 +00:00
})
it('echo', async () => {
const message = uint8ArrayFromString('Hello World!')
const s = goodbye({ source: [message], sink: collect })
2016-03-22 22:14:24 +00:00
const results = await pipe(s, conn, s)
expect(results).to.eql([message])
2016-08-11 14:50:44 +02:00
})
2016-03-22 22:14:24 +00:00
it('should filter out no DNS websocket addresses', function () {
const ma1 = multiaddr('/ip4/127.0.0.1/tcp/80/ws')
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/443/wss')
const ma3 = multiaddr('/ip6/::1/tcp/80/ws')
const ma4 = multiaddr('/ip6/::1/tcp/443/wss')
const valid = ws.filter([ma1, ma2, ma3, ma4])
expect(valid.length).to.equal(0)
})
2016-08-11 14:50:44 +02:00
describe('stress', () => {
it('one big write', async () => {
const rawMessage = new Uint8Array(1000000).fill('a')
2016-03-22 22:14:24 +00:00
const s = goodbye({ source: [rawMessage], sink: collect })
const results = await pipe(s, conn, s)
expect(results).to.eql([rawMessage])
2016-08-11 14:50:44 +02:00
})
2016-03-22 22:14:24 +00:00
it('many writes', async function () {
this.timeout(10000)
2016-08-11 14:50:44 +02:00
const s = goodbye({
source: pipe(
{
[Symbol.iterator] () { return this },
next: () => ({ done: false, value: uint8ArrayFromString(Math.random().toString()) })
},
take(20000)
2016-08-11 14:50:44 +02:00
),
sink: collect
2016-03-22 22:14:24 +00:00
})
2016-08-11 14:50:44 +02:00
const result = await pipe(s, conn, s)
expect(result).to.have.length(20000)
2016-03-22 22:14:24 +00:00
})
})
it('.createServer throws in browser', () => {
expect(new WS({ upgrader: mockUpgrader }).createListener).to.throw()
})
})