Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1175) - Handle HTTP codes other than 200/404 during repl #1194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/replicate.js
Expand Up @@ -65,10 +65,14 @@ function fetchCheckpoint(src, target, id, callback) {
target.get(id, function (err, targetDoc) {
if (err && err.status === 404) {
callback(null, 0);
} else if (err) {
callback(err);
} else {
src.get(id, function (err, sourceDoc) {
if (err && err.status === 404 || targetDoc.last_seq !== sourceDoc.last_seq) {
if (err && err.status === 404 || (!err && (targetDoc.last_seq !== sourceDoc.last_seq))) {
callback(null, 0);
} else if (err) {
callback(err);
} else {
callback(null, sourceDoc.last_seq);
}
Expand Down
97 changes: 97 additions & 0 deletions tests/test.issue1175.js
@@ -0,0 +1,97 @@
"use strict";

if (typeof module !== undefined && module.exports) {
var PouchDB = require('../lib');
var testUtils = require('./test.utils.js');
}


QUnit.module('replication-http-errors:');

function MockDatabase(statusCodeToReturn, dataToReturn) {
this.id = function () {
return 123;
};

this.get = function (id, callback) {
setTimeout(function () {
if (statusCodeToReturn !== 200) {
callback({status: statusCodeToReturn}, dataToReturn)
} else {
callback(null, dataToReturn)
}
}, 0);
};

this.changes = function (opts) {
opts.complete();
return [];
}
}

function getCallback(expectError) {
// returns a function which expects to be called within a certain time. Fails the test otherwise

var maximumTimeToWait = 50;
var _hasBeenCalled;
var _result;
var _err;

var callback = function (err, result) {
_hasBeenCalled = true;
_result = result;
_err = err;
};

var timeOutCallback = function () {
ok(_hasBeenCalled , 'callback has been called');
ok(expectError || !_err , 'error expectation fulfilled');
start();
};

setTimeout(timeOutCallback, maximumTimeToWait);

return callback;
}

asyncTest("Initial replication is ok if source returns HTTP 404", function () {
var source = new MockDatabase(404, null);
var target = new MockDatabase(200, {});
PouchDB.replicate(source, target, {}, getCallback());
});

asyncTest("Initial replication is ok if target returns HTTP 404", function () {
var source = new MockDatabase(200, {});
var target = new MockDatabase(404, null);
PouchDB.replicate(source, target, {}, getCallback());
});

asyncTest("Initial replication is ok if source and target return HTTP 200", function () {
var source = new MockDatabase(200, {});
var target = new MockDatabase(200, {});
PouchDB.replicate(source, target, {}, getCallback());
});

asyncTest("Initial replication returns err if source returns HTTP 500", function () {
var source = new MockDatabase(500, null);
var target = new MockDatabase(200, {});
PouchDB.replicate(source, target, {}, getCallback(true));
});

asyncTest("Initial replication returns err if target returns HTTP 500", function () {
var source = new MockDatabase(200, {});
var target = new MockDatabase(500, null);
PouchDB.replicate(source, target, {}, getCallback(true));
});

asyncTest("Initial replication returns err if target and source return HTTP 500", function () {
var source = new MockDatabase(500, null);
var target = new MockDatabase(500, null);
PouchDB.replicate(source, target, {}, getCallback(true));
});

asyncTest("Subsequent replication returns err if source return HTTP 500", function () {
var source = new MockDatabase(500, null);
var target = new MockDatabase(200, {last_seq: 456});
PouchDB.replicate(source, target, {}, getCallback(true));
});