more unit tests

This commit is contained in:
Klemek
2021-04-04 23:24:33 +02:00
parent 404b02830d
commit c3e53c7df8
2 changed files with 70 additions and 23 deletions
+11 -9
View File
@@ -16,21 +16,23 @@ module.exports = (config) => {
}; };
const fetchList = (cb) => { const fetchList = (cb) => {
const file = fs.createWriteStream(config['robots']['list_file']);
https.get(config['robots']['list_url'], (res) => { https.get(config['robots']['list_url'], (res) => {
res.pipe(file); if (res.statusCode !== 200) {
file.on('finish', () => { cb(res.statusCode);
file.close(cb); } else {
}); const file = fs.createWriteStream(config['robots']['list_file']);
res.pipe(file);
file.on('finish', () => {
file.close(cb);
});
}
}).on('error', (err) => { }).on('error', (err) => {
file.close(() => { cb(err.message);
cb(err.message);
});
}); });
}; };
const readFile = (cb) => { const readFile = (cb) => {
fs.readFile(config['robots']['list_file'], (err, data) => { fs.readFile(config['robots']['list_file'], { encoding: 'utf-8' }, (err, data) => {
if (err) { if (err) {
cb(err, undefined); cb(err, undefined);
} else { } else {
+59 -14
View File
@@ -5,7 +5,7 @@ const dataDir = 'test_data';
const config = { const config = {
robots: { robots: {
list_url: 'https://raw.githubusercontent.com/atmire/COUNTER-Robots/master/COUNTER_Robots_list.json', list_url: '',
list_file: `${dataDir}/robots_list.json`, list_file: `${dataDir}/robots_list.json`,
}, },
}; };
@@ -24,24 +24,69 @@ afterAll(() => {
const botDetector = require('../src/bot_detector')(config); const botDetector = require('../src/bot_detector')(config);
test('load()', (done) => { describe('load()', () => {
let count = 0; test('success', (done) => {
botDetector.load((status, err) => { config.robots = {
expect(err).not.toBeDefined(); list_url: 'https://raw.githubusercontent.com/atmire/COUNTER-Robots/master/COUNTER_Robots_list.json',
expect(status).toBe(count === 0 ? botDetector.status.FETCH_OK : botDetector.status.READ_OK); list_file: `${dataDir}/robots_list_success.json`,
if (count > 0) { };
done(); let count = 0;
} botDetector.load((status, err) => {
count++; 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()', () => { describe('handle()', () => {
beforeAll((done) => { beforeAll((done) => {
botDetector.load((status) => { config.robots = {
if (status === botDetector.status.READ_OK) { list_url: 'https://blog.klemek.fr/invalid.json',
done(); 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();
}
});
}); });
}); });