req.accepts: default to first 'type'

This commit is contained in:
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) ### req.accepts(types)
Check if the given `type(s)` is acceptable, returning 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". case you should respond with 406 "Not Acceptable".
The `type` value may be one or more mime type string The `type` value may be one or more mime type string
such as "application/json", the extension name 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 ```js
// Accept: text/html // Accept: text/html
@ -218,6 +222,7 @@ switch (this.accepts('json', 'html', 'text')) {
case 'json': break; case 'json': break;
case 'html': break; case 'html': break;
case 'text': break; case 'text': break;
case false: break;
} }
``` ```

View file

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

View file

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