forked from Masterminds/sprig
-
Notifications
You must be signed in to change notification settings - Fork 16
/
url_test.go
87 lines (79 loc) · 2.03 KB
/
url_test.go
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
package sprig
import (
"testing"
"github.com/stretchr/testify/assert"
)
var urlTests = map[string]map[string]interface{}{
"proto://auth@host:80/path?query#fragment": {
"fragment": "fragment",
"host": "host:80",
"hostname": "host",
"opaque": "",
"path": "/path",
"query": "query",
"scheme": "proto",
"userinfo": "auth",
},
"proto://host:80/path": {
"fragment": "",
"host": "host:80",
"hostname": "host",
"opaque": "",
"path": "/path",
"query": "",
"scheme": "proto",
"userinfo": "",
},
"something": {
"fragment": "",
"host": "",
"hostname": "",
"opaque": "",
"path": "something",
"query": "",
"scheme": "",
"userinfo": "",
},
"proto://user:passwor%20d@host:80/path": {
"fragment": "",
"host": "host:80",
"hostname": "host",
"opaque": "",
"path": "/path",
"query": "",
"scheme": "proto",
"userinfo": "user:passwor%20d",
},
"proto://host:80/pa%20th?key=val%20ue": {
"fragment": "",
"host": "host:80",
"hostname": "host",
"opaque": "",
"path": "/pa th",
"query": "key=val%20ue",
"scheme": "proto",
"userinfo": "",
},
}
func TestUrlParse(t *testing.T) {
// testing that function is exported and working properly
assert.NoError(t, runt(
`{{ index ( urlParse "proto://auth@host:80/path?query#fragment" ) "host" }}`,
"host:80"))
// testing scenarios
for url, expected := range urlTests {
assert.EqualValues(t, expected, urlParse(url))
}
}
func TestUrlJoin(t *testing.T) {
tests := map[string]string{
`{{ urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "query" "query" "scheme" "proto") }}`: "proto://host:80/path?query#fragment",
`{{ urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "scheme" "proto" "userinfo" "ASDJKJSD") }}`: "proto://ASDJKJSD@host:80/path#fragment",
}
for tpl, expected := range tests {
assert.NoError(t, runt(tpl, expected))
}
for expected, urlMap := range urlTests {
assert.EqualValues(t, expected, urlJoin(urlMap))
}
}