Added extname support to `res.format()`. Closes #1024
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 · parent ffcaa04 · Browse files · 2 files·+97 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+97 added−42 removed
- tests:+59 added−35 removed
- other:+38 added−7 removed
Provenance
- Commit
eb3105e- Tree
6c46fd2- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
ffcaa04
2 files changed+97 added−42 removed
modified:lib/response.js+38 added-7 removed
296 unmodified lines297 * is invoked, otherwise the first match is used. When298 * no match is performed the server responds with299 * 406 "Not Acceptable".300 *301- * Examples:301+ * Content-Type is set for you, however if you choose302+ * you may alter this within the callback using `res.type()`303+ * or `res.set('Content-Type', ...)`.304 *305 * res.format({306 * 'text/plain': function(){305- * res.type('text').send('hey');307+ * res.send('hey');308 * },309 * 310 * 'text/html': function(){311 * res.send('<p>hey</p>');3 unmodified lines315 * res.send({ message: 'hey' });316 * }317 * });318 *319+ * In addition to canonicalized MIME types you may320+ * also use extnames mapped to these types:321+ *322+ * res.format({323+ * text: function(){324+ * res.send('hey');325+ * },326+ * 327+ * html: function(){328+ * res.send('<p>hey</p>');329+ * },330+ * 331+ * json: function(){332+ * res.send({ message: 'hey' });333+ * }334+ * });335+ *336 * @param {Object} obj337 * @return {ServerResponse} for chaining338 * @api public339 */340 341 res.format = function(obj){342 var keys = Object.keys(obj)343+ , types = []344 , req = this.req345 , next = req.next346 , accepted = req.accepted347 , type348 , key;349 350+ // normalize extnames -> mime351+ for (var i = 0; i < keys.length; ++i) {352+ types.push(~keys[i].indexOf('/')353+ ? keys[i]354+ : mime.lookup(keys[i]));355+ }356+357 out:358 for (var i = 0, len = accepted.length; i < len; ++i) {332- for (var j = 0, jlen = keys.length; j < jlen; ++j) {333- if (accept(keys[j].split('/'), accepted[i])) {359+ for (var j = 0, jlen = types.length; j < jlen; ++j) {360+ if (accept(types[j].split('/'), accepted[i])) {361 key = keys[j];362+ type = types[j];363 break out;364 }365 }366 }367 340- if (!accepted.length) key = keys.shift();368+ if (!accepted.length) {369+ key = keys[0];370+ type = types[0];371+ }372 373 if (key) {343- this.set('Content-Type', key);374+ this.set('Content-Type', type);375 obj[key](req, this, next);376 } else {377 var err = new Error('Not Acceptable');378 err.status = 406;348- err.types = keys;379+ err.types = types;380 next(err);381 }382 383 return this;277 unmodified linesmodified:test/res.format.js+59 added-35 removed
24 unmodified lines252627282929303132333435363738394041424344454647343536373848495051525354555657585960616263646541666768697071727374757677787980818283848586874445888990914892939495519697535498991001011025859606162636465666768697071727374757677781038010424 unmodified lines });});
app.use(function(err, req, res, next){ res.send(406, 'Supports: ' + err.types.join(', ')); res.send(err.status, 'Supports: ' + err.types.join(', '));})
var app2 = express();
app2.use(function(req, res, next){ res.format({ text: function(){ res.send('hey') }, html: function(){ res.send('<p>hey</p>') }, json: function(){ res.send({ message: 'hey' }) } });});
app2.use(function(err, req, res, next){ res.send(err.status, '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); describe('with canonicalized mime types', function(){ test(app); })
describe('with extnames', function(){ test(app2); }) })})
function test(app) { 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){ 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('/') .set('Accept', 'text/html; q=.5, application/*, */*; q=.1') .expect('{"message":"hey"}', done); .expect('hey', done); }) })
it('should default the Content-Type', function(done){ describe('when no match is made', function(){ it('should should respond with 406 not acceptable', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, text/plain') .set('Accept', 'foo/bar') .end(function(res){ res.headers['content-type'].should.equal('text/plain'); res.body.should.equal('hey'); res.should.have.status(406); res.body.should.equal('Supports: text/plain, text/html, application/json'); 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(); }); }) }) })})}