add exclude options for onehost; auto create config.js for make test;

This commit is contained in:
fengmk2 2012-06-03 19:18:10 +08:00
parent 58b45862a1
commit d85f2dd4b8
6 changed files with 85 additions and 31 deletions

View File

@ -4,6 +4,9 @@ TESTTIMEOUT = 5000
REPORTER = spec
test:
@if ! test -f config.js; then \
cp config.default.js config.js; \
fi
@NODE_ENV=test ./node_modules/.bin/mocha \
--reporter $(REPORTER) --timeout $(TESTTIMEOUT) $(TESTS)

View File

@ -52,6 +52,9 @@ exports.config = {
// admin 可删除话题,编辑标签,设某人为达人
admins: { admin: true },
// [ [ plugin_name, options ], ... ]
plugins: []
// [ { name: 'plugin_name', options: { ... }, ... ]
plugins: [
// { name: 'onehost', options: { host: 'localhost.cnodejs.org' } },
// { name: 'wordpress_redirect', options: {} }
]
};

View File

@ -17,5 +17,8 @@
"devDependencies": {
"should": ">=0.6.0",
"mocha": ">=0.14.1"
},
"scripts": {
"test": "make test"
}
}

View File

@ -14,8 +14,15 @@
module.exports = function onehost(options) {
options = options || {};
var host = options.host;
var exclude = options.exclude || [];
if (!Array.isArray(exclude)) {
exclude = [ exclude ];
}
if (host) {
exclude.push(host);
}
return function (req, res, next) {
if (!host || host === req.headers.host || req.method !== 'GET') {
if (!host || exclude.indexOf(req.headers.host) >= 0 || req.method !== 'GET') {
return next();
}
res.writeHead(301, {

View File

@ -30,7 +30,7 @@ describe('plugins/onehost.js', function () {
app.close();
});
it('should 301 redirect `GET` request to ' + bindHost, function (done) {
it('should 301 redirect all `GET` to ' + bindHost, function (done) {
app.request().get('/foo/bar').end(function (res) {
res.should.status(301);
res.headers.location.should.equal('http://' + bindHost + '/foo/bar');
@ -38,6 +38,14 @@ describe('plugins/onehost.js', function () {
});
});
it('should 301 when GET request 127.0.0.1:port', function (done) {
app.request({ address: '127.0.0.1', port: app.address().port }).get('/foo/bar').end(function (res) {
res.should.status(301);
res.headers.location.should.equal('http://' + bindHost + '/foo/bar');
done();
});
});
[ 'post', 'put', 'delete', 'head' ].forEach(function (method) {
it('should no redirect for `' + method + '`', function (done) {
app.request()[method]('/foo/bar').end(function (res) {
@ -53,4 +61,36 @@ describe('plugins/onehost.js', function () {
});
});
describe('exclude options', function () {
var app2 = express.createServer();
app2.use(onehost({
host: bindHost,
exclude: '127.0.0.1:58964'
}));
app2.use(function (req, res) {
res.send(req.method + ' ' + req.url);
});
before(function (done) {
app2.listen(58964, done);
});
after(function () {
app2.close();
});
it('should 301 redirect all `GET` to ' + bindHost, function (done) {
app.request().get('/foo/bar').end(function (res) {
res.should.status(301);
res.headers.location.should.equal('http://' + bindHost + '/foo/bar');
done();
});
});
it('should 200 when request GET exclude host', function (done) {
app2.request({ address: '127.0.0.1', port: 58964 }).get('/foo/bar').end(function (res) {
res.should.status(200);
res.body.toString().should.equal('GET /foo/bar');
done();
});
});
});
});

View File

@ -17,11 +17,10 @@ try {
var http = require('http');
var querystring = require('querystring');
module.exports = request;
// need to change > 0.3.x
express.HTTPServer.prototype.request = function () {
return request(this);
express.HTTPServer.prototype.request = function (address) {
return new Request(this, address);
};
if (connect) {
@ -33,17 +32,12 @@ if (connect) {
// return request(this);
// };
function request(app) {
return new Request(app);
}
function Request(app) {
function Request(app, address) {
this.data = [];
this.header = {};
this.app = app;
this.server = app;
this.addr = this.server.address();
this.addr = address || this.server.address();
}
/**
@ -52,29 +46,29 @@ function Request(app) {
Request.prototype.__proto__ = EventEmitter.prototype;
methods.forEach(function(method) {
Request.prototype[method] = function(path) {
methods.forEach(function (method) {
Request.prototype[method] = function (path) {
return this.request(method, path);
};
});
Request.prototype.set = function(field, val) {
Request.prototype.set = function (field, val) {
this.header[field] = val;
return this;
};
Request.prototype.setBody = function(body) {
Request.prototype.setBody = function (body) {
this.set('Content-Type', 'application/x-www-form-urlencoded');
this.write(querystring.stringify(body));
return this;
};
Request.prototype.write = function(data) {
Request.prototype.write = function (data) {
this.data.push(data);
return this;
};
Request.prototype.request = function(method, path) {
Request.prototype.request = function (method, path) {
this.method = method;
this.path = path;
return this;
@ -120,17 +114,21 @@ Request.prototype.end = function (fn) {
res.on('end', function () {
var buf = null;
switch (chunks.length) {
case 0: buf = new Buffer(0); break;
case 1: buf = chunks[0]; break;
default:
buf = new Buffer(size);
var pos = 0;
for (var i = 0, l = chunks.length; i < l; i++) {
var chunk = chunks[i];
chunk.copy(buf, pos);
pos += chunk.length;
}
break;
case 0:
buf = new Buffer(0);
break;
case 1:
buf = chunks[0];
break;
default:
buf = new Buffer(size);
var pos = 0;
for (var i = 0, l = chunks.length; i < l; i++) {
var chunk = chunks[i];
chunk.copy(buf, pos);
pos += chunk.length;
}
break;
}
res.body = buf;
res.bodyJSON = function () {