forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlUtilsSpec.js
148 lines (110 loc) · 5.14 KB
/
urlUtilsSpec.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
'use strict';
describe('urlUtils', function() {
describe('urlResolve', function() {
it('should returned already parsed URLs unchanged', function() {
var urlObj = urlResolve('/foo?bar=baz#qux');
expect(urlResolve(urlObj)).toBe(urlObj);
expect(urlResolve(true)).toBe(true);
expect(urlResolve(null)).toBeNull();
expect(urlResolve(undefined)).toBeUndefined();
});
it('should normalize a relative url', function() {
expect(urlResolve('foo').href).toMatch(/^https?:\/\/[^/]+\/foo$/);
});
it('should parse relative URL into component pieces', function() {
var parsed = urlResolve('foo');
expect(parsed.href).toMatch(/https?:\/\//);
expect(parsed.protocol).toMatch(/^https?/);
expect(parsed.host).not.toBe('');
expect(parsed.hostname).not.toBe('');
expect(parsed.pathname).not.toBe('');
});
it('should return pathname as / if empty path provided', function() {
// IE (all versions) counts / as empty, necessary to use / so that pathname is not context.html
var parsed = urlResolve('/');
expect(parsed.pathname).toBe('/');
});
});
describe('urlIsSameOrigin and urlIsSameOriginAsBaseUrl', function() {
it('should support various combinations of urls - both string and parsed',
inject(function($document) {
function expectIsSameOrigin(url, expectedValue) {
expect(urlIsSameOrigin(url)).toBe(expectedValue);
expect(urlIsSameOrigin(urlResolve(url))).toBe(expectedValue);
// urlIsSameOriginAsBaseUrl() should behave the same as urlIsSameOrigin() by default.
// Behavior when there is a non-default base URL or when the base URL changes dynamically
// is tested in the end-to-end tests in e2e/tests/base-tag.spec.js.
expect(urlIsSameOriginAsBaseUrl(url)).toBe(expectedValue);
expect(urlIsSameOriginAsBaseUrl(urlResolve(url))).toBe(expectedValue);
}
expectIsSameOrigin('path', true);
var origin = urlResolve($document[0].location.href);
expectIsSameOrigin('//' + origin.host + '/path', true);
// Different domain.
expectIsSameOrigin('http://example.com/path', false);
// Auto fill protocol.
expectIsSameOrigin('//example.com/path', false);
// Should not match when the ports are different.
// This assumes that the test is *not* running on port 22 (very unlikely).
expectIsSameOrigin('//' + origin.hostname + ':22/path', false);
})
);
});
describe('urlIsAllowedOriginFactory', function() {
var origin = urlResolve(window.location.href);
var urlIsAllowedOrigin;
beforeEach(function() {
urlIsAllowedOrigin = urlIsAllowedOriginFactory([
'https://foo.com/',
origin.protocol + '://bar.com:1337/'
]);
});
it('should implicitly allow the current origin', function() {
expect(urlIsAllowedOrigin('path')).toBe(true);
});
it('should check against the list of whitelisted origins', function() {
expect(urlIsAllowedOrigin('https://foo.com/path')).toBe(true);
expect(urlIsAllowedOrigin(origin.protocol + '://bar.com:1337/path')).toBe(true);
expect(urlIsAllowedOrigin('https://baz.com:1337/path')).toBe(false);
expect(urlIsAllowedOrigin('https://qux.com/path')).toBe(false);
});
it('should support both strings and parsed URL objects', function() {
expect(urlIsAllowedOrigin('path')).toBe(true);
expect(urlIsAllowedOrigin(urlResolve('path'))).toBe(true);
expect(urlIsAllowedOrigin('https://foo.com/path')).toBe(true);
expect(urlIsAllowedOrigin(urlResolve('https://foo.com/path'))).toBe(true);
});
it('should return true only if the origins (protocol, hostname, post) match', function() {
var differentProtocol = (origin.protocol !== 'http') ? 'http' : 'https';
var differentPort = (parseInt(origin.port, 10) || 0) + 1;
var url;
// Relative path
url = 'path';
expect(urlIsAllowedOrigin(url)).toBe(true);
// Same origin
url = origin.protocol + '://' + origin.host + '/path';
expect(urlIsAllowedOrigin(url)).toBe(true);
// Same origin - implicit protocol
url = '//' + origin.host + '/path';
expect(urlIsAllowedOrigin(url)).toBe(true);
// Same origin - different protocol
url = differentProtocol + '://' + origin.host + '/path';
expect(urlIsAllowedOrigin(url)).toBe(false);
// Same origin - different port
url = origin.protocol + '://' + origin.hostname + ':' + differentPort + '/path';
expect(urlIsAllowedOrigin(url)).toBe(false);
// Allowed origin
url = origin.protocol + '://bar.com:1337/path';
expect(urlIsAllowedOrigin(url)).toBe(true);
// Allowed origin - implicit protocol
url = '//bar.com:1337/path';
expect(urlIsAllowedOrigin(url)).toBe(true);
// Allowed origin - different protocol
url = differentProtocol + '://bar.com:1337/path';
expect(urlIsAllowedOrigin(url)).toBe(false);
// Allowed origin - different port
url = origin.protocol + '://bar.com:1338/path';
expect(urlIsAllowedOrigin(url)).toBe(false);
});
});
});