Skip to content

Commit

Permalink
Pass headers through to request.
Browse files Browse the repository at this point in the history
  • Loading branch information
danw committed Aug 6, 2012
1 parent 2b5c6c2 commit 97ba7dd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
48 changes: 43 additions & 5 deletions lib/stalka.js
Expand Up @@ -7,9 +7,9 @@ module.exports = stalka();
/**
* Module dependencies.
*/
var request = require('request');
var querystring = require('querystring');
var async = require('async');
var request = require('request');
var querystring = require('querystring');
var async = require('async');

/**
* Constructor
Expand All @@ -19,13 +19,39 @@ function stalka() {
};

function Stalka() {
this.running = false;
};

Stalka.prototype.stop = function() {
this.running = false;
}

Stalka.prototype.resume = function() {
this.start(this.dbUri, this.changesWriter, this.options, this.mainCallback);
}

Stalka.prototype.isRunning = function() {
return this.running;
}

Stalka.prototype.getLastSeq = function(callback) {
var db = require('nano')(this.dbUri);
this.readSequence(db, function(err, sequenceDoc) {
callback(err, sequenceDoc);
});
}

Stalka.prototype.registerStatsWriter = function(statsWriter) {
this.statsWriter = statsWriter;
};

Stalka.prototype.start = function (dbUri, changesWriter, options, mainCallback) {
// Store so that we can use them to resume
this.dbUri = dbUri;
this.changesWriter = changesWriter;
this.options = options;
this.mainCallback = mainCallback;

this.running = true;

var db = require('nano')(dbUri);
Expand Down Expand Up @@ -76,7 +102,13 @@ Stalka.prototype.start = function (dbUri, changesWriter, options, mainCallback)
callback(err);
return;
} else {
this.lastSequence = sequenceDoc.lastSequence;
sequenceDoc._rev = newSeqDoc.rev;

if (self.statsWriter) {
self.statsWriter(this.lastSequence);
}

callback();
}
});
Expand Down Expand Up @@ -118,11 +150,17 @@ Stalka.prototype.updateSequence = function(db, sequenceDoc, callback) {

Stalka.prototype.readChanges = function(dbUri, options, callback) {
options = options || {};

if (options.headers) {
var headers = options.headers;
delete options['headers'];
}
headers = headers || {};

if (!options.feed) {
options.feed = 'longpoll';
}

request(dbUri + '/_changes?' + querystring.stringify(options), function (error, response, body) {
request({url: dbUri + '/_changes?' + querystring.stringify(options), headers: headers}, function (error, response, body) {
if (error) {
callback(error, null);
} else {
Expand Down
26 changes: 18 additions & 8 deletions test/stalka.js
Expand Up @@ -117,8 +117,8 @@ describe('Stalka', function() {
}),
describe('#readChanges', function() {
it("should default the feed to 'longpoll'", function(done) {
var stalka = fakeRequestStalka(function(url, callback) {
url.should.include("feed=longpoll");
var stalka = fakeRequestStalka(function(options, callback) {
options.url.should.include("feed=longpoll");
callback();
});

Expand All @@ -127,8 +127,8 @@ describe('Stalka', function() {
});
}),
it("should query with any extra options", function(done) {
var stalka = fakeRequestStalka(function(url, callback) {
url.should.include("pizza=yummy");
var stalka = fakeRequestStalka(function(options, callback) {
options.url.should.include("pizza=yummy");
callback();
});

Expand All @@ -137,8 +137,8 @@ describe('Stalka', function() {
});
}),
it("should query with the correct base url", function(done) {
var stalka = fakeRequestStalka(function(url, callback) {
url.should.match(/^http:\/\/somehost:1234\/somedb\/_changes?.*/);
var stalka = fakeRequestStalka(function(options, callback) {
options.url.should.match(/^http:\/\/somehost:1234\/somedb\/_changes?.*/);
callback();
});

Expand All @@ -147,7 +147,7 @@ describe('Stalka', function() {
});
}),
it("should return the changes on a successful read", function(done) {
var stalka = fakeRequestStalka(function(url, callback) {
var stalka = fakeRequestStalka(function(options, callback) {
callback(null, null, {changes: 'yay'});
});
stalka.readChanges("http://somehost:1234/somedb", null, function(err, changes) {
Expand All @@ -156,13 +156,23 @@ describe('Stalka', function() {
});
}),
it("should return the error on an unsuccessful read", function(done) {
var stalka = fakeRequestStalka(function(url, callback) {
var stalka = fakeRequestStalka(function(options, callback) {
callback("Error reading changes", null, null);
});
stalka.readChanges("http://somehost:1234/somedb", null, function(err, changes) {
err.should.eql("Error reading changes");
done();
});
}),
it("should pass through headers option and remove it from options", function(done) {
var stalka = fakeRequestStalka(function(options, callback) {
options.headers.should.eql({'xyz':1});
options.url.should.not.include("xyz");
callback();
});
stalka.readChanges("http://somehost:1234/somedb", { headers: { 'xyz':1 }}, function(err, changes) {
done();
});
});
}),
describe('#start', function() {
Expand Down

0 comments on commit 97ba7dd

Please sign in to comment.