Permalink
Browse files

update to use q (npm install q) instead of promised-io

  • Loading branch information...
1 parent e48c08c commit cdd2ff77bd7a07b1574561053f8cd3347f55699f @nrstott committed Feb 7, 2013
Showing with 42 additions and 101 deletions.
  1. +1 −1 lib/bogart.js
  2. +3 −18 lib/middleware.js
  3. +11 −1 lib/q.js
  4. +6 −6 lib/view.js
  5. +21 −75 test/middleware.test.js
View
@@ -497,7 +497,7 @@ exports.stream = function() {
var ResponseBuilder = exports.ResponseBuilder = function(viewEngine) {
var Stream = require('stream')
- , responseBuilder = Object.create(Q.defer())
+ , responseBuilder = Object.create(Q.defer(), { then: { get: function() { return this.promise.then.bind(this.promise); } } })
, forEachCallback
, forEachDeferred = Q.defer()
, response = { headers: {}, status: 200, body: {} }
View
@@ -605,25 +605,10 @@ exports.bodyAdapter = function(nextApp) {
return resp;
}
- if (resp.readable) {
- // Readable Node.JS style stream
- return {
- status: 200,
- headers: { 'content-type': 'text/html' },
- body: toForEachable(resp)
- };
- }
-
- if (resp.body && resp.body.readable) {
- // Readable Node.JS style stream
- resp.body = toForEachable(resp.body);
- return resp;
- }
+ var searchFriendlyErrorCode = 'BODY_ADAPTER_BAD_RESPONSE_BODY';
+ var reason = 'Middleware error: bodyAdapter found response body that does not have a forEach method, is not a Buffer, and is not a readable Stream.';
- throw {
- message: 'Middleware error: bodyAdapter found response body that does not have a forEach method, is not a Buffer, and is not a readable Stream.',
- code: 'BODY_ADAPTER_BAD_RESPONSE_BODY'
- };
+ throw new Error(reason + ' ' + searchFriendlyErrorCode);
});
}
};
View
@@ -1,4 +1,4 @@
-var q = require('promised-io/promise');
+var q = require('q');
/**
* Wraps a Node.JS style asynchronous function `function(err, result) {}`
@@ -48,4 +48,14 @@ if (!q.reject) {
}
}
+if (!q.whenCall) {
+ q.whenCall = function(callback, resolvedCallback, errorCallback, progressCallback) {
+ try {
+ return q.when(callback(), resolvedCallback, errorCallback, progressCallback);
+ } catch (err) {
+ return errorCallback(err);
+ }
+ }
+}
+
module.exports = q;
View
@@ -11,7 +11,8 @@ var
async = require('async'),
q = require('./q'),
when = q.when,
- util = require('util');
+ util = require('util'),
+ readFile = q.promisify(fs.readFile, fs);
// TODO: Figure out how to make this work x-platform using the nodules 'engines' overlay
//watchFile = require('fileWatcher').watchFile;
@@ -46,7 +47,7 @@ ViewEngine.prototype.clearCache = function() {
ViewEngine.prototype.cacheView = function(path) {
var
self = this,
- doRead = function(path) { return q.execute(fs.readFile, path, 'utf8'); },
+ doRead = function(path) { return readFile(path, 'utf8'); },
doCache = function(str) { self.viewCache[path] = str; };
//watchFile(path, function() { when(doRead(path), doCache) });
@@ -69,17 +70,16 @@ ViewEngine.prototype.getCacher = function(path) {
}
ViewEngine.prototype.respond = function(view, opts) {
+ var viewEngine = this;
+
opts = opts || {};
- return q.whenCall(function() { return this.render(view, opts); }.bind(this), function(str) {
+ return q.when(viewEngine.render(view, opts), function(str) {
return {
status: opts.status || 200,
headers: opts.headers || { 'content-type': 'text/html', 'content-length': Buffer.byteLength(str, 'utf-8') },
body: [str]
};
- }, function(err) {
- // Workaround for issue where whenCall calls rejectCallback even if one is not provided.
- throw err;
});
};
@@ -19,7 +19,7 @@ test("test parses JSON", function(t) {
body = {
forEach: function(callback) {
callback(JSON.stringify({ a: '1' }));
- return forEachDeferred;
+ return forEachDeferred.promise;
}
};
@@ -50,7 +50,7 @@ test("test parses form", function(t) {
body = {
forEach: function(callback) {
callback('a=1');
- return forEachDeferred;
+ return forEachDeferred.promise;
}
};
@@ -83,14 +83,15 @@ test("test method override", function(t) {
Q.when(app(request), function() {
t.equal('PUT', request.method, 'Should change method to PUT');
}, function(err) {
- console.log('error', err);
+ t.fail(err, err.stack);
t.end();
- });
+ }).done();
t.plan(1);
});
test("test gzip", function(t) {
+ t.plan(2);
var app = bogart.middleware.Gzip(function(req) {
return bogart.html('Hello World');
});
@@ -99,15 +100,12 @@ test("test gzip", function(t) {
Q.when(appPromise, function(resp) {
t.ok(resp, 'Response should not be falsey');
t.ok(resp.body, 'Response should have a body');
- }, function(err) {
- console.log('error', err);
- t.end();
- });
-
- t.plan(2);
+ }).done();
});
test("test gzip downloads as text/html", function(t) {
+ t.plan(2);
+
var router = bogart.router();
var viewEngine = bogart.viewEngine('mustache', path.join(__dirname, 'fixtures'));
@@ -119,28 +117,25 @@ test("test gzip downloads as text/html", function(t) {
var app = Gzip(router);
Q.when(app({ method: 'GET', env: {}, headers: {}, pathInfo: '/' }), function(resp) {
- t.ok(resp, 'Response should not be falsey');
t.equal(resp.status, 200);
t.equal(resp.headers['content-type'], 'text/html');
}, function(err) {
- console.log('error', err);
+ t.fail(err);
t.end();
- });
-
- t.plan(3);
+ }).done();
});
test("test error middleware has default response when error is thrown", function(t) {
- var app = new bogart.middleware.Error(function(req) { throw new Error('intentional'); });
+ var errorApp = new bogart.middleware.Error(function(req) { throw new Error('intentional'); });
- Q.when(app({ method: 'GET', env: {}, headers: {}, pathInfo: '/' }), function(resp) {
+ Q.when(errorApp({ method: 'GET', env: {}, headers: {}, pathInfo: '/' }), function(resp) {
t.ok(resp, 'Response should not be falsey');
t.equal(resp.status, 500);
t.equal(resp.headers['content-type'], 'text/html');
}, function(err) {
- console.log('error', err);
+ t.fail(err, err.stack);
t.end();
- });
+ }).done();
t.plan(3);
});
@@ -170,17 +165,19 @@ test("test flash", function(t) {
t.type(foo, 'undefined', 'Foo should be undefined');
request.headers.cookie = cookieStr;
- Q.when(app(request), function() {
+ return Q.when(app(request), function() {
t.equal(foo, 'bar', 'foo should equal bar');
});
- });
+ }, function() {
+ t.fail(err);
+ }).done();
t.plan(2);
});
test("test error middleware has default response when promise is rejected", function(t) {
- var app = new bogart.middleware.Error(function(req) { return bogart.Q.reject('rejected'); });
+ var app = new bogart.middleware.Error(function(req) { return q.reject('rejected'); });
Q.when(app({ method: 'GET', env: {}, headers: {}, pathInfo: '/' }), function(resp) {
t.ok(resp, 'Response should not be falsey');
@@ -204,7 +201,7 @@ test("test parted json", function(t) {
env: {},
headers: { 'content-type': 'application/json' },
body: [ '{ "hello": "world" }' ]
- });
+ }).done();
t.plan(3);
});
@@ -306,7 +303,7 @@ test("test validate response", function(t) {
};
})().then(null, function(err) {
t.equal(err, 'Response must have a body property.');
- });
+ }).done();
bogart.middleware.validateResponse(function(req) {
return {
@@ -352,57 +349,6 @@ test("test validate response", function(t) {
t.plan(6);
});
-test("test bodyAdapter adapts Stream", function(t) {
- var Stream = require('stream').Stream;
-
- function TestReadStream() {
- Stream.call(this);
-
- var args = Array.prototype.slice.call(arguments)
- , self = this;
-
- process.nextTick(function emitData() {
- var x = args.shift();
- if (x === undefined) {
- self.emit('end');
- return;
- }
-
- self.emit('data', x);
-
- //process.nextTick(function() {
- emitData();
- //});
- });
-
- this.readable = true;
- }
-
- util.inherits(TestReadStream, Stream);
-
- var streamAdapter = bogart.middleware.bodyAdapter(function(req) {
- return new TestReadStream('hello', ' ', 'world');
- });
-
- Q.when(streamAdapter(), function(resp) {
- var str = '';
-
- t.ok(resp, 'Response should not be falsey');
- t.ok(resp.body, 'Response should have a body');
- t.ok(resp.body.forEach, 'Response body should be forEachable');
-
- return resp.body.forEach(function(x) {
- str += x;
- }).then(function() {
- t.equal(str, 'hello world', 'should equal hello world');
- t.end();
- }, function(err) {
- t.ok(false, err, 'found error');
- t.end();
- });
- });
-});
-
test("test cascade works with a router", function(t){
var cascade = bogart.middleware.cascade(function() { return true; })
, router = bogart.router()

0 comments on commit cdd2ff7

Please sign in to comment.