From c3e53c7df8e88ece941377cde41951668bf1015a Mon Sep 17 00:00:00 2001 From: Klemek Date: Sun, 4 Apr 2021 23:24:33 +0200 Subject: [PATCH] more unit tests --- src/bot_detector.js | 20 ++++++----- test/bot_detector.test.js | 73 +++++++++++++++++++++++++++++++-------- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/bot_detector.js b/src/bot_detector.js index 463fed4..4e05e4a 100644 --- a/src/bot_detector.js +++ b/src/bot_detector.js @@ -16,21 +16,23 @@ module.exports = (config) => { }; const fetchList = (cb) => { - const file = fs.createWriteStream(config['robots']['list_file']); https.get(config['robots']['list_url'], (res) => { - res.pipe(file); - file.on('finish', () => { - file.close(cb); - }); + if (res.statusCode !== 200) { + cb(res.statusCode); + } else { + const file = fs.createWriteStream(config['robots']['list_file']); + res.pipe(file); + file.on('finish', () => { + file.close(cb); + }); + } }).on('error', (err) => { - file.close(() => { - cb(err.message); - }); + cb(err.message); }); }; const readFile = (cb) => { - fs.readFile(config['robots']['list_file'], (err, data) => { + fs.readFile(config['robots']['list_file'], { encoding: 'utf-8' }, (err, data) => { if (err) { cb(err, undefined); } else { diff --git a/test/bot_detector.test.js b/test/bot_detector.test.js index da4fc69..60855a5 100644 --- a/test/bot_detector.test.js +++ b/test/bot_detector.test.js @@ -5,7 +5,7 @@ const dataDir = 'test_data'; const config = { robots: { - list_url: 'https://raw.githubusercontent.com/atmire/COUNTER-Robots/master/COUNTER_Robots_list.json', + list_url: '', list_file: `${dataDir}/robots_list.json`, }, }; @@ -24,24 +24,69 @@ afterAll(() => { 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('load()', () => { + test('success', (done) => { + config.robots = { + list_url: 'https://raw.githubusercontent.com/atmire/COUNTER-Robots/master/COUNTER_Robots_list.json', + list_file: `${dataDir}/robots_list_success.json`, + }; + 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++; + }); + }); + + test('fetch and file failure', (done) => { + let count = 0; + config.robots = { + list_url: 'https://blog.klemek.fr/invalid.json', + list_file: `${dataDir}/robots_list_fail_1.json`, + }; + botDetector.load((status) => { + expect(status).toBe(count === 0 ? botDetector.status.FETCH_ERROR : botDetector.status.READ_ERROR); + if (count > 0) { + done(); + } + count++; + }); + }); + + test('fetch failure and file ok', (done) => { + let count = 0; + config.robots = { + list_url: 'https://blog.klemek.fr/invalid.json', + list_file: `${dataDir}/robots_list_fail_2.json`, + }; + fs.writeFile(config.robots.list_file, '[]\n', { encoding: 'utf-8' }, () => { + botDetector.load((status) => { + expect(status).toBe(count === 0 ? botDetector.status.FETCH_ERROR : botDetector.status.READ_OK); + if (count > 0) { + done(); + } + count++; + }); + }); }); }); + describe('handle()', () => { beforeAll((done) => { - botDetector.load((status) => { - if (status === botDetector.status.READ_OK) { - done(); - } + config.robots = { + list_url: 'https://blog.klemek.fr/invalid.json', + list_file: `${dataDir}/robots_list_fake.json`, + }; + fs.writeFile(config.robots.list_file, '[{"pattern":"bot"}]\n', { encoding: 'utf-8' }, () => { + botDetector.load((status) => { + if (status !== botDetector.status.FETCH_ERROR) { + done(); + } + }); }); });