middleware tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
that dont quite work yet because node is being a PITA
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 1a5c32f · Browse files · 4 files·+166 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+166 added−19 removed
- tests:+132 added−19 removed
- other:+34 added−0 removed
Provenance
- Commit
5b296b5- Tree
f992d14- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
1a5c32f
4 files changed+166 added−19 removed
added:lib/test.js+34 added
12345678910111213141516171819202122232425262728293031323334
/*! * Express - test * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var methods = require('./router/methods') , http = require('http');
var server = http.Server(app);server.listen(0, function(){ var addr = server.address(); var req = http.request({ method: 'GET' , path: '/' , host: addr.address , port: addr.port });
req.on('response', function(res){ console.log(res.statusCode); });
req.end();})
exports.request = function(method, path, fn){ this._server = this._server || http.Server(this);};No newline at end of fileadded:test/support/http.js+89 added
1+2+/**3+ * Module dependencies.4+ */5+6+var EventEmitter = require('events').EventEmitter7+ , http = require('http');8+9+module.exports = request;10+11+function request(app) {12+ return new Request(app);13+}14+15+function Request(app) {16+ var self = this;17+ this.data = [];18+ this.header = {};19+ this.app = app;20+ if (!this.server) {21+ this.server = http.Server(app);22+ this.server.listen(0, function(){23+ self.addr = self.server.address();24+ self.listening = true;25+ });26+ }27+}28+29+/**30+ * Inherit from `EventEmitter.prototype`.31+ */32+33+Request.prototype.__proto__ = EventEmitter.prototype;34+35+Request.prototype.get = function(path){36+ return this.request('GET', path);37+};38+39+Request.prototype.set = function(field, val){40+ this.header[field] = val;41+ return this;42+};43+44+Request.prototype.write = function(data){45+ this.data.push(data);46+ return this;47+};48+49+Request.prototype.request = function(method, path){50+ this.method = method;51+ this.path = path;52+ return this;53+};54+55+Request.prototype.end = function(fn){56+ var self = this;57+58+ if (this.listening) {59+ var req = http.request({60+ method: this.method61+ , port: this.addr.port62+ , host: this.addr.address63+ , path: this.path64+ , headers: this.header65+ });66+67+ this.data.forEach(function(chunk){68+ req.write(chunk);69+ });70+ 71+ req.on('response', function(res){72+ var buf = '';73+ res.setEncoding('utf8');74+ res.on('data', function(chunk){ buf += chunk });75+ res.on('end', function(){76+ res.body = buf;77+ fn(res);78+ });79+ });80+81+ req.end();82+ } else {83+ this.server.on('listening', function(){84+ self.end(fn);85+ });86+ }87+88+ return this;89+};added:test/middleware.basic.js+43 added
12345678910111213141516171819202122232425262728293031323334353637383940414243
var express = require('../') , request = require('./support/http');
describe('middleware', function(){ describe('.next()', function(){ it('should behave like connect', function(done){ var app = express() , calls = [];
app.use(function(req, res, next){ calls.push('one'); next(); });
app.use(function(req, res, next){ calls.push('two'); next(); }); app.use(function(req, res){ var buf = ''; res.setHeader('Content-Type', 'application/json'); req.setEncoding('utf8'); req.on('data', function(chunk){ buf += chunk }); req.on('end', function(){ res.end(buf); }); });
request(app) .get('/') .set('Content-Type', 'application/json') .write('{"foo":"bar"}') .end(function(res){ res.headers.should.have.property('content-type', 'application/json'); res.statusCode.should.equal(200); res.body.should.equal('{"foo":"bar"}'); done(); }) }) })})No newline at end of filedeleted:test/middleware.js-19 removed
12345678910111213141516171819
var express = require('../');
describe('middleware', function(){ describe('.use()', function(){ it('should work like connect', function(done){ var app = express();
app.use(express.bodyParser());
app.use(function(req, res){ console.log(req.body); res.end('ok'); });
}) })})No newline at end of file