utils.js
This file
Path
test/utils.js
185 lines · 5.5 KiB · JavaScript
Last changed by
Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7
History of this fileTake it
The walk stopped after 200 commits: lines older than e2f43df point at it.
refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 1–87why, 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(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 88why, one step back88 it('should return the value', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 89why, one step back89 utils.accepts('text/html')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 90why, one step back90 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 91–94why, one step back91 })92 })93 94 describe('when a string is empty', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 95why, one step back95 it('should return the value', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 96why, one step back96 utils.accepts('text/html', '')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 97why, one step back97 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 98–99why, one step back98 })99 })Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7
line 100why, one step back100refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 101why, one step back101 describe('when */* is given', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 102why, one step back102 it('should return the value', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 103why, one step back103 utils.accepts('text/html', 'text/plain, */*')Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7
lines 104–129why, one step back104 .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')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 130why, one step back130 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 131–134why, one step back131 })132 })133134 describe('when accepting type/subtype', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 135why, one step back135 it('should return the value when present', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 136why, one step back136 utils.accepts('text/html', 'text/plain, text/html')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 137why, one step back137 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 138–139why, one step back138 })139 refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
lines 140–141why, one step back140 it('should return undefined otherwise', function(){141 assert(null == utils.accepts('text/html', 'text/plain, application/json'));refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 142–145why, one step back142 })143 })144 145 describe('when accepting */subtype', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 146why, one step back146 it('should return the value when present', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 147why, one step back147 utils.accepts('text/html', 'text/*')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 148why, one step back148 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 149–150why, one step back149 })150 refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
lines 151–152why, one step back151 it('should return undefined otherwise', function(){152 assert(null == utils.accepts('text/html', 'image/*'));refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 153–156why, one step back153 })154 })155 156 describe('when accepting type/*', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 157why, one step back157 it('should return the value when present', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 158why, one step back158 utils.accepts('text/html', '*/html')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 159why, one step back159 .should.equal('text/html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 160–161why, one step back160 })161 refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
lines 162–163why, one step back162 it('should return undefined otherwise', function(){163 assert(null == utils.accepts('text/html', '*/json'));refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 164–167why, one step back164 })165 })166 167 describe('when an extension is given', function(){refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 168why, one step back168 it('should return the value when present', function(){refactored app.render()TJ Holowaychukbefore adoptione2f43df
line 169why, one step back169 utils.accepts('html', 'text/html, application/json')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 170why, one step back170 .should.equal('html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 171–172why, one step back171 })172 refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
lines 173–174why, one step back173 it('should return undefined otherwise', function(){174 assert(null == utils.accepts('html', 'text/plain, application/json'));refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 175–178why, one step back175 })176 177 it('should support *', function(){178 utils.accepts('html', 'text/*')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 179why, one step back179 .should.equal('html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 180–181why, one step back180181 utils.accepts('html', '*/html')refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08
line 182why, one step back182 .should.equal('html');refactored app.render()TJ Holowaychukbefore adoptione2f43df
lines 183–185why, one step back183 })184 })185})