-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
122 lines (107 loc) · 2.84 KB
/
config_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"os"
"strings"
"testing"
)
func TestNewConfig_Default(t *testing.T) {
os.Unsetenv("T2G_FALLBACK_TOKEN")
os.Unsetenv("T2G_SERVER_PORT")
os.Unsetenv("T2G_TOKEN_HEADER_NAMES")
os.Unsetenv("T2G_ADD_TOKEN_HEADER_NAMES")
os.Unsetenv("T2G_UI_TARGET")
os.Unsetenv("T2G_UI_TITLE")
os.Unsetenv("T2G_UI_DESC1")
os.Unsetenv("T2G_UI_DESC2")
os.Unsetenv("T2G_UI_MISC")
c := NewConfig()
var got string
var want string
eq := func(n string, g string, w string) {
if got != want {
t.Errorf("Unexpected %s: got %q want %q", n, g, w)
}
}
eq("fallbackToken", c.fallbackToken, "")
eq("serverPort", c.serverPort, "8080")
eq(
"tokenHeaderNames",
strings.Join(c.tokenHeaderNames, ","),
"Access-Token,Authorization,Token,X-Auth-Request-Access-Token,X-Forwarded-Access-Token",
)
eq("addTokenHeaderNames", strings.Join(c.addTokenHeaderNames, ","), "")
eq("uiTarget", c.uiTarget, "")
eq("uiTitle", c.uiTitle, "")
eq("uiDesc1", c.uiDesc1, "")
eq("uiDesc2", c.uiDesc2, "")
eq("uiMisc", c.uiMisc, "")
}
func TestNewConfig_Custom(t *testing.T) {
t.Setenv("T2G_FALLBACK_TOKEN", "x")
t.Setenv("T2G_SERVER_PORT", "x")
t.Setenv("T2G_TOKEN_HEADER_NAMES", "x")
t.Setenv("T2G_ADD_TOKEN_HEADER_NAMES", "x")
t.Setenv("T2G_UI_TARGET", "x")
t.Setenv("T2G_UI_TITLE", "x")
t.Setenv("T2G_UI_DESC1", "x")
t.Setenv("T2G_UI_DESC2", "x")
t.Setenv("T2G_UI_MISC", "x")
c := NewConfig()
var got string
var want string
eq := func(n string, g string, w string) {
if got != want {
t.Errorf("Unexpected %s: got %q want %q", n, g, w)
}
}
eq("fallbackToken", c.fallbackToken, "x")
eq("serverPort", c.serverPort, "x")
eq("tokenHeaderNames", strings.Join(c.tokenHeaderNames, ","), "x")
eq("addTokenHeaderNames", strings.Join(c.addTokenHeaderNames, ","), "x")
eq("uiTarget", c.uiTarget, "x")
eq("uiTitle", c.uiTitle, "x")
eq("uiDesc1", c.uiDesc1, "x")
eq("uiDesc2", c.uiDesc2, "x")
eq("uiMisc", c.uiMisc, "x")
}
func TestGetEnv(t *testing.T) {
t.Setenv("T2G_FOO", "bar")
got := GetEnv("FOO", "alice")
want := "bar"
if got != want {
t.Errorf("Unexpected result: got %q want %q", got, want)
}
os.Unsetenv("T2G_DOES_NOT_EXIST")
got = GetEnv("DOES_NOT_EXIST", "alice")
want = "alice"
if got != want {
t.Errorf("Unexpected result: got %q want %q", got, want)
}
}
func TestSplitToSlice(t *testing.T) {
for _, tc := range []struct {
name string
input string
want []string
}{{
name: "1_single",
input: "d",
want: []string{"d"},
}, {
name: "2_commas",
input: "Foo,bar,,,lol , Dummy?, Test,",
want: []string{"Foo", "bar", "lol", "Dummy?", "Test"},
}, {
name: "3_empty",
input: "",
want: []string{""},
}} {
t.Run(tc.name, func(t *testing.T) {
got := strings.Join(SplitToSlice(tc.input), ",")
want := strings.Join(tc.want, ",")
if got != want {
t.Errorf("Wrong result: got %q, want %q", got, want)
}
})
}
}