-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
90 lines (81 loc) · 4.58 KB
/
env_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
// Copyright 2022 anoweb Author. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package anoweb
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type testEnvCase struct {
env string
val interface{}
beforeCall func()
expectCall func() interface{}
}
func TestEnv(t *testing.T) {
a := New()
cases := make([]*testEnvCase, 0)
{
// test for setServerMaxHeaderSize
cases = append(cases, &testEnvCase{envServerMaxHeaderSize, 100, func() { a.setServerMaxHeaderSize() }, func() interface{} { return a.Config.Server.MaxHeaderSize }})
// test for setServerReadTimeout
cases = append(cases, &testEnvCase{envServerReadTimeout, "10s", func() { a.setServerReadTimeout() }, func() interface{} { return a.Config.Server.ReadTimeout }})
// test for setServerReadHeaderTimeout
cases = append(cases, &testEnvCase{envServerReadHeaderTimeout, "10s", func() { a.setServerReadHeaderTimeout() }, func() interface{} { return a.Config.Server.ReadHeaderTimeout }})
// test for setServerWriteTimeout
cases = append(cases, &testEnvCase{envServerWriteTimeout, "10s", func() { a.setServerWriteTimeout() }, func() interface{} { return a.Config.Server.WriteTimeout }})
// test for setServerIdleTimeout
cases = append(cases, &testEnvCase{envServerIdleTimeout, "10s", func() { a.setServerIdleTimeout() }, func() interface{} { return a.Config.Server.IdleTimeout }})
// test for setConfigFile
cases = append(cases, &testEnvCase{envConfigFile, "app.yml", func() { a.setConfigFile() }, func() interface{} { return a.ConfigFile }})
// test for setServerHost
cases = append(cases, &testEnvCase{envServerHost, "0.0.0.0", func() { a.setServerHost() }, func() interface{} { return a.Config.Server.Host }})
// test for setServerPort
cases = append(cases, &testEnvCase{envServerPort, 1080, func() { a.setServerPort() }, func() interface{} { return a.Config.Server.Port }})
// test for setBannerEnable
cases = append(cases, &testEnvCase{envBannerEnable, true, func() { a.setBannerEnable() }, func() interface{} { return a.Config.Banner.Enable }})
// test for setBannerType
cases = append(cases, &testEnvCase{envBannerType, "default", func() { a.setBannerType() }, func() interface{} { return a.Config.Banner.Type }})
// test for setBannerText
cases = append(cases, &testEnvCase{envBannerText, "hello world -- GO GO GO", func() { a.setBannerText() }, func() interface{} { return a.Config.Banner.Text }})
// test for setBannerFile
cases = append(cases, &testEnvCase{envBannerFile, "banner.txt", func() { a.setBannerFile() }, func() interface{} { return a.Config.Banner.File }})
// test for setServerTLSEnable
cases = append(cases, &testEnvCase{envServerTLSEnable, true, func() { a.setServerTLSEnable() }, func() interface{} { return a.Config.Server.TLS.Enable }})
// test for envServerTLSCertFile
cases = append(cases, &testEnvCase{envServerTLSCertFile, "cert.pem", func() { a.setServerTLSCertFile() }, func() interface{} { return a.Config.Server.TLS.CertFile }})
// test for setServerTLSKeyFile
cases = append(cases, &testEnvCase{envServerTLSKeyFile, "key.pem", func() { a.setServerTLSKeyFile() }, func() interface{} { return a.Config.Server.TLS.KeyFile }})
// test for setTemplateCache
cases = append(cases, &testEnvCase{envTemplateCache, true, func() { a.setTemplateCache() }, func() interface{} { return a.Config.Template.Cache }})
// test for setTemplateRoot
cases = append(cases, &testEnvCase{envTemplateRoot, "/to/path", func() { a.setTemplateRoot() }, func() interface{} { return a.Config.Template.Root }})
// test for setTemplateSuffix
cases = append(cases, &testEnvCase{envTemplateSuffix, ".tpl", func() { a.setTemplateSuffix() }, func() interface{} { return a.Config.Template.Suffix }})
}
for _, c := range cases {
// set env
_ = os.Setenv(c.env, fmt.Sprintf("%v", c.val))
// before call
c.beforeCall()
expectVal := c.expectCall()
// start require
switch expectVal.(type) {
default:
require.Equal(t, c.val, expectVal)
case time.Duration:
duration, _ := time.ParseDuration(fmt.Sprintf("%v", c.val))
require.Equal(t, duration, expectVal)
}
}
}