Merge branch 'remove-header-fields'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 986916c, 365b3a7 · Browse files · 4 files·+173 added−194 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+173 added−194 removed
- tests:+162 added−166 removed
- other:+11 added−28 removed
Provenance
- Commit
772c135- Tree
c2dab40- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
986916c- Parent 2
365b3a7
4 files changed+173 added−194 removed
modified:lib/router/index.js+1 added-1 removed
264 unmodified lines265 266 Router.prototype._options = function(req, res){267 var path = parse(req.url).pathname268 , body = this._optionsFor(path).join(',');269- res.send(body, { Allow: body });269+ res.header('Allow', body).send(body);270 };271 272 /**273 * Return an array of HTTP verbs or "options" for `path`.111 unmodified linesmodified:lib/response.js+10 added-27 removed
20 unmodified lines21 , basename = path.basename22 , join = path.join;23 24 /**25- * Send a response with the given `body` and optional `headers` and `status` code.25+ * Send a response with the given `body` and optional `status` code.26 *27 * Examples:28 *29 * res.send(new Buffer('wahoo'));30 * res.send({ some: 'json' });31 * res.send('<p>some html</p>');32 * res.send('Sorry, cant find that', 404);33- * res.send('text', { 'Content-Type': 'text/plain' }, 201);33 * res.send(404);34 *35 * @param {String|Object|Number|Buffer} body or status37- * @param {Object|Number} headers or status36 * @param {Number} status37 * @return {ServerResponse}38 * @api public39 */40 43-res.send = function(body, headers, status){44- // allow status as second arg45- if ('number' == typeof headers) {46- status = headers,47- headers = null;48- }49-41+res.send = function(body, status){42 // default status43 status = status || this.statusCode;44 45 // determine content type16 unmodified lines62 if (!this.header('Content-Type')) {63 this.contentType('.bin');64 }65 } else {74- return this.json(body, headers, status);66+ return this.json(body, status);67 }68 break;69 }70 3 unmodified lines74 ? body.length75 : Buffer.byteLength(body));76 }77 86- // merge headers passed87- if (headers) {88- var fields = Object.keys(headers);89- for (var i = 0, len = fields.length; i < len; ++i) {90- var field = fields[i];91- this.header(field, headers[field]);92- }93- }94-78 // strip irrelevant headers79 if (204 == status || 304 == status) {80 this.removeHeader('Content-Type');81 this.removeHeader('Content-Length');5 unmodified lines87 return this;88 };89 90 /**108- * Send JSON response with `obj`, optional `headers`, and optional `status`.91+ * Send JSON response with `obj` and optional `status`.92 *93 * Examples:94 *95 * res.json(null);1 unmodified line97 * res.json('oh noes!', 500);98 * res.json('I dont have that', 404);99 *100 * @param {Mixed} obj118- * @param {Object|Number} headers or status101 * @param {Number} status102+ * @param {Number} status103 * @return {ServerResponse}104 * @api public105 */106 124-res.json = function(obj, headers, status){107+res.json = function(obj, status){108 var body = JSON.stringify(obj)109 , callback = this.req.query.callback110 , jsonp = this.app.enabled('jsonp callback');111 4 unmodified lines116 this.header('Content-Type', 'text/javascript');117 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';118 }119 137- return this.send(body, headers, status);120+ return this.send(body, status);121 };122 123 /**124 * Set status `code`.123 unmodified lines248 * Set or get response header `name` with optional `val`.249 *250 * @param {String} name251 * @param {String} val269- * @return {String}252+ * @return {ServerResponse} for chaining253 * @api public254 */255 256 res.header = function(name, val){274- if (val === undefined) {257+ if (1 == arguments.length) {258 return this.getHeader(name);259 } else {260 this.setHeader(name, val);278- return val;261+ return this;262 }263 };264 265 /**139 unmodified linesmodified:test/response.test.js+107 added-111 removed
23 unmodified lines24 app.get('/error', function(req, res, next){25 res.json('oh noes!', 500);26 });27 28- app.get('/headers', function(req, res, next){29- res.json(undefined, { 'X-Foo': 'bar' }, 302);30- });31-28 assert.response(app,29 { url: '/error' },30 { body: '"oh noes!"'31 , status: 50010 unmodified lines42 assert.response(app,43 { url: '/user' },44 { body: '{"name":"tj"}', headers: { 'Content-Type': json }});45 },50-46+ 47 'test #status()': function(){48 var app = express.createServer();53-49+ 50 app.get('/error', function(req, res, next){51 res.status(500).send('OH NO');52 });57-53+ 54 assert.response(app,55 { url: '/error' },56 { body: 'OH NO', status: 500 });57 },62-58+ 59 'test #send()': function(){60 var app = express.createServer();61 62 app.get('/html', function(req, res){67- res.send('<p>test</p>', { 'Content-Language': 'en' });63+ res.send('<p>test</p>');64 });69-65+ 66 app.get('/json', function(req, res){67 res.header('X-Foo', 'bar');72- res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);68+ res.send({ foo: 'bar' }, 201);69 });70 71 app.get('/text', function(req, res){72 res.header('X-Foo', 'bar');9 unmodified lines82 res.send('Oh noes!', 404);83 });84 85 app.get('/error', function(req, res){90- res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);86+ res.header('Content-Type', 'text/plain');87+ res.send('Oh shit!', 500);88 });89 90 app.get('/buffer', function(req, res){91 res.send(new Buffer('wahoo!'));5 unmodified lines97 98 app.get('/undefined', function(req, res, next){99 res.send(undefined);100 });104-101+ 102 app.get('/bool', function(req, res, next){103 res.send(true);104 });108-105+ 106 assert.response(app,107 { url: '/bool' },108 { body: 'true'109 , headers: { 'Content-Type': 'application/json; charset=utf-8' }});113-110+ 111 assert.response(app,112 { url: '/html' },113 { body: '<p>test</p>'114 , headers: {118- 'Content-Language': 'en'119- , 'Content-Type': 'text/html; charset=utf-8'115+ 'Content-Type': 'text/html; charset=utf-8'116 }});121- 117+ 118 assert.response(app,119 { url: '/json' },120 { body: '{"foo":"bar"}'121 , status: 201122 , headers: {123 'Content-Type': 'application/json; charset=utf-8'128- , 'X-Foo': 'baz'124+ , 'X-Foo': 'bar'125 }});130- 126+ 127 assert.response(app,128 { url: '/text' },129 { body: 'wahoo'130 , headers: {131 'Content-Type': 'text/plain'132 , 'X-Foo': 'bar'133 }});138-134+ 135 assert.response(app,136 { url: '/status/text' },137 { body: 'Oh noes!', status: 404 });142-138+ 139 assert.response(app,140 { url: '/status' },141 { body: 'Not Found'142 , status: 404143 , headers: { 'Content-Type': 'text/plain' }});148- 144+ 145 assert.response(app,146 { url: '/error' },147 { body: 'Oh shit!'148 , status: 500149 , headers: {154- 'Content-Type': 'text/plain; charset=utf-8'150+ 'Content-Type': 'text/plain'151 , 'Content-Length': '8'152 }});157- 153+ 154 assert.response(app,155 { url: '/buffer' },156 { body: 'wahoo!'157 , headers: {158 'Content-Type': 'application/octet-stream'159 , 'Content-Length': '6'160 }});165- 161+ 162 assert.response(app,163 { url: '/204' },164 { status: 204 }, function(res){165 assert.equal(undefined, res.headers['content-type']);166 assert.equal(undefined, res.headers['content-length']);167 });172-168+ 169 assert.response(app,170 { url: '/undefined' },171 { status: 500 });176-172+ 173 assert.response(app,174 { url: '/json?callback=test' },175 { body: '{"foo":"bar"}'176 , status: 201177 , headers: {178 'Content-Type': 'application/json; charset=utf-8'183- , 'X-Foo': 'baz'179+ , 'X-Foo': 'bar'180 }});181 },186-182+ 183 'test #send() JSONP': function(){184 var app = express.createServer();189-185+ 186 app.enable('jsonp callback');191-187+ 188 app.get('/jsonp', function(req, res){189 res.header('X-Foo', 'bar');194- res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);190+ res.send({ foo: 'bar' }, 201);191 });196-192+ 193 assert.response(app,194 { url: '/jsonp?callback=test' },195 { body: 'test({"foo":"bar"});'196 , status: 201197 , headers: {198 'Content-Type': 'text/javascript; charset=utf-8'203- , 'X-Foo': 'baz'199+ , 'X-Foo': 'bar'200 }});201 202 assert.response(app,203 { url: '/jsonp?callback=baz' },204 { body: 'baz({"foo":"bar"});'205 , status: 201, headers: {206 'Content-Type': 'text/javascript; charset=utf-8'211- , 'X-Foo': 'baz'207+ , 'X-Foo': 'bar'208 }});209 210 assert.response(app,211 { url: '/jsonp?callback=invalid()[]' },212 { body: 'invalid({"foo":"bar"});'213 , status: 201214 , headers: {215 'Content-Type': 'text/javascript; charset=utf-8'220- , 'X-Foo': 'baz'216+ , 'X-Foo': 'bar'217 }});218 },223-219+ 220 'test #json() JSONP': function(){221 var app = express.createServer();226-222+ 223 app.enable('jsonp callback');228-224+ 225 app.get('/jsonp', function(req, res){226 res.header('X-Foo', 'bar');231- res.json({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);227+ res.json({ foo: 'bar' }, 201);228 });233-229+ 230 assert.response(app,231 { url: '/jsonp?callback=test' },232 { body: 'test({"foo":"bar"});'233 , status: 201234 , headers: {235 'Content-Type': 'text/javascript; charset=utf-8'240- , 'X-Foo': 'baz'236+ , 'X-Foo': 'bar'237 }});238 239 assert.response(app,240 { url: '/jsonp?callback=baz' },241 { body: 'baz({"foo":"bar"});'242 , status: 201, headers: {243 'Content-Type': 'text/javascript; charset=utf-8'248- , 'X-Foo': 'baz'244+ , 'X-Foo': 'bar'245 }});246 247 assert.response(app,248 { url: '/jsonp?callback=invalid()[]' },249 { body: 'invalid({"foo":"bar"});'250 , status: 201251 , headers: {252 'Content-Type': 'text/javascript; charset=utf-8'257- , 'X-Foo': 'baz'253+ , 'X-Foo': 'bar'254 }});255 },260-256+ 257 'test #contentType()': function(){258 var app = express.createServer();259 260 app.get('/html', function(req, res){5 unmodified lines266 app.get('/json', function(req, res, next){267 res.contentType('json');268 res.send('{"foo":"bar"}');269 });274-270+ 271 app.get('/literal', function(req, res){272 res.contentType('application/json');273 res.send('{"foo":"bar"}')274 });279-275+ 276 assert.response(app,277 { url: '/literal' },278 { headers: { 'Content-Type': 'application/json' }}); 279 assert.response(app,10 unmodified lines290 app.get('/style.css', function(req, res){291 res.attachment();292 res.send('some stylezzz');293 });298-294+ 295 app.get('/*', function(req, res){296 res.attachment(req.params[0]);297 res.send('whatever');298 });2 unmodified lines301 { url: '/javascripts/jquery.js' },302 { body: 'whatever'303 , headers: { 'Content-Type': 'application/javascript'304 , 'Content-Disposition': 'attachment; filename="jquery.js"' }});309-305+ 306 assert.response(app,307 { url: '/style.css' },308 { body: 'some stylezzz'309 , headers: { 'Content-Type': 'text/html; charset=utf-8'4 unmodified lines314 var app = express.createServer()315 , app2 = express.createServer();316 317 app2.set('home', '/blog');322-318+ 319 app2.redirect('google', 'http://google.com');324-320+ 321 app2.redirect('blog', function(req, res){322 return req.params.id323 ? '/user/' + req.params.id + '/blog'324 : null;29 unmodified lines354 355 app2.get('/google', function(req, res){356 res.redirect('google');357 });362-358+ 359 app2.get('/user/:id', function(req, res){360 res.header('X-Foo', 'bar');361 res.redirect('blog');362 });367-363+ 364 assert.response(app,365 { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},366 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });371-367+ 368 assert.response(app,369 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},370 { body: 'Moved Permanently. Redirecting to http://google.com'371 , status: 301, headers: { Location: 'http://google.com' }});376-372+ 373 assert.response(app,374 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},375 { body: 'Moved Temporarily. Redirecting to http://foo.com/'376 , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});381-377+ 378 assert.response(app,379 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},380 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'381 , status: 302, headers: { Location: 'http://foo.com/foo' }});386-382+ 383 assert.response(app,384 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},385 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'386 , status: 302, headers: { Location: 'http://foo.com/foo' }});391-387+ 388 assert.response(app,389 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },390 { body: 'Moved Temporarily. Redirecting to http://foo.com/'391 , status: 302, headers: { Location: 'http://foo.com/' }});396-392+ 393 assert.response(app2,394 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},395 { body: 'Moved Permanently. Redirecting to http://google.com'396 , status: 301, headers: { Location: 'http://google.com' }});401-397+ 398 assert.response(app2,399 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},400 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'401 , status: 302, headers: { Location: 'http://foo.com/blog' }});406-402+ 403 assert.response(app2,404 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},405 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'406 , status: 302, headers: { Location: 'http://foo.com/blog' }});411-407+ 408 assert.response(app2,409 { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},410 { body: 'Moved Temporarily. Redirecting to http://google.com'411 , status: 302, headers: { Location: 'http://google.com' }});416-412+ 413 assert.response(app2,414 { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},415 { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'416 , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});417 },422-418+ 419 'test #redirect() when mounted': function(){420 var app = express.createServer()421 , blog = express.createServer();426-427-422+ 423+ 424 blog.get('/posts', function(req, res){425 res.redirect('/posts/all');426 });431-427+ 428 blog.get('/posts/all', function(req, res){429 res.send('all blog posts');430 });435-431+ 432 app.use('/blog', blog);437-433+ 434 assert.response(app,435 { url: '/blog/posts', headers: { Host: 'foo.com' }},436 { status: 302437 , headers: { Location: 'http://foo.com/blog/posts/all' }});438 },439 440 'test #sendfile()': function(){441 var app = express.createServer();446-442+ 443 app.get('/*', function(req, res, next){444 var file = req.params[0]445 , path = __dirname + '/fixtures/' + file;446 res.sendfile(path);447 });448 449 app.use(express.errorHandler());454-450+ 451 assert.response(app,452 { url: '/../express.test.js' },453 { body: 'Forbidden', status: 403 });458-454+ 455 assert.response(app,456 { url: '/user.json' },457 { body: '{"name":"tj"}'458 , status: 200, headers: { 'Content-Type': 'application/json' }});463-459+ 460 assert.response(app,461 { url: '/hello.haml' },462 { body: '%p Hello World'463 , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});468-464+ 465 assert.response(app,466 { url: '/doesNotExist' },467 { body: 'Cannot GET /doesNotExist', status: 404 });472-468+ 469 assert.response(app,470 { url: '/partials' },471 { body: 'Cannot GET /partials', status: 404 });476-472+ 473 assert.response(app,474 { url: '/large.json' },475 { status: 200, headers: { 'Content-Type': 'application/json' }});476 },9 unmodified lines486 if (err) return res.send('got an error');487 ++calls;488 });489 });494-490+ 491 assert.response(app,492 { url: '/forum' },493 { body: 'got an error' });498-494+ 495 assert.response(app,496 { url: '/does-not-exist' },497 { body: 'got an error' });502-498+ 499 assert.response(app,500 { url: '/large.json' },501 { headers: { 'Accept-Ranges': 'bytes' }});506-502+ 503 beforeExit(function(){504 calls.should.equal(1);505 });506 },9 unmodified lines516 if (err) return res.send('got an error');517 ++calls;518 });519 });524-520+ 521 assert.response(app,522 { url: '/does-not-exist' },523 { body: 'got an error' });528-524+ 525 assert.response(app,526 { url: '/large.json' },527 { headers: {528 'Accept-Ranges': 'bytes'529 , 'Cache-Control': 'public, max-age=3600'530 }});535-531+ 532 beforeExit(function(){533 calls.should.equal(1);534 });535 },540- 536+ 537 'test #sendfile() Accept-Ranges': function(){538 var app = express.createServer();539 540 app.get('/*', function(req, res, next){59 unmodified lines600 app.get('/json', function(req, res, next){601 var path = __dirname + '/fixtures/user.json';602 res.download(path, 'account.json');603 });608-604+ 605 app.get('/callback', function(req, res, next){606 res.download(__dirname + '/fixtures/pets.json', function(err){607 ++calls;608 });609 });614-610+ 611 app.get('/*', function(req, res, next){612 res.download(__dirname + '/fixtures/' + req.params[0]);613 });618-614+ 615 assert.response(app,616 { url: '/callback' },617 { body: '["tobi", "loki", "jane"]' });622-618+ 619 assert.response(app,620 { url: '/user.json' },621 { body: '{"name":"tj"}', status: 200, headers: {622 'Content-Type': 'application/json',12 unmodified lines635 { url: '/missing' },636 function(res){637 assert.equal(null, res.headers['content-disposition']);638 });643-639+ 640 assert.response(app,641 { url: '/some%20random%20text%20file.txt' },642 { body: 'hello' });647-643+ 644 beforeExit(function(){645 calls.should.equal(1);646 });647 },648 649 'test #cookie() path default': function(){650 var app = express.createServer();655-651+ 652 app.set('home', '/foo');657-653+ 654 app.get('/', function(req, res){655 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });656 res.cookie('something', 'else');657 res.redirect('/');658 });663-659+ 660 assert.response(app,661 { url: '/', headers: { Host: 'foo.com' }},662 function(res){663 res.headers['set-cookie']664 .should.eql(['rememberme=yes; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/foo']);665 });666 },671-667+ 668 'test #cookie() explicit path': function(){669 var app = express.createServer();674-670+ 671 app.set('/home', '/foo');676-672+ 673 app.get('/', function(req, res){674 res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });675 res.cookie('something', 'else');676 res.redirect('/');677 });682-678+ 679 assert.response(app,680 { url: '/', headers: { Host: 'foo.com' }},681 function(res){682 res.headers['set-cookie']683 .should.eql(['rememberme=yes; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/']);684 });685 },690-686+ 687 'test #cookie() null path': function(){688 var app = express.createServer();693-689+ 690 app.set('/home', '/foo');695-691+ 692 app.get('/', function(req, res){693 res.cookie('rememberme', 'yes', { path: null, expires: new Date(1), httpOnly: true });694 res.cookie('something', 'else', { path: null });695 res.redirect('/');696 });701-697+ 698 assert.response(app,699 { url: '/', headers: { Host: 'foo.com' }},700 function(res){701 res.headers['set-cookie']702 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);703 });704 },709-705+ 706 'test #clearCookie() default path': function(){707 var app = express.createServer();712-708+ 709 app.set('home', '/foo');714-710+ 711 app.get('/', function(req, res){712 res.clearCookie('rememberme');713 res.redirect('/');714 });4 unmodified lines719 res.headers['set-cookie']720 .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);721 });722 },727-723+ 724 'test #clearCookie() explicit path': function(){725 var app = express.createServer();730-726+ 727 app.set('home', '/bar');732-728+ 729 app.get('/', function(req, res){730 res.clearCookie('rememberme', { path: '/foo' });731 res.redirect('/');732 });4 unmodified lines737 res.headers['set-cookie']738 .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);739 });740 },745-741+ 742 'test HEAD': function(){743 var app = express.createServer();744 745 app.get('/', function(req, res, next){6 unmodified lines752 },753 754 'test .charset with res.send()': function(){755 var app = express.createServer();760-756+ 757 app.get('/', function(req, res){758 res.charset = 'ISO-8859-1';759 res.send('<p>hey</p>');760 });765-761+ 762 assert.response(app,763 { url: '/' },764 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});765 }1 unmodified linemodified:test/router.test.js+55 added-55 removed
74 unmodified lines75 },76 77 'test precedence': function(){78 var app = express.createServer();79-79+ 80 var hits = [];81-81+ 82 app.all('*', function(req, res, next){83 hits.push('all');84 next();85 });86-86+ 87 app.get('/foo', function(req, res, next){88 hits.push('GET /foo');89 next();90 });1 unmodified line92 app.get('/foo', function(req, res, next){93 hits.push('GET /foo2');94 next();95 });96-96+ 97 app.put('/foo', function(req, res, next){98 hits.push('PUT /foo');99 next();100 });101-101+ 102 assert.response(app,103 { url: '/foo' },104 function(){105 hits.should.eql(['all', 'GET /foo', 'GET /foo2']);106 });107 },108-108+ 109 'test named capture groups': function(){110 var app = express.createServer();111-111+ 112 app.get('/user/:id([0-9]{2,10})', function(req, res){113 res.send('user ' + req.params.id);114 });115-115+ 116 app.post('/pin/save/:lat(\\d+.\\d+)/:long(\\d+.\\d+)', function(req, res){117 res.send(req.params.lat + ' ' + req.params.long);118 });119-119+ 120 app.post('/pin/save2/:lat([0-9]+.[0-9]+)/:long([0-9]+.[0-9]+)', function(req, res){121 res.send(req.params.lat + ' ' + req.params.long);122 });123-123+ 124 assert.response(app,125 { url: '/pin/save/1.2/3.4', method: 'POST' },126 { body: '1.2 3.4' });127-127+ 128 assert.response(app,129 { url: '/pin/save2/1.2/3.4', method: 'POST' },130 { body: '1.2 3.4' });131-131+ 132 assert.response(app,133 { url: '/user/12' },134 { body: 'user 12' });135 3 unmodified lines139 },140 141 'test app.param()': function(){142 var app = express.createServer();143-143+ 144 var users = [145 { name: 'tj' }146 , { name: 'tobi' }147 , { name: 'loki' }148 , { name: 'jane' }149 , { name: 'bandit' }150 ];151-151+ 152 app.param('user', function(req, res, next, id){153 if (req.user = users[id]) {154 next();155 } else {156 next(new Error('failed to find user'));157 }158 });159-159+ 160 app.get('/user/:user', function(req, res, next){161 res.send('user ' + req.user.name);162 });163-163+ 164 assert.response(app,165 { url: '/user/0' },166 { body: 'user tj' });167 168 assert.response(app,169 { url: '/user/1' },170 { body: 'user tobi' });171 },172-172+ 173 'test app.param() optional execution': function(beforeExit){174 var app = express.createServer()175 , calls = 0;176-176+ 177 var months = ['Jan', 'Feb', 'Mar'];178-178+ 179 app.param('month', function(req, res, next, n){180 req.params.month = months[n];181 ++calls;182 next();183 });184-184+ 185 app.get('/calendar/:month?', function(req, res, next){186 res.send(req.params.month || months[0]);187 });188-188+ 189 assert.response(app,190 { url: '/calendar' },191 { body: 'Jan' });192-192+ 193 assert.response(app,194 { url: '/calendar/1' },195 { body: 'Feb' });196-196+ 197 beforeExit(function(){198 calls.should.equal(1);199 });200 },20 unmodified lines221 app.get('/user/:id', function(){});222 app.get('/user/:id/:op?', function(){});223 app.put('/user/:id', function(){});224 app.get('/user/:id/edit', function(){});225-225+ 226 var route = app.get('/user/:id')[0]227 route.should.be.an.instanceof(Route);228 route.callback.should.be.a('function');229 route.path.should.equal('/user/:id');230 route.regexp.should.be.an.instanceof(RegExp);231 route.method.should.equal('get');232 route.keys.should.eql([{ name: 'id', optional: false }]);233-233+ 234 app.get('/user').should.have.length(1);235 app.get('/user/:id').should.have.length(1);236 app.get('/user/:id/:op?').should.have.length(1);237 app.put('/user/:id').should.have.length(1);238 app.get('/user/:id/edit').should.have.length(1);239 app.get('/').should.have.be.empty;240 app.all('/user/:id').should.have.length(2);241-241+ 242 app.lookup.get('/user').should.have.length(1);243 app.lookup.get('/user/:id').should.have.length(1);244 app.lookup.get('/user/:id/:op?').should.have.length(1);245 app.lookup.put('/user/:id').should.have.length(1);1 unmodified line247 app.lookup.get('/').should.have.be.empty;248 app.lookup.all('/user/:id').should.have.length(2);249 app.lookup('/user/:id').should.have.length(2);250 },251-251+ 252 'test app.remove': function(){253 var app = express.createServer();254 app.get('/user', function(){});255 app.get('/user', function(){});256 app.put('/user', function(){});257-257+ 258 app.get('/user').should.have.length(2);259 var removed = app.remove.get('/user');260 removed.should.have.length(2);261-261+ 262 var removed = app.remove.get('/user');263 removed.should.have.length(0);264 app.get('/user').should.have.length(0);265-265+ 266 app.get('/user/:id', function(){});267 app.put('/user/:id', function(){});268 app.del('/user/:id', function(){});269-269+ 270 app.remove.all('/user/:id').should.have.length(3);271 app.remove.all('/user/:id').should.have.length(0);272 273 app.get('/user/:id', function(){});274 app.put('/user/:id', function(){});275 app.del('/user/:id', function(){});276-276+ 277 app.remove('/user/:id').should.have.length(3);278 },279 280 'test app.match': function(){2 unmodified lines283 app.get('/user/:id', function(){});284 app.get('/user/:id/:op?', function(){});285 app.put('/user/:id', function(){});286 app.get('/user/:id/edit', function(){});287-287+ 288 var route = app.match.get('/user/12')[0];289 route.should.be.an.instanceof(Route);290 route.callback.should.be.a('function');291 route.path.should.equal('/user/:id');292 route.regexp.should.be.an.instanceof(RegExp);293 route.method.should.equal('get');294 route.keys.should.eql([{ name: 'id', optional: false }]);295 //route.params.id.should.equal('12');296-296+ 297 app.match.get('/user').should.have.length(1);298 app.match.get('/user/12').should.have.length(2);299 app.match.get('/user/12/:op?').should.have.length(1);300 app.match.put('/user/100').should.have.length(1);1 unmodified line302 app.match.get('/').should.have.be.empty;303 app.match.all('/user/123').should.have.length(3);304 app.match('/user/123').should.have.length(3);305 },306-306+ 307 'test Collection': function(){308 var app = express.createServer();309 app.get('/user', function(){});310 app.get('/user/:id', function(){});311 app.get('/user/:id/:op?', function(){});312 app.put('/user/:id', function(){});313 app.get('/user/:id/edit', function(){});314-314+ 315 var ret = app.match.all('/user/12').remove();316 ret.should.have.length(3);317 app.match.all('/user/12').should.have.length(0);318 app.get('/user/:id').should.have.length(0);319 },320-320+ 321 'test "strict routing" setting': function(){322 var app = express.createServer();323-323+ 324 app.enable('strict routing');325-325+ 326 app.get('/:path', function(req, res, next){327 res.send({ type: 'directory' });328 });329-329+ 330 app.get('/:path/', function(req, res, next){331 res.send(['.', '..', 'foo.js', 'bar.js']);332 });333-333+ 334 assert.response(app,335 { url: '/lib' },336 { body: '{"type":"directory"}' });337-337+ 338 assert.response(app,339 { url: '/lib/' },340 { body: '[".","..","foo.js","bar.js"]' });341 },342-342+ 343 'test "case sensitive routes" setting': function(){344 var app = express.createServer();345-345+ 346 app.enable('case sensitive routes');347-347+ 348 app.get('/account', function(req, res){349 res.send('account');350 });351 352 app.get('/Account', function(req, res){353 res.send('Account');354 });355-355+ 356 assert.response(app,357 { url: '/account' },358 { body: 'account' });359-359+ 360 assert.response(app,361 { url: '/Account' },362 { body: 'Account' });363 },364 365 'override OPTIONS default': function(){366 var app = express.createServer();367-367+ 368 app.get('/', function(req, res, next){369 370 });371-371+ 372 app.options('/foo', function(req, res, next){373 res.header('Allow', 'GET')374 res.send('whatever');375 });376-376+ 377 assert.response(app,378 { url: '/', method: 'OPTIONS' },379 { body: 'GET', headers: { Allow: 'GET' }});380-380+ 381 assert.response(app,382 { url: '/foo', method: 'OPTIONS' },383 { body: 'whatever', headers: { Allow: 'GET' }});384 },385 386 'test req.route': function(){387 var app = express.createServer();388-388+ 389 var routes = [];390-390+ 391 app.get('/:foo?', function(req, res, next){392 routes.push(req.route.path);393 next();394 });395-395+ 396 app.get('/foo', function(req, res, next){397 routes.push(req.route.path);398 next();399 });400-400+ 401 assert.response(app,402 { url: '/foo' },403 function(){404 routes.should.eql(['/:foo?', '/foo']);3 unmodified lines