utils.js
This file
Path
test/utils.js
253 lines · 7.6 KiB · JavaScript
Last changed by
improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
History of this fileTake it
The walk stopped after 200 commits: lines older than c805d80 point at it.
Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
lines 1–4why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');4tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 5why, one step back5describe('utils.etag(body, encoding)', function(){Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
line 6why, one step back6 it('should support strings', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 7why, one step back7 utils.etag('express!')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 8why, one step back8 .should.eql('"zZdv4imtWD49AHEviejT6A=="')Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
lines 9–11why, one step back9 })1011 it('should support utf8 strings', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 12why, one step back12 utils.etag('express❤', 'utf8')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 13why, one step back13 .should.eql('"fsFba4IxwQS6h6Umb+FNxw=="')Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
lines 14–16why, one step back14 })1516 it('should support buffer', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 17–18why, one step back17 var buf = new Buffer('express!')18 utils.etag(buf)improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 19why, one step back19 .should.eql('"zZdv4imtWD49AHEviejT6A=="');Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
lines 20–21why, one step back20 })21tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 22–23why, one step back22 it('should support empty string', function(){23 utils.etag('')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
lines 24–46why, one step back24 .should.eql('"1B2M2Y8AsgTpgAmY7PhCfg=="');25 })26})2728describe('utils.wetag(body, encoding)', function(){29 it('should support strings', function(){30 utils.wetag('express!')31 .should.eql('W/"8-3098196679"')32 })3334 it('should support utf8 strings', function(){35 utils.wetag('express❤', 'utf8')36 .should.eql('W/"a-1751845617"')37 })3839 it('should support buffer', function(){40 var buf = new Buffer('express!')41 utils.wetag(buf)42 .should.eql('W/"8-3098196679"');43 })4445 it('should support empty string', function(){46 utils.wetag('')tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 47–48why, one step back47 .should.eql('W/"0-0"');48 })Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
lines 49–246why, one step back49})5051describe('utils.isAbsolute()', function(){52 it('should support windows', function(){53 assert(utils.isAbsolute('c:\\'));54 assert(!utils.isAbsolute(':\\'));55 })5657 it('should unices', function(){58 assert(utils.isAbsolute('/foo/bar'));59 assert(!utils.isAbsolute('foo/bar'));60 })61})6263describe('utils.flatten(arr)', function(){64 it('should flatten an array', function(){65 var arr = ['one', ['two', ['three', 'four'], 'five']];66 utils.flatten(arr)67 .should.eql(['one', 'two', 'three', 'four', 'five']);68 })69})7071describe('utils.escape(html)', function(){72 it('should escape html entities', function(){73 utils.escape('<script>foo & "bar"')74 .should.equal('<script>foo & "bar"')75 })76})7778describe('utils.parseParams(str)', function(){79 it('should default quality to 1', function(){80 utils.parseParams('text/html')81 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);82 })8384 it('should parse qvalues', function(){85 utils.parseParams('text/html; q=0.5')86 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);8788 utils.parseParams('text/html; q=.2')89 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);90 })9192 it('should parse accept parameters', function(){93 utils.parseParams('application/json; ver=2.0')94 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);9596 utils.parseParams('text/html; q=0.5; level=2')97 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);98 utils.parseParams('text/html;q=.2;ver=beta')99 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);100 })101102 it('should work with messed up whitespace', function(){103 utils.parseParams('text/html ; q = .2')104 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);105 })106107 it('should work with multiples', function(){108 var str = 'da, en;q=.5, en-gb;q=.8';109 var arr = utils.parseParams(str);110 arr[0].value.should.equal('da');111 arr[1].value.should.equal('en-gb');112 arr[2].value.should.equal('en');113 })114115 it('should work with long lists', function(){116 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';117 var arr = utils.parseParams(str).map(function(o){ return o.value });118 arr.should.eql(str.split(', '));119 })120121 it('should sort by quality', function(){122 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';123 var arr = utils.parseParams(str);124 arr[0].value.should.equal('application/json');125 arr[1].value.should.equal('text/html');126 arr[2].value.should.equal('text/plain');127 })128129 it('should exclude those with a quality of 0', function(){130 var str = 'text/plain;q=.2, application/json, text/html;q=0';131 var arr = utils.parseParams(str);132 arr.should.have.length(2);133 })134})135136describe('utils.parseAccept(str)', function(){137 it('should provide .type', function(){138 var arr = utils.parseAccept('text/html');139 arr[0].type.should.equal('text');140 })141142 it('should provide .subtype', function(){143 var arr = utils.parseAccept('text/html');144 arr[0].subtype.should.equal('html');145 })146})147148describe('utils.accepts(type, str)', function(){149 describe('when a string is not given', function(){150 it('should return the value', function(){151 utils.accepts('text/html')152 .should.equal('text/html');153 })154 })155156 describe('when a string is empty', function(){157 it('should return the value', function(){158 utils.accepts('text/html', '')159 .should.equal('text/html');160 })161 })162163 describe('when */* is given', function(){164 it('should return the value', function(){165 utils.accepts('text/html', 'text/plain, */*')166 .should.equal('text/html');167 })168 })169170 describe('when an array is given', function(){171 it('should return the best match', function(){172 utils.accepts(['html', 'json'], 'text/plain, application/json')173 .should.equal('json');174175 utils.accepts(['html', 'application/json'], 'text/plain, application/json')176 .should.equal('application/json');177178 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')179 .should.equal('text/html');180 })181 })182183 describe('when a comma-delimited list is give', function(){184 it('should behave like an array', function(){185 utils.accepts('html, json', 'text/plain, application/json')186 .should.equal('json');187188 utils.accepts('html, application/json', 'text/plain, application/json')189 .should.equal('application/json');190191 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')192 .should.equal('text/html');193 })194 })195196 describe('when accepting type/subtype', function(){197 it('should return the value when present', function(){198 utils.accepts('text/html', 'text/plain, text/html')199 .should.equal('text/html');200 })201202 it('should return undefined otherwise', function(){203 assert(null == utils.accepts('text/html', 'text/plain, application/json'));204 })205 })206207 describe('when accepting */subtype', function(){208 it('should return the value when present', function(){209 utils.accepts('text/html', 'text/*')210 .should.equal('text/html');211 })212213 it('should return undefined otherwise', function(){214 assert(null == utils.accepts('text/html', 'image/*'));215 })216 })217218 describe('when accepting type/*', function(){219 it('should return the value when present', function(){220 utils.accepts('text/html', '*/html')221 .should.equal('text/html');222 })223224 it('should return undefined otherwise', function(){225 assert(null == utils.accepts('text/html', '*/json'));226 })227 })228229 describe('when an extension is given', function(){230 it('should return the value when present', function(){231 utils.accepts('html', 'text/html, application/json')232 .should.equal('html');233 })234235 it('should return undefined otherwise', function(){236 assert(null == utils.accepts('html', 'text/plain, application/json'));237 })238239 it('should support *', function(){240 utils.accepts('html', 'text/*')241 .should.equal('html');242243 utils.accepts('html', '*/html')244 .should.equal('html');245 })246 })tests: add accepts test with paramsDouglas Christopher Wilsonbefore adoptione7ad49b
lines 247–252why, one step back247248 describe('when params included', function(){249 it('should match params', function(){250 assert(null == utils.accepts('text/html; charset=us-ascii', 'text/html; charset=utf-8'));251 })252 })Merge pull request #1592 from bartsqueezy/eb1bbb9TJ Holowaychukbefore adoptionc805d80
line 253why, one step back253})