-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathreadme.coffee
121 lines (107 loc) · 3.48 KB
/
readme.coffee
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
test = require 'tape'
UrlPattern = require '../lib/url-pattern'
test 'simple', (t) ->
pattern = new UrlPattern('/api/users/:id')
t.deepEqual pattern.match('/api/users/10'), {id: '10'}
t.equal pattern.match('/api/products/5'), null
t.end()
test 'api versioning', (t) ->
pattern = new UrlPattern('/v:major(.:minor)/*')
t.deepEqual pattern.match('/v1.2/'), {major: '1', minor: '2', _: ''}
t.deepEqual pattern.match('/v2/users'), {major: '2', _: 'users'}
t.equal pattern.match('/v/'), null
t.end()
test 'domain', (t) ->
pattern = new UrlPattern('(http(s)\\://)(:subdomain.):domain.:tld(\\::port)(/*)')
t.deepEqual pattern.match('google.de'),
domain: 'google'
tld: 'de'
t.deepEqual pattern.match('https://www.google.com'),
subdomain: 'www'
domain: 'google'
tld: 'com'
t.deepEqual pattern.match('http://mail.google.com/mail'),
subdomain: 'mail'
domain: 'google'
tld: 'com'
_: 'mail'
t.deepEqual pattern.match('http://mail.google.com:80/mail'),
subdomain: 'mail'
domain: 'google'
tld: 'com'
port: '80'
_: 'mail'
t.equal pattern.match('google'), null
t.deepEqual pattern.match('www.google.com'),
subdomain: 'www'
domain: 'google'
tld: 'com'
t.equal pattern.match('httpp://mail.google.com/mail'), null
t.deepEqual pattern.match('google.de/search'),
domain: 'google'
tld: 'de'
_: 'search'
t.end()
test 'named segment occurs more than once', (t) ->
pattern = new UrlPattern('/api/users/:ids/posts/:ids')
t.deepEqual pattern.match('/api/users/10/posts/5'), {ids: ['10', '5']}
t.end()
test 'regex', (t) ->
pattern = new UrlPattern(/^\/api\/(.*)$/)
t.deepEqual pattern.match('/api/users'), ['users']
t.equal pattern.match('/apiii/users'), null
t.end()
test 'regex group names', (t) ->
pattern = new UrlPattern(/^\/api\/([^\/]+)(?:\/(\d+))?$/, ['resource', 'id'])
t.deepEqual pattern.match('/api/users'),
resource: 'users'
t.equal pattern.match('/api/users/'), null
t.deepEqual pattern.match('/api/users/5'),
resource: 'users'
id: '5'
t.equal pattern.match('/api/users/foo'), null
t.end()
test 'stringify', (t) ->
pattern = new UrlPattern('/api/users/:id')
t.equal '/api/users/10', pattern.stringify(id: 10)
pattern = new UrlPattern('/api/users(/:id)')
t.equal '/api/users', pattern.stringify()
t.equal '/api/users/10', pattern.stringify(id: 10)
t.end()
test 'customization', (t) ->
options =
escapeChar: '!'
segmentNameStartChar: '$'
segmentNameCharset: 'a-zA-Z0-9_-'
segmentValueCharset: 'a-zA-Z0-9'
optionalSegmentStartChar: '['
optionalSegmentEndChar: ']'
wildcardChar: '?'
pattern = new UrlPattern(
'[http[s]!://][$sub_domain.]$domain.$toplevel-domain[/?]'
options
)
t.deepEqual pattern.match('google.de'),
domain: 'google'
'toplevel-domain': 'de'
t.deepEqual pattern.match('http://mail.google.com/mail'),
sub_domain: 'mail'
domain: 'google'
'toplevel-domain': 'com'
_: 'mail'
t.equal pattern.match('http://mail.this-should-not-match.com/mail'), null
t.equal pattern.match('google'), null
t.deepEqual pattern.match('www.google.com'),
sub_domain: 'www'
domain: 'google'
'toplevel-domain': 'com'
t.deepEqual pattern.match('https://www.google.com'),
sub_domain: 'www'
domain: 'google'
'toplevel-domain': 'com'
t.equal pattern.match('httpp://mail.google.com/mail'), null
t.deepEqual pattern.match('google.de/search'),
domain: 'google'
'toplevel-domain': 'de'
_: 'search'
t.end()