utils.js
This file
Path
test/utils.js
250 lines · 7.5 KiB · JavaScript
Last changed by
Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23
History of this fileTake it
The walk stopped after 200 commits: lines older than dc94f30 point at it.
deps: debug@1.0.1Douglas Christopher Wilsonbefore adoptiondc94f30
lines 1–30why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');45describe('utils.etag(body, encoding)', function(){6 it('should support strings', function(){7 utils.etag('express!')8 .should.eql('"zZdv4imtWD49AHEviejT6A=="')9 })1011 it('should support utf8 strings', function(){12 utils.etag('express❤', 'utf8')13 .should.eql('"fsFba4IxwQS6h6Umb+FNxw=="')14 })1516 it('should support buffer', function(){17 var buf = new Buffer('express!')18 utils.etag(buf)19 .should.eql('"zZdv4imtWD49AHEviejT6A=="');20 })2122 it('should support empty string', function(){23 utils.etag('')24 .should.eql('"1B2M2Y8AsgTpgAmY7PhCfg=="');25 })26})2728describe('utils.wetag(body, encoding)', function(){29 it('should support strings', function(){30 utils.wetag('express!')Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23
line 31why, one step back31 .should.eql('W/"8-b8aabac7"')deps: debug@1.0.1Douglas Christopher Wilsonbefore adoptiondc94f30
lines 32–35why, one step back32 })3334 it('should support utf8 strings', function(){35 utils.wetag('express❤', 'utf8')Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23
line 36why, one step back36 .should.eql('W/"a-686b0af1"')deps: debug@1.0.1Douglas Christopher Wilsonbefore adoptiondc94f30
lines 37–41why, one step back37 })3839 it('should support buffer', function(){40 var buf = new Buffer('express!')41 utils.wetag(buf)Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23
line 42why, one step back42 .should.eql('W/"8-b8aabac7"');deps: debug@1.0.1Douglas Christopher Wilsonbefore adoptiondc94f30
lines 43–250why, one step back43 })4445 it('should support empty string', function(){46 utils.wetag('')47 .should.eql('W/"0-0"');48 })49})5051describe('utils.isAbsolute()', function(){52 it('should support windows', function(){53 assert(utils.isAbsolute('c:\\'));54 assert(!utils.isAbsolute(':\\'));55 })5657 it('should support windows unc', function(){58 assert(utils.isAbsolute('\\\\foo\\bar'))59 })6061 it('should support unices', function(){62 assert(utils.isAbsolute('/foo/bar'));63 assert(!utils.isAbsolute('foo/bar'));64 })65})6667describe('utils.flatten(arr)', function(){68 it('should flatten an array', function(){69 var arr = ['one', ['two', ['three', 'four'], 'five']];70 utils.flatten(arr)71 .should.eql(['one', 'two', 'three', 'four', 'five']);72 })73})7475describe('utils.parseParams(str)', function(){76 it('should default quality to 1', function(){77 utils.parseParams('text/html')78 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);79 })8081 it('should parse qvalues', function(){82 utils.parseParams('text/html; q=0.5')83 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);8485 utils.parseParams('text/html; q=.2')86 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);87 })8889 it('should parse accept parameters', function(){90 utils.parseParams('application/json; ver=2.0')91 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);9293 utils.parseParams('text/html; q=0.5; level=2')94 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);95 utils.parseParams('text/html;q=.2;ver=beta')96 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);97 })9899 it('should work with messed up whitespace', function(){100 utils.parseParams('text/html ; q = .2')101 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);102 })103104 it('should work with multiples', function(){105 var str = 'da, en;q=.5, en-gb;q=.8';106 var arr = utils.parseParams(str);107 arr[0].value.should.equal('da');108 arr[1].value.should.equal('en-gb');109 arr[2].value.should.equal('en');110 })111112 it('should work with long lists', function(){113 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';114 var arr = utils.parseParams(str).map(function(o){ return o.value });115 arr.should.eql(str.split(', '));116 })117118 it('should sort by quality', function(){119 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';120 var arr = utils.parseParams(str);121 arr[0].value.should.equal('application/json');122 arr[1].value.should.equal('text/html');123 arr[2].value.should.equal('text/plain');124 })125126 it('should exclude those with a quality of 0', function(){127 var str = 'text/plain;q=.2, application/json, text/html;q=0';128 var arr = utils.parseParams(str);129 arr.should.have.length(2);130 })131})132133describe('utils.parseAccept(str)', function(){134 it('should provide .type', function(){135 var arr = utils.parseAccept('text/html');136 arr[0].type.should.equal('text');137 })138139 it('should provide .subtype', function(){140 var arr = utils.parseAccept('text/html');141 arr[0].subtype.should.equal('html');142 })143})144145describe('utils.accepts(type, str)', function(){146 describe('when a string is not given', function(){147 it('should return the value', function(){148 utils.accepts('text/html')149 .should.equal('text/html');150 })151 })152153 describe('when a string is empty', function(){154 it('should return the value', function(){155 utils.accepts('text/html', '')156 .should.equal('text/html');157 })158 })159160 describe('when */* is given', function(){161 it('should return the value', function(){162 utils.accepts('text/html', 'text/plain, */*')163 .should.equal('text/html');164 })165 })166167 describe('when an array is given', function(){168 it('should return the best match', function(){169 utils.accepts(['html', 'json'], 'text/plain, application/json')170 .should.equal('json');171172 utils.accepts(['html', 'application/json'], 'text/plain, application/json')173 .should.equal('application/json');174175 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')176 .should.equal('text/html');177 })178 })179180 describe('when a comma-delimited list is give', function(){181 it('should behave like an array', function(){182 utils.accepts('html, json', 'text/plain, application/json')183 .should.equal('json');184185 utils.accepts('html, application/json', 'text/plain, application/json')186 .should.equal('application/json');187188 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')189 .should.equal('text/html');190 })191 })192193 describe('when accepting type/subtype', function(){194 it('should return the value when present', function(){195 utils.accepts('text/html', 'text/plain, text/html')196 .should.equal('text/html');197 })198199 it('should return undefined otherwise', function(){200 assert(null == utils.accepts('text/html', 'text/plain, application/json'));201 })202 })203204 describe('when accepting */subtype', function(){205 it('should return the value when present', function(){206 utils.accepts('text/html', 'text/*')207 .should.equal('text/html');208 })209210 it('should return undefined otherwise', function(){211 assert(null == utils.accepts('text/html', 'image/*'));212 })213 })214215 describe('when accepting type/*', function(){216 it('should return the value when present', function(){217 utils.accepts('text/html', '*/html')218 .should.equal('text/html');219 })220221 it('should return undefined otherwise', function(){222 assert(null == utils.accepts('text/html', '*/json'));223 })224 })225226 describe('when an extension is given', function(){227 it('should return the value when present', function(){228 utils.accepts('html', 'text/html, application/json')229 .should.equal('html');230 })231232 it('should return undefined otherwise', function(){233 assert(null == utils.accepts('html', 'text/plain, application/json'));234 })235236 it('should support *', function(){237 utils.accepts('html', 'text/*')238 .should.equal('html');239240 utils.accepts('html', '*/html')241 .should.equal('html');242 })243 })244245 describe('when params included', function(){246 it('should match params', function(){247 assert(null == utils.accepts('text/html; charset=us-ascii', 'text/html; charset=utf-8'));248 })249 })250})