Permalink
Browse files

Adds tests for send and redirect.

  • Loading branch information...
1 parent aa925d3 commit 3ea0d0ebcdaa7cacaf48e3db706093f2b1c1d37a @cpsubrian committed Oct 10, 2012
Showing with 610 additions and 47 deletions.
  1. +29 −28 expres.js
  2. +1 −1 package.json
  3. +7 −3 test/common.js
  4. +7 −7 test/json.js
  5. +8 −8 test/jsonp.js
  6. +200 −0 test/redirect.js
  7. +206 −0 test/send.js
  8. +152 −0 utils.js
View
@@ -1,5 +1,6 @@
-var url = require('url');
-var expres = module.exports = {};
+var url = require('url')
+ , utils = require('./utils')
+ , expres = module.exports = {};
expres.middleware = function (req, res, next) {
var ctx = res;
@@ -275,19 +276,19 @@ expres.methods = {
if (fn) delete obj.default;
var keys = Object.keys(obj);
- var key = req.accepts(keys);
+ var key = utils.accepts(keys, req.headers['Accept'] || req.headers['accept']);
this.set('Vary', 'Accept');
if (key) {
- this.set('Content-Type', normalizeType(key));
+ this.set('Content-Type', utils.normalizeType(key));
obj[key](req, this, next);
} else if (fn) {
fn();
} else {
var err = new Error('Not Acceptable');
err.status = 406;
- err.types = normalizeTypes(keys);
+ err.types = utils.normalizeTypes(keys);
next(err);
}
@@ -359,57 +360,57 @@ expres.methods = {
*
* res.redirect('/login');
*
- * @param {String} url
+ * @param {String} toUrl
* @param {Number} code
*/
- redirect: function(url){
+ redirect: function(toUrl){
var req = this.req
, head = 'HEAD' == req.method
, status = 302
, body;
- // allow status / url
+ // allow status / toUrl
if (2 == arguments.length) {
- if ('number' == typeof url) {
- status = url;
- url = arguments[1];
+ if ('number' == typeof toUrl) {
+ status = toUrl;
+ toUrl = arguments[1];
} else {
status = arguments[1];
}
}
// setup redirect map
- var map = { back: req.get('Referrer') || '/' };
+ var map = { back: req.headers['Referrer'] || '/' };
// perform redirect
- url = map[url] || url;
+ toUrl = map[toUrl] || toUrl;
// relative
- if (!~url.indexOf('://') && 0 != url.indexOf('//')) {
+ if (!~toUrl.indexOf('://') && 0 != toUrl.indexOf('//')) {
var path = '';
// relative to path
- if ('.' == url[0]) {
- url = req.path + '/' + url;
- // relative to mount-point
- } else if ('/' != url[0]) {
- url = path + '/' + url;
+ if ('.' == toUrl[0]) {
+ toUrl = url.parse(req.url).path + '/' + toUrl;
+ // relative to root
+ } else if ('/' != toUrl[0]) {
+ toUrl = '/' + toUrl;
}
// Absolute
- var host = req.get('Host');
- url = '//' + host + url;
+ var host = req.headers['host'] || req.headers['Host'];
+ toUrl = '//' + host + toUrl;
}
// Support text/{plain,html} by default
this.format({
- text: function(){
- body = statusCodes[status] + '. Redirecting to ' + url;
+ 'text/plain': function(){
+ body = http.STATUS_CODES[status] + '. Redirecting to ' + toUrl;
},
- html: function(){
- var u = utils.escape(url);
- body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
+ 'text/html': function(){
+ var u = utils.escape(toUrl);
+ body = '<p>' + http.STATUS_CODES[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
},
default: function(){
@@ -418,8 +419,8 @@ expres.methods = {
});
// Respond
- this.statusCode = status;
- this.set('Location', url);
+ this.status(status);
+ this.set('Location', toUrl);
this.set('Content-Length', Buffer.byteLength(body));
this.end(head ? null : body);
}
View
@@ -6,7 +6,7 @@
"dependencies": {},
"devDependencies": {
"mocha": "*",
- "superagent": "~0.9.5",
+ "supertest": "~0.3.1",
"clone": "~0.1.1"
},
"scripts": {
View
@@ -3,7 +3,7 @@ util = require('util');
http = require('http');
expres = require('../');
clone = require('clone');
-superagent = require('superagent');
+supertest = require('supertest');
var server;
@@ -13,8 +13,12 @@ response = function () {
return res;
};
-get = function (path, cb, done) {
- superagent.get('http://localhost:9000' + path, cb);
+get = function (path, cb) {
+ request().get(path).set('Host', 'example.com').end(cb);
+};
+
+request = function () {
+ return supertest(server);
};
respond = function (cb) {
View
@@ -9,7 +9,7 @@ describe('json', function () {
res.json({ foo: 'bar' });
});
- get('/?callback=foo', function (res) {
+ get('/?callback=foo', function (err, res) {
assert.equal(res.text, '{\n "foo": "bar"\n}');
done();
});
@@ -21,7 +21,7 @@ describe('json', function () {
res.json({ hello: 'world' });
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.statusCode, 200);
assert.equal(res.headers['content-type'], 'application/vnd.example+json');
done();
@@ -34,7 +34,7 @@ describe('json', function () {
res.json(null);
});
- get('/', function (res){
+ get('/', function (err, res){
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, 'null');
done();
@@ -48,7 +48,7 @@ describe('json', function () {
res.json(['foo', 'bar', 'baz']);
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '[\n "foo",\n "bar",\n "baz"\n]');
done();
@@ -62,7 +62,7 @@ describe('json', function () {
res.json({ name: 'tobi' });
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "name": "tobi"\n}');
done();
@@ -77,7 +77,7 @@ describe('json', function () {
res.json(201, { id: 1 });
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.statusCode, 201);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "id": 1\n}');
@@ -92,7 +92,7 @@ describe('json', function () {
res.json({ id: 1 }, 201);
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.statusCode, 201);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "id": 1\n}');
View
@@ -9,7 +9,7 @@ describe('jsonp', function(){
res.jsonp({ count: 1 });
});
- get('/?callback=something', function (res) {
+ get('/?callback=something', function (err, res) {
assert.equal(res.headers['content-type'], 'text/javascript; charset=utf-8');
assert.equal(res.text, 'something({\n "count": 1\n});');
done();
@@ -21,7 +21,7 @@ describe('jsonp', function(){
res.jsonp({ count: 1 });
});
- get('/?callback=callbacks[123]', function (res) {
+ get('/?callback=callbacks[123]', function (err, res) {
assert.equal(res.headers['content-type'], 'text/javascript; charset=utf-8');
assert.equal(res.text, 'callbacks[123]({\n "count": 1\n});');
done();
@@ -33,7 +33,7 @@ describe('jsonp', function(){
res.jsonp({});
});
- get('/?callback=foo;bar()', function (res) {
+ get('/?callback=foo;bar()', function (err, res) {
assert.equal(res.headers['content-type'], 'text/javascript; charset=utf-8');
assert.equal(res.text, 'foobar({});');
done();
@@ -46,7 +46,7 @@ describe('jsonp', function(){
res.jsonp(null);
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, 'null');
done();
@@ -60,7 +60,7 @@ describe('jsonp', function(){
res.jsonp(['foo', 'bar', 'baz']);
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '[\n "foo",\n "bar",\n "baz"\n]');
done();
@@ -74,7 +74,7 @@ describe('jsonp', function(){
res.jsonp({ name: 'tobi' });
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "name": "tobi"\n}');
done();
@@ -89,7 +89,7 @@ describe('jsonp', function(){
res.jsonp(201, { id: 1 });
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.statusCode, 201);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "id": 1\n}');
@@ -104,7 +104,7 @@ describe('jsonp', function(){
res.jsonp({ id: 1 }, 201);
});
- get('/', function (res) {
+ get('/', function (err, res) {
assert.equal(res.statusCode, 201);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.equal(res.text, '{\n "id": 1\n}');
Oops, something went wrong.

0 comments on commit 3ea0d0e

Please sign in to comment.