req.acceptsCharsets - default to first type if header not set
This commit is contained in:
parent
8e10f12d38
commit
c24ab00b23
2 changed files with 25 additions and 5 deletions
|
@ -449,7 +449,8 @@ module.exports = {
|
||||||
if (!Array.isArray(charsets)) charsets = [].slice.call(arguments);
|
if (!Array.isArray(charsets)) charsets = [].slice.call(arguments);
|
||||||
var n = new Negotiator(this.req);
|
var n = new Negotiator(this.req);
|
||||||
if (!charsets.length) return n.preferredCharsets();
|
if (!charsets.length) return n.preferredCharsets();
|
||||||
return n.preferredCharsets(charsets)[0];
|
if (!this.header['accept-charset']) return charsets[0];
|
||||||
|
return n.preferredCharsets(charsets)[0] || false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,10 +20,29 @@ describe('ctx.acceptsCharsets()', function(){
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('with multiple arguments', function(){
|
describe('with multiple arguments', function(){
|
||||||
it('should return the best fit', function(){
|
describe('when Accept-Charset is populated', function(){
|
||||||
var ctx = context();
|
describe('if any types match', function(){
|
||||||
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
|
it('should return the best fit', function(){
|
||||||
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-8');
|
var ctx = context();
|
||||||
|
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
|
||||||
|
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-8');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('if no types match', function(){
|
||||||
|
it('should return false', function(){
|
||||||
|
var ctx = context();
|
||||||
|
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
|
||||||
|
ctx.acceptsCharsets('utf-16').should.be.false;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('when Accept-Charset is not populated', function(){
|
||||||
|
it('should return the first type', function(){
|
||||||
|
var ctx = context();
|
||||||
|
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7');
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue