Ensure flag values are set correctly

This commit is contained in:
Joel Speed 2019-06-09 10:16:44 +02:00
parent 8e92e3dc3d
commit 765443bc41
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB
2 changed files with 47 additions and 1 deletions

View File

@ -29,6 +29,7 @@ func New() *Options {
// correct configuration options // correct configuration options
func Load(config io.Reader, configType string, args []string) (*Options, error) { func Load(config io.Reader, configType string, args []string) (*Options, error) {
flagSet := flag.NewFlagSet("oauth2-proxy", flag.ExitOnError) flagSet := flag.NewFlagSet("oauth2-proxy", flag.ExitOnError)
flagSet.SetNormalizeFunc(wordSepNormalizeFunc)
// Add FlagSets to main flagSet // Add FlagSets to main flagSet
flagSet.AddFlagSet(cookieFlagSet) flagSet.AddFlagSet(cookieFlagSet)
@ -68,3 +69,14 @@ func Load(config io.Reader, configType string, args []string) (*Options, error)
} }
return options, nil return options, nil
} }
// wordSepNormalizeFunc replaces "-" in flags entered with "."
// This ensures that flags are mapped to the correct values in the Options struct
func wordSepNormalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
from := []string{"-"}
to := "."
for _, sep := range from {
name = strings.Replace(name, sep, to, -1)
}
return flag.NormalizedName(name)
}

View File

@ -110,7 +110,41 @@ var _ = Describe("Load", func() {
} }
Expect(opts.Cookie).To(Equal(expected)) Expect(opts.Cookie).To(Equal(expected))
}) })
Context("with flag configuration", func() {
Context("with hyphens", func() {
BeforeEach(func() {
args = []string{
"--cookie-name=flag_cookie_name",
"--cookie-secret=flag_secret_1234",
"--cookie-domain=flag.example.com",
"--cookie-path=/flag",
"--cookie-expire=48h",
"--cookie-refresh=4h",
"--cookie-secure=false",
"--cookie-httponly=false",
}
}) })
It("returns no error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("the environment overrides the config file", func() {
expected := &CookieOptions{
Name: "flag_cookie_name",
Secret: "flag_secret_1234",
Domain: "flag.example.com",
Path: "/flag",
Expire: time.Duration(48) * time.Hour,
Refresh: time.Duration(4) * time.Hour,
Secure: false,
HTTPOnly: false,
}
Expect(opts.Cookie).To(Equal(expected))
})
})
})
})
}) })
}) })