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
+6 -4
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) => {
if (res.statusCode !== 200) {
cb(res.statusCode);
} else {
const file = fs.createWriteStream(config['robots']['list_file']);
res.pipe(file); res.pipe(file);
file.on('finish', () => { file.on('finish', () => {
file.close(cb); 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 {
+48 -3
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,7 +24,12 @@ afterAll(() => {
const botDetector = require('../src/bot_detector')(config); const botDetector = require('../src/bot_detector')(config);
test('load()', (done) => { 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; let count = 0;
botDetector.load((status, err) => { botDetector.load((status, err) => {
expect(err).not.toBeDefined(); expect(err).not.toBeDefined();
@@ -34,16 +39,56 @@ test('load()', (done) => {
} }
count++; 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) => {
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) => { botDetector.load((status) => {
if (status === botDetector.status.READ_OK) { if (status !== botDetector.status.FETCH_ERROR) {
done(); done();
} }
}); });
}); });
});
test('not bot', (done) => { test('not bot', (done) => {
const req = { const req = {