utils.js
This file
Path
test/utils.js
205 lines · 6.0 KiB · JavaScript
Last changed by
mergeTJ Holowaychukbefore adoptione2ad0d3
History of this fileTake it
The walk stopped after 200 commits: lines older than 64aac19 point at it.
any engineTJ Holowaychukbefore adoption64aac19
lines 1–4why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');4mergeTJ Holowaychukbefore adoptione2ad0d3
lines 5–24why, one step back5describe('utils.etag(body)', function(){67 var str = 'Hello CRC';8 var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';9 10 it('should support strings', function(){11 utils.etag(str).should.eql('"-2034458343"');12 })1314 it('should support utf8 strings', function(){15 utils.etag(strUTF8).should.eql('"1395090196"');16 })1718 it('should support buffer', function(){19 utils.etag(new Buffer(strUTF8)).should.eql('"1395090196"');20 utils.etag(new Buffer(str)).should.eql('"-2034458343"');21 })2223})24any engineTJ Holowaychukbefore adoption64aac19
lines 25–205why, one step back25describe('utils.isAbsolute()', function(){26 it('should support windows', function(){27 assert(utils.isAbsolute('c:\\'));28 assert(!utils.isAbsolute(':\\'));29 })30 31 it('should unices', function(){32 assert(utils.isAbsolute('/foo/bar'));33 assert(!utils.isAbsolute('foo/bar'));34 })35})3637describe('utils.flatten(arr)', function(){38 it('should flatten an array', function(){39 var arr = ['one', ['two', ['three', 'four'], 'five']];40 utils.flatten(arr)41 .should.eql(['one', 'two', 'three', 'four', 'five']);42 })43})4445describe('utils.escape(html)', function(){46 it('should escape html entities', function(){47 utils.escape('<script>foo & "bar"')48 .should.equal('<script>foo & "bar"')49 })50})5152describe('utils.parseQuality(str)', function(){53 it('should default quality to 1', function(){54 utils.parseQuality('text/html')55 .should.eql([{ value: 'text/html', quality: 1 }]);56 })57 58 it('should parse qvalues', function(){59 utils.parseQuality('text/html; q=0.5')60 .should.eql([{ value: 'text/html', quality: 0.5 }]);6162 utils.parseQuality('text/html; q=.2')63 .should.eql([{ value: 'text/html', quality: 0.2 }]);64 })65 66 it('should work with messed up whitespace', function(){67 utils.parseQuality('text/html ; q = .2')68 .should.eql([{ value: 'text/html', quality: 0.2 }]);69 })70 71 it('should work with multiples', function(){72 var str = 'da, en;q=.5, en-gb;q=.8';73 var arr = utils.parseQuality(str);74 arr[0].value.should.equal('da');75 arr[1].value.should.equal('en-gb');76 arr[2].value.should.equal('en');77 })78 79 it('should sort by quality', function(){80 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';81 var arr = utils.parseQuality(str);82 arr[0].value.should.equal('application/json');83 arr[1].value.should.equal('text/html');84 arr[2].value.should.equal('text/plain');85 })86 87 it('should exclude those with a quality of 0', function(){88 var str = 'text/plain;q=.2, application/json, text/html;q=0';89 var arr = utils.parseQuality(str);90 arr.should.have.length(2);91 })92})9394describe('utils.parseAccept(str)', function(){95 it('should provide .type', function(){96 var arr = utils.parseAccept('text/html');97 arr[0].type.should.equal('text');98 })99 100 it('should provide .subtype', function(){101 var arr = utils.parseAccept('text/html');102 arr[0].subtype.should.equal('html');103 })104})105106describe('utils.accepts(type, str)', function(){107 describe('when a string is not given', function(){108 it('should return the value', function(){109 utils.accepts('text/html')110 .should.equal('text/html');111 })112 })113 114 describe('when a string is empty', function(){115 it('should return the value', function(){116 utils.accepts('text/html', '')117 .should.equal('text/html');118 })119 })120121 describe('when */* is given', function(){122 it('should return the value', function(){123 utils.accepts('text/html', 'text/plain, */*')124 .should.equal('text/html');125 })126 })127128 describe('when an array is given', function(){129 it('should return the best match', function(){130 utils.accepts(['html', 'json'], 'text/plain, application/json')131 .should.equal('json');132133 utils.accepts(['html', 'application/json'], 'text/plain, application/json')134 .should.equal('application/json');135136 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')137 .should.equal('text/html');138 })139 })140141 describe('when a comma-delimited list is give', function(){142 it('should behave like an array', function(){143 utils.accepts('html, json', 'text/plain, application/json')144 .should.equal('json');145146 utils.accepts('html, application/json', 'text/plain, application/json')147 .should.equal('application/json');148149 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')150 .should.equal('text/html');151 })152 })153154 describe('when accepting type/subtype', function(){155 it('should return the value when present', function(){156 utils.accepts('text/html', 'text/plain, text/html')157 .should.equal('text/html');158 })159 160 it('should return undefined otherwise', function(){161 assert(null == utils.accepts('text/html', 'text/plain, application/json'));162 })163 })164 165 describe('when accepting */subtype', function(){166 it('should return the value when present', function(){167 utils.accepts('text/html', 'text/*')168 .should.equal('text/html');169 })170 171 it('should return undefined otherwise', function(){172 assert(null == utils.accepts('text/html', 'image/*'));173 })174 })175 176 describe('when accepting type/*', function(){177 it('should return the value when present', function(){178 utils.accepts('text/html', '*/html')179 .should.equal('text/html');180 })181 182 it('should return undefined otherwise', function(){183 assert(null == utils.accepts('text/html', '*/json'));184 })185 })186 187 describe('when an extension is given', function(){188 it('should return the value when present', function(){189 utils.accepts('html', 'text/html, application/json')190 .should.equal('html');191 })192 193 it('should return undefined otherwise', function(){194 assert(null == utils.accepts('html', 'text/plain, application/json'));195 })196 197 it('should support *', function(){198 utils.accepts('html', 'text/*')199 .should.equal('html');200201 utils.accepts('html', '*/html')202 .should.equal('html');203 })204 })205})