req.accepts: default to first 'type'

master
Jonathan Ong 2013-11-20 18:00:24 -08:00
parent 5ae3f4eb30
commit 1d38d4acd1
3 changed files with 34 additions and 9 deletions

View File

@ -177,12 +177,16 @@ this.is('html');
### req.accepts(types)
Check if the given `type(s)` is acceptable, returning
the best match when true, otherwise `undefined`, in which
the best match when true, otherwise `false`, in which
case you should respond with 406 "Not Acceptable".
The `type` value may be one or more mime type string
such as "application/json", the extension name
such as "json", or an array `["json", "html", "text/plain"]`. When a list or array is given the _best_ match, if any is returned.
such as "json", or an array `["json", "html", "text/plain"]`.
When a list or array is given the _best_ match, if any is returned.
Note that if the client did not send an `Accept` header,
the first `type` will be returned.
```js
// Accept: text/html
@ -218,6 +222,7 @@ switch (this.accepts('json', 'html', 'text')) {
case 'json': break;
case 'html': break;
case 'text': break;
case false: break;
}
```

View File

@ -404,6 +404,7 @@ module.exports = {
if (!Array.isArray(types)) types = [].slice.call(arguments);
var n = new Negotiator(this.req);
if (!types.length) return n.preferredMediaTypes();
if (!this.header.accept) return types[0];
var mimes = types.map(extToMime);
var accepts = n.preferredMediaTypes(mimes);
var first = accepts[0];

View File

@ -3,17 +3,36 @@ var context = require('../context');
describe('ctx.accepts(types)', function(){
describe('with no arguments', function(){
it('should return all accepted types', function(){
var ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']);
describe('when Accept is populated', function(){
it('should return all accepted types', function(){
var ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts().should.eql(['text/html', 'text/plain', 'image/jpeg', 'application/*']);
})
})
describe('when Accept is not populated', function(){
it('should return []', function(){
var ctx = context();
ctx.accepts().should.eql([]);
})
})
})
describe('with no valid types', function(){
it('should return false', function(){
var ctx = context();
ctx.accepts('', 'hey').should.be.false;
describe('when Accept is populated', function(){
it('should return false', function(){
var ctx = context();
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
ctx.accepts('image/png', 'image/tiff').should.be.false;
})
})
describe('when Accept is not populated', function(){
it('should return the first type', function(){
var ctx = context();
ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*').should.equal('text/html');
})
})
})