Comparar commits
91 Commits
| Autor | SHA1 | Data | |
|---|---|---|---|
| ee89ff5026 | |||
| 8df9e745d5 | |||
| c76e954504 | |||
| 136f054614 | |||
| e9a4b4f8ff | |||
| 5415c98ff9 | |||
| 39efa452fc | |||
| bddcdee3fe | |||
| 2e8d44b444 | |||
| 70d68419b0 | |||
| 26fab2a27d | |||
| 0f5560eebd | |||
| 170dcc2907 | |||
| 18d6c78ef4 | |||
| 1248f0338b | |||
| b400814d00 | |||
| a934929bb3 | |||
| 140efb574c | |||
| 3c0c96114f | |||
| 362c96c8c1 | |||
| 5db3d0f9fc | |||
| 80c814c393 | |||
| bf66465937 | |||
| 72eea7e6cd | |||
| 8f4cd13c89 | |||
| 6556cefc71 | |||
| 7bfb58920a | |||
| 2e324ccf5f | |||
| 1a10ee76b3 | |||
| 619e6349f6 | |||
| b1ff68548f | |||
| 376b6c3bad | |||
| f4c8a59b17 | |||
| e6129d8ba5 | |||
| d2baf11b8a | |||
| 82b5b12ca7 | |||
| c145ab9b81 | |||
| c10223b803 | |||
| 1e09b54ad2 | |||
| d073e0aeb5 | |||
| ce7293de13 | |||
| 108e66c24b | |||
| f90401b8c0 | |||
| a32e705d49 | |||
| 640cf4ca21 | |||
| 442e782692 | |||
| 0a874ad8b3 | |||
| 18083f0c13 | |||
| f25aaf11e9 | |||
| 9b09257b28 | |||
| f895516a2c | |||
| 76aa718b75 | |||
| 0acee67339 | |||
| 4475e335ef | |||
| 895673141d | |||
| e4cd99ae1c | |||
| c39a398d83 | |||
| 2787bd5bf0 | |||
| 6aaa7dc26d | |||
| ebf60d2340 | |||
| 02d43846f6 | |||
| 8930cd563c | |||
| 2b90cd7d51 | |||
| d5fde6a4b9 | |||
| 99b2e0fa08 | |||
| ebcb1ca90e | |||
| 1763b073f9 | |||
| 908e467548 | |||
| 3c6ad5350b | |||
| 0ff3aa4b20 | |||
| fd42b5c42c | |||
| 1311f2ac25 | |||
| 82a9817061 | |||
| 685eec0149 | |||
| 910dae16ab | |||
| fd53197b46 | |||
| d84d0b69ef | |||
| b694ba27be | |||
| e3cbac2d77 | |||
| bbaa295ee2 | |||
| 1150a88001 | |||
| 58cfd60000 | |||
| fcf268742d | |||
| 30d71c8f8f | |||
| c3f9398b12 | |||
| f1ac6ab764 | |||
| d6ca5f71bc | |||
| cdca9cf88f | |||
| 5840b42f4a | |||
| a5be68b5b2 | |||
| 9fda13bc25 |
@@ -1,4 +1,59 @@
|
||||
|
||||
3.0.0beta5 / 2012-07-03
|
||||
==================
|
||||
|
||||
* add "make check" support
|
||||
* add route-map example
|
||||
* add `res.json(obj, status)` support back for BC
|
||||
* add "methods" dep, remove internal methods module
|
||||
* update connect dep
|
||||
* update auth example to utilize cores pbkdf2
|
||||
* updated tests to use "supertest"
|
||||
|
||||
3.0.0beta4 / 2012-06-25
|
||||
==================
|
||||
|
||||
* Added `req.auth`
|
||||
* Added `req.range(size)`
|
||||
* Added `res.links(obj)`
|
||||
* Added `res.send(body, status)` support back for backwards compat
|
||||
* Added `.default()` support to `res.format()`
|
||||
* Added 2xx / 304 check to `req.fresh`
|
||||
* Revert "Added + support to the router"
|
||||
* Fixed `res.send()` freshness check, respect res.statusCode
|
||||
|
||||
3.0.0beta3 / 2012-06-15
|
||||
==================
|
||||
|
||||
* Added hogan `--hjs` to express(1) [nullfirm]
|
||||
* Added another example to content-negotiation
|
||||
* Added `fresh` dep
|
||||
* Changed: `res.send()` always checks freshness
|
||||
* Fixed: expose connects mime module. Cloases #1165
|
||||
|
||||
3.0.0beta2 / 2012-06-06
|
||||
==================
|
||||
|
||||
* Added `+` support to the router
|
||||
* Added `req.host`
|
||||
* Changed `req.param()` to check route first
|
||||
* Update connect dep
|
||||
|
||||
3.0.0beta1 / 2012-06-01
|
||||
==================
|
||||
|
||||
* Added `res.format()` callback to override default 406 behaviour
|
||||
* Fixed `res.redirect()` 406. Closes #1154
|
||||
|
||||
3.0.0alpha5 / 2012-05-30
|
||||
==================
|
||||
|
||||
* Added `req.ip`
|
||||
* Added `{ signed: true }` option to `res.cookie()`
|
||||
* Removed `res.signedCookie()`
|
||||
* Changed: dont reverse `req.ips`
|
||||
* Fixed "trust proxy" setting check for `req.ips`
|
||||
|
||||
3.0.0alpha4 / 2012-05-09
|
||||
==================
|
||||
|
||||
|
||||
+5
-1
@@ -10,15 +10,19 @@ docs/%.md: lib/%.js
|
||||
@mkdir -p docs
|
||||
dox --raw < $< | ./support/docs > $@
|
||||
|
||||
check: test
|
||||
|
||||
test: test-unit test-acceptance
|
||||
|
||||
test-unit:
|
||||
@NODE_ENV=test ./node_modules/.bin/mocha \
|
||||
--reporter $(REPORTER)
|
||||
--reporter $(REPORTER) \
|
||||
--bail
|
||||
|
||||
test-acceptance:
|
||||
@NODE_ENV=test ./node_modules/.bin/mocha \
|
||||
--reporter $(REPORTER) \
|
||||
--bail \
|
||||
test/acceptance/*.js
|
||||
|
||||
test-cov: lib-cov
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ app.listen(3000);
|
||||
First install the dev dependencies to install all the example / test suite deps:
|
||||
|
||||
$ cd express
|
||||
$ npm install -d
|
||||
$ npm install
|
||||
|
||||
then run whichever tests you want:
|
||||
|
||||
|
||||
+26
-4
@@ -19,6 +19,7 @@ program
|
||||
.option('-s, --sessions', 'add session support')
|
||||
.option('-e, --ejs', 'add ejs engine support (defaults to jade)')
|
||||
.option('-J, --jshtml', 'add jshtml engine support (defaults to jade)')
|
||||
.option('-h, --hogan', 'add hogan.js engine support')
|
||||
.option('-c, --css <engine>', 'add stylesheet <engine> support (less|stylus) (defaults to plain css)')
|
||||
.option('-f, --force', 'force on non-empty directory')
|
||||
.parse(process.argv);
|
||||
@@ -36,6 +37,7 @@ var eol = 'win32' == os.platform() ? '\r\n' : '\n'
|
||||
program.template = 'jade';
|
||||
if (program.ejs) program.template = 'ejs';
|
||||
if (program.jshtml) program.template = 'jshtml';
|
||||
if (program.hogan) program.template = 'hjs';
|
||||
|
||||
/**
|
||||
* Routes index template.
|
||||
@@ -57,7 +59,7 @@ var index = [
|
||||
*/
|
||||
|
||||
var jadeLayout = [
|
||||
'!!!'
|
||||
'doctype 5'
|
||||
, 'html'
|
||||
, ' head'
|
||||
, ' title= title'
|
||||
@@ -122,6 +124,23 @@ var jshtmlIndex = [
|
||||
, '<p>Welcome to @write(title)</p>'
|
||||
].join(eol);
|
||||
|
||||
/**
|
||||
* Hogan.js index template.
|
||||
*/
|
||||
var hoganIndex = [
|
||||
'<!DOCTYPE html>'
|
||||
, '<html>'
|
||||
, ' <head>'
|
||||
, ' <title>{{ title }}</title>'
|
||||
, ' <link rel=\'stylesheet\' href=\'/stylesheets/style.css\' />'
|
||||
, ' </head>'
|
||||
, ' <body>'
|
||||
, ' <h1>{{ title }}</h1>'
|
||||
, ' <p>Welcome to {{ title }}</p>'
|
||||
, ' </body>'
|
||||
, '</html>'
|
||||
].join(eol);
|
||||
|
||||
/**
|
||||
* Default css template.
|
||||
*/
|
||||
@@ -185,10 +204,10 @@ var app = [
|
||||
, ' app.set(\'views\', __dirname + \'/views\');'
|
||||
, ' app.set(\'view engine\', \':TEMPLATE\');'
|
||||
, ' app.use(express.favicon());'
|
||||
, ' app.use(express.logger(\'dev\'));{css}'
|
||||
, ' app.use(express.logger(\'dev\'));'
|
||||
, ' app.use(express.bodyParser());'
|
||||
, ' app.use(express.methodOverride());{sess}'
|
||||
, ' app.use(app.router);'
|
||||
, ' app.use(app.router);{css}'
|
||||
, ' app.use(express.static(__dirname + \'/public\'));'
|
||||
, '});'
|
||||
, ''
|
||||
@@ -198,7 +217,7 @@ var app = [
|
||||
, ''
|
||||
, 'app.get(\'/\', routes.index);'
|
||||
, ''
|
||||
, 'http.createServer(app).listen(app.get(\'port\'), function() {'
|
||||
, 'http.createServer(app).listen(app.get(\'port\'), function(){'
|
||||
, ' console.log("Express server listening on port " + app.get(\'port\'));'
|
||||
, '});'
|
||||
, ''
|
||||
@@ -275,6 +294,9 @@ function createApplicationAt(path) {
|
||||
write(path + '/views/layout.jshtml', jshtmlLayout);
|
||||
write(path + '/views/index.jshtml', jshtmlIndex);
|
||||
break;
|
||||
case 'hjs':
|
||||
write(path + '/views/index.hjs', hoganIndex);
|
||||
break;
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
+17
-22
@@ -220,7 +220,7 @@
|
||||
|
||||
// Accept: text/html
|
||||
req.accepts('html');
|
||||
// => "html"
|
||||
// => "html"
|
||||
|
||||
// Accept: text/*, application/json
|
||||
req.accepts('html');
|
||||
@@ -256,8 +256,8 @@
|
||||
|
||||
Return the value of param `name` when present or `defaultValue`.
|
||||
|
||||
- Checks body params, ex: id=12, {"id":12}
|
||||
- Checks route placeholders, ex: _/user/:id_
|
||||
- Checks body params, ex: id=12, {"id":12}
|
||||
- Checks query string params, ex: ?id=12
|
||||
|
||||
To utilize request bodies, `req.body`
|
||||
@@ -285,17 +285,6 @@
|
||||
|
||||
req.is('html');
|
||||
// => false
|
||||
|
||||
Now within our route callbacks, we can use to to assert content types
|
||||
such as "image/jpeg", "image/png", etc.
|
||||
|
||||
app.post('/image/upload', function(req, res, next){
|
||||
if (req.is('image/*')) {
|
||||
// do something
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
# res
|
||||
@@ -372,6 +361,8 @@
|
||||
and optional callback `fn(err)`. The callback is invoked
|
||||
when the data transfer is complete, or when an error has
|
||||
ocurred. Be sure to check `res.headerSent` if you plan to respond.
|
||||
|
||||
This method uses `res.sendfile()`.
|
||||
|
||||
# res.format()
|
||||
|
||||
@@ -404,7 +395,7 @@
|
||||
});
|
||||
|
||||
In addition to canonicalized MIME types you may
|
||||
## also use extnames mapped to these types
|
||||
also use extnames mapped to these types:
|
||||
|
||||
res.format({
|
||||
text: function(){
|
||||
@@ -417,22 +408,30 @@
|
||||
|
||||
json: function(){
|
||||
res.send({ message: 'hey' });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
By default Express passes an `Error`
|
||||
with a `.status` of 406 to `next(err)`
|
||||
if a match is not made, however you may
|
||||
provide an optional callback `fn` to
|
||||
be invoked instead.
|
||||
|
||||
# res.attachment()
|
||||
|
||||
Set _Content-Disposition_ header to _attachment_ with optional `filename`.
|
||||
|
||||
# res.set()
|
||||
# res.set
|
||||
|
||||
Set header `field` to `val`, or pass
|
||||
an object of of header fields.
|
||||
an object of header fields.
|
||||
|
||||
## Examples
|
||||
|
||||
res.set('Accept', 'application/json');
|
||||
res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
||||
|
||||
Aliased as `res.header()`.
|
||||
|
||||
# res.get()
|
||||
|
||||
@@ -442,11 +441,6 @@
|
||||
|
||||
Clear cookie `name`.
|
||||
|
||||
# res.signedCookie()
|
||||
|
||||
Set a signed cookie with the given `name` and `val`.
|
||||
See `res.cookie()` for details.
|
||||
|
||||
# res.cookie()
|
||||
|
||||
Set cookie `name` to `val`, with the given `options`.
|
||||
@@ -454,6 +448,7 @@
|
||||
## Options
|
||||
|
||||
- `maxAge` max-age in milliseconds, converted to `expires`
|
||||
- `signed` sign the cookie
|
||||
- `path` defaults to "/"
|
||||
|
||||
## Examples
|
||||
|
||||
+2
-13
@@ -39,7 +39,7 @@
|
||||
|
||||
// Accept: text/html
|
||||
req.accepts('html');
|
||||
// => "html"
|
||||
// => "html"
|
||||
|
||||
// Accept: text/*, application/json
|
||||
req.accepts('html');
|
||||
@@ -75,8 +75,8 @@
|
||||
|
||||
Return the value of param `name` when present or `defaultValue`.
|
||||
|
||||
- Checks body params, ex: id=12, {"id":12}
|
||||
- Checks route placeholders, ex: _/user/:id_
|
||||
- Checks body params, ex: id=12, {"id":12}
|
||||
- Checks query string params, ex: ?id=12
|
||||
|
||||
To utilize request bodies, `req.body`
|
||||
@@ -104,15 +104,4 @@
|
||||
|
||||
req.is('html');
|
||||
// => false
|
||||
|
||||
Now within our route callbacks, we can use to to assert content types
|
||||
such as "image/jpeg", "image/png", etc.
|
||||
|
||||
app.post('/image/upload', function(req, res, next){
|
||||
if (req.is('image/*')) {
|
||||
// do something
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+15
-9
@@ -73,6 +73,8 @@
|
||||
and optional callback `fn(err)`. The callback is invoked
|
||||
when the data transfer is complete, or when an error has
|
||||
ocurred. Be sure to check `res.headerSent` if you plan to respond.
|
||||
|
||||
This method uses `res.sendfile()`.
|
||||
|
||||
# res.format()
|
||||
|
||||
@@ -105,7 +107,7 @@
|
||||
});
|
||||
|
||||
In addition to canonicalized MIME types you may
|
||||
## also use extnames mapped to these types
|
||||
also use extnames mapped to these types:
|
||||
|
||||
res.format({
|
||||
text: function(){
|
||||
@@ -118,22 +120,30 @@
|
||||
|
||||
json: function(){
|
||||
res.send({ message: 'hey' });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
By default Express passes an `Error`
|
||||
with a `.status` of 406 to `next(err)`
|
||||
if a match is not made, however you may
|
||||
provide an optional callback `fn` to
|
||||
be invoked instead.
|
||||
|
||||
# res.attachment()
|
||||
|
||||
Set _Content-Disposition_ header to _attachment_ with optional `filename`.
|
||||
|
||||
# res.set()
|
||||
# res.set
|
||||
|
||||
Set header `field` to `val`, or pass
|
||||
an object of of header fields.
|
||||
an object of header fields.
|
||||
|
||||
## Examples
|
||||
|
||||
res.set('Accept', 'application/json');
|
||||
res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
||||
|
||||
Aliased as `res.header()`.
|
||||
|
||||
# res.get()
|
||||
|
||||
@@ -143,11 +153,6 @@
|
||||
|
||||
Clear cookie `name`.
|
||||
|
||||
# res.signedCookie()
|
||||
|
||||
Set a signed cookie with the given `name` and `val`.
|
||||
See `res.cookie()` for details.
|
||||
|
||||
# res.cookie()
|
||||
|
||||
Set cookie `name` to `val`, with the given `options`.
|
||||
@@ -155,6 +160,7 @@
|
||||
## Options
|
||||
|
||||
- `maxAge` max-age in milliseconds, converted to `expires`
|
||||
- `signed` sign the cookie
|
||||
- `path` defaults to "/"
|
||||
|
||||
## Examples
|
||||
|
||||
+19
-19
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
var express = require('../../lib/express')
|
||||
, crypto = require('crypto');
|
||||
, hash = require('./pass').hash;
|
||||
|
||||
var app = module.exports = express();
|
||||
|
||||
@@ -27,24 +27,22 @@ app.locals.use(function(req,res){
|
||||
if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
|
||||
})
|
||||
|
||||
// Generate a salt for the user to prevent rainbow table attacks
|
||||
// for better security take a look at the bcrypt c++ addon:
|
||||
// https://github.com/ncb000gt/node.bcrypt.js
|
||||
// dummy database
|
||||
|
||||
var users = {
|
||||
tj: {
|
||||
name: 'tj'
|
||||
, salt: 'randomly-generated-salt'
|
||||
, pass: hash('foobar', 'randomly-generated-salt')
|
||||
}
|
||||
tj: { name: 'tj' }
|
||||
};
|
||||
|
||||
// Used to generate a hash of the plain-text password + salt
|
||||
function hash(msg, key) {
|
||||
return crypto
|
||||
.createHmac('sha256', key)
|
||||
.update(msg)
|
||||
.digest('hex');
|
||||
}
|
||||
// when you create a user, generate a salt
|
||||
// and hash the password ('foobar' is the pass here)
|
||||
|
||||
hash('foobar', function(err, salt, hash){
|
||||
if (err) throw err;
|
||||
// store the salt & hash in the "db"
|
||||
users.tj.salt = salt;
|
||||
users.tj.hash = hash;
|
||||
});
|
||||
|
||||
|
||||
// Authenticate using our plain-object database of doom!
|
||||
function authenticate(name, pass, fn) {
|
||||
@@ -55,9 +53,11 @@ function authenticate(name, pass, fn) {
|
||||
// apply the same algorithm to the POSTed password, applying
|
||||
// the hash against the pass / salt, if there is a match we
|
||||
// found the user
|
||||
if (user.pass == hash(pass, user.salt)) return fn(null, user);
|
||||
// Otherwise password is invalid
|
||||
fn(new Error('invalid password'));
|
||||
hash(pass, user.salt, function(err, hash){
|
||||
if (err) return fn(err);
|
||||
if (hash == user.hash) return fn(null, user);
|
||||
fn(new Error('invalid password'));
|
||||
})
|
||||
}
|
||||
|
||||
function restrict(req, res, next) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
|
||||
// check out https://github.com/visionmedia/node-pwd
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var crypto = require('crypto');
|
||||
|
||||
/**
|
||||
* Bytesize.
|
||||
*/
|
||||
|
||||
var len = 128;
|
||||
|
||||
/**
|
||||
* Iterations. ~300ms
|
||||
*/
|
||||
|
||||
var iterations = 12000;
|
||||
|
||||
/**
|
||||
* Hashes a password with optional `salt`, otherwise
|
||||
* generate a salt for `pass` and invoke `fn(err, salt, hash)`.
|
||||
*
|
||||
* @param {String} password to hash
|
||||
* @param {String} optional salt
|
||||
* @param {Function} callback
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.hash = function (pwd, salt, fn) {
|
||||
if (3 == arguments.length) {
|
||||
crypto.pbkdf2(pwd, salt, iterations, len, fn);
|
||||
} else {
|
||||
fn = salt;
|
||||
crypto.randomBytes(len, function(err, salt){
|
||||
if (err) return fn(err);
|
||||
salt = salt.toString('base64');
|
||||
crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){
|
||||
if (err) return fn(err);
|
||||
fn(null, salt, hash);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
var users = [];
|
||||
|
||||
users.push({ name: 'Tobi' });
|
||||
users.push({ name: 'Loki' });
|
||||
users.push({ name: 'Jane' });
|
||||
|
||||
module.exports = users;
|
||||
@@ -1,12 +1,7 @@
|
||||
|
||||
var express = require('../../')
|
||||
, app = module.exports = express();
|
||||
|
||||
var users = [];
|
||||
|
||||
users.push({ name: 'Tobi' });
|
||||
users.push({ name: 'Loki' });
|
||||
users.push({ name: 'Jane' });
|
||||
, app = module.exports = express()
|
||||
, users = require('./db');
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.format({
|
||||
@@ -28,6 +23,18 @@ app.get('/', function(req, res){
|
||||
})
|
||||
});
|
||||
|
||||
// or you could write a tiny middleware like
|
||||
// this to abstract make things a bit more declarative:
|
||||
|
||||
function format(mod) {
|
||||
var obj = require(mod);
|
||||
return function(req, res){
|
||||
res.format(obj);
|
||||
}
|
||||
}
|
||||
|
||||
app.get('/users', format('./users'));
|
||||
|
||||
if (!module.parent) {
|
||||
app.listen(3000);
|
||||
console.log('listening on port 3000');
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
var users = require('./db');
|
||||
|
||||
exports.html = function(req, res){
|
||||
res.send('<ul>' + users.map(function(user){
|
||||
return '<li>' + user.name + '</li>';
|
||||
}).join('') + '</ul>');
|
||||
};
|
||||
|
||||
exports.text = function(req, res){
|
||||
res.send(users.map(function(user){
|
||||
return ' - ' + user.name + '\n';
|
||||
}).join(''));
|
||||
};
|
||||
|
||||
exports.json = function(req, res){
|
||||
res.json(users);
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
var express = require('../../lib/express')
|
||||
, verbose = process.env.NODE_ENV != 'test'
|
||||
, app = module.exports = express();
|
||||
|
||||
app.map = function(a, route){
|
||||
route = route || '';
|
||||
for (var key in a) {
|
||||
switch (typeof a[key]) {
|
||||
// { '/path': { ... }}
|
||||
case 'object':
|
||||
app.map(a[key], route + key);
|
||||
break;
|
||||
// get: function(){ ... }
|
||||
case 'function':
|
||||
if (verbose) console.log('%s %s', key, route);
|
||||
app[key](route, a[key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var users = {
|
||||
list: function(req, res){
|
||||
res.send('user list');
|
||||
},
|
||||
|
||||
get: function(req, res){
|
||||
res.send('user ' + req.params.uid);
|
||||
},
|
||||
|
||||
del: function(req, res){
|
||||
res.send('delete users');
|
||||
}
|
||||
};
|
||||
|
||||
var pets = {
|
||||
list: function(req, res){
|
||||
res.send('user ' + req.params.uid + '\'s pets');
|
||||
},
|
||||
|
||||
del: function(req, res){
|
||||
res.send('delete ' + req.params.uid + '\'s pet ' + req.params.pid);
|
||||
}
|
||||
};
|
||||
|
||||
app.map({
|
||||
'/users': {
|
||||
get: users.list,
|
||||
del: users.del,
|
||||
'/:uid': {
|
||||
get: users.get,
|
||||
'/pets': {
|
||||
get: pets.list,
|
||||
'/:pid': {
|
||||
del: pets.del
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
@@ -1,11 +1,10 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var connect = require('connect')
|
||||
, Router = require('./router')
|
||||
, methods = Router.methods.concat('del', 'all')
|
||||
, methods = require('methods')
|
||||
, middleware = require('./middleware')
|
||||
, debug = require('debug')('express:application')
|
||||
, locals = require('./utils').locals
|
||||
@@ -427,7 +426,6 @@ methods.forEach(function(method){
|
||||
app.all = function(path){
|
||||
var args = arguments;
|
||||
methods.forEach(function(method){
|
||||
if ('all' == method || 'del' == method) return;
|
||||
app[method].apply(this, args);
|
||||
}, this);
|
||||
return this;
|
||||
|
||||
+7
-7
@@ -22,7 +22,13 @@ exports = module.exports = createApplication;
|
||||
* Framework version.
|
||||
*/
|
||||
|
||||
exports.version = '3.0.0alpha4';
|
||||
exports.version = '3.0.0beta5';
|
||||
|
||||
/**
|
||||
* Expose mime.
|
||||
*/
|
||||
|
||||
exports.mime = connect.mime;
|
||||
|
||||
/**
|
||||
* Create an express application.
|
||||
@@ -73,12 +79,6 @@ exports.response = res;
|
||||
exports.Route = Route;
|
||||
exports.Router = Router;
|
||||
|
||||
/**
|
||||
* Expose HTTP methods.
|
||||
*/
|
||||
|
||||
exports.methods = require('./router/methods');
|
||||
|
||||
// Error handler title
|
||||
|
||||
exports.errorHandler.title = 'Express';
|
||||
|
||||
+104
-31
@@ -6,8 +6,10 @@
|
||||
var http = require('http')
|
||||
, utils = require('./utils')
|
||||
, connect = require('connect')
|
||||
, fresh = require('fresh')
|
||||
, parseRange = require('range-parser')
|
||||
, parse = connect.utils.parseUrl
|
||||
, mime = require('mime');
|
||||
, mime = connect.mime;
|
||||
|
||||
/**
|
||||
* Request prototype.
|
||||
@@ -131,6 +133,32 @@ req.acceptsLanguage = function(lang){
|
||||
: true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse Range header field,
|
||||
* capping to the given `size`.
|
||||
*
|
||||
* Unspecified ranges such as "0-" require
|
||||
* knowledge of your resource length. In
|
||||
* the case of a byte range this is of course
|
||||
* the total number of bytes. If the Range
|
||||
* header field is not given `null` is returned,
|
||||
* `-1` when unsatisfiable, `-2` when syntactically invalid.
|
||||
*
|
||||
* NOTE: remember that ranges are inclusive, so
|
||||
* for example "Range: users=0-3" should respond
|
||||
* with 4 users when available, not 3.
|
||||
*
|
||||
* @param {Number} size
|
||||
* @return {Array}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.range = function(size){
|
||||
var range = this.get('Range');
|
||||
if (!range) return;
|
||||
return parseRange(size, range);
|
||||
};
|
||||
|
||||
/**
|
||||
* Return an array of Accepted media types
|
||||
* ordered from highest quality to lowest.
|
||||
@@ -208,8 +236,8 @@ req.__defineGetter__('acceptedCharsets', function(){
|
||||
/**
|
||||
* Return the value of param `name` when present or `defaultValue`.
|
||||
*
|
||||
* - Checks body params, ex: id=12, {"id":12}
|
||||
* - Checks route placeholders, ex: _/user/:id_
|
||||
* - Checks body params, ex: id=12, {"id":12}
|
||||
* - Checks query string params, ex: ?id=12
|
||||
*
|
||||
* To utilize request bodies, `req.body`
|
||||
@@ -223,19 +251,12 @@ req.__defineGetter__('acceptedCharsets', function(){
|
||||
*/
|
||||
|
||||
req.param = function(name, defaultValue){
|
||||
// req.body
|
||||
if (this.body && undefined !== this.body[name]) return this.body[name];
|
||||
|
||||
// route params
|
||||
if (this.params
|
||||
&& this.params.hasOwnProperty(name)
|
||||
&& undefined !== this.params[name]) {
|
||||
return this.params[name];
|
||||
}
|
||||
|
||||
// query-string
|
||||
if (undefined !== this.query[name]) return this.query[name];
|
||||
|
||||
var params = this.params || {};
|
||||
var body = this.body || {};
|
||||
var query = this.query || {};
|
||||
if (null != params[name] && params.hasOwnProperty(name)) return params[name];
|
||||
if (null != body[name]) return body[name];
|
||||
if (null != query[name]) return query[name];
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
@@ -260,17 +281,6 @@ req.param = function(name, defaultValue){
|
||||
* req.is('html');
|
||||
* // => false
|
||||
*
|
||||
* Now within our route callbacks, we can use to to assert content types
|
||||
* such as "image/jpeg", "image/png" as shown here:
|
||||
*
|
||||
* app.post('/image/upload', function(req, res, next){
|
||||
* if (req.is('image/*')) {
|
||||
* // do something
|
||||
* } else {
|
||||
* next();
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @param {String} type
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
@@ -325,12 +335,25 @@ req.__defineGetter__('secure', function(){
|
||||
return 'https' == this.protocol;
|
||||
});
|
||||
|
||||
/**
|
||||
* Return the remote address, or when
|
||||
* "trust proxy" is `true` return
|
||||
* the upstream addr.
|
||||
*
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.__defineGetter__('ip', function(){
|
||||
return this.ips[0] || this.connection.remoteAddress;
|
||||
});
|
||||
|
||||
/**
|
||||
* When "trust proxy" is `true`, parse
|
||||
* the "X-Forwarded-For" ip address list.
|
||||
*
|
||||
* For example if the value were "client, proxy1, proxy2"
|
||||
* you would receive the array `["proxy2", "proxy1", "client"]`
|
||||
* you would receive the array `["client", "proxy1", "proxy2"]`
|
||||
* where "proxy2" is the furthest down-stream.
|
||||
*
|
||||
* @return {Array}
|
||||
@@ -338,12 +361,40 @@ req.__defineGetter__('secure', function(){
|
||||
*/
|
||||
|
||||
req.__defineGetter__('ips', function(){
|
||||
var trustProxy = this.app.get('trust proxy');
|
||||
var val = this.get('X-Forwarded-For');
|
||||
return val
|
||||
? val.split(/ *, */).reverse()
|
||||
return trustProxy && val
|
||||
? val.split(/ *, */)
|
||||
: [];
|
||||
});
|
||||
|
||||
/**
|
||||
* Return basic auth credentials.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* // http://tobi:hello@example.com
|
||||
* req.auth
|
||||
* // => { username: 'tobi', password: 'hello' }
|
||||
*
|
||||
* @return {Object}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.__defineGetter__('auth', function(){
|
||||
// missing
|
||||
var auth = this.get('Authorization');
|
||||
if (!auth) return {};
|
||||
|
||||
// malformed
|
||||
auth = auth.split(' ')[1];
|
||||
if (!auth) return {};
|
||||
|
||||
// credentials
|
||||
auth = new Buffer(auth, 'base64').toString().split(':');
|
||||
return { username: auth[0], password: auth[1] };
|
||||
});
|
||||
|
||||
/**
|
||||
* Return subdomains as an array.
|
||||
*
|
||||
@@ -372,6 +423,17 @@ req.__defineGetter__('path', function(){
|
||||
return parse(this).pathname;
|
||||
});
|
||||
|
||||
/**
|
||||
* Parse the "Host" header field hostname.
|
||||
*
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
req.__defineGetter__('host', function(){
|
||||
return this.get('Host').split(':')[0];
|
||||
});
|
||||
|
||||
/**
|
||||
* Check if the request is fresh, aka
|
||||
* Last-Modified and/or the ETag
|
||||
@@ -382,7 +444,18 @@ req.__defineGetter__('path', function(){
|
||||
*/
|
||||
|
||||
req.__defineGetter__('fresh', function(){
|
||||
return ! this.stale;
|
||||
var method = this.method;
|
||||
var s = this.res.statusCode;
|
||||
|
||||
// GET or HEAD for weak freshness validation only
|
||||
if ('GET' != method && 'HEAD' != method) return false;
|
||||
|
||||
// 2xx or 304 as per rfc2616 14.26
|
||||
if ((s >= 200 && s < 300) || 304 == s) {
|
||||
return fresh(this.headers, this.res._headers);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -395,7 +468,7 @@ req.__defineGetter__('fresh', function(){
|
||||
*/
|
||||
|
||||
req.__defineGetter__('stale', function(){
|
||||
return connect.utils.modified(this, this.res);
|
||||
return !this.fresh;
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
+77
-34
@@ -12,8 +12,9 @@ var fs = require('fs')
|
||||
, normalizeTypes = require('./utils').normalizeTypes
|
||||
, statusCodes = http.STATUS_CODES
|
||||
, send = connect.static.send
|
||||
, cookie = require('cookie')
|
||||
, crc = require('crc')
|
||||
, mime = require('mime')
|
||||
, mime = connect.mime
|
||||
, basename = path.basename
|
||||
, extname = path.extname
|
||||
, join = path.join;
|
||||
@@ -39,6 +40,27 @@ res.status = function(code){
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set Link header field with the given `links`.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* res.links({
|
||||
* next: 'http://api.example.com/users?page=2',
|
||||
* last: 'http://api.example.com/users?page=5'
|
||||
* });
|
||||
*
|
||||
* @param {Object} links
|
||||
* @return {ServerResponse}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
res.links = function(links){
|
||||
return this.set('Link', Object.keys(links).map(function(rel){
|
||||
return '<' + links[rel] + '>; rel="' + rel + '"';
|
||||
}).join(', '));
|
||||
};
|
||||
|
||||
/**
|
||||
* Send a response.
|
||||
*
|
||||
@@ -63,10 +85,16 @@ res.send = function(body){
|
||||
|
||||
// allow status / body
|
||||
if (2 == arguments.length) {
|
||||
this.statusCode = body;
|
||||
body = arguments[1];
|
||||
// res.send(body, status) backwards compat
|
||||
if ('number' != typeof body && 'number' == typeof arguments[1]) {
|
||||
this.statusCode = arguments[1];
|
||||
} else {
|
||||
this.statusCode = body;
|
||||
body = arguments[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// convert string objects to primitives
|
||||
if (body instanceof String) body = body.toString();
|
||||
|
||||
@@ -109,9 +137,11 @@ res.send = function(body){
|
||||
if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)
|
||||
? crc.buffer.crc32(body)
|
||||
: crc.crc32(body));
|
||||
if (req.fresh) this.statusCode = 304;
|
||||
}
|
||||
|
||||
// freshness
|
||||
if (req.fresh) this.statusCode = 304;
|
||||
|
||||
// strip irrelevant headers
|
||||
if (204 == this.statusCode || 304 == this.statusCode) {
|
||||
this.removeHeader('Content-Type');
|
||||
@@ -143,10 +173,16 @@ res.send = function(body){
|
||||
res.json = function(obj){
|
||||
// allow status / body
|
||||
if (2 == arguments.length) {
|
||||
this.statusCode = obj;
|
||||
obj = arguments[1];
|
||||
// res.json(body, status) backwards compat
|
||||
if ('number' == typeof arguments[1]) {
|
||||
this.statusCode = arguments[1];
|
||||
} else {
|
||||
this.statusCode = obj;
|
||||
obj = arguments[1];
|
||||
}
|
||||
}
|
||||
|
||||
// settings
|
||||
var app = this.app
|
||||
, jsonp = app.get('jsonp callback')
|
||||
, replacer = app.get('json replacer')
|
||||
@@ -154,9 +190,11 @@ res.json = function(obj){
|
||||
, body = JSON.stringify(obj, replacer, spaces)
|
||||
, callback = this.req.query.callback;
|
||||
|
||||
// content-type
|
||||
this.charset = this.charset || 'utf-8';
|
||||
this.set('Content-Type', 'application/json');
|
||||
|
||||
// jsonp
|
||||
if (callback && jsonp) {
|
||||
this.set('Content-Type', 'text/javascript');
|
||||
body = callback.replace(/[^[]\w$.]/g, '') + '(' + body + ');';
|
||||
@@ -251,7 +289,7 @@ res.sendfile = function(path, options, fn){
|
||||
* when the data transfer is complete, or when an error has
|
||||
* ocurred. Be sure to check `res.headerSent` if you plan to respond.
|
||||
*
|
||||
* This method uses `res.attachment()` and `res.sendfile()`.
|
||||
* This method uses `res.sendfile()`.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {String|Function} filename or fn
|
||||
@@ -266,7 +304,9 @@ res.download = function(path, filename, fn){
|
||||
filename = null;
|
||||
}
|
||||
|
||||
return this.attachment(filename || path).sendfile(path, fn);
|
||||
filename = filename || path;
|
||||
this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');
|
||||
return this.sendfile(path, fn);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -339,6 +379,12 @@ res.type = function(type){
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* By default Express passes an `Error`
|
||||
* with a `.status` of 406 to `next(err)`
|
||||
* if a match is not made. If you provide
|
||||
* a `.default` callback it will be invoked
|
||||
* instead.
|
||||
*
|
||||
* @param {Object} obj
|
||||
* @return {ServerResponse} for chaining
|
||||
* @api public
|
||||
@@ -347,14 +393,20 @@ res.type = function(type){
|
||||
res.format = function(obj){
|
||||
var keys = Object.keys(obj)
|
||||
, req = this.req
|
||||
, next = req.next
|
||||
, key = req.accepts(keys);
|
||||
, next = req.next;
|
||||
|
||||
var fn = obj.default;
|
||||
if (fn) delete obj.default;
|
||||
|
||||
var key = req.accepts(keys);
|
||||
|
||||
this.set('Vary', 'Accept');
|
||||
|
||||
if (key) {
|
||||
this.set('Content-Type', normalizeType(key));
|
||||
obj[key](req, this, next);
|
||||
} else if (fn) {
|
||||
fn();
|
||||
} else {
|
||||
var err = new Error('Not Acceptable');
|
||||
err.status = 406;
|
||||
@@ -383,7 +435,7 @@ res.attachment = function(filename){
|
||||
|
||||
/**
|
||||
* Set header `field` to `val`, or pass
|
||||
* an object of of header fields.
|
||||
* an object of header fields.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
@@ -438,30 +490,13 @@ res.clearCookie = function(name, options){
|
||||
: opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set a signed cookie with the given `name` and `val`.
|
||||
* See `res.cookie()` for details.
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {String|Object} val
|
||||
* @param {Object} options
|
||||
* @api public
|
||||
*/
|
||||
|
||||
res.signedCookie = function(name, val, options){
|
||||
var secret = this.req.secret;
|
||||
if (!secret) throw new Error('connect.cookieParser("secret") required for signed cookies');
|
||||
if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
|
||||
val = utils.sign(val, secret);
|
||||
return this.cookie(name, val, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set cookie `name` to `val`, with the given `options`.
|
||||
*
|
||||
* Options:
|
||||
*
|
||||
* - `maxAge` max-age in milliseconds, converted to `expires`
|
||||
* - `signed` sign the cookie
|
||||
* - `path` defaults to "/"
|
||||
*
|
||||
* Examples:
|
||||
@@ -480,11 +515,14 @@ res.signedCookie = function(name, val, options){
|
||||
|
||||
res.cookie = function(name, val, options){
|
||||
options = options || {};
|
||||
var secret = this.req.secret;
|
||||
var signed = options.signed;
|
||||
if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');
|
||||
if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
|
||||
if (signed) val = utils.sign(val, secret);
|
||||
if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);
|
||||
if (null == options.path) options.path = '/';
|
||||
var cookie = utils.serializeCookie(name, val, options);
|
||||
this.set('Set-Cookie', cookie);
|
||||
this.set('Set-Cookie', cookie.serialize(name, String(val), options));
|
||||
return this;
|
||||
};
|
||||
|
||||
@@ -545,7 +583,7 @@ res.redirect = function(url){
|
||||
var path = app.path();
|
||||
|
||||
// relative to path
|
||||
if (0 == url.indexOf('./') || 0 == url.indexOf('..')) {
|
||||
if ('.' == url[0]) {
|
||||
url = req.path + '/' + url;
|
||||
// relative to mount-point
|
||||
} else if ('/' != url[0]) {
|
||||
@@ -565,12 +603,17 @@ res.redirect = function(url){
|
||||
|
||||
html: function(){
|
||||
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
|
||||
},
|
||||
|
||||
default: function(){
|
||||
body = '';
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Respond
|
||||
this.statusCode = status;
|
||||
this.set('Location', url);
|
||||
this.set('Content-Length', Buffer.byteLength(body));
|
||||
this.end(head ? null : body);
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
var Route = require('./route')
|
||||
, utils = require('../utils')
|
||||
, debug = require('debug')('express:router')
|
||||
, parse = require('connect').utils.parseUrl;
|
||||
, parse = require('connect').utils.parseUrl
|
||||
, methods = require('methods');
|
||||
|
||||
/**
|
||||
* Expose `Router` constructor.
|
||||
@@ -14,12 +15,6 @@ var Route = require('./route')
|
||||
|
||||
exports = module.exports = Router;
|
||||
|
||||
/**
|
||||
* Expose HTTP methods.
|
||||
*/
|
||||
|
||||
var methods = exports.methods = require('./methods');
|
||||
|
||||
/**
|
||||
* Initialize a new `Router` with the given `options`.
|
||||
*
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
/**
|
||||
* HTTP methods supported by node.
|
||||
*/
|
||||
|
||||
module.exports = [
|
||||
'get'
|
||||
, 'post'
|
||||
, 'put'
|
||||
, 'head'
|
||||
, 'delete'
|
||||
, 'options'
|
||||
, 'trace'
|
||||
, 'copy'
|
||||
, 'lock'
|
||||
, 'mkcol'
|
||||
, 'move'
|
||||
, 'propfind'
|
||||
, 'proppatch'
|
||||
, 'unlock'
|
||||
, 'report'
|
||||
, 'mkactivity'
|
||||
, 'checkout'
|
||||
, 'merge'
|
||||
, 'm-search'
|
||||
, 'notify'
|
||||
, 'subscribe'
|
||||
, 'unsubscribe'
|
||||
, 'patch'
|
||||
];
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mime = require('mime');
|
||||
var mime = require('connect').mime;
|
||||
|
||||
/**
|
||||
* Make `locals()` bound to the given `obj`.
|
||||
|
||||
+11
-6
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "express",
|
||||
"description": "Sinatra inspired web development framework",
|
||||
"version": "3.0.0alpha4",
|
||||
"version": "3.0.0beta5",
|
||||
"author": "TJ Holowaychuk <tj@vision-media.ca>",
|
||||
"contributors": [
|
||||
{ "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" },
|
||||
@@ -10,21 +10,26 @@
|
||||
{ "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
|
||||
],
|
||||
"dependencies": {
|
||||
"connect": "2.2.2",
|
||||
"commander": "0.5.2",
|
||||
"mime": "1.2.5",
|
||||
"mkdirp": "0.3.1",
|
||||
"connect": "2.3.6",
|
||||
"commander": "0.6.1",
|
||||
"range-parser": "0.0.4",
|
||||
"mkdirp": "0.3.3",
|
||||
"cookie": "0.0.3",
|
||||
"crc": "0.2.0",
|
||||
"fresh": "0.1.0",
|
||||
"methods": "0.0.1",
|
||||
"debug": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ejs": "*",
|
||||
"mocha": "*",
|
||||
"jade": "*",
|
||||
"hjs": "*",
|
||||
"stylus": "*",
|
||||
"should": "*",
|
||||
"connect-redis": "*",
|
||||
"github-flavored-markdown": "*"
|
||||
"github-flavored-markdown": "*",
|
||||
"supertest": "0.0.1"
|
||||
},
|
||||
"publishConfig": { "tag": "3.0" },
|
||||
"keywords": ["express", "framework", "sinatra", "web", "rest", "restful", "router"],
|
||||
|
||||
+10
-61
@@ -1,8 +1,8 @@
|
||||
var app = require('../../examples/auth/app')
|
||||
, request = require('../support/http');
|
||||
|
||||
function redirects(to,fn){
|
||||
return function(res){
|
||||
function redirects(to, fn){
|
||||
return function(err, res){
|
||||
res.statusCode.should.equal(302)
|
||||
res.headers.should.have.property('location').match(to);
|
||||
fn()
|
||||
@@ -14,80 +14,29 @@ function getCookie(res) {
|
||||
}
|
||||
|
||||
describe('auth', function(){
|
||||
var cookie;
|
||||
|
||||
describe('GET /',function(){
|
||||
it('should redirect to /login', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(redirects(/\/login$/,done))
|
||||
.get('/')
|
||||
.end(redirects(/\/login$/, done))
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /restricted (w/o cookie)',function(){
|
||||
it('should redirect to /login', function(done){
|
||||
request(app)
|
||||
.get('/restricted')
|
||||
.end(redirects(/\/login$/,done))
|
||||
.get('/restricted')
|
||||
.end(redirects(/\/login$/,done))
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /login', function(){
|
||||
it('should fail without proper credentials', function(done){
|
||||
request(app)
|
||||
.post('/login')
|
||||
.set('content-type','application/x-www-form-urlencoded')
|
||||
.write('&username=not-tj&password=foobar')
|
||||
.end(redirects(/\/login$/,done))
|
||||
})
|
||||
|
||||
it('should authenticate', function(done){
|
||||
request(app)
|
||||
.post('/login')
|
||||
.set('content-type', 'application/x-www-form-urlencoded')
|
||||
.write('username=tj&password=foobar')
|
||||
.end(function(res){
|
||||
res.statusCode.should.equal(302);
|
||||
cookie = getCookie(res);
|
||||
request(app)
|
||||
.get('/login')
|
||||
.set('Cookie', cookie)
|
||||
.end(function(res){
|
||||
res.body.should.include('Authenticated as tj');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /restricted (w. cookie)',function(){
|
||||
it('should respond with 200', function(done){
|
||||
request(app)
|
||||
.get('/restricted')
|
||||
.set('Cookie', cookie)
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /logout',function(){
|
||||
it('should respond with 302 and clear cookie',function(done){
|
||||
request(app)
|
||||
.get('/logout')
|
||||
.set('Cookie', cookie)
|
||||
.end(function(res){
|
||||
res.statusCode.should.equal(302);
|
||||
res.headers.should.not.have.property('set-cookie')
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /restricted (w. expired cookie)',function(){
|
||||
it('should respond with 302',function(done){
|
||||
request(app)
|
||||
.get('/restricted')
|
||||
.set('Cookie', cookie)
|
||||
.expect(302, done)
|
||||
.post('/login')
|
||||
.type('urlencoded')
|
||||
.send('username=not-tj&password=foobar')
|
||||
.end(redirects(/\/login$/, done))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -7,20 +7,16 @@ describe('content-negotiation', function(){
|
||||
it('should default to text/html', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<ul><li>Tobi</li><li>Loki</li><li>Jane</li></ul>');
|
||||
done();
|
||||
})
|
||||
.expect('<ul><li>Tobi</li><li>Loki</li><li>Jane</li></ul>')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should accept to text/plain', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/plain')
|
||||
.end(function(res){
|
||||
res.body.should.equal(' - Tobi\n - Loki\n - Jane\n');
|
||||
done();
|
||||
})
|
||||
.expect(' - Tobi\n - Loki\n - Jane\n')
|
||||
.end(done);
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
var app = require('../../examples/cookies/app')
|
||||
, request = require('../support/http');
|
||||
|
||||
@@ -5,29 +6,29 @@ describe('cookies', function(){
|
||||
describe('GET /', function(){
|
||||
it('should have a form', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(/<form/,done)
|
||||
.get('/')
|
||||
.expect(/<form/, done);
|
||||
})
|
||||
|
||||
it('should respond with no cookies', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.not.have.property('set-cookie')
|
||||
done()
|
||||
})
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.headers.should.not.have.property('set-cookie')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /', function(){
|
||||
it('should set a cookie', function(done){
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('Content-Type','application/x-www-form-urlencoded')
|
||||
.write('remember=1')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('set-cookie')
|
||||
done()
|
||||
})
|
||||
.post('/')
|
||||
.send({ remember: 1 })
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('set-cookie')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
var app = require('../../examples/downloads/app')
|
||||
, request = require('../support/http');
|
||||
|
||||
@@ -5,28 +6,28 @@ describe('downloads', function(){
|
||||
describe('GET /', function(){
|
||||
it('should have a link to amazing.txt', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(/href="\/files\/amazing.txt"/,done)
|
||||
.get('/')
|
||||
.expect(/href="\/files\/amazing.txt"/, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /files/amazing.txt', function(){
|
||||
it('should have a download header', function(done){
|
||||
request(app)
|
||||
.get('/files/amazing.txt')
|
||||
.end(function(res){
|
||||
res.should.have.property('statusCode',200)
|
||||
res.headers.should.have.property('content-disposition', 'attachment; filename="amazing.txt"')
|
||||
done()
|
||||
})
|
||||
.get('/files/amazing.txt')
|
||||
.end(function(err, res){
|
||||
res.status.should.equal(200);
|
||||
res.headers.should.have.property('content-disposition', 'attachment; filename="amazing.txt"')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /files/missing.txt', function(){
|
||||
it('should respond with 404', function(done){
|
||||
request(app)
|
||||
.get('/files/missing.txt')
|
||||
.expect(404,done)
|
||||
.get('/files/missing.txt')
|
||||
.expect(404, done)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -7,12 +7,12 @@ describe('ejs', function(){
|
||||
it('should respond with html', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.should.have.status(200);
|
||||
res.should.have.header('Content-Type', 'text/html; charset=utf-8');
|
||||
res.body.should.include('<li>tobi tobi@learnboost.com</li>');
|
||||
res.body.should.include('<li>loki loki@learnboost.com</li>');
|
||||
res.body.should.include('<li>jane jane@learnboost.com</li>');
|
||||
res.text.should.include('<li>tobi tobi@learnboost.com</li>');
|
||||
res.text.should.include('<li>loki loki@learnboost.com</li>');
|
||||
res.text.should.include('<li>jane jane@learnboost.com</li>');
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
@@ -37,7 +37,6 @@ describe('error-pages', function(){
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('Accept: application/json',function(){
|
||||
describe('GET /403', function(){
|
||||
it('should respond with 403', function(done){
|
||||
@@ -53,9 +52,8 @@ describe('error-pages', function(){
|
||||
request(app)
|
||||
.get('/404')
|
||||
.set('Accept','application/json')
|
||||
.end(function(res){
|
||||
res.should.have.property('statusCode',200)
|
||||
res.should.have.property('body',JSON.stringify({error:'Not found'}))
|
||||
.end(function(err, res){
|
||||
res.body.should.eql({ error: 'Not found' });
|
||||
done()
|
||||
})
|
||||
})
|
||||
@@ -65,7 +63,7 @@ describe('error-pages', function(){
|
||||
it('should respond with 500', function(done){
|
||||
request(app)
|
||||
.get('/500')
|
||||
.set('Accept','application/json')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(500, done)
|
||||
})
|
||||
})
|
||||
@@ -76,22 +74,19 @@ describe('error-pages', function(){
|
||||
describe('GET /403', function(){
|
||||
it('should respond with 403', function(done){
|
||||
request(app)
|
||||
.get('/403')
|
||||
.set('Accept','text/plain')
|
||||
.expect(403, done)
|
||||
.get('/403')
|
||||
.set('Accept','text/plain')
|
||||
.expect(403, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /404', function(){
|
||||
it('should respond with 404', function(done){
|
||||
request(app)
|
||||
.get('/404')
|
||||
.set('Accept','text/plain')
|
||||
.end(function(res){
|
||||
res.should.have.property('statusCode',200)
|
||||
res.should.have.property('body','Not found')
|
||||
done()
|
||||
})
|
||||
.get('/404')
|
||||
.set('Accept', 'text/plain')
|
||||
.expect(200)
|
||||
.expect('Not found', done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
|
||||
var app = require('../../examples/multipart/app')
|
||||
, request = require('../support/http')
|
||||
, path = 'test/acceptance/fixtures/grey.png'
|
||||
, fs = require('fs')
|
||||
|
||||
var logo = fs.readFileSync(path)
|
||||
, boundary = '------expressmultipart';
|
||||
|
||||
describe('multipart', function(){
|
||||
describe('GET /', function(){
|
||||
it('should respond with a form', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect(/<form/, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /', function(){
|
||||
it('should upload logo as multipart', function(done){
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('content-type','multipart/form-data; boundary='+boundary.slice(2))
|
||||
.write(boundary + '\r\n')
|
||||
.write('Content-Disposition: form-data; name="title"\r\n')
|
||||
.write('\r\n')
|
||||
.write('grey\r\n')
|
||||
.write(boundary + '\r\n')
|
||||
.write('Content-Disposition: form-data; name="image"; filename="grey.png"\r\n')
|
||||
.write('Content-Type: image/png\r\n')
|
||||
.write('\r\n')
|
||||
.write(logo+'\r\n')
|
||||
.write(boundary+'--\r\n')
|
||||
.end(function(res){
|
||||
res.body.should.match(/uploaded grey.png/)
|
||||
res.body.should.match(/\(224 Kb\)/)
|
||||
res.body.should.match(/as grey/)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
+19
-20
@@ -7,7 +7,7 @@ describe('mvc', function(){
|
||||
it('should redirect to /users', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.should.have.status(302);
|
||||
res.headers.location.should.include('/users');
|
||||
done();
|
||||
@@ -19,11 +19,11 @@ describe('mvc', function(){
|
||||
it('should display a list of users', function(done){
|
||||
request(app)
|
||||
.get('/users')
|
||||
.end(function(res){
|
||||
res.body.should.include('<h1>Users</h1>');
|
||||
res.body.should.include('>TJ<');
|
||||
res.body.should.include('>Guillermo<');
|
||||
res.body.should.include('>Nathan<');
|
||||
.end(function(err, res){
|
||||
res.text.should.include('<h1>Users</h1>');
|
||||
res.text.should.include('>TJ<');
|
||||
res.text.should.include('>Guillermo<');
|
||||
res.text.should.include('>Nathan<');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -34,8 +34,8 @@ describe('mvc', function(){
|
||||
it('should display the user', function(done){
|
||||
request(app)
|
||||
.get('/user/0')
|
||||
.end(function(res){
|
||||
res.body.should.include('<h1>TJ <a href="/user/0/edit">edit');
|
||||
.end(function(err, res){
|
||||
res.text.should.include('<h1>TJ <a href="/user/0/edit">edit');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -43,10 +43,10 @@ describe('mvc', function(){
|
||||
it('should display the users pets', function(done){
|
||||
request(app)
|
||||
.get('/user/0')
|
||||
.end(function(res){
|
||||
res.body.should.include('/pet/0">Tobi');
|
||||
res.body.should.include('/pet/1">Loki');
|
||||
res.body.should.include('/pet/2">Jane');
|
||||
.end(function(err, res){
|
||||
res.text.should.include('/pet/0">Tobi');
|
||||
res.text.should.include('/pet/1">Loki');
|
||||
res.text.should.include('/pet/2">Jane');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -65,9 +65,9 @@ describe('mvc', function(){
|
||||
it('should display the edit form', function(done){
|
||||
request(app)
|
||||
.get('/user/1/edit')
|
||||
.end(function(res){
|
||||
res.body.should.include('<h1>Guillermo</h1>');
|
||||
res.body.should.include('value="put"');
|
||||
.end(function(err, res){
|
||||
res.text.should.include('<h1>Guillermo</h1>');
|
||||
res.text.should.include('value="put"');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -77,13 +77,12 @@ describe('mvc', function(){
|
||||
it('should update the user', function(done){
|
||||
request(app)
|
||||
.put('/user/1')
|
||||
.set('Content-Type', 'application/json')
|
||||
.write('{"user":{"name":"Tobo"}}')
|
||||
.end(function(res){
|
||||
.send({ user: { name: 'Tobo' }})
|
||||
.end(function(err, res){
|
||||
request(app)
|
||||
.get('/user/1/edit')
|
||||
.end(function(res){
|
||||
res.body.should.include('<h1>Tobo</h1>');
|
||||
.end(function(err, res){
|
||||
res.text.should.include('<h1>Tobo</h1>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,7 +37,7 @@ describe('resource', function(){
|
||||
describe('DELETE /users/1', function(){
|
||||
it('should respond with users 1 through 3', function(done){
|
||||
request(app)
|
||||
.delete('/users/1')
|
||||
.del('/users/1')
|
||||
.expect(/^destroyed/,done)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
|
||||
var request = require('supertest')
|
||||
, app = require('../../examples/route-map');
|
||||
|
||||
describe('route-map', function(){
|
||||
describe('GET /users', function(){
|
||||
it('should respond with users', function(done){
|
||||
request(app)
|
||||
.get('/users')
|
||||
.expect('user list', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('DELETE /users', function(){
|
||||
it('should delete users', function(done){
|
||||
request(app)
|
||||
.del('/users')
|
||||
.expect('delete users', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /users/:id', function(){
|
||||
it('should get a user', function(done){
|
||||
request(app)
|
||||
.get('/users/12')
|
||||
.expect('user 12', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /users/:id/pets', function(){
|
||||
it('should get a users pets', function(done){
|
||||
request(app)
|
||||
.get('/users/12/pets')
|
||||
.expect('user 12\'s pets', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /users/:id/pets/:pid', function(){
|
||||
it('should get a users pet', function(done){
|
||||
request(app)
|
||||
.del('/users/12/pets/2')
|
||||
.expect('delete 12\'s pet 2', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -24,9 +24,9 @@ describe('web-service', function(){
|
||||
it('should respond users json', function(done){
|
||||
request(app)
|
||||
.get('/api/users?api-key=foo')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.should.be.json;
|
||||
res.body.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]');
|
||||
res.text.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]');
|
||||
done();
|
||||
});
|
||||
})
|
||||
@@ -37,10 +37,10 @@ describe('web-service', function(){
|
||||
it('should respond with 404 json', function(done){
|
||||
request(app)
|
||||
.get('/api/something?api-key=bar')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.should.have.status(404);
|
||||
res.should.be.json;
|
||||
res.body.should.equal('{"error":"Lame, can\'t find that"}');
|
||||
res.text.should.equal('{"error":"Lame, can\'t find that"}');
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ describe('app.all()', function(){
|
||||
});
|
||||
|
||||
request(app)
|
||||
.delete('/tobi')
|
||||
.del('/tobi')
|
||||
.expect(404, done);
|
||||
})
|
||||
})
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ describe('app.del()', function(){
|
||||
});
|
||||
|
||||
request(app)
|
||||
.delete('/tobi')
|
||||
.del('/tobi')
|
||||
.expect('deleted tobi!', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,9 +34,10 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
if (err) return done(err);
|
||||
calls.should.eql(['use', 'one', 'two']);
|
||||
res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -59,10 +60,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
.expect('<p>tobi holowaychuk is a ferret</p>', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -83,10 +81,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi ibot is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
.expect('<p>tobi ibot is a ferret</p>', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+4
-11
@@ -12,12 +12,8 @@ describe('OPTIONS', function(){
|
||||
|
||||
request(app)
|
||||
.options('/users')
|
||||
.end(function(res){
|
||||
res.body.should.equal('GET,PUT');
|
||||
res.headers.should.have.property('content-type');
|
||||
res.headers.should.have.property('allow', 'GET,PUT');
|
||||
done();
|
||||
});
|
||||
.expect('GET,PUT')
|
||||
.expect('Allow', 'GET,PUT', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -35,10 +31,7 @@ describe('app.options()', function(){
|
||||
|
||||
request(app)
|
||||
.options('/users')
|
||||
.end(function(res){
|
||||
res.body.should.equal('GET');
|
||||
res.headers.should.have.property('allow', 'GET');
|
||||
done();
|
||||
});
|
||||
.expect('GET')
|
||||
.expect('Allow', 'GET', done);
|
||||
})
|
||||
})
|
||||
+7
-16
@@ -29,14 +29,11 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/user/tj')
|
||||
.end(function(res){
|
||||
res.body.should.equal('tj');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('tj');
|
||||
request(app)
|
||||
.get('/user/123')
|
||||
.end(function(res){
|
||||
res.should.have.status(404);
|
||||
done();
|
||||
});
|
||||
.expect(404, done);
|
||||
});
|
||||
|
||||
})
|
||||
@@ -67,15 +64,12 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/user/123')
|
||||
.end(function(res){
|
||||
res.body.should.equal('123');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('123');
|
||||
|
||||
request(app)
|
||||
.get('/post/123')
|
||||
.end(function(res){
|
||||
res.body.should.equal('123');
|
||||
done();
|
||||
})
|
||||
.expect('123', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -99,10 +93,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/user/123')
|
||||
.end(function(res){
|
||||
res.body.should.equal('123');
|
||||
done();
|
||||
})
|
||||
.expect('123', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,10 +17,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/foo?name=tobi')
|
||||
.end(function(res){
|
||||
res.body.should.equal('name=tobi');
|
||||
done();
|
||||
});
|
||||
.expect('name=tobi', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,10 +17,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('HEY');
|
||||
done();
|
||||
});
|
||||
.expect('HEY', done);
|
||||
})
|
||||
|
||||
it('should not be influenced by other app protos', function(done){
|
||||
@@ -41,10 +38,7 @@ describe('app', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('HEY');
|
||||
done();
|
||||
});
|
||||
.expect('HEY', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+24
-3
@@ -1,11 +1,14 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http')
|
||||
, assert = require('assert');
|
||||
, assert = require('assert')
|
||||
, methods = require('methods');
|
||||
|
||||
describe('app.router', function(){
|
||||
describe('methods supported', function(){
|
||||
express.methods.forEach(function(method){
|
||||
methods.forEach(function(method){
|
||||
it('should include ' + method.toUpperCase(), function(done){
|
||||
if (method == 'delete') method = 'del';
|
||||
var app = express();
|
||||
var calls = [];
|
||||
|
||||
@@ -265,7 +268,7 @@ describe('app.router', function(){
|
||||
|
||||
request(app)
|
||||
.get('/user/10')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(200);
|
||||
request(app)
|
||||
.get('/user/tj')
|
||||
@@ -551,4 +554,22 @@ describe('app.router', function(){
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should allow rewriting of the url', function(done){
|
||||
var app = express();
|
||||
|
||||
app.get('/account/edit', function(req, res, next){
|
||||
req.user = { id: 12 }; // faux authenticated user
|
||||
req.url = '/user/' + req.user.id + '/edit';
|
||||
next();
|
||||
});
|
||||
|
||||
app.get('/user/:id/edit', function(req, res){
|
||||
res.send('editing user ' + req.params.id);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/account/edit')
|
||||
.expect('editing user 12', done);
|
||||
})
|
||||
})
|
||||
|
||||
+4
-7
@@ -12,14 +12,11 @@ describe('exports', function(){
|
||||
express.should.have.property('session');
|
||||
express.should.have.property('static');
|
||||
})
|
||||
|
||||
it('should expose HTTP methods', function(){
|
||||
express.methods.should.be.an.instanceof(Array);
|
||||
express.methods.should.include('get');
|
||||
express.methods.should.include('put');
|
||||
express.methods.should.include('post');
|
||||
|
||||
it('should expose .mime', function(){
|
||||
express.mime.should.equal(require('connect').mime);
|
||||
})
|
||||
|
||||
|
||||
it('should expose Router', function(){
|
||||
express.Router.should.be.a('function');
|
||||
})
|
||||
|
||||
@@ -13,10 +13,7 @@ describe('throw after .end()', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.should.have.status(200);
|
||||
res.body.should.equal('yay');
|
||||
done();
|
||||
});
|
||||
.expect('yay')
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
+2
-8
@@ -15,10 +15,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', '12345')
|
||||
.end(function(res){
|
||||
res.body.should.equal('true');
|
||||
done();
|
||||
});
|
||||
.expect(304, done);
|
||||
})
|
||||
|
||||
it('should return false when the resource is modified', function(done){
|
||||
@@ -32,10 +29,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', '12345')
|
||||
.end(function(res){
|
||||
res.body.should.equal('false');
|
||||
done();
|
||||
});
|
||||
.expect('false', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+5
-9
@@ -1,6 +1,7 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
, request = require('./support/http')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('req', function(){
|
||||
describe('.get(field)', function(){
|
||||
@@ -8,16 +9,14 @@ describe('req', function(){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
assert(req.get('Something-Else') === undefined);
|
||||
res.end(req.get('Content-Type'));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('Content-Type', 'application/json')
|
||||
.end(function(res){
|
||||
res.body.should.equal('application/json');
|
||||
done();
|
||||
});
|
||||
.expect('application/json', done);
|
||||
})
|
||||
|
||||
it('should special-case Referer', function(done){
|
||||
@@ -30,10 +29,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('Referrer', 'http://foobar.com')
|
||||
.end(function(res){
|
||||
res.body.should.equal('http://foobar.com');
|
||||
done();
|
||||
});
|
||||
.expect('http://foobar.com', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
var express = require('../');
|
||||
|
||||
function req(ret) {
|
||||
return {
|
||||
get: function(){ return ret }
|
||||
, __proto__: express.request
|
||||
};
|
||||
}
|
||||
|
||||
describe('req', function(){
|
||||
describe('.host', function(){
|
||||
it('should return hostname', function(){
|
||||
req('example.com:3000').host.should.equal('example.com');
|
||||
req('example.com').host.should.equal('example.com');
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,57 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
|
||||
describe('req', function(){
|
||||
describe('.ip', function(){
|
||||
describe('when X-Forwarded-For is present', function(){
|
||||
describe('when "trust proxy" is enabled', function(){
|
||||
it('should return the client addr', function(done){
|
||||
var app = express();
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ip);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-For', 'client, p1, p2')
|
||||
.expect('client', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('when "trust proxy" is disabled', function(){
|
||||
it('should return the remote address', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ip);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-For', 'client, p1, p2')
|
||||
.expect('127.0.0.1', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when X-Forwarded-For is not present', function(){
|
||||
it('should return the remote address', function(done){
|
||||
var app = express();
|
||||
|
||||
app.enable('trust proxy');
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ip);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('127.0.0.1', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
+28
-9
@@ -5,17 +5,36 @@ var express = require('../')
|
||||
describe('req', function(){
|
||||
describe('.ips', function(){
|
||||
describe('when X-Forwarded-For is present', function(){
|
||||
it('should return an array of the specified addresses', function(done){
|
||||
var app = express();
|
||||
describe('when "trust proxy" is enabled', function(){
|
||||
it('should return an array of the specified addresses', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ips);
|
||||
});
|
||||
app.enable('trust proxy');
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-For', 'client, p1, p2')
|
||||
.expect('["p2","p1","client"]', done);
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ips);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-For', 'client, p1, p2')
|
||||
.expect('["client","p1","p2"]', done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('when "trust proxy" is disabled', function(){
|
||||
it('should return an empty array', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.send(req.ips);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-For', 'client, p1, p2')
|
||||
.expect('[]', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+6
-19
@@ -13,10 +13,7 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('tj');
|
||||
done();
|
||||
})
|
||||
.expect('tj', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,10 +27,7 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.get('/?name=tj')
|
||||
.end(function(res){
|
||||
res.body.should.equal('tj');
|
||||
done();
|
||||
})
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
it('should check req.body', function(done){
|
||||
@@ -47,27 +41,20 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.post('/')
|
||||
.set('Content-Type', 'application/json')
|
||||
.write('{"name":"tj"}')
|
||||
.end(function(res){
|
||||
res.body.should.equal('tj');
|
||||
done();
|
||||
})
|
||||
.send({ name: 'tj' })
|
||||
.expect('tj', done);
|
||||
})
|
||||
|
||||
it('should check req.params', function(done){
|
||||
var app = express();
|
||||
|
||||
app.get('/user/:name', function(req, res){
|
||||
res.end(req.param('name'));
|
||||
res.end(req.param('filter') + req.param('name'));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/user/tj')
|
||||
.end(function(res){
|
||||
res.body.should.equal('tj');
|
||||
done();
|
||||
})
|
||||
.expect('undefinedtj', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+1
-4
@@ -13,10 +13,7 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.get('/login?redirect=/post/1/comments')
|
||||
.end(function(res){
|
||||
res.body.should.equal('/login');
|
||||
done();
|
||||
})
|
||||
.expect('/login', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+4
-16
@@ -13,10 +13,7 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('http');
|
||||
done();
|
||||
})
|
||||
.expect('http', done);
|
||||
})
|
||||
|
||||
describe('when "trust proxy" is enabled', function(){
|
||||
@@ -32,10 +29,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'https')
|
||||
.end(function(res){
|
||||
res.body.should.equal('https');
|
||||
done();
|
||||
})
|
||||
.expect('https', done);
|
||||
})
|
||||
|
||||
it('should default to http', function(done){
|
||||
@@ -49,10 +43,7 @@ describe('req', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('http');
|
||||
done();
|
||||
})
|
||||
.expect('http', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -67,10 +58,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('X-Forwarded-Proto', 'https')
|
||||
.end(function(res){
|
||||
res.body.should.equal('http');
|
||||
done();
|
||||
})
|
||||
.expect('http', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
var express = require('../');
|
||||
|
||||
function req(ret) {
|
||||
return {
|
||||
get: function(){ return ret }
|
||||
, __proto__: express.request
|
||||
};
|
||||
}
|
||||
|
||||
describe('req', function(){
|
||||
describe('.range(size)', function(){
|
||||
it('should return parsed ranges', function(){
|
||||
var ret = [{ start: 0, end: 50 }, { start: 60, end: 100 }];
|
||||
ret.type = 'bytes';
|
||||
req('bytes=0-50,60-100').range(120).should.eql(ret);
|
||||
})
|
||||
|
||||
it('should cap to the given size', function(){
|
||||
var ret = [{ start: 0, end: 74 }];
|
||||
ret.type = 'bytes';
|
||||
req('bytes=0-100').range(75).should.eql(ret);
|
||||
})
|
||||
|
||||
it('should have a .type', function(){
|
||||
var ret = [{ start: 0, end: Infinity }];
|
||||
ret.type = 'users';
|
||||
req('users=0-').range(Infinity).should.eql(ret);
|
||||
})
|
||||
})
|
||||
})
|
||||
+2
-8
@@ -15,10 +15,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', '12345')
|
||||
.end(function(res){
|
||||
res.body.should.equal('false');
|
||||
done();
|
||||
});
|
||||
.expect(304, done);
|
||||
})
|
||||
|
||||
it('should return true when the resource is modified', function(done){
|
||||
@@ -32,10 +29,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', '12345')
|
||||
.end(function(res){
|
||||
res.body.should.equal('true');
|
||||
done();
|
||||
});
|
||||
.expect('true', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,10 +15,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Host', 'tobi.ferrets.example.com')
|
||||
.end(function(res){
|
||||
res.body.should.equal('["ferrets","tobi"]');
|
||||
done();
|
||||
})
|
||||
.expect('["ferrets","tobi"]', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -33,10 +30,7 @@ describe('req', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
res.body.should.equal('[]');
|
||||
done();
|
||||
})
|
||||
.expect('[]', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,10 +13,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-disposition', 'attachment');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Disposition', 'attachment', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,10 +28,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-disposition', 'attachment; filename="image.png"');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Disposition', 'attachment; filename="image.png"', done);
|
||||
})
|
||||
|
||||
it('should set the Content-Type', function(done){
|
||||
@@ -47,10 +41,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'image/png');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'image/png', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,10 +15,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('text/x-foo; charset=utf-8');
|
||||
done();
|
||||
})
|
||||
.expect("text/x-foo; charset=utf-8", done);
|
||||
})
|
||||
|
||||
it('should take precedence over res.send() defaults', function(done){
|
||||
@@ -31,10 +28,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=whoop');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'text/html; charset=whoop', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,9 +13,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = 'sid=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
res.headers['set-cookie'].should.eql([val]);
|
||||
.end(function(err, res){
|
||||
var val = 'sid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
res.header['set-cookie'].should.eql([val]);
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -31,9 +31,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = 'sid=; path=/admin; expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
res.headers['set-cookie'].should.eql([val]);
|
||||
.end(function(err, res){
|
||||
var val = 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
res.header['set-cookie'].should.eql([val]);
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
+52
-10
@@ -1,6 +1,7 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
, request = require('./support/http')
|
||||
, cookie = require('cookie');
|
||||
|
||||
describe('res', function(){
|
||||
describe('.cookie(name, object)', function(){
|
||||
@@ -13,8 +14,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = ['user=j%3A%7B%22name%22%3A%22tobi%22%7D; path=/'];
|
||||
.end(function(err, res){
|
||||
var val = ['user=j:{%22name%22:%22tobi%22}; Path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
@@ -31,8 +32,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = ['name=tobi; path=/'];
|
||||
.end(function(err, res){
|
||||
var val = ['name=tobi; Path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
@@ -49,8 +50,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = ['name=tobi; path=/', 'age=1; path=/'];
|
||||
.end(function(err, res){
|
||||
var val = ['name=tobi; Path=/', 'age=1; Path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
@@ -68,8 +69,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = ['name=tobi; path=/; httpOnly; secure'];
|
||||
.end(function(err, res){
|
||||
var val = ['name=tobi; Path=/; HttpOnly; Secure'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
@@ -86,11 +87,52 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers['set-cookie'][0].should.not.include('Thu, 01 Jan 1970 00:00:01 GMT');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('signed', function(){
|
||||
it('should generate a signed JSON cookie', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(express.cookieParser('foo bar baz'));
|
||||
|
||||
app.use(function(req, res){
|
||||
res.cookie('user', { name: 'tobi' }, { signed: true }).end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
var val = res.headers['set-cookie'][0];
|
||||
val = cookie.parse(val.split('.')[0]);
|
||||
val.user.should.equal('j:{"name":"tobi"}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.signedCookie(name, string)', function(){
|
||||
it('should set a signed cookie', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(express.cookieParser('foo bar baz'));
|
||||
|
||||
app.use(function(req, res){
|
||||
res.cookie('name', 'tobi', { signed: true }).end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
var val = ['name=tobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('res', function(){
|
||||
describe('.download(path)', function(){
|
||||
it('should transfer as an attachment', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.download('test/fixtures/user.html');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
|
||||
res.should.have.header('Content-Disposition', 'attachment; filename="user.html"');
|
||||
res.text.should.equal('<p>{{user.name}}</p>');
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe('.download(path, filename)', function(){
|
||||
it('should provide an alternate filename', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.download('test/fixtures/user.html', 'document');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
|
||||
res.should.have.header('Content-Disposition', 'attachment; filename="document"');
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe('.download(path, fn)', function(){
|
||||
it('should invoke the callback', function(done){
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
|
||||
app.use(function(req, res){
|
||||
res.download('test/fixtures/user.html', done);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
|
||||
res.should.have.header('Content-Disposition', 'attachment; filename="user.html"');
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe('.download(path, filename, fn)', function(){
|
||||
it('should invoke the callback', function(done){
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
|
||||
app.use(function(req, res){
|
||||
res.download('test/fixtures/user.html', 'document', done);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.should.have.header('Content-Type', 'text/html; charset=UTF-8');
|
||||
res.should.have.header('Content-Disposition', 'attachment; filename="document"');
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
+23
-14
@@ -44,6 +44,15 @@ app2.use(function(err, req, res, next){
|
||||
res.send(err.status, 'Supports: ' + err.types.join(', '));
|
||||
})
|
||||
|
||||
var app3 = express();
|
||||
|
||||
app3.use(function(req, res, next){
|
||||
res.format({
|
||||
text: function(){ res.send('hey') },
|
||||
default: function(){ res.send('default') }
|
||||
})
|
||||
});
|
||||
|
||||
describe('req', function(){
|
||||
describe('.format(obj)', function(){
|
||||
describe('with canonicalized mime types', function(){
|
||||
@@ -53,6 +62,15 @@ describe('req', function(){
|
||||
describe('with extnames', function(){
|
||||
test(app2);
|
||||
})
|
||||
|
||||
describe('given .default', function(){
|
||||
it('should be invoked instead of auto-responding', function(done){
|
||||
request(app3)
|
||||
.get('/')
|
||||
.set('Accept: text/html')
|
||||
.expect('default', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,21 +93,15 @@ function test(app) {
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html; q=.5, text/plain')
|
||||
.end(function(res){
|
||||
res.headers['content-type'].should.equal('text/plain');
|
||||
res.body.should.equal('hey');
|
||||
done();
|
||||
});
|
||||
.expect('Content-Type', 'text/plain')
|
||||
.expect('hey', done);
|
||||
})
|
||||
|
||||
it('should Vary: Accept', function(done){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html; q=.5, text/plain')
|
||||
.end(function(res){
|
||||
res.headers.vary.should.equal('Accept');
|
||||
done();
|
||||
});
|
||||
.expect('Vary', 'Accept', done);
|
||||
})
|
||||
|
||||
describe('when Accept is not present', function(){
|
||||
@@ -105,11 +117,8 @@ function test(app) {
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'foo/bar')
|
||||
.end(function(res){
|
||||
res.should.have.status(406);
|
||||
res.body.should.equal('Supports: text/plain, text/html, application/json');
|
||||
done();
|
||||
});
|
||||
.expect('Supports: text/plain, text/html, application/json')
|
||||
.expect(406, done)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
+6
-15
@@ -1,23 +1,14 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
, res = express.response;
|
||||
|
||||
describe('res', function(){
|
||||
describe('.get(field)', function(){
|
||||
it('should get the response header field', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.setHeader('Content-Type', 'text/x-foo');
|
||||
res.end(res.get('Content-Type'));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('text/x-foo');
|
||||
done();
|
||||
})
|
||||
it('should get the response header field', function(){
|
||||
res.setHeader('Content-Type', 'text/x-foo');
|
||||
res.get('Content-Type').should.equal('text/x-foo');
|
||||
res.get('Content-type').should.equal('text/x-foo');
|
||||
res.get('content-type').should.equal('text/x-foo');
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+35
-16
@@ -15,9 +15,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/?callback=something')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
|
||||
res.body.should.equal('something({"count":1});');
|
||||
res.text.should.equal('something({"count":1});');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -31,9 +31,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/?callback=callbacks[123]')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
|
||||
res.body.should.equal('callbacks[123]({"count":1});');
|
||||
res.text.should.equal('callbacks[123]({"count":1});');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -49,9 +49,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.body.should.equal('null');
|
||||
res.text.should.equal('null');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -67,9 +67,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.body.should.equal('["foo","bar","baz"]');
|
||||
res.text.should.equal('["foo","bar","baz"]');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -85,9 +85,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.body.should.equal('{"name":"tobi"}');
|
||||
res.text.should.equal('{"name":"tobi"}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -109,8 +109,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('{"name":"tobi"}');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('{"name":"tobi"}');
|
||||
done();
|
||||
});
|
||||
})
|
||||
@@ -140,8 +140,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('{\n "name": "tobi",\n "age": 2\n}');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');
|
||||
done();
|
||||
});
|
||||
})
|
||||
@@ -158,10 +158,29 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(201);
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.body.should.equal('{"id":1}');
|
||||
res.text.should.equal('{"id":1}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.json(object, status)', function(){
|
||||
it('should respond with json and set the .statusCode for backwards compat', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.json({ id: 1 }, 201);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(201);
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.text.should.equal('{"id":1}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
var express = require('../')
|
||||
, res = express.response;
|
||||
|
||||
describe('res', function(){
|
||||
describe('.links(obj)', function(){
|
||||
it('should set Link header field', function(){
|
||||
res.links({
|
||||
next: 'http://api.example.com/users?page=2',
|
||||
last: 'http://api.example.com/users?page=5'
|
||||
});
|
||||
|
||||
res.get('link')
|
||||
.should.equal(
|
||||
'<http://api.example.com/users?page=2>; rel="next", '
|
||||
+ '<http://api.example.com/users?page=5>; rel="last"');
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -40,9 +40,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
calls.should.eql(['render', 'one', 'two']);
|
||||
res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -68,8 +68,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('<p>tobi holowaychuk is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -95,8 +95,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi ibot is a ferret</p>');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('<p>tobi ibot is a ferret</p>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
+39
-16
@@ -17,7 +17,7 @@ describe('res', function(){
|
||||
.get('/')
|
||||
.set('Host', 'example.com')
|
||||
.set('X-Forwarded-Proto', 'https')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(302);
|
||||
res.headers.should.have.property('location', 'https://example.com/login');
|
||||
done();
|
||||
@@ -33,7 +33,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(302);
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
done();
|
||||
@@ -51,7 +51,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/login');
|
||||
done();
|
||||
})
|
||||
@@ -69,7 +69,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/post/1')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/post/1/./edit');
|
||||
done();
|
||||
})
|
||||
@@ -87,7 +87,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/post/1')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/post/1/../new');
|
||||
done();
|
||||
})
|
||||
@@ -105,7 +105,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/login');
|
||||
done();
|
||||
})
|
||||
@@ -129,7 +129,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/blog/admin')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/blog/admin/login');
|
||||
done();
|
||||
})
|
||||
@@ -150,7 +150,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/blog')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/blog/admin/login');
|
||||
done();
|
||||
})
|
||||
@@ -171,7 +171,7 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/blog')
|
||||
.set('Host', 'example.com')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://example.com/admin/login');
|
||||
done();
|
||||
})
|
||||
@@ -190,7 +190,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(303);
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
done();
|
||||
@@ -208,9 +208,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.head('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
res.body.should.equal('');
|
||||
res.text.should.equal('');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -227,9 +227,9 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
res.body.should.equal('<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>');
|
||||
res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -246,9 +246,32 @@ describe('res', function(){
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/plain, */*')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
res.body.should.equal('Moved Temporarily. Redirecting to http://google.com');
|
||||
res.headers.should.have.property('content-length', '51');
|
||||
res.text.should.equal('Moved Temporarily. Redirecting to http://google.com');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when accepting neither text or html', function(){
|
||||
it('should respond with an empty body', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.redirect('http://google.com');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'foo/bar')
|
||||
.end(function(err, res){
|
||||
res.should.have.status(302);
|
||||
res.headers.should.have.property('location', 'http://google.com');
|
||||
res.headers.should.not.have.property('content-type');
|
||||
res.headers.should.have.property('content-length', '0');
|
||||
res.text.should.equal('');
|
||||
done();
|
||||
})
|
||||
})
|
||||
|
||||
+15
-61
@@ -15,10 +15,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should support absolute paths with "view engine"', function(done){
|
||||
@@ -33,10 +30,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should expose app.locals', function(done){
|
||||
@@ -51,10 +45,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should support index.<engine>', function(done){
|
||||
@@ -69,10 +60,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<h1>blog post</h1>');
|
||||
done();
|
||||
});
|
||||
.expect('<h1>blog post</h1>', done);
|
||||
})
|
||||
|
||||
describe('when an error occurs', function(){
|
||||
@@ -91,10 +79,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.match(/user is not defined/);
|
||||
done();
|
||||
});
|
||||
.expect(/user is not defined/, done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -111,10 +96,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>This is an email</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>This is an email</p>', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -133,10 +115,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should expose app.locals', function(done){
|
||||
@@ -151,10 +130,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should expose res.locals', function(done){
|
||||
@@ -169,10 +145,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>tobi</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>tobi</p>', done);
|
||||
})
|
||||
|
||||
it('should give precedence to res.locals over app.locals', function(done){
|
||||
@@ -188,10 +161,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>jane</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>jane</p>', done);
|
||||
})
|
||||
|
||||
it('should give precedence to res.render() locals over res.locals', function(done){
|
||||
@@ -207,10 +177,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>jane</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>jane</p>', done);
|
||||
})
|
||||
|
||||
it('should give precedence to res.render() locals over app.locals', function(done){
|
||||
@@ -226,10 +193,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>jane</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>jane</p>', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -249,10 +213,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>loki</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>loki</p>', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -272,10 +233,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>loki</p>');
|
||||
done();
|
||||
});
|
||||
.expect('<p>loki</p>', done);
|
||||
})
|
||||
|
||||
describe('when an error occurs', function(){
|
||||
@@ -292,12 +250,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.match(/is not defined/);
|
||||
done();
|
||||
});
|
||||
.expect(/is not defined/, done);
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+76
-53
@@ -13,10 +13,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('');
|
||||
done();
|
||||
})
|
||||
.expect('', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,10 +27,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('');
|
||||
done();
|
||||
})
|
||||
.expect('', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -47,11 +41,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('Created');
|
||||
res.statusCode.should.equal(201);
|
||||
done();
|
||||
})
|
||||
.expect('Created')
|
||||
.expect(201, done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -65,14 +56,26 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('Created :)');
|
||||
res.statusCode.should.equal(201);
|
||||
done();
|
||||
})
|
||||
.expect('Created :)')
|
||||
.expect(201, done);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('.send(body, code)', function(){
|
||||
it('should be supported for backwards compat', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.send('Bad!', 400);
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('Bad!')
|
||||
.expect(400, done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('.send(String)', function(){
|
||||
it('should send as html', function(done){
|
||||
var app = express();
|
||||
@@ -83,9 +86,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
|
||||
res.body.should.equal('<p>hey</p>');
|
||||
res.text.should.equal('<p>hey</p>');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
@@ -101,10 +104,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('etag', '-1498647312');
|
||||
done();
|
||||
})
|
||||
.expect('ETag', '-1498647312')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should not override Content-Type', function(done){
|
||||
@@ -116,12 +117,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'text/plain');
|
||||
res.body.should.equal('hey');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'text/plain')
|
||||
.expect('hey')
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -135,9 +133,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
|
||||
res.body.should.equal('<p>hey</p>');
|
||||
res.text.should.equal('<p>hey</p>');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
@@ -152,9 +150,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/plain');
|
||||
res.body.should.equal('hey');
|
||||
res.text.should.equal('hey');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
@@ -171,9 +169,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'application/octet-stream');
|
||||
res.body.should.equal('hello');
|
||||
res.text.should.equal('hello');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
@@ -189,10 +187,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('etag', '-1498647312');
|
||||
done();
|
||||
})
|
||||
.expect('ETag', '-1498647312')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should not override Content-Type', function(done){
|
||||
@@ -204,9 +200,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'text/plain');
|
||||
res.body.should.equal('hey');
|
||||
res.text.should.equal('hey');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
})
|
||||
@@ -223,9 +219,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
|
||||
res.body.should.equal('{"name":"tobi"}');
|
||||
res.text.should.equal('{"name":"tobi"}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -241,10 +237,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.head('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('');
|
||||
done();
|
||||
})
|
||||
.expect('', done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -258,10 +251,10 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.not.have.property('content-type');
|
||||
res.headers.should.not.have.property('content-length');
|
||||
res.body.should.equal('');
|
||||
res.text.should.equal('');
|
||||
done();
|
||||
})
|
||||
})
|
||||
@@ -277,15 +270,29 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.headers.should.not.have.property('content-type');
|
||||
res.headers.should.not.have.property('content-length');
|
||||
res.body.should.equal('');
|
||||
res.text.should.equal('');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should always check regardless of length', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.set('ETag', 'asdf');
|
||||
res.send('hey');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', 'asdf')
|
||||
.expect(304, done);
|
||||
})
|
||||
|
||||
it('should respond with 304 Not Modified when fresh', function(done){
|
||||
var app = express();
|
||||
|
||||
@@ -299,4 +306,20 @@ describe('res', function(){
|
||||
.set('If-None-Match', '-1498647312')
|
||||
.expect(304, done);
|
||||
})
|
||||
|
||||
it('should not perform freshness check unless 2xx or 304', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.status(500);
|
||||
res.set('ETag', 'asdf');
|
||||
res.send('hey');
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('If-None-Match', 'asdf')
|
||||
.expect('hey')
|
||||
.expect(500, done);
|
||||
})
|
||||
})
|
||||
|
||||
+42
-49
@@ -6,25 +6,34 @@ var express = require('../')
|
||||
describe('res', function(){
|
||||
describe('.sendfile(path, fn)', function(){
|
||||
it('should invoke the callback when complete', function(done){
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.sendfile('test/fixtures/user.html', function(err){
|
||||
assert(!err);
|
||||
++calls;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
calls.should.equal(1);
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
});
|
||||
.expect(200)
|
||||
.end(function(){});
|
||||
})
|
||||
|
||||
|
||||
it('should utilize the same options as express.static()', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res){
|
||||
res.sendfile('test/fixtures/user.html', { maxAge: 60000 });
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('Cache-Control', 'public, max-age=60')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should invoke the callback on 404', function(done){
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
@@ -39,9 +48,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
calls.should.equal(1);
|
||||
res.body.should.equal('Not Found');
|
||||
res.text.should.equal('Not Found');
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
});
|
||||
@@ -57,10 +66,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.should.have.header('content-type', 'text/plain');
|
||||
done();
|
||||
});
|
||||
.expect('Content-Type', 'text/plain')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should invoke the callback on 403', function(done){
|
||||
@@ -77,12 +84,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('Forbidden');
|
||||
res.statusCode.should.equal(200);
|
||||
calls.should.equal(1);
|
||||
done();
|
||||
});
|
||||
.expect('Forbidden')
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -97,8 +100,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>{{user.name}}</p>');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('<p>{{user.name}}</p>');
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
|
||||
done();
|
||||
});
|
||||
@@ -115,8 +118,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>{{user.name}}</p>');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('<p>{{user.name}}</p>');
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
|
||||
done();
|
||||
});
|
||||
@@ -131,8 +134,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('<p>{{user.name}}</p>');
|
||||
.end(function(err, res){
|
||||
res.text.should.equal('<p>{{user.name}}</p>');
|
||||
res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');
|
||||
done();
|
||||
});
|
||||
@@ -147,10 +150,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.statusCode.should.equal(403);
|
||||
done();
|
||||
});
|
||||
.expect(403, done);
|
||||
})
|
||||
|
||||
it('should allow ../ when "root" is set', function(done){
|
||||
@@ -162,10 +162,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.statusCode.should.equal(200);
|
||||
done();
|
||||
});
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should disallow requesting out of "root"', function(done){
|
||||
@@ -177,10 +174,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.statusCode.should.equal(403);
|
||||
done();
|
||||
});
|
||||
.expect(403, done);
|
||||
})
|
||||
|
||||
it('should next(404) when not found', function(done){
|
||||
@@ -202,7 +196,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
.end(function(err, res){
|
||||
res.statusCode.should.equal(404);
|
||||
calls.should.equal(1);
|
||||
done();
|
||||
@@ -211,17 +205,16 @@ describe('res', function(){
|
||||
|
||||
describe('with non-GET', function(){
|
||||
it('should still serve', function(done){
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
var app = express()
|
||||
, calls = 0;
|
||||
|
||||
app.use(function(req, res){
|
||||
res.sendfile(__dirname + '/fixtures/name.txt');
|
||||
});
|
||||
app.use(function(req, res){
|
||||
res.sendfile(__dirname + '/fixtures/name.txt');
|
||||
});
|
||||
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('tobi', done);
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('tobi', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+5
-9
@@ -14,10 +14,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'text/x-foo');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'text/x-foo')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should coerce to a string', function(){
|
||||
@@ -40,11 +38,9 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('x-foo', 'bar');
|
||||
res.headers.should.have.property('x-bar', 'baz');
|
||||
done();
|
||||
})
|
||||
.expect('X-Foo', 'bar')
|
||||
.expect('X-Bar', 'baz')
|
||||
.end(done);
|
||||
})
|
||||
|
||||
it('should coerce to a string', function(){
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
|
||||
var express = require('../')
|
||||
, request = require('./support/http');
|
||||
|
||||
describe('res', function(){
|
||||
describe('.signedCookie(name, object)', function(){
|
||||
it('should generate a signed JSON cookie', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(express.cookieParser('foo bar baz'));
|
||||
|
||||
app.use(function(req, res){
|
||||
res.signedCookie('user', { name: 'tobi' }).end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = res.headers['set-cookie'][0];
|
||||
val = decodeURIComponent(val.split('.')[0]);
|
||||
val.should.equal('user=j:{"name":"tobi"}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.signedCookie(name, string)', function(){
|
||||
it('should set a signed cookie', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(express.cookieParser('foo bar baz'));
|
||||
|
||||
app.use(function(req, res){
|
||||
res.signedCookie('name', 'tobi').end();
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
var val = ['name=tobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; path=/'];
|
||||
res.headers['set-cookie'].should.eql(val);
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -13,11 +13,8 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('Created');
|
||||
res.statusCode.should.equal(201);
|
||||
done();
|
||||
})
|
||||
.expect('Created')
|
||||
.expect(201, done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+3
-12
@@ -13,10 +13,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'application/javascript');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'application/javascript', done);
|
||||
})
|
||||
|
||||
it('should default to application/octet-stream', function(done){
|
||||
@@ -28,10 +25,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'application/octet-stream');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'application/octet-stream', done);
|
||||
})
|
||||
|
||||
it('should set the Content-Type with type/subtype', function(done){
|
||||
@@ -44,10 +38,7 @@ describe('res', function(){
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.end(function(res){
|
||||
res.headers.should.have.property('content-type', 'application/vnd.amazon.ebook');
|
||||
done();
|
||||
})
|
||||
.expect('Content-Type', 'application/vnd.amazon.ebook', done);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+1
-104
@@ -1,105 +1,2 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
, methods = require('../../').methods
|
||||
, 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, 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){
|
||||
this.end(function(res){
|
||||
if ('number' == typeof body) {
|
||||
res.statusCode.should.equal(body);
|
||||
} else if (body instanceof RegExp) {
|
||||
res.body.should.match(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;
|
||||
};
|
||||
module.exports = require('supertest');
|
||||
Referência em uma Nova Issue
Bloquear um usuário