utils.js
This file
Path
test/utils.js
221 lines · 6.9 KiB · JavaScript
Last changed by
fix long list params testTJ Holowaychukbefore adoption99bc628
History of this fileTake it
The walk stopped after 200 commits: lines older than 8ac3e80 point at it.
Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 1–4why, one step back12var utils = require('../lib/utils')3 , assert = require('assert');4mergeTJ Holowaychukbefore adoptione2ad0d3
lines 5–8why, 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>';fix long list params testTJ Holowaychukbefore adoption99bc628
line 9why, one step back9mergeTJ Holowaychukbefore adoptione2ad0d3
lines 10–24why, one step back10 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})24Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 25–29why, one step back25describe('utils.isAbsolute()', function(){26 it('should support windows', function(){27 assert(utils.isAbsolute('c:\\'));28 assert(!utils.isAbsolute(':\\'));29 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 30why, one step back30Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 31–51why, one step back31 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})51add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 52why, one step back52describe('utils.parseParams(str)', function(){Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 53why, one step back53 it('should default quality to 1', function(){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 54why, one step back54 utils.parseParams('text/html')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 55why, one step back55 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 56why, one step back56 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 57why, one step back57Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 58why, one step back58 it('should parse qvalues', function(){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 59why, one step back59 utils.parseParams('text/html; q=0.5')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 60why, one step back60 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 61why, one step back61add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 62why, one step back62 utils.parseParams('text/html; q=.2')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 63why, one step back63 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 64why, one step back64 })add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 65–67why, one step back6566 it('should parse accept parameters', function(){67 utils.parseParams('application/json; ver=2.0')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 68why, one step back68 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 69–70why, one step back6970 utils.parseParams('text/html; q=0.5; level=2')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 71why, one step back71 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 72why, one step back72 utils.parseParams('text/html;q=.2;ver=beta')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 73why, one step back73 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 74–75why, one step back74 })75Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 76why, one step back76 it('should work with messed up whitespace', function(){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 77why, one step back77 utils.parseParams('text/html ; q = .2')Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 78why, one step back78 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 79why, one step back79 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 80why, one step back80Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 81–82why, one step back81 it('should work with multiples', function(){82 var str = 'da, en;q=.5, en-gb;q=.8';add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 83why, one step back83 var arr = utils.parseParams(str);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 84–86why, one step back84 arr[0].value.should.equal('da');85 arr[1].value.should.equal('en-gb');86 arr[2].value.should.equal('en');Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
lines 87–90why, one step back87 })8889 it('should work with long lists', function(){90 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';fix long list params testTJ Holowaychukbefore adoption99bc628
lines 91–92why, one step back91 var arr = utils.parseParams(str).map(function(o){ return o.value });92 arr.should.eql(str.split(', '));Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 93why, one step back93 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 94why, one step back94Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 95–96why, one step back95 it('should sort by quality', function(){96 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 97why, one step back97 var arr = utils.parseParams(str);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 98–101why, one step back98 arr[0].value.should.equal('application/json');99 arr[1].value.should.equal('text/html');100 arr[2].value.should.equal('text/plain');101 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 102why, one step back102Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 103–104why, one step back103 it('should exclude those with a quality of 0', function(){104 var str = 'text/plain;q=.2, application/json, text/html;q=0';add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 105why, one step back105 var arr = utils.parseParams(str);Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 106–114why, one step back106 arr.should.have.length(2);107 })108})109110describe('utils.parseAccept(str)', function(){111 it('should provide .type', function(){112 var arr = utils.parseAccept('text/html');113 arr[0].type.should.equal('text');114 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 115why, one step back115Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 116–128why, one step back116 it('should provide .subtype', function(){117 var arr = utils.parseAccept('text/html');118 arr[0].subtype.should.equal('html');119 })120})121122describe('utils.accepts(type, str)', function(){123 describe('when a string is not given', function(){124 it('should return the value', function(){125 utils.accepts('text/html')126 .should.equal('text/html');127 })128 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 129why, one step back129Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 130–174why, one step back130 describe('when a string is empty', function(){131 it('should return the value', function(){132 utils.accepts('text/html', '')133 .should.equal('text/html');134 })135 })136137 describe('when */* is given', function(){138 it('should return the value', function(){139 utils.accepts('text/html', 'text/plain, */*')140 .should.equal('text/html');141 })142 })143144 describe('when an array is given', function(){145 it('should return the best match', function(){146 utils.accepts(['html', 'json'], 'text/plain, application/json')147 .should.equal('json');148149 utils.accepts(['html', 'application/json'], 'text/plain, application/json')150 .should.equal('application/json');151152 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')153 .should.equal('text/html');154 })155 })156157 describe('when a comma-delimited list is give', function(){158 it('should behave like an array', function(){159 utils.accepts('html, json', 'text/plain, application/json')160 .should.equal('json');161162 utils.accepts('html, application/json', 'text/plain, application/json')163 .should.equal('application/json');164165 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')166 .should.equal('text/html');167 })168 })169170 describe('when accepting type/subtype', function(){171 it('should return the value when present', function(){172 utils.accepts('text/html', 'text/plain, text/html')173 .should.equal('text/html');174 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 175why, one step back175Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 176–179why, one step back176 it('should return undefined otherwise', function(){177 assert(null == utils.accepts('text/html', 'text/plain, application/json'));178 })179 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 180why, one step back180Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 181–185why, one step back181 describe('when accepting */subtype', function(){182 it('should return the value when present', function(){183 utils.accepts('text/html', 'text/*')184 .should.equal('text/html');185 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 186why, one step back186Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 187–190why, one step back187 it('should return undefined otherwise', function(){188 assert(null == utils.accepts('text/html', 'image/*'));189 })190 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 191why, one step back191Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 192–196why, one step back192 describe('when accepting type/*', function(){193 it('should return the value when present', function(){194 utils.accepts('text/html', '*/html')195 .should.equal('text/html');196 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 197why, one step back197Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 198–201why, one step back198 it('should return undefined otherwise', function(){199 assert(null == utils.accepts('text/html', '*/json'));200 })201 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 202why, one step back202Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 203–207why, one step back203 describe('when an extension is given', function(){204 it('should return the value when present', function(){205 utils.accepts('html', 'text/html, application/json')206 .should.equal('html');207 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 208why, one step back208Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 209–211why, one step back209 it('should return undefined otherwise', function(){210 assert(null == utils.accepts('html', 'text/plain, application/json'));211 })fix long list params testTJ Holowaychukbefore adoption99bc628
line 212why, one step back212Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 213–221why, one step back213 it('should support *', function(){214 utils.accepts('html', 'text/*')215 .should.equal('html');216217 utils.accepts('html', '*/html')218 .should.equal('html');219 })220 })221})