Skip to content
Myr

utils.js

This file

Path

test/utils.js

159 lines · 4.6 KiB · JavaScript

Last changed by

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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–87why, 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(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 88why, one step back
88 it('should return the value', function(){

mergedTj Holowaychukbefore adoption547d18c

line 89why, one step back
89 utils.accepts('text/html')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 90why, one step back
90 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 91–94why, one step back
91 })
92 })93 94 describe('when a string is empty', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 95why, one step back
95 it('should return the value', function(){

mergedTj Holowaychukbefore adoption547d18c

line 96why, one step back
96 utils.accepts('text/html', '')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 97why, one step back
97 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 98–101why, one step back
98 })
99 })100 101 describe('when */* is given', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 102why, one step back
102 it('should return the value', function(){

mergedTj Holowaychukbefore adoption547d18c

line 103why, one step back
103 utils.accepts('text/html', 'text/plain, */*')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 104why, one step back
104 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 105–108why, one step back
105 })
106 })107108 describe('when accepting type/subtype', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 109why, one step back
109 it('should return the value when present', function(){

mergedTj Holowaychukbefore adoption547d18c

line 110why, one step back
110 utils.accepts('text/html', 'text/plain, text/html')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 111why, one step back
111 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 112–113why, one step back
112 })
113

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

lines 114–115why, one step back
114 it('should return undefined otherwise', function(){
115 assert(null == utils.accepts('text/html', 'text/plain, application/json'));

mergedTj Holowaychukbefore adoption547d18c

lines 116–119why, one step back
116 })
117 })118 119 describe('when accepting */subtype', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 120why, one step back
120 it('should return the value when present', function(){

mergedTj Holowaychukbefore adoption547d18c

line 121why, one step back
121 utils.accepts('text/html', 'text/*')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 122why, one step back
122 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 123–124why, one step back
123 })
124

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

lines 125–126why, one step back
125 it('should return undefined otherwise', function(){
126 assert(null == utils.accepts('text/html', 'image/*'));

mergedTj Holowaychukbefore adoption547d18c

lines 127–130why, one step back
127 })
128 })129 130 describe('when accepting type/*', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 131why, one step back
131 it('should return the value when present', function(){

mergedTj Holowaychukbefore adoption547d18c

line 132why, one step back
132 utils.accepts('text/html', '*/html')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 133why, one step back
133 .should.equal('text/html');

mergedTj Holowaychukbefore adoption547d18c

lines 134–135why, one step back
134 })
135

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

lines 136–137why, one step back
136 it('should return undefined otherwise', function(){
137 assert(null == utils.accepts('text/html', '*/json'));

mergedTj Holowaychukbefore adoption547d18c

lines 138–141why, one step back
138 })
139 })140 141 describe('when an extension is given', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 142why, one step back
142 it('should return the value when present', function(){

mergedTj Holowaychukbefore adoption547d18c

line 143why, one step back
143 utils.accepts('html', 'text/html, application/json')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 144why, one step back
144 .should.equal('html');

mergedTj Holowaychukbefore adoption547d18c

lines 145–146why, one step back
145 })
146

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

lines 147–148why, one step back
147 it('should return undefined otherwise', function(){
148 assert(null == utils.accepts('html', 'text/plain, application/json'));

mergedTj Holowaychukbefore adoption547d18c

lines 149–152why, one step back
149 })
150 151 it('should support *', function(){152 utils.accepts('html', 'text/*')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 153why, one step back
153 .should.equal('html');

mergedTj Holowaychukbefore adoption547d18c

lines 154–155why, one step back
154
155 utils.accepts('html', '*/html')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

line 156why, one step back
156 .should.equal('html');

mergedTj Holowaychukbefore adoption547d18c

lines 157–159why, one step back
157 })
158 })159})