utils.js
This file
Path
test/utils.js
272 lines · 8.0 KiB · JavaScript
Last changed by
replace utils.escape with html-escapeDouglas Christopher Wilsonbefore adoptionf433b7c
History of this fileTake it
The walk stopped after 200 commits: lines older than a10f695 point at it.
pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 1–4why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');4tests: add more testsDouglas Christopher Wilsonbefore adoption8751d7e
lines 5–26why, one step back5describe('utils.deprecate(fn, msg)', function(){6 var env7 before(function(){8 env = process.env.NODE_ENV9 })10 after(function(){11 process.env.NODE_ENV = env12 })1314 it('should pass-through fn in test environment', function(){15 var fn = function(){}16 process.env.NODE_ENV = 'test'17 utils.deprecate(fn).should.equal(fn)18 })1920 it('should return new fn in other environment', function(){21 var fn = function(){}22 process.env.NODE_ENV = ''23 utils.deprecate(fn).should.not.equal(fn)24 })25})26tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 27why, one step back27describe('utils.etag(body, encoding)', function(){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 28why, one step back28 it('should support strings', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 29why, one step back29 utils.etag('express!')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 30why, one step back30 .should.eql('"zZdv4imtWD49AHEviejT6A=="')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 31–33why, one step back31 })3233 it('should support utf8 strings', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
line 34why, one step back34 utils.etag('express❤', 'utf8')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 35why, one step back35 .should.eql('"fsFba4IxwQS6h6Umb+FNxw=="')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 36–38why, one step back36 })3738 it('should support buffer', function(){tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 39–40why, one step back39 var buf = new Buffer('express!')40 utils.etag(buf)improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
line 41why, one step back41 .should.eql('"zZdv4imtWD49AHEviejT6A=="');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 42–43why, one step back42 })43tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 44–45why, one step back44 it('should support empty string', function(){45 utils.etag('')improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a
lines 46–68why, one step back46 .should.eql('"1B2M2Y8AsgTpgAmY7PhCfg=="');47 })48})4950describe('utils.wetag(body, encoding)', function(){51 it('should support strings', function(){52 utils.wetag('express!')53 .should.eql('W/"8-3098196679"')54 })5556 it('should support utf8 strings', function(){57 utils.wetag('express❤', 'utf8')58 .should.eql('W/"a-1751845617"')59 })6061 it('should support buffer', function(){62 var buf = new Buffer('express!')63 utils.wetag(buf)64 .should.eql('W/"8-3098196679"');65 })6667 it('should support empty string', function(){68 utils.wetag('')tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845
lines 69–70why, one step back69 .should.eql('W/"0-0"');70 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 71–78why, one step back71})7273describe('utils.isAbsolute()', function(){74 it('should support windows', function(){75 assert(utils.isAbsolute('c:\\'));76 assert(!utils.isAbsolute(':\\'));77 })78tests: add more testsDouglas Christopher Wilsonbefore adoption8751d7e
lines 79–83why, one step back79 it('should support windows unc', function(){80 assert(utils.isAbsolute('\\\\foo\\bar'))81 })8283 it('should support unices', function(){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 84–265why, one step back84 assert(utils.isAbsolute('/foo/bar'));85 assert(!utils.isAbsolute('foo/bar'));86 })87})8889describe('utils.flatten(arr)', function(){90 it('should flatten an array', function(){91 var arr = ['one', ['two', ['three', 'four'], 'five']];92 utils.flatten(arr)93 .should.eql(['one', 'two', 'three', 'four', 'five']);94 })95})9697describe('utils.parseParams(str)', function(){98 it('should default quality to 1', function(){99 utils.parseParams('text/html')100 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);101 })102103 it('should parse qvalues', function(){104 utils.parseParams('text/html; q=0.5')105 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);106107 utils.parseParams('text/html; q=.2')108 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);109 })110111 it('should parse accept parameters', function(){112 utils.parseParams('application/json; ver=2.0')113 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);114115 utils.parseParams('text/html; q=0.5; level=2')116 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);117 utils.parseParams('text/html;q=.2;ver=beta')118 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);119 })120121 it('should work with messed up whitespace', function(){122 utils.parseParams('text/html ; q = .2')123 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);124 })125126 it('should work with multiples', function(){127 var str = 'da, en;q=.5, en-gb;q=.8';128 var arr = utils.parseParams(str);129 arr[0].value.should.equal('da');130 arr[1].value.should.equal('en-gb');131 arr[2].value.should.equal('en');132 })133134 it('should work with long lists', function(){135 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';136 var arr = utils.parseParams(str).map(function(o){ return o.value });137 arr.should.eql(str.split(', '));138 })139140 it('should sort by quality', function(){141 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';142 var arr = utils.parseParams(str);143 arr[0].value.should.equal('application/json');144 arr[1].value.should.equal('text/html');145 arr[2].value.should.equal('text/plain');146 })147148 it('should exclude those with a quality of 0', function(){149 var str = 'text/plain;q=.2, application/json, text/html;q=0';150 var arr = utils.parseParams(str);151 arr.should.have.length(2);152 })153})154155describe('utils.parseAccept(str)', function(){156 it('should provide .type', function(){157 var arr = utils.parseAccept('text/html');158 arr[0].type.should.equal('text');159 })160161 it('should provide .subtype', function(){162 var arr = utils.parseAccept('text/html');163 arr[0].subtype.should.equal('html');164 })165})166167describe('utils.accepts(type, str)', function(){168 describe('when a string is not given', function(){169 it('should return the value', function(){170 utils.accepts('text/html')171 .should.equal('text/html');172 })173 })174175 describe('when a string is empty', function(){176 it('should return the value', function(){177 utils.accepts('text/html', '')178 .should.equal('text/html');179 })180 })181182 describe('when */* is given', function(){183 it('should return the value', function(){184 utils.accepts('text/html', 'text/plain, */*')185 .should.equal('text/html');186 })187 })188189 describe('when an array is given', function(){190 it('should return the best match', function(){191 utils.accepts(['html', 'json'], 'text/plain, application/json')192 .should.equal('json');193194 utils.accepts(['html', 'application/json'], 'text/plain, application/json')195 .should.equal('application/json');196197 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')198 .should.equal('text/html');199 })200 })201202 describe('when a comma-delimited list is give', function(){203 it('should behave like an array', function(){204 utils.accepts('html, json', 'text/plain, application/json')205 .should.equal('json');206207 utils.accepts('html, application/json', 'text/plain, application/json')208 .should.equal('application/json');209210 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')211 .should.equal('text/html');212 })213 })214215 describe('when accepting type/subtype', function(){216 it('should return the value when present', function(){217 utils.accepts('text/html', 'text/plain, text/html')218 .should.equal('text/html');219 })220221 it('should return undefined otherwise', function(){222 assert(null == utils.accepts('text/html', 'text/plain, application/json'));223 })224 })225226 describe('when accepting */subtype', function(){227 it('should return the value when present', function(){228 utils.accepts('text/html', 'text/*')229 .should.equal('text/html');230 })231232 it('should return undefined otherwise', function(){233 assert(null == utils.accepts('text/html', 'image/*'));234 })235 })236237 describe('when accepting type/*', function(){238 it('should return the value when present', function(){239 utils.accepts('text/html', '*/html')240 .should.equal('text/html');241 })242243 it('should return undefined otherwise', function(){244 assert(null == utils.accepts('text/html', '*/json'));245 })246 })247248 describe('when an extension is given', function(){249 it('should return the value when present', function(){250 utils.accepts('html', 'text/html, application/json')251 .should.equal('html');252 })253254 it('should return undefined otherwise', function(){255 assert(null == utils.accepts('html', 'text/plain, application/json'));256 })257258 it('should support *', function(){259 utils.accepts('html', 'text/*')260 .should.equal('html');261262 utils.accepts('html', '*/html')263 .should.equal('html');264 })265 })tests: add accepts test with paramsDouglas Christopher Wilsonbefore adoptione7ad49b
lines 266–271why, one step back266267 describe('when params included', function(){268 it('should match params', function(){269 assert(null == utils.accepts('text/html; charset=us-ascii', 'text/html; charset=utf-8'));270 })271 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 272why, one step back272})