Imported Upstream version 2.3.3
[scm/test.git] / config / url_config_test.go
1 package config
2
3 import (
4         "testing"
5
6         "github.com/stretchr/testify/assert"
7 )
8
9 func TestURLConfig(t *testing.T) {
10         u := NewURLConfig(EnvironmentOf(MapFetcher(map[string][]string{
11                 "http.key":                           []string{"root", "root-2"},
12                 "http.https://host.com.key":          []string{"host", "host-2"},
13                 "http.https://user@host.com/a.key":   []string{"user-a", "user-b"},
14                 "http.https://user@host.com.key":     []string{"user", "user-2"},
15                 "http.https://host.com/a.key":        []string{"host-a", "host-b"},
16                 "http.https://host.com:8080.key":     []string{"port", "port-2"},
17                 "http.https://host.com/repo.git.key": []string{".git"},
18                 "http.https://host.com/repo.key":     []string{"no .git"},
19                 "http.https://host.com/repo2.key":    []string{"no .git"},
20         })))
21
22         getOne := map[string]string{
23                 "https://root.com/a/b/c":                      "root-2",
24                 "https://host.com/":                           "host-2",
25                 "https://host.com/a/b/c":                      "host-b",
26                 "https://user:pass@host.com/a/b/c":            "user-b",
27                 "https://user:pass@host.com/z/b/c":            "user-2",
28                 "https://host.com:8080/a":                     "port-2",
29                 "https://host.com/repo.git/info/lfs":          ".git",
30                 "https://host.com/repo.git/info":              ".git",
31                 "https://host.com/repo.git":                   ".git",
32                 "https://host.com/repo":                       "no .git",
33                 "https://host.com/repo2.git/info/lfs/foo/bar": "no .git",
34                 "https://host.com/repo2.git/info/lfs":         "no .git",
35                 "https://host.com/repo2.git/info":             "host-2", // doesn't match /.git/info/lfs\Z/
36                 "https://host.com/repo2.git":                  "host-2", // ditto
37                 "https://host.com/repo2":                      "no .git",
38         }
39
40         for rawurl, expected := range getOne {
41                 value, _ := u.Get("http", rawurl, "key")
42                 assert.Equal(t, expected, value, "get one: "+rawurl)
43         }
44
45         getAll := map[string][]string{
46                 "https://root.com/a/b/c":           []string{"root", "root-2"},
47                 "https://host.com/":                []string{"host", "host-2"},
48                 "https://host.com/a/b/c":           []string{"host-a", "host-b"},
49                 "https://user:pass@host.com/a/b/c": []string{"user-a", "user-b"},
50                 "https://user:pass@host.com/z/b/c": []string{"user", "user-2"},
51                 "https://host.com:8080/a":          []string{"port", "port-2"},
52         }
53
54         for rawurl, expected := range getAll {
55                 values := u.GetAll("http", rawurl, "key")
56                 assert.Equal(t, expected, values, "get all: "+rawurl)
57         }
58 }