-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #765 from prey/verify-websocket-arrival-message
add logic for send ack to ws ack
- Loading branch information
Showing
4 changed files
with
188 additions
and
5 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,28 @@ | ||
const storage = require('./utils/storage'); | ||
const ackType = 'ack'; | ||
|
||
const existKeyAckInJson = (json) => { | ||
//eslint-disable-next-line no-prototype-builtins | ||
if (json.hasOwnProperty('ack_id')) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
const existKeyIdInJson = (json) => { | ||
//eslint-disable-next-line no-prototype-builtins | ||
if (json.hasOwnProperty('id')) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
exports.processAck = (json, cb) => { | ||
if (!existKeyAckInJson(json)) { | ||
return cb(new Error('there is no key ack_id in the json')); | ||
} | ||
return cb(null, { | ||
ack_id: json.ack_id, | ||
type: ackType, | ||
id: existKeyIdInJson(json) ? json.id : '' | ||
}); | ||
}; |
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
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
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,54 @@ | ||
const { | ||
describe, it, before, after, | ||
} = require('mocha'); | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
const ack = require('../../../lib/agent/ack'); | ||
const websocket = require('../../../lib/agent/plugins/control-panel/websockets'); | ||
|
||
describe('validation ack acknowledge', () => { | ||
describe('validation if json is valid', () => { | ||
it('there is no key ack_id in the json', (done) => { | ||
ack.processAck({}, (err) => { | ||
should.exist(err); | ||
err.message.should.containEql('there is no key ack_id in the json'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('validation if json is valid', () => { | ||
it('process ack and register', (done) => { | ||
ack.processAck({ | ||
ack_id: '3', type: 'ack', id: '1234', | ||
}, (err, registeredJson) => { | ||
should.not.exist(err); | ||
JSON.stringify(registeredJson).should.equal(JSON.stringify({ | ||
ack_id: '3', type: 'ack', id: '1234', | ||
})); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('send to server ack', () => { | ||
let websocketStub = null; | ||
websocket.responsesAck.push({ | ||
ack_id: '3', | ||
type: 'ack', | ||
id: '1', | ||
sent: false, | ||
retries: 0, | ||
}); | ||
before(() => { | ||
websocketStub = sinon.stub(websocket, 'sendAckToServer').callsFake(() => ''); | ||
}); | ||
|
||
after(() => { | ||
websocketStub.restore(); | ||
}); | ||
|
||
it('notify ack', (done) => { | ||
websocket.notifyAck('3,', 'ack', false, 0); | ||
done(); | ||
}); | ||
}); | ||
}); |