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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 | 1×
1×
1×
1×
1×
121×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import assert from 'assert'
import Validators, { url } from '../index'
import getErrorId from './helper'
const ERROR_ID = 'form.errors.url'
function test (value, params) {
return getErrorId(url(params)(value))
}
describe('Validator: url', function () {
it('should be invalid when `value` is not a valid url', function () {
assert.strictEqual(ERROR_ID, test(''))
assert.strictEqual(ERROR_ID, test('http://'))
assert.strictEqual(ERROR_ID, test('http://.'))
assert.strictEqual(ERROR_ID, test('http://..'))
assert.strictEqual(ERROR_ID, test('http://../'))
assert.strictEqual(ERROR_ID, test('http://?'))
assert.strictEqual(ERROR_ID, test('http://??'))
assert.strictEqual(ERROR_ID, test('http://??/'))
assert.strictEqual(ERROR_ID, test('http://#'))
assert.strictEqual(ERROR_ID, test('http://##'))
assert.strictEqual(ERROR_ID, test('http://##/'))
assert.strictEqual(ERROR_ID, test('http://foo.bar?q=Spaces should be encoded'))
assert.strictEqual(ERROR_ID, test('//'))
assert.strictEqual(ERROR_ID, test('//a'))
assert.strictEqual(ERROR_ID, test('///a'))
assert.strictEqual(ERROR_ID, test('///'))
assert.strictEqual(ERROR_ID, test('http:///a'))
assert.strictEqual(ERROR_ID, test('foo.com'))
assert.strictEqual(ERROR_ID, test('rdar://1234'))
assert.strictEqual(ERROR_ID, test('h://test'))
assert.strictEqual(ERROR_ID, test('http:// shouldfail.com'))
assert.strictEqual(ERROR_ID, test(':// should fail'))
assert.strictEqual(ERROR_ID, test('http://foo.bar/foo(bar)baz quux'))
assert.strictEqual(ERROR_ID, test('ftps://foo.bar/'))
assert.strictEqual(ERROR_ID, test('http://-error-.invalid/'))
assert.strictEqual(ERROR_ID, test('http://-a.b.co'))
assert.strictEqual(ERROR_ID, test('http://a.b-.co'))
assert.strictEqual(ERROR_ID, test('http://1.1.1.1.1'))
assert.strictEqual(ERROR_ID, test('http://123.123.123'))
assert.strictEqual(ERROR_ID, test('http://3628126748'))
assert.strictEqual(ERROR_ID, test('http://.www.foo.bar/'))
assert.strictEqual(ERROR_ID, test('http://.www.foo.bar./'))
})
it('should be valid when `value` is a valid url', function () {
assert.ok(!test('http://foo.com/blah_blah'))
assert.ok(!test('http://foo.com/blah_blah/'))
assert.ok(!test('http://foo.com/blah_blah_(wikipedia)'))
assert.ok(!test('http://foo.com/blah_blah_(wikipedia)_(again)'))
assert.ok(!test('http://www.example.com/wpstyle/?p=364'))
assert.ok(!test('https://www.example.com/foo/?bar=baz&inga=42&quux'))
assert.ok(!test('http://✪df.ws/123'))
assert.ok(!test('http://userid:password@example.com:8080'))
assert.ok(!test('http://userid:password@example.com:8080/'))
assert.ok(!test('http://userid@example.com'))
assert.ok(!test('http://userid@example.com/'))
assert.ok(!test('http://userid@example.com:8080'))
assert.ok(!test('http://userid@example.com:8080/'))
assert.ok(!test('http://userid:password@example.com'))
assert.ok(!test('http://userid:password@example.com/'))
assert.ok(!test('http://142.42.1.1/'))
assert.ok(!test('http://142.42.1.1:8080/'))
assert.ok(
!test(
'https://userid::password@[2001:db8:85a3::8a2e:370:7334]:483/path/example?foobar=8&abc=2&bcd=5#product=browser+what&stuff'
)
)
assert.ok(!test('http://➡.ws/䨹'))
assert.ok(!test('http://⌘.ws'))
assert.ok(!test('http://⌘.ws/'))
assert.ok(!test('http://foo.com/blah_(wikipedia)#cite-1'))
assert.ok(!test('http://foo.com/blah_(wikipedia)_blah#cite-1'))
assert.ok(!test('http://foo.com/unicode_(✪)_in_parens'))
assert.ok(!test('http://foo.com/(something)?after=parens'))
assert.ok(!test('http://☺.damowmow.com/'))
assert.ok(!test('http://code.google.com/events/#&product=browser'))
assert.ok(!test('http://j.mp'))
assert.ok(!test('http://foo.bar/?q=Test%20URL-encoded%20stuff'))
assert.ok(!test('http://مثال.إختبار'))
assert.ok(!test('http://例子.测试'))
assert.ok(!test('http://उदाहरण.परीक्षा'))
assert.ok(!test("http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com"))
assert.ok(!test('http://1337.net'))
assert.ok(!test('http://a.b-c.de'))
assert.ok(!test('http://223.255.255.254'))
assert.ok(!test('https://foo_bar.example.com/'))
})
it("should be invalid if the protocols don't match", function () {
assert.strictEqual(ERROR_ID, test('http://google.com', { protocol: 'ftp' }))
assert.strictEqual(ERROR_ID, test('http://google.com', { protocol: ['ftp', 'https'] }))
assert.strictEqual(ERROR_ID, test('http://google.com', { protocols: 'https' }))
assert.strictEqual(ERROR_ID, test('http://google.com', { protocols: ['ftp', 'https'] }))
assert.strictEqual(ERROR_ID, test('https://www.google.com', { protocol: 'ftp' }))
assert.strictEqual(ERROR_ID, test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', { protocol: 'http' }))
})
it('should only be valid with certain protocols', function () {
assert.ok(!test('ftp://foo.bar/baz', { protocol: 'ftp' }))
assert.ok(!test('http://google.com', { protocol: 'http' }))
assert.ok(!test('http://google.com', { protocol: ['http', 'https'] }))
assert.ok(!test('http://google.com', { protocol: ['ftp', 'http', 'https'] }))
assert.ok(!test('http://google.com', { protocols: 'http' }))
assert.ok(!test('http://google.com', { protocols: ['ftp', 'http', 'https'] }))
assert.ok(!test('https://www.google.com', { protocol: 'https' }))
assert.ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', { protocol: 'ftp' }))
})
it('should be valid with no protocol identifier', function () {
assert.ok(!test('google.com', { protocolIdentifier: false }))
assert.ok(!test('userid:password@google.com/path?search=foo#stuff', { protocolIdentifier: false }))
})
it('should be invalid if basic auth not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://userid:password@google.com', { basicAuth: false }))
assert.strictEqual(ERROR_ID, test('http://userid@google.com', { basicAuth: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@217.64.0.1:3838/path?search=foo#stuff', { basicAuth: false }))
})
it('should be invalid if ipv4 not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://217.64.0.1', { ipv4: false }))
assert.strictEqual(ERROR_ID, test('http://217.64.0.1:3838', { ipv4: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@217.64.0.1:3838/path?search=foo#stuff', { ipv4: false }))
})
it('should be invalid if ipv6 not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://[::1]', { ipv6: false }))
assert.strictEqual(ERROR_ID, test('http://[2001:db8:85a3::8a2e:370:7334]:3838', { ipv6: false }))
assert.strictEqual(
ERROR_ID,
test('http://userid:password@[2001:db8:85a3::8a2e:370:7334]:3838/path?search=foo#stuff', { ipv6: false })
)
})
it('should be invalid if host not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://google.com', { host: false }))
assert.strictEqual(ERROR_ID, test('http://www.google.com', { host: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@google.com/path?search=foo#stuff', { host: false }))
})
it('should be invalid if local not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://localhost', { local: false }))
assert.strictEqual(ERROR_ID, test('http://localhost:3000', { local: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@localhost:4000/path?search=foo#stuff', { local: false }))
})
it('should be invalid if port not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://localhost:2000', { port: false }))
assert.strictEqual(ERROR_ID, test('http://217.64.0.1:3000', { port: false }))
assert.strictEqual(ERROR_ID, test('https://userid:password@google.com:483/path?search=foo#stuff', { port: false }))
})
it('should be invalid if path not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://localhost/path', { path: false }))
assert.strictEqual(ERROR_ID, test('http://217.64.0.1:3000/path', { path: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', { path: false }))
})
it('should be invalid if search not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://localhost?search', { search: false }))
assert.strictEqual(ERROR_ID, test('http://217.64.0.1:3000/?search', { search: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', { search: false }))
})
it('should be invalid if hash not allowed', function () {
assert.strictEqual(ERROR_ID, test('http://localhost#hash', { hash: false }))
assert.strictEqual(ERROR_ID, test('http://217.64.0.1:3000/#hash99', { hash: false }))
assert.strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', { hash: false }))
})
it('should use default urlProtocols option', function () {
let defaultValue = Validators.defaultOptions.urlProtocols
Validators.defaultOptions.urlProtocols = ['ftp']
assert.strictEqual(ERROR_ID, test('http://google.com'))
assert.strictEqual(ERROR_ID, test('https://www.google.com'))
assert.strictEqual(ERROR_ID, test('http://foo:bar@128.193.1.32:3000/foo?foo=bar'))
assert.ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar'))
Validators.defaultOptions.urlProtocols = ['http', 'https']
assert.ok(!test('http://google.com'))
assert.ok(!test('https://www.google.com'))
assert.ok(!test('http://foo:bar@128.193.1.32:3000/foo?foo=bar'))
assert.strictEqual(ERROR_ID, test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar'))
Validators.defaultOptions.urlProtocols = defaultValue
})
it('should use formatMessage', function () {
let defaultValue = Validators.formatMessage
Validators.formatMessage = function (msg) {
return Object.assign({}, msg, { id: msg.id + '2' })
}
assert.strictEqual(ERROR_ID + '2', test('http:/:/'))
Validators.formatMessage = defaultValue
})
it('should parse a URL', function () {
let h = url.parseURL('http://userid:password@google.com:8080/path?search=foo#stuff')
assert.strictEqual(h.protocol, 'http')
assert.strictEqual(h.basicAuth.username, 'userid')
assert.strictEqual(h.basicAuth.password, 'password')
assert.strictEqual(h.host, 'google.com')
assert.strictEqual(h.port, 8080)
assert.strictEqual(h.path, '/path')
assert.strictEqual(h.search, '?search=foo')
assert.strictEqual(h.hash, '#stuff')
h = url.parseURL('http://userid:password@localhost:8080/path?search=foo#stuff')
assert.strictEqual(h.host, 'localhost')
h = url.parseURL('http://userid:password@128.193.1.32:8080/path?search=foo#stuff')
assert.strictEqual(h.ipv4, '128.193.1.32')
h = url.parseURL('http://userid:password@[2001:db8:85a3::8a2e:370:7334]:8080/path?search=foo#stuff')
assert.strictEqual(h.ipv6, '2001:db8:85a3::8a2e:370:7334')
})
it('should not parse a URL', function () {
assert.strictEqual(url.parseURL('http://userid:password@google.com', { basicAuth: false }), null)
assert.strictEqual(url.parseURL('http://google.com', { host: false }), null)
assert.strictEqual(url.parseURL('http://128.193.1.32', { ipv4: false }), null)
assert.strictEqual(url.parseURL('http://[2001:db8:85a3::8a2e:370:7334]', { ipv6: false }), null)
assert.strictEqual(url.parseURL('http://google.com/path', { path: false }), null)
assert.strictEqual(url.parseURL('http://google.com?search', { search: false }), null)
assert.strictEqual(url.parseURL('http://google.com#hash', { hash: false }), null)
})
})
|