Skip to content
Myr

Merge pull request #1578 from Notificare/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Correct sorting of long list of accept header

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parents 2e53cb7, 88273a5 · Browse files · 2 files+39 added−11 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+39 added−11 removed

  • tests:+31 added−8 removed
  • other:+8 added−3 removed

2 files+39 added−11 removed

modified:lib/utils.js+8 added-3 removed
215 unmodified lines
216 .filter(function(obj){217 return obj.quality;218 })219 .sort(function(a, b){220 if (a.quality === b.quality) {221 return a.originalIndex - b.originalIndex;222 } else {220 return b.quality - a.quality;223 return b.quality - a.quality;224 }225 });226};227228/**229 * Parse accept params `str` returning an230 * object with `.value`, `.quality` and `.params`.231 * also includes `.originalIndex` for stable sorting232 *233 * @param {String} str234 * @return {Object}235 * @api private236 */237233function acceptParams(str) {238function acceptParams(str, index) {239 var parts = str.split(/ *; */);235 var ret = { value: parts[0], quality: 1, params: {} };240 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };241242 for (var i = 1; i < parts.length; ++i) {243 var pms = parts[i].split(/ *= */);244 if ('q' == pms[0]) {
63 unmodified lines
modified:test/utils.js+31 added-8 removed
50 unmodified lines
5152describe('utils.parseParams(str)', function(){53 it('should default quality to 1', function(){54 utils.parseParams('text/html')55 .should.eql([{ value: 'text/html', quality: 1, params: {}}]);55 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);56 })57 58 it('should parse qvalues', function(){59 utils.parseParams('text/html; q=0.5')60 .should.eql([{ value: 'text/html', quality: 0.5, params: {}}]);60 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);6162 utils.parseParams('text/html; q=.2')63 .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);63 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);64 })6566 it('should parse accept parameters', function(){67 utils.parseParams('application/json; ver=2.0')68 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}}]);68 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);6970 utils.parseParams('text/html; q=0.5; level=2')71 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}}]);71 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);72 utils.parseParams('text/html;q=.2;ver=beta')73 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}}]);73 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);74 })7576 it('should work with messed up whitespace', function(){77 utils.parseParams('text/html ; q = .2')78 .should.eql([{ value: 'text/html', quality: 0.2, params: {}}]);78 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);79 })80 81 it('should work with multiples', function(){82 var str = 'da, en;q=.5, en-gb;q=.8';
1 unmodified line
84 arr[0].value.should.equal('da');85 arr[1].value.should.equal('en-gb');86 arr[2].value.should.equal('en');87 })8889 it('should work with long lists', function(){90 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';91 var arr = utils.parseParams(str);92 arr[0].value.should.equal('en');93 arr[1].value.should.equal('nl');94 arr[2].value.should.equal('fr');95 arr[4].value.should.equal('de');96 arr[5].value.should.equal('ja');97 arr[6].value.should.equal('it');98 arr[7].value.should.equal('es');99 arr[8].value.should.equal('pt');100 arr[9].value.should.equal('pt-PT');101 arr[10].value.should.equal('da');102 arr[11].value.should.equal('fi');103 arr[12].value.should.equal('nb');104 arr[13].value.should.equal('sv');105 arr[14].value.should.equal('ko');106 arr[15].value.should.equal('zh-Hans');107 arr[16].value.should.equal('zh-Hant');108 arr[17].value.should.equal('ru');109 arr[18].value.should.equal('pl');110 })111 112 it('should sort by quality', function(){113 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';114 var arr = utils.parseParams(str);
119 unmodified lines
234 utils.accepts('html', '*/html')235 .should.equal('html');236 })237 })215})238})