This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl-parser.test.js
60 lines (52 loc) · 2.31 KB
/
url-parser.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
const parse = require('./url-parser');
test('Detect protocol', () => {
expect(parse('http://site.com').protocol).toBe('http');
expect(parse('https://site.com').protocol).toBe('https');
expect(parse('ftp://site.com').protocol).toBe('ftp');
});
test('Detect aut', () => {
expect(parse('http://user:[email protected]').auth).toEqual({ username: 'user', password: 'pass' });
expect(parse('http://j%C3%A3o:man%C3%89%[email protected]').auth).toEqual({ username: 'jão', password: 'manÉ%' });
});
test('Detect port', () => {
expect(parse('http://site.com:8000').port).toBe('8000');
});
test('Detect hostname', () => {
expect(parse('http://site.com').hostname).toBe('site.com');
expect(parse('http://www.site.com').hostname).toBe('www.site.com');
expect(parse('http://foo.bar.site.com').hostname).toBe('foo.bar.site.com');
});
test('Detect path', () => {
expect(parse('http://site.com').path).toBe('/');
expect(parse('http://site.com/').path).toBe('/');
expect(parse('http://site.com/spam').path).toBe('/spam');
expect(parse('http://site.com/spam/egg').path).toBe('/spam/egg');
expect(parse('http://site.com/spam/egg/').path).toBe('/spam/egg');
});
test('Detect hash', () => {
expect(parse('http://site.com').hash).toBe('');
expect(parse('http://site.com#').hash).toBe('');
expect(parse('http://site.com#a-hash').hash).toBe('a-hash');
});
test('Detect query string', () => {
expect(parse('http://site.com').query).toEqual({});
expect(parse('http://site.com?').query).toEqual({});
expect(parse('http://site.com?foo=bar').query).toEqual({ foo: 'bar' });
expect(parse('http://site.com?p%C3%A1=p%C3%A9').query).toEqual({ pá: 'pé' });
expect(parse('http://site.com?foo=bar&baz=qux').query).toEqual({ foo: 'bar', baz: 'qux' });
expect(parse('http://site.com?name=jos%C3%A9').query).toEqual({ name: 'josé' });
expect(parse('http://site.com?foo=one&foo=two&foo=three').query).toEqual({
foo: ['one', 'two', 'three']
});
});
test('Detects everything at the same time', () => {
expect(parse('sftp://poxa:[email protected]:3000/yay/wow/?spam=egg&spam=r%C3%A3&pat%C3%AA=cream#something')).toEqual({
protocol: 'sftp',
auth: { username: 'poxa', password: 'vida' },
hostname: 'a.b.c.site.com',
port: '3000',
path: '/yay/wow',
hash: 'something',
query: { spam: ['egg', 'rã'], patê: 'cream' },
});
})