blob: 1c3c187d9896655cb6ed0518df7b1ea90e53cc7d [file] [log] [blame]
const request = require('request');
exports.checkForDocumentCreated = function checkForDocumentCreated (url, timeout, cb) {
const timeOutId = setTimeout(() => {
throw new Error('timeout waiting for doc to appear (' + url + ')');
}, timeout);
const intervalId = setInterval(() => {
request(url, (er, res, body) => {
if (res && /^2..$/.test(res.statusCode)) {
clearTimeout(timeOutId);
clearInterval(intervalId);
cb(null);
}
});
}, 1000);
};
exports.checkForDatabaseCreated = function checkForDatabaseCreated (couchUrl, databaseName, timeout, cb) {
const timeOutId = setTimeout(() => {
throw new Error('Timeout ('+timeout+') waiting for db to appear');
}, timeout);
const intervalId = setInterval(() => {
request(couchUrl + '/_all_dbs', function (er, res, body) {
if (body) {
const reg = new RegExp('"' + databaseName + '"', 'g');
if (reg.test(body)) {
clearTimeout(timeOutId);
clearInterval(intervalId);
cb(null);
}
}
});
}, 1000);
};
exports.checkForDocumentDeleted = function checkForDocumentDeleted (couchUrl, timeout, cb) {
const timeOutId = setTimeout(() => {
throw new Error('timeout waiting for doc to be deleted');
}, timeout);
const intervalId = setInterval(() => {
request(couchUrl, function (er, res, body) {
if (res.statusCode === 404) {
clearTimeout(timeOutId);
clearInterval(intervalId);
cb(null);
}
});
}, 1000);
};