2017-04-12 19:26:15 +00:00
|
|
|
package config
|
2017-02-03 15:22:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-04-12 19:35:15 +00:00
|
|
|
func TestURLConfig(t *testing.T) {
|
2017-04-12 21:29:11 +00:00
|
|
|
u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{
|
2020-10-12 20:59:05 +00:00
|
|
|
"http.key": []string{"root", "root-2"},
|
|
|
|
"http.https://host.com.key": []string{"host", "host-2"},
|
|
|
|
"http.https://user@host.com/a.key": []string{"user-a", "user-b"},
|
|
|
|
"http.https://user@host.com.key": []string{"user", "user-2"},
|
|
|
|
"http.https://host.com/a.key": []string{"host-a", "host-b"},
|
|
|
|
"http.https://host.com:8080.key": []string{"port", "port-2"},
|
|
|
|
"http.https://host.com/repo.git.key": []string{".git"},
|
|
|
|
"http.https://host.com/repo.key": []string{"no .git"},
|
|
|
|
"http.https://host.com/repo2.key": []string{"no .git"},
|
|
|
|
"http.http://host.com/repo.key": []string{"http"},
|
|
|
|
"http.https://host.com:443/repo3.git.key": []string{"port"},
|
|
|
|
"http.ssh://host.com:22/repo3.git.key": []string{"ssh-port"},
|
|
|
|
"http.https://host.*/a.key": []string{"wild"},
|
2021-08-26 16:35:45 +00:00
|
|
|
"httpXhttps://host.*/aXkey": []string{"invalid"},
|
2017-04-12 19:26:15 +00:00
|
|
|
})))
|
2017-02-03 15:22:02 +00:00
|
|
|
|
2017-04-13 15:14:34 +00:00
|
|
|
getOne := map[string]string{
|
2017-05-01 21:09:41 +00:00
|
|
|
"https://root.com/a/b/c": "root-2",
|
|
|
|
"https://host.com/": "host-2",
|
|
|
|
"https://host.com/a/b/c": "host-b",
|
|
|
|
"https://user:pass@host.com/a/b/c": "user-b",
|
|
|
|
"https://user:pass@host.com/z/b/c": "user-2",
|
|
|
|
"https://host.com:8080/a": "port-2",
|
|
|
|
"https://host.com/repo.git/info/lfs": ".git",
|
|
|
|
"https://host.com/repo.git/info": ".git",
|
|
|
|
"https://host.com/repo.git": ".git",
|
|
|
|
"https://host.com/repo": "no .git",
|
|
|
|
"https://host.com/repo2.git/info/lfs/foo/bar": "no .git",
|
|
|
|
"https://host.com/repo2.git/info/lfs": "no .git",
|
2020-10-12 20:59:05 +00:00
|
|
|
"https://host.com:443/repo2.git/info/lfs": "no .git",
|
2017-05-01 21:09:41 +00:00
|
|
|
"https://host.com/repo2.git/info": "host-2", // doesn't match /.git/info/lfs\Z/
|
|
|
|
"https://host.com/repo2.git": "host-2", // ditto
|
2020-10-12 20:59:05 +00:00
|
|
|
"https://host.com/repo3.git/info/lfs": "port",
|
|
|
|
"ssh://host.com/repo3.git/info/lfs": "ssh-port",
|
2017-05-01 21:09:41 +00:00
|
|
|
"https://host.com/repo2": "no .git",
|
2018-11-27 00:04:21 +00:00
|
|
|
"http://host.com/repo": "http",
|
2020-10-12 20:59:05 +00:00
|
|
|
"http://host.com:80/repo": "http",
|
2018-11-27 01:07:15 +00:00
|
|
|
"https://host.wild/a/b/c": "wild",
|
2017-02-03 15:22:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 15:14:34 +00:00
|
|
|
for rawurl, expected := range getOne {
|
2017-04-15 16:42:19 +00:00
|
|
|
value, _ := u.Get("http", rawurl, "key")
|
2017-05-01 21:09:41 +00:00
|
|
|
assert.Equal(t, expected, value, "get one: "+rawurl)
|
2017-02-03 15:22:02 +00:00
|
|
|
}
|
2017-04-13 15:14:34 +00:00
|
|
|
|
2021-08-26 16:35:45 +00:00
|
|
|
value, _ := u.Get("http", "https://host.wild/a/b/c", "k")
|
|
|
|
assert.Equal(t, value, "")
|
|
|
|
value, _ = u.Get("ttp", "https://host.wild/a/b/c", "key")
|
|
|
|
assert.Equal(t, value, "")
|
|
|
|
|
2017-04-13 15:14:34 +00:00
|
|
|
getAll := map[string][]string{
|
|
|
|
"https://root.com/a/b/c": []string{"root", "root-2"},
|
|
|
|
"https://host.com/": []string{"host", "host-2"},
|
|
|
|
"https://host.com/a/b/c": []string{"host-a", "host-b"},
|
|
|
|
"https://user:pass@host.com/a/b/c": []string{"user-a", "user-b"},
|
|
|
|
"https://user:pass@host.com/z/b/c": []string{"user", "user-2"},
|
|
|
|
"https://host.com:8080/a": []string{"port", "port-2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for rawurl, expected := range getAll {
|
2017-04-15 16:42:19 +00:00
|
|
|
values := u.GetAll("http", rawurl, "key")
|
2017-05-01 21:09:41 +00:00
|
|
|
assert.Equal(t, expected, values, "get all: "+rawurl)
|
2017-04-13 15:14:34 +00:00
|
|
|
}
|
2017-02-03 15:22:02 +00:00
|
|
|
}
|