utils.js
This file
Path
test/utils.js
185 lines · 5.5 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 858e4dc point at it.
wsTJ Holowaychukbefore adoption858e4dc
lines 1–185why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');45describe('utils.isAbsolute()', function(){6 it('should support windows', function(){7 assert(utils.isAbsolute('c:\\'));8 assert(!utils.isAbsolute(':\\'));9 })10 11 it('should unices', function(){12 assert(utils.isAbsolute('/foo/bar'));13 assert(!utils.isAbsolute('foo/bar'));14 })15})1617describe('utils.flatten(arr)', function(){18 it('should flatten an array', function(){19 var arr = ['one', ['two', ['three', 'four'], 'five']];20 utils.flatten(arr)21 .should.eql(['one', 'two', 'three', 'four', 'five']);22 })23})2425describe('utils.escape(html)', function(){26 it('should escape html entities', function(){27 utils.escape('<script>foo & "bar"')28 .should.equal('<script>foo & "bar"')29 })30})3132describe('utils.parseQuality(str)', function(){33 it('should default quality to 1', function(){34 utils.parseQuality('text/html')35 .should.eql([{ value: 'text/html', quality: 1 }]);36 })37 38 it('should parse qvalues', function(){39 utils.parseQuality('text/html; q=0.5')40 .should.eql([{ value: 'text/html', quality: 0.5 }]);4142 utils.parseQuality('text/html; q=.2')43 .should.eql([{ value: 'text/html', quality: 0.2 }]);44 })45 46 it('should work with messed up whitespace', function(){47 utils.parseQuality('text/html ; q = .2')48 .should.eql([{ value: 'text/html', quality: 0.2 }]);49 })50 51 it('should work with multiples', function(){52 var str = 'da, en;q=.5, en-gb;q=.8';53 var arr = utils.parseQuality(str);54 arr[0].value.should.equal('da');55 arr[1].value.should.equal('en-gb');56 arr[2].value.should.equal('en');57 })58 59 it('should sort by quality', function(){60 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';61 var arr = utils.parseQuality(str);62 arr[0].value.should.equal('application/json');63 arr[1].value.should.equal('text/html');64 arr[2].value.should.equal('text/plain');65 })66 67 it('should exclude those with a quality of 0', function(){68 var str = 'text/plain;q=.2, application/json, text/html;q=0';69 var arr = utils.parseQuality(str);70 arr.should.have.length(2);71 })72})7374describe('utils.parseAccept(str)', function(){75 it('should provide .type', function(){76 var arr = utils.parseAccept('text/html');77 arr[0].type.should.equal('text');78 })79 80 it('should provide .subtype', function(){81 var arr = utils.parseAccept('text/html');82 arr[0].subtype.should.equal('html');83 })84})8586describe('utils.accepts(type, str)', function(){87 describe('when a string is not given', function(){88 it('should return the value', function(){89 utils.accepts('text/html')90 .should.equal('text/html');91 })92 })93 94 describe('when a string is empty', function(){95 it('should return the value', function(){96 utils.accepts('text/html', '')97 .should.equal('text/html');98 })99 })100101 describe('when */* is given', function(){102 it('should return the value', function(){103 utils.accepts('text/html', 'text/plain, */*')104 .should.equal('text/html');105 })106 })107108 describe('when an array is given', function(){109 it('should return the best match', function(){110 utils.accepts(['html', 'json'], 'text/plain, application/json')111 .should.equal('json');112113 utils.accepts(['html', 'application/json'], 'text/plain, application/json')114 .should.equal('application/json');115116 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')117 .should.equal('text/html');118 })119 })120121 describe('when a comma-delimited list is give', function(){122 it('should behave like an array', function(){123 utils.accepts('html, json', 'text/plain, application/json')124 .should.equal('json');125126 utils.accepts('html, application/json', 'text/plain, application/json')127 .should.equal('application/json');128129 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')130 .should.equal('text/html');131 })132 })133134 describe('when accepting type/subtype', function(){135 it('should return the value when present', function(){136 utils.accepts('text/html', 'text/plain, text/html')137 .should.equal('text/html');138 })139 140 it('should return undefined otherwise', function(){141 assert(null == utils.accepts('text/html', 'text/plain, application/json'));142 })143 })144 145 describe('when accepting */subtype', function(){146 it('should return the value when present', function(){147 utils.accepts('text/html', 'text/*')148 .should.equal('text/html');149 })150 151 it('should return undefined otherwise', function(){152 assert(null == utils.accepts('text/html', 'image/*'));153 })154 })155 156 describe('when accepting type/*', function(){157 it('should return the value when present', function(){158 utils.accepts('text/html', '*/html')159 .should.equal('text/html');160 })161 162 it('should return undefined otherwise', function(){163 assert(null == utils.accepts('text/html', '*/json'));164 })165 })166 167 describe('when an extension is given', function(){168 it('should return the value when present', function(){169 utils.accepts('html', 'text/html, application/json')170 .should.equal('html');171 })172 173 it('should return undefined otherwise', function(){174 assert(null == utils.accepts('html', 'text/plain, application/json'));175 })176 177 it('should support *', function(){178 utils.accepts('html', 'text/*')179 .should.equal('html');180181 utils.accepts('html', '*/html')182 .should.equal('html');183 })184 })185})