Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bc92d4
commit 0b25edb
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = `<proxy${cmd} success=${success}><error id="${id}"><message>${msg}</message></error></proxy${cmd}>` | ||
return `<?xml version="1.0" encoding="UTF-8"?>\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) | ||
}) | ||
}) |