forked from expressjs/express
/
utils.js
163 lines (136 loc) · 4.49 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var utils = require('../lib/utils')
, assert = require('assert');
describe('utils.isAbsolute()', function(){
it('should support windows', function(){
assert(utils.isAbsolute('c:\\'));
assert(!utils.isAbsolute(':\\'));
})
it('should unices', function(){
assert(utils.isAbsolute('/foo/bar'));
assert(!utils.isAbsolute('foo/bar'));
})
})
describe('utils.flatten(arr)', function(){
it('should flatten an array', function(){
var arr = ['one', ['two', ['three', 'four'], 'five']];
utils.flatten(arr)
.should.eql(['one', 'two', 'three', 'four', 'five']);
})
})
describe('utils.escape(html)', function(){
it('should escape html entities', function(){
utils.escape('<script>foo & "bar"')
.should.equal('<script>foo & "bar"')
})
})
describe('utils.parseQuality(str)', function(){
it('should default quality to 1', function(){
utils.parseQuality('text/html')
.should.eql([{ value: 'text/html', quality: 1 }]);
})
it('should parse qvalues', function(){
utils.parseQuality('text/html; q=0.5')
.should.eql([{ value: 'text/html', quality: 0.5 }]);
utils.parseQuality('text/html; q=.2')
.should.eql([{ value: 'text/html', quality: 0.2 }]);
})
it('should work with messed up whitespace', function(){
utils.parseQuality('text/html ; q = .2')
.should.eql([{ value: 'text/html', quality: 0.2 }]);
})
it('should work with multiples', function(){
var str = 'da, en;q=.5, en-gb;q=.8';
var arr = utils.parseQuality(str);
arr[0].value.should.equal('da');
arr[1].value.should.equal('en-gb');
arr[2].value.should.equal('en');
})
it('should sort by quality', function(){
var str = 'text/plain;q=.2, application/json, text/html;q=0.5';
var arr = utils.parseQuality(str);
arr[0].value.should.equal('application/json');
arr[1].value.should.equal('text/html');
arr[2].value.should.equal('text/plain');
})
it('should exclude those with a quality of 0', function(){
var str = 'text/plain;q=.2, application/json, text/html;q=0';
var arr = utils.parseQuality(str);
arr.should.have.length(2);
})
})
describe('utils.parseAccept(str)', function(){
it('should provide .type', function(){
var arr = utils.parseAccept('text/html');
arr[0].type.should.equal('text');
})
it('should provide .subtype', function(){
var arr = utils.parseAccept('text/html');
arr[0].subtype.should.equal('html');
})
})
describe('utils.accepts(type, str)', function(){
describe('when a string is not given', function(){
it('should return true', function(){
utils.accepts('text/html')
.should.be.true;
})
})
describe('when a string is empty', function(){
it('should return true', function(){
utils.accepts('text/html', '')
.should.be.true;
})
})
describe('when */* is given', function(){
it('should return true', function(){
utils.accepts('text/html', 'text/plain, */*')
.should.be.true;
})
})
describe('when accepting type/subtype', function(){
it('should return true when present', function(){
utils.accepts('text/html', 'text/plain, text/html')
.should.be.true;
})
it('should return false otherwise', function(){
utils.accepts('text/html', 'text/plain, application/json')
.should.be.false;
})
})
describe('when accepting */subtype', function(){
it('should return true when present', function(){
utils.accepts('text/html', 'text/*')
.should.be.true;
})
it('should return false otherwise', function(){
utils.accepts('text/html', 'image/*')
.should.be.false;
})
})
describe('when accepting type/*', function(){
it('should return true when present', function(){
utils.accepts('text/html', '*/html')
.should.be.true;
})
it('should return false otherwise', function(){
utils.accepts('text/html', '*/json')
.should.be.false;
})
})
describe('when an extension is given', function(){
it('should return true when present', function(){
utils.accepts('html', 'text/html, application/json')
.should.be.true;
})
it('should return false otherwise', function(){
utils.accepts('html', 'text/plain, application/json')
.should.be.false;
})
it('should support *', function(){
utils.accepts('html', 'text/*')
.should.be.true;
utils.accepts('html', '*/html')
.should.be.true;
})
})
})