more unit tests
This commit is contained in:
+6
-4
@@ -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 {
|
||||||
|
|||||||
@@ -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();
|
||||||
@@ -36,12 +41,52 @@ test('load()', (done) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('handle()', () => {
|
test('fetch and file failure', (done) => {
|
||||||
beforeAll((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) => {
|
botDetector.load((status) => {
|
||||||
if (status === botDetector.status.READ_OK) {
|
expect(status).toBe(count === 0 ? botDetector.status.FETCH_ERROR : botDetector.status.READ_ERROR);
|
||||||
|
if (count > 0) {
|
||||||
done();
|
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) => {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user