Permalink
Browse files

Merge pull request #18 from aheckmann/master

express 3 support
  • Loading branch information...
2 parents db2dc42 + 8765cb8 commit 1ff670851387f13333cd50f642b36ae78b4a2c7d @rauchg rauchg committed Oct 3, 2012
Showing with 480 additions and 665 deletions.
  1. +0 −6 .gitmodules
  2. +4 −0 .travis.yml
  3. +7 −0 History.md
  4. +1 −2 Makefile
  5. +14 −30 Readme.md
  6. +94 −89 index.js
  7. +12 −7 package.json
  8. +4 −4 test/fixtures/both.jade
  9. +1 −1 test/fixtures/nested.jade
  10. +2 −2 test/fixtures/promise.jade
  11. +2 −2 test/fixtures/query.jade
  12. +339 −522 test/index.js
View
6 .gitmodules
@@ -1,6 +0,0 @@
-[submodule "support/expresso"]
- path = support/expresso
- url = https://github.com/visionmedia/expresso.git
-[submodule "support/jade"]
- path = support/jade
- url = https://github.com/visionmedia/jade.git
View
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
View
7 History.md
@@ -1,4 +1,11 @@
+0.1.0 / 2012-10-02
+==================
+
+ * fixed; works with express 3 #10
+ * fixed; refactored deprecated mongoose 'run' methods to 'exec' [jedwood](https://github.com/jedwood)
+ * tests; use mocha
+
0.0.5 / 2012-02-29
==================
View
3 Makefile
@@ -1,6 +1,5 @@
test:
- @NODE_ENV=test ./node_modules/expresso/bin/expresso \
- test/index.js
+ @time ./node_modules/.bin/mocha $(T) $(TESTS)
.PHONY: test
View
44 Readme.md
@@ -1,18 +1,16 @@
<img src="https://github.com/LearnBoost/express-mongoose/raw/master/express-mongoose.png"/>
-Express-mongoose adds Mongoose Query and Promise support to Express.
+Adds [Mongoose](http://mongoosejs.com) [Query](http://mongoosejs.com/docs/api.html#query-js) and [Promise](http://mongoosejs.com/docs/api.html#promise-js) support to [Express](http://expressjs.com).
-Methods which now support Promises:
+Methods which now support `Promises`:
- `res.render`
- - `res.partial`
- `res.send`
- `res.redirect`
-Methods which now support Queries:
+Methods which now support `Queries`:
- `res.render`
- - `res.partial`
- `res.send`
## Installation
@@ -44,23 +42,12 @@ In your routes:
res.render('dashboard', {
likes: req.user.getLikes()
, latestNews: News.getLatest()
- , locals: {
- backwardcompatible: new Promise(somethingAsync)
- }
+ , stuff: new Promise(somethingAsync)
});
});
- app.get('/partial', function (req, res, next) {
- res.partial('likes', {
- likes: req.user.getLikes()
- , locals: {
- backwardcompatible: new Promise(somethingAsync)
- }
- });
- });
-
-With `res.send` support you can pass a Query or Promise and the result will be rendered as json.
-If an error occurs, the error will be next()ed along as expected.
+With `res.send` support you can pass a `Query` or `Promise` and the result will be rendered as json.
+If an error occurs, the error will be passed to `next()` as expected.
app.get('/send', function (req, res) {
var News = db.model('News');
@@ -92,22 +79,19 @@ If an error occurs, the error will be next()ed along as expected.
});
});
-### Features
-
- - res.render support
- - res.partial support
- - res.send support
- - res.redirect support
- - nested locals support
-
### Error handling
- If a Query or Promise resolves to an error it will be forwarded on with `next(err)` as expected.
+ If a `Query` or `Promise` resolves to an error it will be forwarded on with `next(err)` as expected.
### Compatibility
- - Express: 2.x
- - Mongoose: 1.x
+```
+Express >=3.x use >= 0.1.0
+Express < 3.x use < 0.1.0
+
+Mongoose: >= 1.x
+MongoDB: any
+```
## Authors
View
183 index.js
@@ -8,47 +8,120 @@
* Version.
*/
-exports.version = '0.0.4';
+exports.version = JSON.parse(require('fs').readFileSync(__dirname + '/package.json')).version;
/**
* Module dependencies.
*/
-var res = require('http').ServerResponse.prototype
+var express = require('express')
, Promise = require('mongoose').Promise
, Query = require('mongoose').Query
+ , slice = require('sliced')
/**
- * Wrap the original rendering methods with support
- * for Queries and Promises.
+ * Wrap response.render with support for mongoose
+ * Queries and Promises.
*/
-res.render = wrap(res.render);
-res.partial = wrap(res.partial);
+var render = express.response.render;
+express.response.render = function expressmongoose_render (view, options, callback) {
+ if (!options || 'function' == typeof options) {
+ return render.call(this, view, options, callback);
+ }
+
+ var self = this;
+ return resolve(options, function (err, result) {
+ if (err) {
+ return 'function' == typeof callback
+ ? callback(err)
+ : self.req.next(err);
+ }
+
+ // must return here so partials always work
+ return render.call(self, view, result, callback);
+ });
+}
+
+/**
+ * Add Promise/Query support to res.send.
+ */
+
+var send = express.response.send;
+express.response.send = function expressmongoose_send () {
+ var args = slice(arguments);
+ var self = this;
+
+ function handleResult (err, result) {
+ if (err) return self.req.next(err);
+ args[0] = result;
+ send.apply(self, args);
+ }
+
+ if (args[0] instanceof Promise) {
+ return args[0].addBack(handleResult);
+ }
+
+ if (args[0] instanceof Query) {
+ return args[0].exec(handleResult);
+ }
+
+ if ('Object' == args[0].constructor.name) {
+ return resolve(args[0], handleResult);
+ }
+
+ send.apply(this, args);
+};
+
+/**
+ * Extends res.redirect with mongoose Promise support.
+ *
+ * Does not accept Queries since those return documents.
+ * Instead, manually handle the result of your query first,
+ * then resolve your promise with the url and optional status.
+ *
+ * var promise = new mongoose.Promise;
+ * res.redirect(promise);
+ *
+ * // later...
+ * promise.complete(url [, status]);
+ *
+ * The promise may pass an optional status code as the
+ * first argument.
+ *
+ * promise.complete(301, '/elsewhere');
+ */
+
+var redirect = express.response.redirect;
+express.response.redirect = function expressmongoose_redirect () {
+ var self = this;
+ var args = slice(arguments);
-function wrap (method) {
- return function expressmongoose (view, options, callback, parent, sub) {
+ function handleResult (err, url, code) {
+ if (err) return self.req.next(err);
- if (!options || 'function' == typeof options) {
- return method.call(this, view, options, callback, parent, sub);
+ if ('string' != typeof url) {
+ return self.req.next(new Error('URL Expected'));
}
- var self = this;
- return resolve(options, function (err, result) {
- if (err) {
- return 'function' == typeof callback
- ? callback(err)
- : self.req.next(err);
- }
-
- // must return here so partials always work
- return method.call(self, view, result, callback, parent, sub);
- });
+ args[0] = url;
+ if (code) args[1] = code;
+ redirect.apply(self, args);
}
+
+ if (args[0] instanceof Promise) {
+ return args[0].addBack(handleResult);
+ }
+
+ redirect.apply(this, args);
}
+// TODO res.json
+// TODO res.jsonp
+
/**
* Resolves any Queries and Promises within the passed options.
+ * @api private
*/
function resolve (options, callback, nested) {
@@ -105,73 +178,5 @@ function resolve (options, callback, nested) {
return callback(null, options);
}, true);
}
-
}
-/**
- * Add Promise/Query support to res.send.
- */
-
-var send = res.send;
-res.send = function (body, headers, status) {
- var self = this;
-
- function handleResult (err, result) {
- if (err) return self.req.next(err);
- send.call(self, result, headers, status);
- }
-
- if (body instanceof Promise) {
- return body.addBack(handleResult);
- }
-
- if (body instanceof Query) {
- return body.exec(handleResult);
- }
-
- if ('Object' == body.constructor.name) {
- return resolve(body, handleResult);
- }
-
- send.call(this, body, headers, status);
-};
-
-/**
- * Extends res.redirect with mongoose Promise support.
- *
- * Does not accept Queries since those return documents.
- * Instead, manually handle the result of your query first,
- * then resolve your promise with the url and optional status.
- *
- * var promise = new mongoose.Promise;
- * res.redirect(promise);
- *
- * // later...
- * promise.complete(url [, status]);
- *
- * The promise can pass an optional status code as the
- * second argument.
- *
- * promise.complete('/elsewhere', 301);
- */
-
-var redirect = res.redirect;
-res.redirect = function (url, status) {
- var self = this;
-
- function handleResult (err, result, code) {
- if (err) return self.req.next(err);
-
- if ('string' != typeof result) {
- return self.req.next(new Error('URL Expected'));
- }
-
- redirect.call(self, result, code || status);
- }
-
- if (url instanceof Promise) {
- return url.addBack(handleResult);
- }
-
- redirect.call(this, url, status);
-}
View
19 package.json
@@ -1,21 +1,26 @@
{
"name": "express-mongoose"
, "description": "Adds Mongoose Promise/Query support to Express rendering."
-, "version": "0.0.5"
-, "author": "Aaron Heckmann <aaron@learnboost.com>"
+, "version": "0.1.0"
+, "author": "Aaron Heckmann <aaron.heckmann+github@gmail.com>"
, "keywords": ["express", "mongoose", "mongo", "render", "promise", "query"]
, "homepage": "https://github.com/learnboost/express-mongoose"
, "dependencies": {
+ "sliced": "0.0.3"
}
, "devDependencies": {
- "should": "0.2.1"
- , "mongoose": ">= 1.0.0"
- , "express": ">= 2.0.0"
- , "jade": ">= 0.20.3"
- , "expresso": ">= 0.9.2"
+ "mongoose": ">= 1.0.0"
+ , "express": "3.0.x"
+ , "jade": "0.27.5"
+ , "mocha": "1.6.0"
+ , "superagent": "0.9.5"
}
, "main": "index"
, "engines": { "node": ">= 0.2.0" }
+, "scripts": { "test": "make test" }
+, "bugs": {
+ "url":"https://github.com/learnboost/express-mongoose/issues/new"
+ }
, "repository": {
"type": "git"
, "url": "git://github.com/LearnBoost/express-mongoose.git"
View
8 test/fixtures/both.jade
@@ -1,7 +1,7 @@
ul
- - each drumset in query
- - if ('electronic' == drumset.type)
+ each drumset in query
+ if ('electronic' == drumset.type)
li= drumset.brand
- - each drumset in promise
- - if ('black' == drumset.color)
+ each drumset in promise
+ if ('black' == drumset.color)
li= drumset.brand
View
2 test/fixtures/nested.jade
@@ -1,2 +1,2 @@
ul
- != partial('nested-partial')
+ include nested-partial
View
4 test/fixtures/promise.jade
@@ -1,4 +1,4 @@
ul
- - each drumset in promise
- - if ('GMS' === drumset.brand)
+ each drumset in promise
+ if ('GMS' === drumset.brand)
li= drumset.color
View
4 test/fixtures/query.jade
@@ -1,4 +1,4 @@
ul
- - each drumset in query
- - if ('electronic' == drumset.type)
+ each drumset in query
+ if ('electronic' == drumset.type)
li= drumset.brand
View
861 test/index.js
@@ -6,9 +6,10 @@
const mongoose = require('mongoose')
, express = require('express')
, assert = require('assert')
- , should = require('should')
, Promise = mongoose.Promise
, Schema = mongoose.Schema
+ , http = require('http')
+ , request = require('superagent')
require('../');
@@ -29,20 +30,20 @@ var DrumsetSchema = new Schema({
DrumsetSchema.statics.useQuery = function () {
// return a Query
- return this.find({ color: 'black' }).sort('_id', 1);
+ return this.find({ color: 'black' }).sort('_id');
}
DrumsetSchema.statics.usePromise = function () {
var promise = new Promise();
this.find({ type: 'Acoustic' })
- .sort('_id', 1)
+ .sort('_id')
.exec(promise.resolve.bind(promise));
return promise;
}
DrumsetSchema.statics.queryError = function () {
// should produce an invalid query error
- return this.find({ color: { $fake: { $boom: [] }} }).sort('_id', 1);
+ return this.find({ color: { $fake: { $boom: [] }} }).sort('_id');
}
DrumsetSchema.statics.promiseError = function () {
@@ -63,612 +64,428 @@ DrumsetSchema.statics.usePromiseRedirect = function (status) {
mongoose.model('Drumset', DrumsetSchema);
/**
- * Mongoose connection helper.
- */
-
-function connect () {
- return mongoose.createConnection('mongodb://localhost/express_goose_test');
-}
-
-/**
- * Dummy data.
- */
-
-var db = connect();
-var collection = 'drumsets_' + (Math.random() * 100000 | 0);
-var Drumset = db.model('Drumset', collection);
-var pending = 4;
-
-Drumset.create({
- brand: 'Roland'
- , color: 'black'
- , type: 'electronic'
- , _id: '4da8b662057a83596c000001'
-}, added);
-
-Drumset.create({
- brand: 'GMS'
- , color: 'Silver Sparkle'
- , type: 'Acoustic'
- , _id: '4da8b662057a83596c000002'
-}, added);
-
-Drumset.create({
- brand: 'DW'
- , color: 'Broken Glass'
- , type: 'Acoustic'
- , _id: '4da8b662057a83596c000003'
-}, added);
-
-Drumset.create({
- brand: 'Meinl'
- , color: 'black'
- , type: 'Acoustic'
- , _id: '4da8b662057a83596c000004'
-}, added);
-
-function added (err) {
- if (err) return console.error(err);
- if (--pending) return;
- db.close();
- assignExports();
-}
-
-/**
* Creates a test server.
*/
function makeapp () {
- var app = express.createServer();
+ var app = express();
app.set('views', __dirname + '/fixtures');
app.set('view engine', 'jade');
return app;
}
/**
- * DB is ready, export our expresso tests.
+ * Mongoose connection helper.
*/
-function assignExports () {
-
- /**
- * Clean up the test db when finished.
- */
+function connect () {
+ return mongoose.createConnection('mongodb://localhost/express_goose_test');
+}
- var testsrunning = 4;
- function finishTest () {
- if (--testsrunning) return;
- var db = connect();
- db.once('open', function () {
- db.db.dropDatabase(function () {
+describe('express-mongoose', function(){
+ var collection = 'drumsets_' + (Math.random() * 100000 | 0);
+ var db, Drumset;
+
+ before(function(done){
+ // add dummy data
+ db = connect();
+ Drumset = db.model('Drumset', collection);
+ var pending = 4;
+
+ Drumset.create({
+ brand: 'Roland'
+ , color: 'black'
+ , type: 'electronic'
+ , _id: '4da8b662057a83596c000001'
+ }, added);
+
+ Drumset.create({
+ brand: 'GMS'
+ , color: 'Silver Sparkle'
+ , type: 'Acoustic'
+ , _id: '4da8b662057a83596c000002'
+ }, added);
+
+ Drumset.create({
+ brand: 'DW'
+ , color: 'Broken Glass'
+ , type: 'Acoustic'
+ , _id: '4da8b662057a83596c000003'
+ }, added);
+
+ Drumset.create({
+ brand: 'Meinl'
+ , color: 'black'
+ , type: 'Acoustic'
+ , _id: '4da8b662057a83596c000004'
+ }, added);
+
+ function added (err) {
+ if (added.err) return;
+
+ if (err) {
db.close();
- });
- });
- }
-
-
- exports['test render'] = function () {
- var app = makeapp();
- var db = connect();
- var Drumset = db.model('Drumset', collection);
-
- app.get('/renderquery', function (req, res) {
- res.render('query', {
- query: Drumset.useQuery()
- });
- });
-
- app.get('/renderpromise', function (req, res) {
- res.render('promise', {
- promise: Drumset.usePromise()
- });
- });
-
- app.get('/renderboth', function (req, res) {
- res.render('both', {
- query: Drumset.useQuery()
- , promise: Drumset.usePromise()
- });
- });
-
- app.get('/renderqueryerror', function (req, res) {
- res.render('query', {
- query: Drumset.queryError()
- });
- });
-
- app.get('/renderpromiseerror', function (req, res) {
- res.render('promise', {
- promise: Drumset.promiseError()
- });
- });
-
- app.get('/renderbotherror', function (req, res) {
- res.render('both', {
- query: Drumset.queryError()
- , promise: Drumset.promiseError()
- });
- });
-
- app.get('/renderbothnest', function (req, res) {
- res.render('both', {
- locals: {
- promise: Drumset.usePromise()
- }
- , query: Drumset.useQuery()
- });
- });
-
- app.get('/renderlocalsonly', function (req, res) {
- res.render('promise', {
- locals: {
- promise: Drumset.usePromise()
- }
- });
- });
-
- app.get('/renderlocalsonlynest', function (req, res) {
- res.render('nested', {
- locals: {
- title: 'yes'
- }
- });
- });
-
- app.get('/renderbothnesterror', function (req, res) {
- res.render('both', {
- locals: {
- query: Drumset.queryError()
- }
- , promise: Drumset.usePromise()
- });
- });
-
- // test
+ return done(added.err = err);
+ }
- var pending = 7;
- function done () {
if (--pending) return;
- db.close();
- finishTest();
+ done();
}
+ })
- app.setMaxListeners(0);
-
- assert.response(app,
- { url: '/renderquery' }
- , { status: 200
- , body: '<ul><li>Roland</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/renderpromise' }
- , { status: 200
- , body: '<ul><li>Silver Sparkle</li></ul>'
- }
- , done
- );
+ after(function(done){
+ // clean up the test db
+ db.db.dropDatabase(function () {
+ db.close();
+ done();
+ });
+ })
- assert.response(app,
- { url: '/renderboth' }
- , { status: 200
- , body: '<ul><li>Roland</li><li>Meinl</li></ul>'
- }
- , done
- );
+ function test (routes, next) {
+ var app = makeapp();
- assert.response(app,
- { url: '/renderbothnest' }
- , { status: 200
- , body: '<ul><li>Roland</li><li>Meinl</li></ul>'
- }
- , done
- );
+ Object.keys(routes).forEach(function (route) {
+ app.get(route, routes[route]);
+ })
- assert.response(app,
- { url: '/renderlocalsonly' }
- , { status: 200
- , body: '<ul><li>Silver Sparkle</li></ul>'
- }
- , done
- );
+ // error handler
+ app.use(function (err, req, res, next) {
+ res.statusCode = 500;
+ res.send(err.stack);
+ })
- assert.response(app,
- { url: '/renderlocalsonlynest' }
- , { status: 200
- , body: '<ul><li>yes</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/renderqueryerror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: Can't use $fake with String."));
- done();
- }
- );
+ var server = http.Server(app);
+ var address;
- assert.response(app,
- { url: '/renderpromiseerror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
+ before(function (done) {
+ server.listen(0, function () {
+ address = server.address();
done();
- }
- );
+ });
+ });
- assert.response(app,
- { url: '/renderbotherror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
- done();
- }
- );
+ after(function (done) {
+ server.close(done);
+ })
- assert.response(app,
- { url: '/renderbothnesterror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: Can't use $fake with String."));
- done();
- }
- );
- };
+ next(function req (path, cb) {
+ var url = 'http://' + address.address + ':' + address.port + path;
+ return request.get(url, cb);
+ })
+ }
- exports['test partial'] = function () {
- var app = makeapp();
- var db = connect();
- var Drumset = db.model('Drumset', collection);
+ describe('render', function(){
+ var routes = {};
- app.get('/partialquery', function (req, res) {
- res.partial('query', {
+ routes['/renderquery'] = function (req, res) {
+ res.render('query', {
query: Drumset.useQuery()
});
- });
-
- app.get('/partiallocalsonly', function (req, res) {
- res.partial('promise', {
- locals: {
- promise: Drumset.usePromise()
- }
- });
- });
+ }
- app.get('/partialpromise', function (req, res) {
- res.partial('promise', {
+ routes['/renderpromise'] = function (req, res) {
+ res.render('promise', {
promise: Drumset.usePromise()
});
- });
+ }
- app.get('/partialboth', function (req, res) {
- res.partial('both', {
+ routes['/renderboth'] = function (req, res) {
+ res.render('both', {
query: Drumset.useQuery()
, promise: Drumset.usePromise()
});
- });
+ }
- app.get('/partialqueryerror', function (req, res) {
- res.partial('query', {
+ routes['/renderqueryerror'] = function (req, res) {
+ res.render('query', {
query: Drumset.queryError()
});
- });
+ }
- app.get('/partialpromiseerror', function (req, res) {
- res.partial('promise', {
+ routes['/renderpromiseerror'] = function (req, res) {
+ res.render('promise', {
promise: Drumset.promiseError()
});
- });
+ }
- app.get('/partialbotherror', function (req, res) {
- res.partial('both', {
+ routes['/renderbotherror'] = function (req, res) {
+ res.render('both', {
query: Drumset.queryError()
, promise: Drumset.promiseError()
});
- });
-
- // test
-
- var pending = 6;
- function done () {
- if (--pending) return;
- db.close();
- finishTest();
}
- assert.response(app,
- { url: '/partialquery' }
- , { status: 200
- , body: '<ul><li>Roland</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/partialpromise' }
- , { status: 200
- , body: '<ul><li>Silver Sparkle</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/partialboth' }
- , { status: 200
- , body: '<ul><li>Roland</li><li>Meinl</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/partiallocalsonly' }
- , { status: 200
- , body: '<ul><li>Silver Sparkle</li></ul>'
- }
- , done
- );
-
- assert.response(app,
- { url: '/partialqueryerror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: Can't use $fake with String."));
- done();
- }
- );
+ routes['/renderlocalsonlynest'] = function (req, res) {
+ res.render('nested', {
+ title: 'yes'
+ });
+ }
- assert.response(app,
- { url: '/partialpromiseerror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
- done();
- }
- );
+ test(routes, function (req) {
+ it('/renderquery', function(done){
+ req('/renderquery', function (res) {
+ assert.equal(200, res.status);
+ assert.equal('<ul><li>Roland</li></ul>', res.text);
+ done();
+ })
+ })
+
+ it('/renderpromise', function(done){
+ req('/renderpromise', function (res) {
+ assert.equal(200, res.status);
+ assert.equal('<ul><li>Silver Sparkle</li></ul>', res.text);
+ done();
+ })
+ })
+
+ it('/renderboth', function(done){
+ req('/renderboth', function (res) {
+ assert.equal(200, res.status);
+ assert.equal('<ul><li>Roland</li><li>Meinl</li></ul>', res.text);
+ done();
+ })
+ })
+
+ it('/renderlocalsonlynest', function(done){
+ req('/renderlocalsonlynest', function (res) {
+ assert.equal(200, res.status);
+ assert.equal('<ul><li>yes</li></ul>', res.text);
+ done();
+ })
+ })
+
+ it('/renderqueryerror', function(done){
+ req('/renderqueryerror', function (res) {
+ assert.equal(500, res.status);
+ assert.ok(~res.text.indexOf("Error: Can't use $fake with String."));
+ done();
+ })
+ })
+
+ it('/renderpromiseerror', function(done){
+ req('/renderpromiseerror', function (res) {
+ assert.equal(500, res.status);
+ assert.ok(~res.text.indexOf("Error: splat!"));
+ done();
+ });
+ })
- assert.response(app,
- { url: '/partialbotherror' }
- , function (res) {
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
- done();
- }
- );
- };
+ it('/renderbotherror', function(done){
+ req('/renderbotherror', function (res) {
+ assert.equal(500, res.status);
+ assert.ok(~res.text.indexOf("Error: splat!"));
+ done();
+ });
+ })
+ })
+ })
- exports['test send'] = function () {
- var app = makeapp();
- var db = connect();
- var Drumset = db.model('Drumset', collection);
+ describe('send', function(){
+ var routes = {}
- app.get('/sendquery', function (req, res) {
+ routes['/sendquery'] = function (req, res) {
res.send(Drumset.useQuery());
- });
+ }
- app.get('/sendpromise', function (req, res) {
+ routes['/sendpromise'] = function (req, res) {
res.send(Drumset.usePromise());
- });
+ }
- app.get('/sendboth', function (req, res) {
+ routes['/sendboth'] = function (req, res) {
res.send({
query: Drumset.useQuery()
, promise: Drumset.usePromise()
});
- });
+ }
- app.get('/sendqueryerror', function (req, res) {
+ routes['/sendqueryerror'] = function (req, res) {
res.send(Drumset.queryError());
- });
+ }
- app.get('/sendpromiseerror', function (req, res) {
+ routes['/sendpromiseerror'] = function (req, res) {
res.send(Drumset.promiseError());
- });
+ }
- app.get('/sendbotherror', function (req, res) {
+ routes['/sendbotherror'] = function (req, res) {
res.send({
query: Drumset.queryError()
, promise: Drumset.promiseError()
});
- });
-
- // test
-
- var pending = 6;
- function done () {
- if (--pending) return;
- db.close();
- finishTest();
}
- assert.response(app,
- { url: '/sendquery' }
- , function (res) {
- done();
-
- assert.equal(res.statusCode, 200);
- assert.ok(/^application\/json/.test(res.headers['content-type']));
-
- var body = JSON.parse(res.body);
-
- assert.ok(!!body);
- assert.ok(Array.isArray(body));
- assert.equal(body.length, 2);
- body.forEach(function (doc) {
- assert.equal(doc.color, 'black');
+ test(routes, function (req) {
+ it('/sendquery', function(done){
+ req('/sendquery', function (res) {
+ assert.equal(res.status, 200);
+ assert.ok(/^application\/json/.test(res.headers['content-type']));
+
+ var body = JSON.parse(res.text);
+
+ assert.ok(!!body);
+ assert.ok(Array.isArray(body));
+ assert.equal(body.length, 2);
+ body.forEach(function (doc) {
+ assert.equal(doc.color, 'black');
+ });
+
+ done();
+ })
+ })
+
+ it('/sendpromise', function (done) {
+ req('/sendpromise', function (res) {
+ assert.equal(res.status, 200);
+ assert.ok(/^application\/json/.test(res.headers['content-type']));
+
+ var body = JSON.parse(res.text);
+
+ assert.ok(!!body);
+ assert.ok(Array.isArray(body));
+ assert.equal(body.length, 3);
+ body.forEach(function (doc) {
+ assert.equal(doc.type, 'Acoustic');
+ });
+
+ done();
+ })
+ })
+
+ it('/sendboth', function (done) {
+ req('/sendboth', function (res) {
+ assert.equal(res.status, 200);
+ assert.ok(/^application\/json/.test(res.headers['content-type']));
+
+ var body = JSON.parse(res.text);
+
+ assert.ok(!!body);
+ assert.equal(body.query.length, 2);
+ assert.equal(body.promise.length, 3);
+
+ done();
+ })
+ })
+
+ it('/sendqueryerror', function(done){
+ req('/sendqueryerror', function (res) {
+ assert.equal(res.status, 500);
+ assert.ok(~res.text.indexOf("Error: Can't use $fake with String."));
+ done();
});
- }
- );
+ })
- assert.response(app,
- { url: '/sendpromise' }
- , function (res) {
- done();
-
- assert.equal(res.statusCode, 200);
- assert.ok(/^application\/json/.test(res.headers['content-type']));
-
- var body = JSON.parse(res.body);
-
- assert.ok(!!body);
- assert.ok(Array.isArray(body));
- assert.equal(body.length, 3);
- body.forEach(function (doc) {
- assert.equal(doc.type, 'Acoustic');
+ it('/sendpromiseerror', function(done){
+ req('/sendpromiseerror', function (res) {
+ assert.equal(res.status, 500);
+ assert.ok(~res.text.indexOf("Error: splat!"));
+ done();
});
- }
- );
-
- assert.response(app,
- { url: '/sendboth' }
- , function (res) {
- done();
-
- assert.equal(res.statusCode, 200);
- assert.ok(/^application\/json/.test(res.headers['content-type']));
-
- var body = JSON.parse(res.body);
-
- assert.ok(!!body);
- assert.equal(body.query.length, 2);
- assert.equal(body.promise.length, 3);
- }
- );
-
- assert.response(app,
- { url: '/sendqueryerror' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: Can't use $fake with String."));
- }
- );
-
- assert.response(app,
- { url: '/sendpromiseerror' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
- }
- );
-
- assert.response(app,
- { url: '/sendbotherror' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 500);
- assert.ok(~res.body.indexOf("Error: splat!"));
- }
- );
- };
-
- exports.redirect = function () {
- var app = makeapp();
- var db = connect();
- var Drumset = db.model('Drumset', collection);
-
- app.get('/redirect', function (req, res) {
+ })
+
+ it('/sendbotherror', function(done){
+ req('/sendbotherror', function (res) {
+ assert.equal(res.statusCode, 500);
+ assert.ok(~res.text.indexOf("Error: splat!"));
+ done();
+ })
+ })
+ })
+ })
+
+ describe('redirect', function(){
+ var routes = {};
+
+ routes['/redirect'] = function (req, res) {
res.redirect('/sound');
- });
+ }
- app.get('/redirect/status', function (req, res) {
- res.redirect('/sound', 301);
- });
+ routes['/redirect/status'] = function (req, res) {
+ res.redirect(301, '/sound');
+ }
- app.get('/redirectpromise', function (req, res) {
+ routes['/redirectpromise'] = function (req, res) {
res.redirect(Drumset.usePromiseRedirect());
- });
+ }
- app.get('/redirectpromise/status', function (req, res) {
+ routes['/redirectpromise/status'] = function (req, res) {
res.redirect(Drumset.usePromiseRedirect(), 301);
- });
+ }
- app.get('/redirectpromisestatus', function (req, res) {
+ routes['/redirectpromisestatus'] = function (req, res) {
res.redirect(Drumset.usePromiseRedirect(301));
- });
+ }
- app.get('/redirectpromisestatus/override', function (req, res) {
+ routes['/redirectpromisestatus/override'] = function (req, res) {
res.redirect(Drumset.usePromiseRedirect(301), 500);
- });
+ }
- app.get('/redirectpromiseerror', function (req, res) {
+ routes['/redirectpromiseerror'] = function (req, res) {
res.redirect(Drumset.promiseError());
- });
-
- // test
-
- var pending = 7;
- function done () {
- if (--pending) return;
- db.close();
- finishTest();
}
- assert.response(app,
- { url: '/redirect' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 302);
- assert.ok('/sound', res.headers['Location']);
- }
- );
-
- assert.response(app,
- { url: '/redirect/status' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 301);
- assert.ok('/sound', res.headers['Location']);
- }
- );
-
- assert.response(app,
- { url: '/redirectpromise' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 302);
- assert.ok('/redirect/promise', res.headers['Location']);
- }
- );
-
- assert.response(app,
- { url: '/redirectpromise/status' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 301);
- assert.ok('/redirect/promise', res.headers['Location']);
- }
- );
+ test(routes, function (req) {
+ it('/redirect' , function (done) {
+ var r = req('/redirect');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 302);
+ assert.ok(/\/sound$/.test(res.headers['location']));
+ done();
+ })
+ })
+
+ it('/redirect/status',function (done) {
+ var r = req('/redirect/status');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 301);
+ assert.ok(/\/sound$/.test(res.headers['location']));
+ done();
+ })
+ })
+
+ it('/redirectpromise',function (done) {
+ var r = req('/redirectpromise');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 302);
+ assert.ok(/\/promise\/redirect$/.test(res.headers['location']));
+ done();
+ })
+ })
+
+ it('/redirectpromise/status',function (done) {
+ var r = req('/redirectpromise/status');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 301);
+ assert.ok(/\/promise\/redirect$/.test(res.headers['location']));
+ done();
+ })
+ })
+
+ it('/redirectpromisestatus',function (done) {
+ var r = req('/redirectpromisestatus');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 301);
+ assert.ok(/\/promise\/redirect$/.test(res.headers['location']));
+ done();
+ });
+ });
- assert.response(app,
- { url: '/redirectpromisestatus' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 301);
- assert.ok('/redirect/promise', res.headers['Location']);
- }
- );
+ it('/redirectpromisestatus/override',function (done) {
+ var r = req('/redirectpromisestatus/override');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 301);
+ assert.ok(/\/promise\/redirect$/.test(res.headers['location']));
+ done();
+ })
+ })
+
+ it('/redirectpromiseerror',function (done) {
+ var r = req('/redirectpromiseerror');
+ r.redirects(0).end(function (res) {
+ assert.equal(res.statusCode, 500);
+ assert.ok(/splat!/.test(res.text));
+ done();
+ })
+ })
+ })
+ })
+})
- assert.response(app,
- { url: '/redirectpromisestatus/override' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 301);
- assert.ok('/redirect/promise', res.headers['Location']);
- }
- );
-
- assert.response(app,
- { url: '/redirectpromiseerror' }
- , function (res) {
- done();
- assert.equal(res.statusCode, 500);
- assert.ok(/splat!/.test(res.body));
- }
- );
- }
-}

0 comments on commit 1ff6708

Please sign in to comment.