-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
196 lines (155 loc) · 6.05 KB
/
test.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var assert = require('assert');
var Url = require('./index');
describe('new Urlgray()', function() {
it('initializes object', function() {
var url = new Url('http://localhost');
assert.equal(url, 'http://localhost');
assert.equal(url.url, 'http://localhost');
});
it('handles double wrapping', function() {
var url = new Url(new Url('http://localhost'));
assert.equal(url, 'http://localhost');
assert.equal(url.url, 'http://localhost');
});
});
describe('Urlgray.q', function() {
it('sets query with key/val args', function() {
assert.equal(new Url('http://localhost').q('foo', 'bar'),
'http://localhost?foo=bar');
});
it('appends query with key/val args', function() {
assert.equal(new Url('http://localhost/?foo=bar').q('qux', 'qaz'),
'http://localhost/?foo=bar&qux=qaz');
});
it('sets query with object arg', function() {
assert.equal(new Url('http://localhost').q({foo: 'bar', qux: 'qaz'}),
'http://localhost?foo=bar&qux=qaz');
});
it('appends query with object arg', function() {
assert.equal(new Url('http://localhost/?foo=bar').q({qux: 'qaz'}),
'http://localhost/?foo=bar&qux=qaz');
});
it('chains', function() {
assert.equal(new Url('http://localhost/').q({foo: 'bar'})
.q('qux', 'qaz'),
'http://localhost/?foo=bar&qux=qaz');
});
it('handles empty', function() {
assert.equal(Url(''), '');
assert.equal(Url('').q({foo: 'bar'}), '?foo=bar');
});
});
describe('Urlgray.base', function() {
it('returns base', function() {
var url = new Url('http://localhost/?foo=bar');
assert.equal(url.base, 'http://localhost/');
assert.equal(url, 'http://localhost/?foo=bar');
});
});
describe('Urlgray.query', function() {
it('returns query', function() {
var url = new Url('http://localhost/?foo=bar');
assert.deepEqual(url.query, {foo: 'bar'});
assert.equal(url, 'http://localhost/?foo=bar');
});
});
describe('Urlgray.unQ', function() {
it('removes query', function() {
assert.equal(new Url('http://localhost/?foo=bar').unQ('foo'),
'http://localhost/');
});
it('chains', function() {
assert.equal(new Url('http://localhost/').q({foo: 'bar'})
.unQ('foo'),
'http://localhost/');
});
});
describe('_getBase', function() {
it('returns base', function() {
assert.equal(Url._getBase('http://localhost/?foo=bar'),
'http://localhost/');
});
});
describe('_getQueryString', function() {
it('returns querystring', function() {
assert.equal(Url._getQueryString('http://localhost/?foo=bar'),
'foo=bar');
});
it('returns empty querystring', function() {
assert.equal(Url._getQueryString('http://localhost/'),
'');
});
});
describe('_parseQuery', function() {
it('parses query', function() {
assert.deepEqual(
Url._parseQuery('http://localhost/?foo=bar&qux=qaz&a=b'),
{foo: 'bar', qux: 'qaz', a: 'b'});
});
it('parses empty query', function() {
assert.deepEqual(Url._parseQuery('http://localhost/'),
{});
});
it('parses array value', function() {
assert.deepEqual(Url._parseQuery('http://localhost/?foo=bar&foo=qaz'),
{foo: ['bar', 'qaz']});
});
it('parses three-element array value', function() {
assert.deepEqual(
Url._parseQuery('http://localhost/?foo=bar&foo=qaz&foo=qux'),
{foo: ['bar', 'qaz', 'qux']});
});
it('handles empty', function() {
assert.deepEqual(Url._parseQuery(''), {});
});
});
describe('_setQuery', function() {
it('sets', function() {
assert.equal(Url._setQuery('http://localhost/', 'foo', 'bar'),
'http://localhost/?foo=bar');
});
it('sets with one existing parameter', function() {
assert.equal(Url._setQuery('http://localhost/?foo=bar', 'qux', 'qaz'),
'http://localhost/?foo=bar&qux=qaz');
});
it('overwrites', function() {
assert.equal(Url._setQuery('http://localhost/?qux=qaz', 'qux', 'bar'),
'http://localhost/?qux=bar');
});
it('overwrites with one existing parameter', function() {
assert.equal(Url._setQuery('http://localhost/?foo=bar&qux=qaz', 'qux',
'bar'),
'http://localhost/?foo=bar&qux=bar');
});
it('handles array value', function() {
assert.equal(Url._setQuery('http://localhost/', 'foo',
['bar', 'baz']),
'http://localhost/?foo=bar&foo=baz');
});
it('handles array with one existing parameter', function() {
assert.equal(Url._setQuery('http://localhost/?qux=qaz', 'foo',
['bar', 'baz']),
'http://localhost/?foo=bar&qux=qaz&foo=baz');
});
it('handles array value overwrite', function() {
assert.equal(Url._setQuery('http://localhost/?foo=qaz', 'foo',
['bar', 'baz']),
'http://localhost/?foo=bar&foo=baz');
});
});
describe('_unQuery', function() {
it('unsets', function() {
assert.equal(Url._unQuery('http://localhost/?foo=bar', 'foo'),
'http://localhost/');
});
it('unsets multiple', function() {
assert.equal(Url._unQuery('http://localhost/?foo=bar&qux=qaz&a=b',
['foo', 'a']),
'http://localhost/?qux=qaz');
});
it('unsets array', function() {
assert.equal(Url._unQuery('http://localhost/?foo=bar&foo=baz',
['foo', 'bar']),
'http://localhost/');
});
});