mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
Added req.accepts() comma-delimited string support
This commit is contained in:
parent
86a9e0803a
commit
d6d16b7899
|
|
@ -115,7 +115,7 @@ exports.acceptsArray = function(types, str){
|
|||
*/
|
||||
|
||||
exports.accepts = function(type, str){
|
||||
if (!Array.isArray(type)) type = [type];
|
||||
if ('string' == typeof type) type = type.split(/ *, */);
|
||||
return exports.acceptsArray(type, str);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,19 @@ describe('req', function(){
|
|||
})
|
||||
})
|
||||
|
||||
it('should accept a comma-delimited list of types', function(done){
|
||||
var app = express();
|
||||
|
||||
app.use(function(req, res, next){
|
||||
res.end(req.accepts('json, html'));
|
||||
});
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect('html', done);
|
||||
})
|
||||
|
||||
describe('.accept(types)', function(){
|
||||
it('should return the first when Accept is not present', function(done){
|
||||
var app = express();
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ describe('utils.accepts(type, str)', function(){
|
|||
.should.equal('text/html');
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('when */* is given', function(){
|
||||
it('should return the value', function(){
|
||||
utils.accepts('text/html', 'text/plain, */*')
|
||||
|
|
@ -105,6 +105,32 @@ describe('utils.accepts(type, str)', function(){
|
|||
})
|
||||
})
|
||||
|
||||
describe('when an array is given', function(){
|
||||
it('should return the best match', function(){
|
||||
utils.accepts(['html', 'json'], 'text/plain, application/json')
|
||||
.should.equal('json');
|
||||
|
||||
utils.accepts(['html', 'application/json'], 'text/plain, application/json')
|
||||
.should.equal('application/json');
|
||||
|
||||
utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')
|
||||
.should.equal('text/html');
|
||||
})
|
||||
})
|
||||
|
||||
describe('when a comma-delimited list is give', function(){
|
||||
it('should behave like an array', function(){
|
||||
utils.accepts('html, json', 'text/plain, application/json')
|
||||
.should.equal('json');
|
||||
|
||||
utils.accepts('html, application/json', 'text/plain, application/json')
|
||||
.should.equal('application/json');
|
||||
|
||||
utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')
|
||||
.should.equal('text/html');
|
||||
})
|
||||
})
|
||||
|
||||
describe('when accepting type/subtype', function(){
|
||||
it('should return the value when present', function(){
|
||||
utils.accepts('text/html', 'text/plain, text/html')
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user