diff --git a/src/test/proxy.ts b/src/test/proxy.ts new file mode 100644 index 00000000..42bb5850 --- /dev/null +++ b/src/test/proxy.ts @@ -0,0 +1,91 @@ +import { assert } from 'chai' +import { ProxyConnect } from '../proxyConnect' +import { encode } from 'iconv-lite' + +describe('ProxyConnect', () => { + function _err(cmd: string, success: number, id: number, msg: string): string { + let err = `${msg}` + return `\n${err}` + } + + let host = 'host' + let port = 9001 + const encoding = 'iso-8859-1' + const duplicateKeyString = _err('init', 0, 0, 'IDE Key already exists') + const deregisterString = _err('stop', 1, 0, '') + const registerString = _err('init', 1, 0, '') + let proxyConn: ProxyConnect + const timeoutError = `Error: Timeout connecting to ${host}:${port}` + const resovleError = `Failure to resolve ${host}` + beforeEach(() => { + proxyConn = new ProxyConnect(host, port, 1, undefined, 3000) + proxyConn._socket.connect = function() { + console.log('') + } + proxyConn._socket.on('timeout', () => { + return + }) + }) + + it('should be defined', (done: MochaDone) => { + proxyConn.on('error', (msg: string) => { + assert.equal(msg, timeoutError) + done() + }) + assert.exists(proxyConn) + proxyConn._socket.emit('error', new Error(`Timeout connecting to ${host}:${port}`)) + }) + + it('should fail if proxy is unreachable', (done: MochaDone) => { + proxyConn.on('error', (msg: string) => { + assert.equal(msg, resovleError) + done() + }) + proxyConn.sendProxyInitCommand() + proxyConn._socket.emit('lookup', new Error('Failure to resolve'), host) + }) + + it('should throw an error for duplicate IDE key', (done: MochaDone) => { + proxyConn.on('error', (err: Error) => { + assert.equal('IDE Key already exists', err.message) + done() + }) + proxyConn.responseStrategy(encode(duplicateKeyString, encoding)) + }) + it('should request registration from proxy', (done: MochaDone) => { + proxyConn.on('info', (msg: string) => { + assert.equal(msg, `Registering 'vsc' with proxy @ ${host}:${port}`) + done() + }) + + proxyConn.sendProxyInitCommand() + }) + + it('should register with proxy', (done: MochaDone) => { + proxyConn.on('response', (msg: string) => { + assert.equal(msg, 'Registration successful') + done() + }) + + proxyConn.sendProxyInitCommand() + proxyConn._socket.emit('data', registerString) + }) + it('should request deregistration from proxy', (done: MochaDone) => { + proxyConn.on('info', (msg: string) => { + assert.equal(msg, `Deregistering 'vsc' with proxy @ ${host}:${port}`) + done() + }) + + proxyConn.sendProxyStopCommand() + }) + + it('should de-register from proxy', (done: MochaDone) => { + proxyConn.on('response', (msg: string) => { + assert.equal(msg, 'Deregistration successful') + done() + }) + + proxyConn.sendProxyStopCommand() + proxyConn._socket.emit('data', deregisterString) + }) +})