2021-09-29 15:38:50 +00:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConstants(t *testing.T) {
|
|
|
|
assert.Equal(t, "LISTEN_ADDR", string(ListenAddr))
|
|
|
|
assert.Equal(t, "LISTEN_PORT", string(ListenPort))
|
|
|
|
assert.Equal(t, "TEMPLATE_NAME", string(TemplateName))
|
|
|
|
assert.Equal(t, "CONFIG_FILE", string(ConfigFilePath))
|
2021-10-06 17:38:00 +00:00
|
|
|
assert.Equal(t, "DEFAULT_ERROR_PAGE", string(DefaultErrorPage))
|
2022-01-03 16:51:30 +00:00
|
|
|
assert.Equal(t, "DEFAULT_HTTP_CODE", string(DefaultHTTPCode))
|
2022-01-27 12:29:49 +00:00
|
|
|
assert.Equal(t, "SHOW_DETAILS", string(ShowDetails))
|
2022-02-23 06:09:54 +00:00
|
|
|
assert.Equal(t, "PROXY_HTTP_HEADERS", string(ProxyHTTPHeaders))
|
2022-04-12 10:34:35 +00:00
|
|
|
assert.Equal(t, "DISABLE_L10N", string(DisableL10n))
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvVariable_Lookup(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
giveEnv envVariable
|
|
|
|
}{
|
|
|
|
{giveEnv: ListenAddr},
|
|
|
|
{giveEnv: ListenPort},
|
|
|
|
{giveEnv: TemplateName},
|
|
|
|
{giveEnv: ConfigFilePath},
|
2021-10-06 17:38:00 +00:00
|
|
|
{giveEnv: DefaultErrorPage},
|
2022-01-03 16:51:30 +00:00
|
|
|
{giveEnv: DefaultHTTPCode},
|
2022-01-27 12:29:49 +00:00
|
|
|
{giveEnv: ShowDetails},
|
2022-02-23 06:09:54 +00:00
|
|
|
{giveEnv: ProxyHTTPHeaders},
|
2022-04-12 10:34:35 +00:00
|
|
|
{giveEnv: DisableL10n},
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.giveEnv.String(), func(t *testing.T) {
|
|
|
|
assert.NoError(t, os.Unsetenv(tt.giveEnv.String())) // make sure that env is unset for test
|
|
|
|
|
|
|
|
defer func() { assert.NoError(t, os.Unsetenv(tt.giveEnv.String())) }()
|
|
|
|
|
|
|
|
value, exists := tt.giveEnv.Lookup()
|
|
|
|
assert.False(t, exists)
|
|
|
|
assert.Empty(t, value)
|
|
|
|
|
|
|
|
assert.NoError(t, os.Setenv(tt.giveEnv.String(), "foo"))
|
|
|
|
|
|
|
|
value, exists = tt.giveEnv.Lookup()
|
|
|
|
assert.True(t, exists)
|
|
|
|
assert.Equal(t, "foo", value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|