Permalink
Browse files

added json shorthand

  • Loading branch information...
1 parent f572b8a commit 17b7b8d5b58ae2ddbe4b1d9fd4a72a3de84084fb @extrabacon committed Feb 19, 2014
Showing with 11 additions and 3 deletions.
  1. +9 −0 index.js
  2. +2 −3 test/test-delayed-response.js
View
@@ -31,6 +31,15 @@ var DelayedResponse = function (req, res, next) {
util.inherits(DelayedResponse, EventEmitter);
/**
+ * Shorthand for adding the "Content-Type" header for returning JSON.
+ * @return {DelayedResponse} The same instance, for chaining calls
+ */
+DelayedResponse.prototype.json = function () {
+ this.res.setHeader('Content-Type', 'application/json');
+ return this;
+};
+
+/**
* Starts the polling process, keeping the connection alive.
* @param {Number} interval The interval at which "heartbeat" events are emitted
* @param {Number} initialDelay The initial delay before starting the polling process
@@ -140,8 +140,8 @@ describe('DelayedResponse', function () {
it('should render JSON when ending with an object', function (done) {
var app = express();
app.use(function (req, res) {
- res.set('Content-Type', 'application/json');
var delayed = new DelayedResponse(req, res);
+ delayed.json();
delayed.start(100, 0);
setTimeout(function () {
delayed.end(null, { success: true });
@@ -170,8 +170,8 @@ describe('DelayedResponse', function () {
it('should wait for promise resolution', function (done) {
var app = express();
app.use(function (req, res) {
- res.set('Content-Type', 'application/json');
var delayed = new DelayedResponse(req, res);
+ delayed.json();
delayed.start(100, 0);
var promise = when.resolve({ success: true });
delayed.end(promise);
@@ -183,7 +183,6 @@ describe('DelayedResponse', function () {
it('should wait for promise rejection', function (done) {
var app = express();
app.use(function (req, res) {
- res.set('Content-Type', 'application/json');
var delayed = new DelayedResponse(req, res);
delayed.start(100, 0);
var promise = when.reject(new Error('failure'));

0 comments on commit 17b7b8d

Please sign in to comment.