renamed `res.respondTo()` to `res.format()`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
since you are not really responding with it, its basically as switch
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent a47660b · Browse files · 3 files·+83 added−83 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+83 added−83 removed
- tests:+80 added−80 removed
- other:+3 added−3 removed
Provenance
- Commit
ffcaa04- Tree
34681ea- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
a47660b
3 files changed+83 added−83 removed
modified:lib/response.js+3 added-3 removed
298 unmodified lines299 * 406 "Not Acceptable".300 *301 * Examples:302 *303- * res.respondTo({303+ * res.format({304 * 'text/plain': function(){305 * res.type('text').send('hey');306 * },307 * 10 unmodified lines318 * @return {ServerResponse} for chaining319 * @api public320 */321 322-res.respondTo = function(obj){322+res.format = function(obj){323 var keys = Object.keys(obj)324 , req = this.req325 , next = req.next326 , accepted = req.accepted213 unmodified lines540 url = req.protocol + '://' + host + url;541 }542 543 // Support text/{plain,html} by default544- this.respondTo({544+ this.format({545 'text/plain': function(){546 body = statusCodes[status] + '. Redirecting to ' + url;547 },548 81 unmodified linesadded:test/res.format.js+80 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
var express = require('../') , request = require('./support/http') , utils = require('../lib/utils') , assert = require('assert');
var app = express();
app.use(function(req, res, next){ res.format({ 'text/plain': function(){ res.send('hey'); },
'text/html': function(){ res.send('<p>hey</p>'); }, 'application/json': function(a, b, c){ assert(req == a); assert(res == b); assert(next == c); res.send({ message: 'hey' }); } });});
app.use(function(err, req, res, next){ res.send(406, 'Supports: ' + err.types.join(', '));})
describe('req', function(){ describe('.format(obj)', function(){ it('should utilize qvalues in negotiation', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, application/json, */*; q=.1') .expect('{"message":"hey"}', done); })
it('should allow wildcard type/subtypes', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, application/*, */*; q=.1') .expect('{"message":"hey"}', done); })
it('should default the Content-Type', function(done){ 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(); }); })
describe('when Accept is not present', function(){ it('should invoke the first callback', function(done){ request(app) .get('/') .expect('hey', done); }) })
describe('when no match is made', function(){ it('should should respond with 406 not acceptable', function(done){ 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(); }); }) }) })})deleted:test/res.respondTo.js-80 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
var express = require('../') , request = require('./support/http') , utils = require('../lib/utils') , assert = require('assert');
var app = express();
app.use(function(req, res, next){ res.respondTo({ 'text/plain': function(){ res.send('hey'); },
'text/html': function(){ res.send('<p>hey</p>'); }, 'application/json': function(a, b, c){ assert(req == a); assert(res == b); assert(next == c); res.send({ message: 'hey' }); } });});
app.use(function(err, req, res, next){ res.send(406, 'Supports: ' + err.types.join(', '));})
describe('req', function(){ describe('.respondTo(obj)', function(){ it('should utilize qvalues in negotiation', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, application/json, */*; q=.1') .expect('{"message":"hey"}', done); })
it('should allow wildcard type/subtypes', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, application/*, */*; q=.1') .expect('{"message":"hey"}', done); })
it('should default the Content-Type', function(done){ 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(); }); })
describe('when Accept is not present', function(){ it('should invoke the first callback', function(done){ request(app) .get('/') .expect('hey', done); }) })
describe('when no match is made', function(){ it('should should respond with 406 not acceptable', function(done){ 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(); }); }) }) })})