diff --git a/package.json b/package.json index c5d4e031b..4c0a9fe2f 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,9 @@ "utils-merge": "1.0.0" }, "devDependencies": { + "mocha": "~1.18.2", "should": "~3.3.0", - "mocha": "~1.18.2" + "supertest": "~0.11.0" }, "licenses": [ { diff --git a/test/app.listen.js b/test/app.listen.js index 59515e5cd..a009bb024 100644 --- a/test/app.listen.js +++ b/test/app.listen.js @@ -1,8 +1,8 @@ process.env.NODE_ENV = 'test'; -var connect = require('../'); -var request = require('./support/http'); +var connect = require('..'); +var request = require('supertest'); describe('app.listen()', function(){ it('should wrap in an http.Server', function(done){ @@ -12,10 +12,10 @@ describe('app.listen()', function(){ res.end(); }); - app.listen(5555, function(){ + app.listen(0, function(){ request(app) .get('/') .expect(200, done); }); - }) -}) + }); +}); diff --git a/test/fqdn.js b/test/fqdn.js new file mode 100644 index 000000000..fcadb05bc --- /dev/null +++ b/test/fqdn.js @@ -0,0 +1,88 @@ + +process.env.NODE_ENV = 'test'; + +var connect = require('..'); +var http = require('http'); +var request = require('supertest'); + +describe('app.use()', function(){ + var app; + + beforeEach(function(){ + app = connect(); + }); + + it('should not obscure FQDNs', function(done){ + app.use(function(req, res){ + res.end(req.url); + }); + + app.handle({ method: 'GET', url: 'http://example.com/foo' }, { + end: function(str){ + str.should.equal('http://example.com/foo'); + done(); + } + }); + }); + + describe('with a connect app', function(){ + it('should ignore FQDN in search', function (done) { + app.use('/proxy', function (req, res) { + res.end(req.url); + }); + + app.handle({ method: 'GET', url: '/proxy?url=http://example.com/blog/post/1' }, { + end: function(str){ + str.should.equal('/?url=http://example.com/blog/post/1'); + done(); + } + }); + }); + + it('should adjust FQDN req.url', function(done){ + app.use('/blog', function(req, res){ + res.end(req.url); + }); + + app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, { + end: function(str){ + str.should.equal('http://example.com/post/1'); + done(); + } + }); + }); + + it('should adjust FQDN req.url with multiple handlers', function(done){ + app.use(function(req,res,next) { + next(); + }); + + app.use('/blog', function(req, res){ + res.end(req.url); + }); + + app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, { + end: function(str){ + str.should.equal('http://example.com/post/1'); + done(); + } + }); + }); + + it('should adjust FQDN req.url with multiple routed handlers', function(done) { + app.use('/blog', function(req,res,next) { + next(); + }); + app.use('/blog', function(req, res) { + res.end(req.url); + }); + + app.handle({ method: 'GET', url: 'http://example.com/blog/post/1' }, { + end: function(str){ + str.should.equal('http://example.com/post/1'); + done(); + } + }); + }); + }); +}); diff --git a/test/mounting.js b/test/mounting.js index 9718d2811..6bf306597 100644 --- a/test/mounting.js +++ b/test/mounting.js @@ -1,111 +1,55 @@ process.env.NODE_ENV = 'test'; -var connect = require('../'); +var connect = require('..'); var http = require('http'); -var request = require('./support/http'); +var request = require('supertest'); +var should = require('should'); describe('app.use()', function(){ var app; beforeEach(function(){ app = connect(); - }) + }); describe('with a connect app', function(){ it('should mount', function(done){ var blog = connect(); - + blog.use(function(req, res){ req.url.should.equal('/'); res.end('blog'); }); - + app.use('/blog', blog); - + request(app) .get('/blog') - .expect('blog', done); - }) + .expect(200, 'blog', done); + }); it('should retain req.originalUrl', function(done){ var app = connect(); - + app.use('/blog', function(req, res){ res.end(req.originalUrl); }); - - request(app) - .get('/blog/post/1') - .expect('/blog/post/1', done); - }) - it('should adjust req.url', function(done){ - var app = connect(); - - app.use('/blog', function(req, res){ - res.end(req.url); - }); - request(app) .get('/blog/post/1') - .expect('/post/1', done); - }) - - it('should ignore FQDN in search', function (done) { - var app = connect(); - - app.use('/proxy', function (req, res) { - res.end(req.url); - }); - - request(app) - .get('/proxy?url=http://example.com/blog/post/1') - .expect('/?url=http://example.com/blog/post/1', done); + .expect(200, '/blog/post/1', done); }); - it('should adjust FQDN req.url', function(done){ - var app = connect(); - - app.use('/blog', function(req, res){ - res.end(req.url); - }); - - request(app) - .get('http://example.com/blog/post/1') - .expect('http://example.com/post/1', done); - }) - - it('should adjust FQDN req.url with multiple handlers', function(done){ - var app = connect(); - - app.use(function(req,res,next) { - next(); - }); - + it('should adjust req.url', function(done){ app.use('/blog', function(req, res){ res.end(req.url); }); request(app) - .get('http://example.com/blog/post/1') - .expect('http://example.com/post/1', done); - }) - - it('should adjust FQDN req.url with multiple routed handlers', function(done) { - var app = connect(); - - app.use('/blog', function(req,res,next) { - next(); - }); - app.use('/blog', function(req, res) { - res.end(req.url); - }); - - request(app) - .get('http://example.com/blog/post/1') - .expect('http://example.com/post/1', done); - }) + .get('/blog/post/1') + .expect(200, '/post/1', done); + }); it('should strip trailing slash', function(done){ var blog = connect(); @@ -116,11 +60,11 @@ describe('app.use()', function(){ }); app.use('/blog/', blog); - + request(app) .get('/blog') .expect('blog', done); - }) + }); it('should set .route', function(){ var blog = connect(); @@ -130,11 +74,9 @@ describe('app.use()', function(){ app.route.should.equal('/'); blog.route.should.equal('/blog'); admin.route.should.equal('/admin'); - }) + }); it('should not add trailing slash to req.url', function(done) { - var app = connect(); - app.use('/admin', function(req, res, next) { next(); }); @@ -155,27 +97,27 @@ describe('app.use()', function(){ req.url.should.equal('/'); res.end('blog'); }); - + app.use('/blog', blog); - + request(app) .get('/blog') .expect('blog', done); - }) - }) + }); + }); it('should be case insensitive (lower-case route, mixed-case request)', function(done){ var blog = http.createServer(function(req, res){ req.url.should.equal('/'); res.end('blog'); }); - + app.use('/blog', blog); - + request(app) .get('/BLog') .expect('blog', done); - }) + }); it('should be case insensitive (mixed-case route, lower-case request)', function(done){ var blog = http.createServer(function(req, res){ @@ -188,7 +130,7 @@ describe('app.use()', function(){ request(app) .get('/blog') .expect('blog', done); - }) + }); it('should be case insensitive (mixed-case route, mixed-case request)', function(done){ var blog = http.createServer(function(req, res){ @@ -201,5 +143,5 @@ describe('app.use()', function(){ request(app) .get('/blOG') .expect('blog', done); - }) -}) + }); +}); diff --git a/test/server.js b/test/server.js index a72bd9929..fad541fb7 100644 --- a/test/server.js +++ b/test/server.js @@ -1,30 +1,23 @@ process.env.NODE_ENV = 'test'; -var connect = require('../'); -var request = require('./support/http'); +var connect = require('..'); +var request = require('supertest'); var should = require('should'); describe('app', function(){ + var app; + + beforeEach(function(){ + app = connect(); + }); + it('should inherit from event emitter', function(done){ - var app = connect(); app.on('foo', done); app.emit('foo'); - }) - - it('should not obscure FQDNs', function(done){ - var app = connect(); + }); - app.use(function(req, res){ - res.end(req.url); - }); - - request(app) - .get('http://example.com/foo') - .expect('http://example.com/foo', done); - }) - - it('should work as middlware', function(done){ + it('should work as middleware', function(done){ var http = require('http'); // custom server handler array @@ -60,27 +53,27 @@ describe('app', function(){ }); }); }); - }) + }); it('should escape the 500 response body', function(done){ - var app = connect(); app.use(function(req, res, next){ next(new Error('error!')); }); request(app) .get('/') - .end(function(res){ - res.statusCode.should.eql(500); - res.body.should.containEql('Error: error!
'); - res.body.should.containEql('
   at'); - done(); - }); + .expect(/Error: error!
/) + .expect(/
   at/) + .expect(500, done); }) it('should escape the 404 response body', function(done){ - var app = connect(); - request(app) - .get('/foo/') - .expect('Cannot GET /foo/<script>stuff</script>\n', done); - }) -}) + app.handle({ method: 'GET', url: '/foo/' }, { + setHeader: function(){}, + end: function(str){ + this.statusCode.should.equal(404); + str.should.equal('Cannot GET /foo/<script>stuff</script>\n'); + done(); + } + }); + }); +}); diff --git a/test/support/http.js b/test/support/http.js deleted file mode 100644 index ab032de54..000000000 --- a/test/support/http.js +++ /dev/null @@ -1,111 +0,0 @@ - -/** - * Module dependencies. - */ - -var EventEmitter = require('events').EventEmitter - , methods = ['get', 'post', 'put', 'delete', 'head'] - , http = require('http'); - -module.exports = request; - -function request(app) { - return new Request(app); -} - -function Request(app) { - var self = this; - this.data = []; - this.header = {}; - this.app = app; - if (!this.server) { - this.server = http.Server(app); - this.server.listen(0, '127.0.0.1', function(){ - self.addr = self.server.address(); - self.listening = true; - }); - } -} - -/** - * Inherit from `EventEmitter.prototype`. - */ - -Request.prototype.__proto__ = EventEmitter.prototype; - -methods.forEach(function(method){ - Request.prototype[method] = function(path){ - return this.request(method, path); - }; -}); - -Request.prototype.set = function(field, val){ - this.header[field] = val; - return this; -}; - -Request.prototype.write = function(data){ - this.data.push(data); - return this; -}; - -Request.prototype.request = function(method, path){ - this.method = method; - this.path = path; - return this; -}; - -Request.prototype.expect = function(body, fn){ - var args = arguments; - this.end(function(res){ - switch (args.length) { - case 3: - res.headers.should.have.property(body.toLowerCase(), args[1]); - args[2](); - break; - default: - if ('number' == typeof body) { - res.statusCode.should.equal(body); - } else { - res.body.should.equal(body); - } - fn(); - } - }); -}; - -Request.prototype.end = function(fn){ - var self = this; - - if (this.listening) { - var req = http.request({ - method: this.method - , port: this.addr.port - , host: this.addr.address - , path: this.path - , headers: this.header - }); - - this.data.forEach(function(chunk){ - req.write(chunk); - }); - - req.on('response', function(res){ - var buf = ''; - res.setEncoding('utf8'); - res.on('data', function(chunk){ buf += chunk }); - res.on('end', function(){ - res.body = buf; - fn(res); - }); - }); - - req.end(); - } else { - this.server.on('listening', function(){ - self.end(fn); - }); - } - - return this; -};