Skip to content

Commit

Permalink
Merge pull request #103 from earldouglas/offline-mode
Browse files Browse the repository at this point in the history
Add tests for offline mode Web request blocking
  • Loading branch information
HardikJ committed Jan 5, 2015
2 parents 5e9ee3f + a36d4c4 commit 5c81803
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/filters/bucket/pagecontent.js
Expand Up @@ -186,7 +186,7 @@ PCBucket.prototype.getLatestFormat = function(restbase, req) {
}
};
} else {
return { status: 404 };
return apiRes;
}
});
};
Expand Down
5 changes: 0 additions & 5 deletions lib/filters/global/actions.js
Expand Up @@ -9,12 +9,7 @@ module.exports = {
'/v1/{domain}/_svc/action/query': {
all: {
request_handler: function(restbase, req) {
if (restbase._options.conf.offline) {
throw new Error("We are offline, you are trying to fallback to the dynamic API.");
}

var rp = req.params;

req.uri = 'http://' + rp.domain + '/w/api.php';
var body = req.body || req.query;
body.action = 'query';
Expand Down
7 changes: 6 additions & 1 deletion lib/filters/global/web.js
Expand Up @@ -14,7 +14,12 @@ function handleAll (restbase, req) {
'http://parsoid-lb.eqiad.wikimedia.org/v2/en.wikipedia.org/');

if (restbase._options.conf.offline) {
throw new Error("We are offline, you are tring to fallback to dynamic api");
return Promise.resolve({
status: 500,
body: {
error: 'We are offline, but your request needs to be serviced online.'
}
});
}

//yield requestPr(req);
Expand Down
67 changes: 63 additions & 4 deletions test/main.js
Expand Up @@ -35,9 +35,9 @@ function startRestbase(offline) {
return restbase({
logging: {
name: 'restbase-tests',
level: 'warn',
offline: offline
}
level: offline ? 'fatal' : 'warn', // hide warnings during offline tests
},
offline: offline
}).then(function(server){
stopRestbase =
function () {
Expand All @@ -63,7 +63,66 @@ describe('Offline mode feature tests after a server restart', function() {
this.timeout(20000);
before(function () { return startRestbase(true); });

require('./features/pagecontent/idempotent')(config);
var assert = require('./utils/assert.js');
var preq = require('preq');

var offlineMessage = 'We are offline, but your request needs to be serviced online.';

describe('offline mode', function() {
it('should allow content revision retrieval from storage', function() {
this.timeout(20000);
return preq.get({
uri: config.bucketURL + '/Idempotent/html/76f22880-362c-11e4-9234-0123456789ab'
})
.then(function(res) {
assert.deepEqual(res.status, 200);
});
});
it('should not allow latest content retrieval from storage', function() {
this.timeout(20000);
return assert.fails(
preq.get({
uri: config.bucketURL + '/Idempotent/html'
}),
function(e) {
assert.deepEqual(e.status, 500);
assert.deepEqual(e.body.error, offlineMessage);
}
);
});
it('should prevent content retrieval from the Web', function() {
this.timeout(20000);
return assert.fails(
preq.get({
uri: config.baseURL + '/_svc/parsoid/Monads/1'
}),
function (e) {
assert.deepEqual(e.status, 500);
assert.deepEqual(e.body.error, offlineMessage);
}
);
});
it('should prevent query submission over the Web', function() {
this.timeout(20000);
return assert.fails(
preq.post({
uri: config.hostPort + '/v1/en.wikipedia.org/_svc/action/query',
headers: { host: 'en.wikipedia.org' },
body: {
format: 'json',
action: 'query',
titles: 'Main Page',
prop: 'revisions',
rvprop: 'content'
}
}),
function (e) {
assert.deepEqual(e.status, 500);
assert.deepEqual(e.body.error, offlineMessage);
}
);
});
});

after(function () { return stopRestbase(); });
});

0 comments on commit 5c81803

Please sign in to comment.