emailjs-tcp-socket
Version:
This shim brings the W3C Raw Socket API to node.js and Chromium. Its purpose is to enable apps to use the same api in Firefox OS, Chrome OS, and on the server.
77 lines (67 loc) • 2.11 kB
JavaScript
const echo = require('../echo')
const TCPSocket = require('../../').default
const { PORT_NET, PORT_STARTTLS, PORT_TLS } = require('../constants')
const { startServers, stopServers } = echo()
const a2s = arr => String.fromCharCode.apply(null, new Uint8Array(arr))
const s2a = str => new Uint8Array(str.split('').map(char => char.charCodeAt(0))).buffer
describe('TCP node shim integration tests', function () {
const payload = 'the.payload.woopwoop!'
let received
before(() => startServers())
beforeEach(() => {
received = ''
})
after(() => stopServers())
describe('tcp', function () {
it('should open, read, write, and close', function (done) {
const socket = TCPSocket.open('localhost', PORT_NET)
socket.onopen = () => { socket.send(s2a(payload)) }
socket.onclose = () => {
expect(received).to.equal(payload)
done()
}
socket.ondata = ({ data }) => {
received += a2s(data)
if (received === payload) {
socket.close()
}
}
})
})
describe('tls', function () {
it('should open, read, write, and close', function (done) {
const useSecureTransport = true
var socket = TCPSocket.open('localhost', PORT_TLS, { useSecureTransport })
socket.onopen = () => { socket.send(s2a(payload)) }
socket.onclose = () => {
expect(received).to.equal(payload)
done()
}
socket.ondata = ({ data }) => {
received += a2s(data)
if (received === payload) {
socket.close()
}
}
})
})
describe.skip('starttls', function () {
it('should open, read, write, and close', function (done) {
var socket = TCPSocket.open('localhost', PORT_STARTTLS)
socket.onopen = () => {
socket.upgradeToSecure()
socket.send(s2a(payload))
}
socket.onclose = () => {
expect(received).to.equal(payload)
done()
}
socket.ondata = ({ data }) => {
received += a2s(data)
if (received === payload) {
socket.close()
}
}
})
})
})