-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathurlPolyfill.spec.ts
More file actions
155 lines (124 loc) · 5.84 KB
/
Copy pathurlPolyfill.spec.ts
File metadata and controls
155 lines (124 loc) · 5.84 KB
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
import { buildUrl, getPathName, isValidUrl, normalizeUrl, getPristineWindow } from '@datadog/js-core/util'
describe('normalize url', () => {
it('should resolve absolute paths', () => {
expect(normalizeUrl('/my/path')).toEqual(`${location.origin}/my/path`)
})
it('should resolve relative paths', () => {
history.pushState({}, '', '/foo/')
expect(normalizeUrl('./my/path')).toEqual(`${location.origin}/foo/my/path`)
})
it('should add protocol to relative url', () => {
expect(normalizeUrl('//foo.com:9876/my/path')).toEqual('http://foo.com:9876/my/path')
})
it('should keep full url unchanged', () => {
expect(normalizeUrl('https://foo.com/my/path')).toEqual('https://foo.com/my/path')
})
it('should keep non http url unchanged', () => {
expect(normalizeUrl('ftp://foo.com/my/path')).toEqual('ftp://foo.com/my/path')
})
it('should keep file url unchanged', () => {
// On firefox, URL host is empty for file URI: 'https://bugzilla.mozilla.org/show_bug.cgi?id=1578787'
// In some cases, Mobile Safari also have this issue.
// As we should follow the browser behavior, having one or the other doesn't matter too much, so
// let's check for both.
expect(['file:///my/path', 'file://foo.com/my/path']).toContain(normalizeUrl('file://foo.com/my/path'))
})
describe('with a <base href> differing from the current path', () => {
// The browser resolves document-relative request URLs against the document base URI, not the
// page location. normalizeUrl must match that so the recorded URL pairs with its
// PerformanceResourceTiming entry.
let base: HTMLBaseElement
beforeEach(() => {
history.pushState({}, '', '/deep/route')
base = document.createElement('base')
base.href = '/'
document.head.appendChild(base)
})
afterEach(() => {
base.remove()
})
it('should resolve document-relative paths against the base URI', () => {
expect(normalizeUrl('api/foo')).toEqual(`${location.origin}/api/foo`)
})
it('should still resolve root-relative paths against the origin', () => {
expect(normalizeUrl('/api/foo')).toEqual(`${location.origin}/api/foo`)
})
it('should keep absolute urls unchanged', () => {
expect(normalizeUrl('https://foo.com/my/path')).toEqual('https://foo.com/my/path')
})
})
})
describe('isValidUrl', () => {
it('should ensure url is valid', () => {
expect(isValidUrl('http://www.datadoghq.com')).toBe(true)
expect(isValidUrl('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe(true)
expect(isValidUrl('file:///www.datadoghq.com')).toBe(true)
expect(isValidUrl('/plop')).toBe(false)
expect(isValidUrl('')).toBe(false)
})
it('should return the same result if the URL has been wrongfully overridden between calls', () => {
expect(isValidUrl('http://www.datadoghq.com')).toBe(true)
spyOn(window, 'URL').and.throwError('wrong URL override')
expect(isValidUrl('http://www.datadoghq.com')).toBe(true)
})
})
describe('getPathName', () => {
it('should retrieve url path name', () => {
expect(getPathName('http://www.datadoghq.com')).toBe('/')
expect(getPathName('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe('/foo/bar')
expect(getPathName('file://foo.com/bar?a=b#hello')).toBe('/bar')
})
})
describe('buildUrl', () => {
it('should normalize href for absolute URLs', () => {
expect(buildUrl('http://foo.com').href).toBe('http://foo.com/')
expect(buildUrl('http://foo.com:8080').href).toBe('http://foo.com:8080/')
expect(buildUrl('http://foo.com:80').href).toBe('http://foo.com/')
expect(buildUrl('https://foo.com:443').href).toBe('https://foo.com/')
expect(['file:///my/path', 'file://foo.com/my/path']).toContain(buildUrl('file://foo.com/my/path').href)
})
it('should normalize href for relative URLs', () => {
expect(buildUrl('./bar', 'http://foo.com').href).toBe('http://foo.com/bar')
expect(buildUrl('/bar', 'http://foo.com').href).toBe('http://foo.com/bar')
expect(buildUrl('./bar', 'http://foo.com/foo').href).toBe('http://foo.com/bar')
expect(buildUrl('/bar', 'http://foo.com/foo').href).toBe('http://foo.com/bar')
expect(buildUrl('./bar', 'http://foo.com/foo/').href).toBe('http://foo.com/foo/bar')
expect(buildUrl('/bar', 'http://foo.com/foo/').href).toBe('http://foo.com/bar')
expect(['file:///bar', 'file://foo.com/bar']).toContain(buildUrl('./bar', 'file://foo.com/faa').href)
expect(['file:///bar', 'file://foo.com/bar']).toContain(buildUrl('/bar', 'file://foo.com/faa').href)
})
})
describe('getNativeURLFromIframe', () => {
it('should get native URL constructor from iframe', () => {
const { URL } = getPristineWindow()
expect(URL).toBeDefined()
expect(typeof URL).toBe('function')
const url = new URL('http://example.com')
expect(url.href).toBe('http://example.com/')
})
it('should work even if main window URL is overridden', () => {
const originalURL = window.URL
;(window as any).URL = function badURL() {
throw new Error('Bad polyfill')
}
const { URL } = getPristineWindow()
expect(URL).toBeDefined()
expect(typeof URL).toBe('function')
const url = new URL('http://example.com')
expect(url.href).toBe('http://example.com/')
;(window as any).URL = originalURL
})
it('should keep the same constructor and still resolve relative URLs correctly', () => {
const { URL: nativeURL1 } = getPristineWindow()
expect(nativeURL1).toBeDefined()
history.pushState({}, '', '/foo/')
const url1 = buildUrl('./bar', location.href)
expect(url1.href).toBe(`${location.origin}/foo/bar`)
history.pushState({}, '', '/baz/')
const { URL: nativeURL2 } = getPristineWindow()
expect(nativeURL2).toBeDefined()
expect(nativeURL2).toBe(nativeURL1)
const url2 = buildUrl('./qux', location.href)
expect(url2.href).toBe(`${location.origin}/baz/qux`)
})
})