Skip to content

Commit

Permalink
test(proxy): unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
towhidabsar authored and SidIcarus committed Jun 29, 2018
1 parent 9bc92d4 commit 0b25edb
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions 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 = `<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)
})
})

0 comments on commit 0b25edb

Please sign in to comment.