Skip to content
Myr

utils.js

This file

Path

test/utils.js

164 lines · 4.5 KiB · JavaScript

Last changed by

mergedTj Holowaychukbefore adoption547d18c

History of this file

Take it

utils.isAbsolute() testsTj Holowaychukbefore adoption5221ecc

lines 1–16why, one step back
1
23var utils = require('../lib/utils')4 , assert = require('assert');56describe('utils.isAbsolute()', function(){7 it('should support windows', function(){8 assert(utils.isAbsolute('c:\\'));9 assert(!utils.isAbsolute(':\\'));10 })11 12 it('should unices', function(){13 assert(utils.isAbsolute('/foo/bar'));14 assert(!utils.isAbsolute('foo/bar'));15 })16})

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

lines 17–20why, one step back
17
18describe('utils.flatten(arr)', function(){19 it('should flatten an array', function(){20 var arr = ['one', ['two', ['three', 'four'], 'five']];

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

line 21why, one step back
21 utils.flatten(arr)

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

line 22why, one step back
22 .should.eql(['one', 'two', 'three', 'four', 'five']);

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

lines 23–29why, one step back
23 })
24})2526describe('utils.escape(html)', function(){27 it('should escape html entities', function(){28 utils.escape('<script>foo & "bar"')29 .should.equal('&lt;script&gt;foo &amp; &quot;bar&quot;')

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

lines 30–31why, one step back
30 })
31})

mergedTj Holowaychukbefore adoption547d18c

lines 32–164why, one step back
32
33describe('utils.parseQuality(str)', function(){34 it('should default quality to 1', function(){35 utils.parseQuality('text/html')36 .should.eql([{ value: 'text/html', quality: 1 }]);37 })38 39 it('should parse qvalues', function(){40 utils.parseQuality('text/html; q=0.5')41 .should.eql([{ value: 'text/html', quality: 0.5 }]);4243 utils.parseQuality('text/html; q=.2')44 .should.eql([{ value: 'text/html', quality: 0.2 }]);45 })46 47 it('should work with messed up whitespace', function(){48 utils.parseQuality('text/html ; q = .2')49 .should.eql([{ value: 'text/html', quality: 0.2 }]);50 })51 52 it('should work with multiples', function(){53 var str = 'da, en;q=.5, en-gb;q=.8';54 var arr = utils.parseQuality(str);55 arr[0].value.should.equal('da');56 arr[1].value.should.equal('en-gb');57 arr[2].value.should.equal('en');58 })59 60 it('should sort by quality', function(){61 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';62 var arr = utils.parseQuality(str);63 arr[0].value.should.equal('application/json');64 arr[1].value.should.equal('text/html');65 arr[2].value.should.equal('text/plain');66 })67 68 it('should exclude those with a quality of 0', function(){69 var str = 'text/plain;q=.2, application/json, text/html;q=0';70 var arr = utils.parseQuality(str);71 arr.should.have.length(2);72 })73})7475describe('utils.parseAccept(str)', function(){76 it('should provide .type', function(){77 var arr = utils.parseAccept('text/html');78 arr[0].type.should.equal('text');79 })80 81 it('should provide .subtype', function(){82 var arr = utils.parseAccept('text/html');83 arr[0].subtype.should.equal('html');84 })85})8687describe('utils.accepts(type, str)', function(){88 describe('when a string is not given', function(){89 it('should return true', function(){90 utils.accepts('text/html')91 .should.be.true;92 })93 })94 95 describe('when a string is empty', function(){96 it('should return true', function(){97 utils.accepts('text/html', '')98 .should.be.true;99 })100 })101 102 describe('when */* is given', function(){103 it('should return true', function(){104 utils.accepts('text/html', 'text/plain, */*')105 .should.be.true;106 })107 })108109 describe('when accepting type/subtype', function(){110 it('should return true when present', function(){111 utils.accepts('text/html', 'text/plain, text/html')112 .should.be.true;113 })114 115 it('should return false otherwise', function(){116 utils.accepts('text/html', 'text/plain, application/json')117 .should.be.false;118 })119 })120 121 describe('when accepting */subtype', function(){122 it('should return true when present', function(){123 utils.accepts('text/html', 'text/*')124 .should.be.true;125 })126 127 it('should return false otherwise', function(){128 utils.accepts('text/html', 'image/*')129 .should.be.false;130 })131 })132 133 describe('when accepting type/*', function(){134 it('should return true when present', function(){135 utils.accepts('text/html', '*/html')136 .should.be.true;137 })138 139 it('should return false otherwise', function(){140 utils.accepts('text/html', '*/json')141 .should.be.false;142 })143 })144 145 describe('when an extension is given', function(){146 it('should return true when present', function(){147 utils.accepts('html', 'text/html, application/json')148 .should.be.true;149 })150 151 it('should return false otherwise', function(){152 utils.accepts('html', 'text/plain, application/json')153 .should.be.false;154 })155 156 it('should support *', function(){157 utils.accepts('html', 'text/*')158 .should.be.true;159160 utils.accepts('html', '*/html')161 .should.be.true;162 })163 })164})