diff --git a/src/bot_detector.js b/src/bot_detector.js index 4291a7e..463fed4 100644 --- a/src/bot_detector.js +++ b/src/bot_detector.js @@ -16,14 +16,16 @@ module.exports = (config) => { }; const fetchList = (cb) => { + const file = fs.createWriteStream(config['robots']['list_file']); https.get(config['robots']['list_url'], (res) => { - const file = fs.createWriteStream(config['robots']['list_file']); res.pipe(file); file.on('finish', () => { file.close(cb); }); }).on('error', (err) => { - cb(err.message); + file.close(() => { + cb(err.message); + }); }); }; @@ -45,9 +47,11 @@ module.exports = (config) => { fetchList((err) => { cb(err ? _this.status.FETCH_ERROR : _this.status.FETCH_OK, err); readFile((err, data) => { - _this.count = data.length; - _this.regex = new RegExp('(' + data.filter(v => v['pattern']).map(v => v['pattern']) - .join('|') + ')'); + if (!err) { + _this.count = data.length; + _this.regex = new RegExp('(' + data.filter(v => v['pattern']).map(v => v['pattern']) + .join('|') + ')'); + } cb(err ? _this.status.READ_ERROR : _this.status.READ_OK, err); }); }); diff --git a/test/bot_detector.test.js b/test/bot_detector.test.js new file mode 100644 index 0000000..da4fc69 --- /dev/null +++ b/test/bot_detector.test.js @@ -0,0 +1,71 @@ +const fs = require('fs'); +const utils = require('./test_utils'); + +const dataDir = 'test_data'; + +const config = { + robots: { + list_url: 'https://raw.githubusercontent.com/atmire/COUNTER-Robots/master/COUNTER_Robots_list.json', + list_file: `${dataDir}/robots_list.json`, + }, +}; + + +beforeAll(() => { + utils.deleteFolderSync(dataDir); + fs.mkdirSync(dataDir); +}); + +afterAll(() => { + if (fs.existsSync(dataDir)) { + utils.deleteFolderSync(dataDir); + } +}); + +const botDetector = require('../src/bot_detector')(config); + +test('load()', (done) => { + let count = 0; + botDetector.load((status, err) => { + expect(err).not.toBeDefined(); + expect(status).toBe(count === 0 ? botDetector.status.FETCH_OK : botDetector.status.READ_OK); + if (count > 0) { + done(); + } + count++; + }); +}); + +describe('handle()', () => { + beforeAll((done) => { + botDetector.load((status) => { + if (status === botDetector.status.READ_OK) { + done(); + } + }); + }); + + test('not bot', (done) => { + const req = { + headers: { + 'user-agent': 'my user agent', + }, + }; + botDetector.handle(req, null, () => { + expect(req.isRobot).toBeFalsy(); + done(); + }); + }); + + test('bot', (done) => { + const req = { + headers: { + 'user-agent': 'bot', + }, + }; + botDetector.handle(req, null, () => { + expect(req.isRobot).toBeTruthy(); + done(); + }); + }); +});