-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathurlMatcherFactorySpec.js
155 lines (129 loc) · 5.52 KB
/
urlMatcherFactorySpec.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
describe("UrlMatcher", function () {
it("matches static URLs", function () {
expect(new UrlMatcher('/hello/world').exec('/hello/world')).toEqual({});
});
it("matches static case insensitive URLs", function () {
expect(new UrlMatcher('/hello/world', true).exec('/heLLo/World')).toEqual({});
});
it("matches against the entire path", function () {
var matcher = new UrlMatcher('/hello/world');
expect(matcher.exec('/hello/world/')).toBeNull();
expect(matcher.exec('/hello/world/suffix')).toBeNull();
});
it("parses parameter placeholders", function () {
var matcher = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
var params = matcher.parameters();
expect(params.length).toBe(5);
expect(params).toContain('id');
expect(params).toContain('type');
expect(params).toContain('repeat');
expect(params).toContain('from');
expect(params).toContain('to');
});
it("handles proper snake case parameter names", function(){
var matcher = new UrlMatcher('/users/?from&to&snake-case&snake-case-triple');
var params = matcher.parameters();
expect(params.length).toBe(4);
expect(params).toContain('from');
expect(params).toContain('to');
expect(params).toContain('snake-case');
expect(params).toContain('snake-case-triple');
});
it("handles invalid snake case parameter names", function(){
expect(function() { new UrlMatcher('/users/?from&to&-snake'); }).toThrow(
"Invalid parameter name '-snake' in pattern '/users/?from&to&-snake'"
);
expect(function() { new UrlMatcher('/users/?from&to&snake-'); }).toThrow(
"Invalid parameter name 'snake-' in pattern '/users/?from&to&snake-'"
);
});
it(".exec() captures parameter values", function () {
expect(
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to')
.exec('/users/123/details//0', {}))
.toEqual({ id:'123', type:'', repeat:'0' });
});
it(".exec() captures catch-all parameters", function () {
var m = new UrlMatcher('/document/*path');
expect(m.exec('/document/a/b/c', {})).toEqual({ path: 'a/b/c' });
expect(m.exec('/document/', {})).toEqual({ path: '' });
});
it(".exec() uses the optional regexp with curly brace placeholders", function () {
expect(
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to')
.exec('/users/123/details/what/thisShouldBeDigits', {}))
.toBeNull();
});
it(".exec() treats the URL as already decoded and does not decode it further", function () {
expect(new UrlMatcher('/users/:id').exec('/users/100%25', {})).toEqual({ id: '100%25'});
});
it('.exec() throws on unbalanced capture list', function () {
var shouldThrow = {
"/url/{matchedParam:([a-z]+)}/child/{childParam}": '/url/someword/child/childParam',
"/url/{matchedParam:([a-z]+)}/child/{childParam}?foo": '/url/someword/child/childParam'
};
angular.forEach(shouldThrow, function(url, route) {
expect(function() { new UrlMatcher(route).exec(url, {}); }).toThrow(
"Unbalanced capture group in route '" + route + "'"
);
});
var shouldPass = {
"/url/{matchedParam:[a-z]+}/child/{childParam}": '/url/someword/child/childParam',
"/url/{matchedParam:[a-z]+}/child/{childParam}?foo": '/url/someword/child/childParam'
};
angular.forEach(shouldPass, function(url, route) {
expect(function() { new UrlMatcher(route).exec(url, {}); }).not.toThrow();
});
});
it(".format() reconstitutes the URL", function () {
expect(
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from')
.format({ id:'123', type:'default', repeat:444, ignored:'value', from:'1970' }))
.toEqual('/users/123/details/default/444?from=1970');
});
it(".format() encodes URL parameters", function () {
expect(new UrlMatcher('/users/:id').format({ id:'100%'})).toEqual('/users/100%25');
});
it(".format() encodes URL parameters with hashes", function () {
expect(
new UrlMatcher('/users/:id#:section')
.format({ id: 'bob', section: 'contact-details' }))
.toEqual('/users/bob#contact-details');
});
it(".concat() concatenates matchers", function () {
var matcher = new UrlMatcher('/users/:id/details/{type}?from').concat('/{repeat:[0-9]+}?to');
var params = matcher.parameters();
expect(params.length).toBe(5);
expect(params).toContain('id');
expect(params).toContain('type');
expect(params).toContain('repeat');
expect(params).toContain('from');
expect(params).toContain('to');
});
it(".concat() returns a new matcher", function () {
var base = new UrlMatcher('/users/:id/details/{type}?from');
var matcher = base.concat('/{repeat:[0-9]+}?to');
expect(matcher).toNotBe(base);
});
});
describe("urlMatcherFactory", function () {
var $umf;
beforeEach(module('ui.router.util'));
beforeEach(inject(function($urlMatcherFactory) {
$umf = $urlMatcherFactory;
}));
it("compiles patterns", function () {
var matcher = $umf.compile('/hello/world');
expect(matcher instanceof UrlMatcher).toBe(true);
});
it("recognizes matchers", function () {
expect($umf.isMatcher(new UrlMatcher('/'))).toBe(true);
});
it("should handle case sensistive URL by default", function () {
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toBeNull();
});
it("should handle case insensistive URL", function () {
$umf.caseInsensitiveMatch(true);
expect($umf.compile('/hello/world').exec('/heLLo/WORLD')).toEqual({});
});
});