-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils_test.go
199 lines (147 loc) · 3.58 KB
/
utils_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package traffic
import (
"os"
"testing"
assert "github.com/pilu/miniassert"
)
func resetGlobalEnv() {
env = make(map[string]interface{})
}
func TestSetVar(t *testing.T) {
resetGlobalEnv()
SetVar("foo", "bar")
assert.Equal(t, "bar", env["foo"])
resetGlobalEnv()
}
func TestGetVar(t *testing.T) {
resetGlobalEnv()
env["foo-2"] = "bar-2"
assert.Equal(t, "bar-2", GetVar("foo-2"))
assert.Nil(t, GetVar("os_foo"))
os.Setenv("TRAFFIC_OS_FOO", "bar")
assert.Equal(t, "bar", GetVar("os_foo"))
resetGlobalEnv()
}
func TestGetStringVar(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, "", getStringVar("foo"))
SetVar("foo", "bar")
assert.Equal(t, "bar", getStringVar("foo"))
resetGlobalEnv()
}
func TestPathToRegexp(t *testing.T) {
tests := [][]interface{}{
{
`/`,
`\A/\z`,
true,
},
{
`/foo/bar`,
`\A/foo/bar\z`,
true,
},
{
`/foo/bar`,
`\A/foo/bar\z`,
true,
},
{
`/:foo/bar/:baz`,
`\A/(?P<foo>[^/#?]+)/bar/(?P<baz>[^/#?]+)\z`,
false,
},
{
`(/categories/:category_id)?/posts/:id`,
`\A(/categories/(?P<category_id>[^/#?]+))?/posts/(?P<id>[^/#?]+)\z`,
false,
},
}
for _, pair := range tests {
path := pair[0].(string)
expectedRegexp := pair[1].(string)
expectedIsStatic := pair[2].(bool)
r, isStatic := pathToRegexp(path)
assert.Equal(t, expectedRegexp, r.String())
assert.Equal(t, expectedIsStatic, isStatic)
}
}
func TestEnv(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, EnvDevelopment, Env())
SetVar("env", "production")
assert.Equal(t, "production", Env())
resetGlobalEnv()
}
func TestRootPath(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, ".", RootPath())
SetVar("root", "foo")
assert.Equal(t, "foo", RootPath())
resetGlobalEnv()
}
func TestViewsPath(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, DefaultViewsPath, ViewsPath())
SetVar("views", "foo/views")
assert.Equal(t, "foo/views", ViewsPath())
SetVar("root", "/root")
assert.Equal(t, "/root/foo/views", ViewsPath())
SetVar("views", "/absolute/path")
assert.Equal(t, "/absolute/path", ViewsPath())
resetGlobalEnv()
}
func TestConfigFilePath(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, DefaultConfigFile, ConfigFilePath())
SetVar("config_file", "foo.conf")
assert.Equal(t, "foo.conf", ConfigFilePath())
SetVar("root", "/root")
assert.Equal(t, "/root/foo.conf", ConfigFilePath())
SetVar("config_file", "/absolute/foo.conf")
assert.Equal(t, "/absolute/foo.conf", ConfigFilePath())
resetGlobalEnv()
}
func TestPublicPath(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, DefaultPublicPath, PublicPath())
SetVar("public", "custom-public")
assert.Equal(t, "custom-public", PublicPath())
SetVar("root", "/root")
assert.Equal(t, "/root/custom-public", PublicPath())
SetVar("public", "/absolute/public")
assert.Equal(t, "/absolute/public", PublicPath())
resetGlobalEnv()
}
func TestPort(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, DefaultPort, Port())
SetVar("port", 80)
assert.Equal(t, 80, Port())
SetVar("port", "80")
assert.Equal(t, 80, Port())
SetVar("port", "")
assert.Equal(t, DefaultPort, Port())
resetGlobalEnv()
}
func TestSetPort(t *testing.T) {
resetGlobalEnv()
assert.Nil(t, GetVar("port"))
SetPort(80)
assert.Equal(t, 80, GetVar("port"))
resetGlobalEnv()
}
func TestHost(t *testing.T) {
resetGlobalEnv()
assert.Equal(t, DefaultHost, Host())
SetVar("host", "1.2.3.4")
assert.Equal(t, "1.2.3.4", Host())
resetGlobalEnv()
}
func TestSetHost(t *testing.T) {
resetGlobalEnv()
assert.Nil(t, GetVar("host"))
SetHost("1.2.3.4")
assert.Equal(t, "1.2.3.4", GetVar("host"))
resetGlobalEnv()
}