Skip to content
Myr

utils.js

This file

Path

test/utils.js

185 lines · 5.5 KiB · JavaScript

Last changed by

Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7

History of this file

Take it

The walk stopped after 200 commits: lines older than e2f43df point at it.

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 1–87why, 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})1617describe('utils.flatten(arr)', function(){18 it('should flatten an array', function(){19 var arr = ['one', ['two', ['three', 'four'], 'five']];20 utils.flatten(arr)21 .should.eql(['one', 'two', 'three', 'four', 'five']);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;')29 })30})3132describe('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(){

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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');

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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(){

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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');

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 98–99why, one step back
98 })
99 })

Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7

line 100why, one step back
100

refactored app.render()TJ Holowaychukbefore adoptione2f43df

line 101why, one step back
101 describe('when */* is given', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

Added `req.accepts()` comma-delimited string supportTJ Holowaychukbefore adoptiond6d16b7

lines 104–129why, one step back
104 .should.equal('text/html');
105 })106 })107108 describe('when an array is given', function(){109 it('should return the best match', function(){110 utils.accepts(['html', 'json'], 'text/plain, application/json')111 .should.equal('json');112113 utils.accepts(['html', 'application/json'], 'text/plain, application/json')114 .should.equal('application/json');115116 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')117 .should.equal('text/html');118 })119 })120121 describe('when a comma-delimited list is give', function(){122 it('should behave like an array', function(){123 utils.accepts('html, json', 'text/plain, application/json')124 .should.equal('json');125126 utils.accepts('html, application/json', 'text/plain, application/json')127 .should.equal('application/json');128129 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 131–134why, one step back
131 })
132 })133134 describe('when accepting type/subtype', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 138–139why, one step back
138 })
139

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 142–145why, one step back
142 })
143 })144 145 describe('when accepting */subtype', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 149–150why, one step back
149 })
150

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 153–156why, one step back
153 })
154 })155 156 describe('when accepting type/*', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 160–161why, one step back
160 })
161

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 164–167why, one step back
164 })
165 })166 167 describe('when an extension is given', function(){

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 171–172why, one step back
171 })
172

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 175–178why, one step back
175 })
176 177 it('should support *', function(){178 utils.accepts('html', 'text/*')

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

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

refactored utils.accepts()TJ Holowaychukbefore adoption86a9e08

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

refactored app.render()TJ Holowaychukbefore adoptione2f43df

lines 183–185why, one step back
183 })
184 })185})