Skip to content
Myr

utils.js

This file

Path

test/utils.js

163 lines · 4.5 KiB · JavaScript

Last changed by

Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption613bdc9

History of this file

Take it

utils.isAbsolute() testsTj Holowaychukbefore adoption5221ecc

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

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

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

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

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

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

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

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

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

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

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

mergedTj Holowaychukbefore adoption547d18c

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