2019-05-05 20:12:36 +02:00
|
|
|
package main_test
|
2014-11-15 06:06:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-05 20:12:36 +02:00
|
|
|
proxy "github.com/pusher/oauth2_proxy"
|
2017-10-23 18:23:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-11-15 06:06:07 +02:00
|
|
|
)
|
|
|
|
|
2019-05-05 20:12:36 +02:00
|
|
|
type EnvTest struct {
|
|
|
|
TestField string `cfg:"target_field" env:"TEST_ENV_FIELD"`
|
|
|
|
EnvTestEmbed
|
|
|
|
}
|
|
|
|
|
|
|
|
type EnvTestEmbed struct {
|
|
|
|
TestFieldEmbed string `cfg:"target_field_embed" env:"TEST_ENV_FIELD_EMBED"`
|
2014-11-15 06:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadEnvForStruct(t *testing.T) {
|
|
|
|
|
2019-05-05 20:12:36 +02:00
|
|
|
cfg := make(proxy.EnvOptions)
|
|
|
|
cfg.LoadEnvForStruct(&EnvTest{})
|
2014-11-15 06:06:07 +02:00
|
|
|
|
|
|
|
_, ok := cfg["target_field"]
|
|
|
|
assert.Equal(t, ok, false)
|
|
|
|
|
|
|
|
os.Setenv("TEST_ENV_FIELD", "1234abcd")
|
2019-05-05 20:12:36 +02:00
|
|
|
cfg.LoadEnvForStruct(&EnvTest{})
|
2014-11-15 06:06:07 +02:00
|
|
|
v := cfg["target_field"]
|
|
|
|
assert.Equal(t, v, "1234abcd")
|
|
|
|
}
|
2019-05-05 20:12:36 +02:00
|
|
|
|
|
|
|
func TestLoadEnvForStructWithEmbeddedFields(t *testing.T) {
|
|
|
|
|
|
|
|
cfg := make(proxy.EnvOptions)
|
|
|
|
cfg.LoadEnvForStruct(&EnvTest{})
|
|
|
|
|
|
|
|
_, ok := cfg["target_field_embed"]
|
|
|
|
assert.Equal(t, ok, false)
|
|
|
|
|
|
|
|
os.Setenv("TEST_ENV_FIELD_EMBED", "1234abcd")
|
|
|
|
cfg.LoadEnvForStruct(&EnvTest{})
|
|
|
|
v := cfg["target_field_embed"]
|
|
|
|
assert.Equal(t, v, "1234abcd")
|
|
|
|
}
|