Skip to content

Commit

Permalink
Add failed test case and reconnecting when the client is terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
yhsiang committed Feb 14, 2016
1 parent ea7407c commit 3a6f684
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
24 changes: 14 additions & 10 deletions src/lib.js
Expand Up @@ -2,16 +2,16 @@ import request from 'request-promise'
import WebSocket from 'ws'
import log from 'cologger'

export async function getToken() {
async function getToken() {
let res = JSON.parse(await request({
method: 'POST',
url: `https://www.irccloud.com/chat/auth-formtoken`,
}))
if (!res.success) throw(res)
if (!res.success) return Promise.reject(res)
return res.token
}

export async function login({token, user}) {
async function login({token, user}) {
let res = JSON.parse(await request({
method: 'POST',
form: Object.assign({token}, user),
Expand All @@ -20,11 +20,11 @@ export async function login({token, user}) {
'x-auth-formtoken':token
},
}))
if (!res.success) throw(res)
if (!res.success) return Promise.reject(res)
return res
}

export function backlog({url, streamid, session}) {
function backlog({url, streamid, session}) {
return request({
url: `https://www.irccloud.com${url}?streamid=${streamid}`,
headers: {
Expand All @@ -34,7 +34,7 @@ export function backlog({url, streamid, session}) {
})
}

export function createWebSocket({ websocket_host, session, websocket_port}) {
function createWebSocket({ websocket_host, session, websocket_port}) {
let streamid, url = `wss://${websocket_host}/`
if (websocket_port) url = `ws://${websocket_host}:${websocket_port}/`
const ws = new WebSocket(url, {
Expand All @@ -43,7 +43,10 @@ export function createWebSocket({ websocket_host, session, websocket_port}) {
'Cookie': `session=${session}`,
}
})
ws.on('message', (message) => {
ws.on('error', (error) => {
log.error(error.message)
})
ws.on('message', message => {
const ws_res = JSON.parse(message)
if (ws_res.type === 'header') {
streamid = ws_res.streamid
Expand All @@ -57,9 +60,10 @@ export function createWebSocket({ websocket_host, session, websocket_port}) {
if (ws_res.type === 'buffer_msg') {
log.info(`[${ws_res.chan}] <${ws_res.from}> ${ws_res.msg}`)
}
if (ws_res.type === 'close') {
ws.send('close')
}
})
ws.on('close', () => {
log.info(`Reconnecting...`)
return createWebSocket({websocket_host, session, websocket_port})
})
return ws
}
Expand Down
49 changes: 40 additions & 9 deletions test/lib-test.js
Expand Up @@ -7,26 +7,38 @@ import * as irccloud from '../src/lib'
import log from 'cologger'
//
const wss = new Server({port: 8080});
let once = 1;
wss.on('connection', socket => {
socket.send(JSON.stringify({type: 'header', streamid: 'sacasc'}))
socket.send(JSON.stringify({type: 'oob_include', url: '/chat/backlog/123'}))
socket.send(JSON.stringify({type: 'buffer_msg', chan: '#ma19', from: 'yhsiang', msg: 'test'}))
socket.send(JSON.stringify({type: 'close'}))
socket.on('message', (message) => {
if (once === 1) {
socket.send(JSON.stringify({type: 'header', streamid: 'sacasc'}))
socket.send(JSON.stringify({type: 'oob_include', url: '/chat/backlog/123'}))
socket.send(JSON.stringify({type: 'buffer_msg', chan: '#ma19', from: 'yhsiang', msg: 'test'}))
wss.clients[0].close()
once++
} else {
wss.close()
})
}
})


nock('https://www.irccloud.com')
.post('/chat/auth-formtoken')
.reply(200, {
_reqid: 0,
success: false,
message: 'unhandle_request' })
.post('/chat/login')
.reply(200, {
_reqid: 0,
success: false,
message: 'invalid_form_token' })
.post('/chat/auth-formtoken')
.times(2)
.reply(200, {
_reqid: 0,
success: true,
token: 'token' })
.post('/chat/login')
.times(2)
.reply(200, {
_reqid: 0,
success: true,
Expand All @@ -39,10 +51,29 @@ nock('https://www.irccloud.com')
success: true,
})

test('get token failed test', t => {
t.plan(1)
irccloud.connect({
email: 'EMAIL',
password: 'PASSWORD',
}).catch(err => {
t.notOk(err.success, 'should failed')
})
})

test('login failed test', t => {
t.plan(1)
irccloud.connect({
email: 'EMAIL',
password: 'PASSWORD',
}).catch(err => {
t.notOk(err.success, 'should failed')
})
})

test('irccloud-cli test', t => {
t.plan(1)
let mock = sinon.mock(log)
let success = mock.expects('success').twice()
let success = sinon.mock(log).expects('success').twice()
irccloud.connect({
email: 'EMAIL',
password: 'PASSWORD',
Expand Down

0 comments on commit 3a6f684

Please sign in to comment.