| 1 |
package sprig |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
var urlTests = map[string]map[string]interface{}{ |
| 10 |
"proto://auth@host:80/path?query#fragment": { |
| 11 |
"fragment": "fragment", |
| 12 |
"host": "host:80", |
| 13 |
"hostname": "host", |
| 14 |
"opaque": "", |
| 15 |
"path": "/path", |
| 16 |
"query": "query", |
| 17 |
"scheme": "proto", |
| 18 |
"userinfo": "auth", |
| 19 |
}, |
| 20 |
"proto://host:80/path": { |
| 21 |
"fragment": "", |
| 22 |
"host": "host:80", |
| 23 |
"hostname": "host", |
| 24 |
"opaque": "", |
| 25 |
"path": "/path", |
| 26 |
"query": "", |
| 27 |
"scheme": "proto", |
| 28 |
"userinfo": "", |
| 29 |
}, |
| 30 |
"something": { |
| 31 |
"fragment": "", |
| 32 |
"host": "", |
| 33 |
"hostname": "", |
| 34 |
"opaque": "", |
| 35 |
"path": "something", |
| 36 |
"query": "", |
| 37 |
"scheme": "", |
| 38 |
"userinfo": "", |
| 39 |
}, |
| 40 |
"proto://user:passwor%20d@host:80/path": { |
| 41 |
"fragment": "", |
| 42 |
"host": "host:80", |
| 43 |
"hostname": "host", |
| 44 |
"opaque": "", |
| 45 |
"path": "/path", |
| 46 |
"query": "", |
| 47 |
"scheme": "proto", |
| 48 |
"userinfo": "user:passwor%20d", |
| 49 |
}, |
| 50 |
"proto://host:80/pa%20th?key=val%20ue": { |
| 51 |
"fragment": "", |
| 52 |
"host": "host:80", |
| 53 |
"hostname": "host", |
| 54 |
"opaque": "", |
| 55 |
"path": "/pa th", |
| 56 |
"query": "key=val%20ue", |
| 57 |
"scheme": "proto", |
| 58 |
"userinfo": "", |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
func TestUrlParse(t *testing.T) { |
| 63 |
// testing that function is exported and working properly |
| 64 |
assert.NoError(t, runt( |
| 65 |
`{{ index ( urlParse "proto://auth@host:80/path?query#fragment" ) "host" }}`, |
| 66 |
"host:80")) |
| 67 |
|
| 68 |
// testing scenarios |
| 69 |
for url, expected := range urlTests { |
| 70 |
assert.EqualValues(t, expected, urlParse(url)) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestUrlJoin(t *testing.T) { |
| 75 |
tests := map[string]string{ |
| 76 |
`{{ urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "query" "query" "scheme" "proto") }}`: "proto://host:80/path?query#fragment", |
| 77 |
`{{ urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "scheme" "proto" "userinfo" "ASDJKJSD") }}`: "proto://ASDJKJSD@host:80/path#fragment", |
| 78 |
} |
| 79 |
for tpl, expected := range tests { |
| 80 |
assert.NoError(t, runt(tpl, expected)) |
| 81 |
} |
| 82 |
|
| 83 |
for expected, urlMap := range urlTests { |
| 84 |
assert.EqualValues(t, expected, urlJoin(urlMap)) |
| 85 |
} |
| 86 |
|
| 87 |
} |