-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathbuild-url.test.js
76 lines (62 loc) · 2.37 KB
/
build-url.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const { test } = require('tap')
const { buildURL } = require('../lib/utils')
test('should produce valid URL', (t) => {
t.plan(1)
const url = buildURL('/hi', 'http://localhost')
t.equal(url.href, 'http://localhost/hi')
})
test('should produce valid URL', (t) => {
t.plan(1)
const url = buildURL('http://localhost/hi', 'http://localhost')
t.equal(url.href, 'http://localhost/hi')
})
test('should return same source when base is not specified', (t) => {
t.plan(1)
const url = buildURL('http://localhost/hi')
t.equal(url.href, 'http://localhost/hi')
})
test('should handle lack of trailing slash in base', (t) => {
t.plan(3)
let url = buildURL('hi', 'http://localhost/hi')
t.equal(url.href, 'http://localhost/hi')
url = buildURL('hi/', 'http://localhost/hi')
t.equal(url.href, 'http://localhost/hi/')
url = buildURL('hi/more', 'http://localhost/hi')
t.equal(url.href, 'http://localhost/hi/more')
})
test('should handle default port in base', (t) => {
t.plan(2)
let url = buildURL('/hi', 'http://localhost:80/hi')
t.equal(url.href, 'http://localhost/hi')
url = buildURL('/hi', 'https://localhost:443/hi')
t.equal(url.href, 'https://localhost/hi')
})
test('should append instead of override base', (t) => {
t.plan(2)
let url = buildURL('//10.0.0.10/hi', 'http://localhost')
t.equal(url.href, 'http://localhost//10.0.0.10/hi')
url = buildURL('//httpbin.org/hi', 'http://localhost')
t.equal(url.href, 'http://localhost//httpbin.org/hi')
})
const errorInputs = [
{ source: 'http://10.0.0.10/hi', base: 'http://localhost' },
{ source: 'https://10.0.0.10/hi', base: 'http://localhost' },
{ source: 'blah://10.0.0.10/hi', base: 'http://localhost' },
{ source: 'urn:foo:bar', base: 'http://localhost' },
{ source: 'http://localhost/private', base: 'http://localhost/exposed/' },
{ source: 'http://localhost/exposed-extra', base: 'http://localhost/exposed' },
{ source: '/private', base: 'http://localhost/exposed/' },
{ source: '/exposed-extra', base: 'http://localhost/exposed' },
{ source: '../private', base: 'http://localhost/exposed/' },
{ source: 'exposed-extra', base: 'http://localhost/exposed' }
]
test('should throw when trying to override base', (t) => {
t.plan(errorInputs.length)
errorInputs.forEach(({ source, base }) => {
t.test(source, (t) => {
t.plan(1)
t.throws(() => buildURL(source, base))
})
})
})