mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-21 19:10:42 +08:00
fix: config parsing keys
This commit is contained in:
+9
-9
@@ -31,7 +31,7 @@ type Config struct {
|
||||
Key string
|
||||
Prefix string
|
||||
NoSniff bool
|
||||
LogFormat string
|
||||
LogFormat string `mapstructure:"log_format"`
|
||||
Auth bool
|
||||
CORS CORS
|
||||
Users []User
|
||||
@@ -74,12 +74,12 @@ func ParseConfig(filename string, flags *pflag.FlagSet) (*Config, error) {
|
||||
v.SetDefault("Port", DefaultPort)
|
||||
v.SetDefault("Auth", DefaultAuth)
|
||||
v.SetDefault("Prefix", DefaultPrefix)
|
||||
v.SetDefault("LogFormat", DefaultLogFormat)
|
||||
v.SetDefault("Log_Format", DefaultLogFormat)
|
||||
|
||||
// Other defaults
|
||||
v.SetDefault("CORS.AllowedHeaders", []string{"*"})
|
||||
v.SetDefault("CORS.AllowedHosts", []string{"*"})
|
||||
v.SetDefault("CORS.AllowedMethods", []string{"*"})
|
||||
v.SetDefault("CORS.Allowed_Headers", []string{"*"})
|
||||
v.SetDefault("CORS.Allowed_Hosts", []string{"*"})
|
||||
v.SetDefault("CORS.Allowed_Methods", []string{"*"})
|
||||
|
||||
// Read and unmarshal configuration
|
||||
err := v.ReadInConfig()
|
||||
@@ -172,8 +172,8 @@ func (c *Config) Validate() error {
|
||||
type CORS struct {
|
||||
Enabled bool
|
||||
Credentials bool
|
||||
AllowedHeaders []string
|
||||
AllowedHosts []string
|
||||
AllowedMethods []string
|
||||
ExposedHeaders []string
|
||||
AllowedHeaders []string `mapstructure:"allowed_headers"`
|
||||
AllowedHosts []string `mapstructure:"allowed_hosts"`
|
||||
AllowedMethods []string `mapstructure:"allowed_methods"`
|
||||
ExposedHeaders []string `mapstructure:"exposed_headers"`
|
||||
}
|
||||
|
||||
@@ -142,3 +142,29 @@ rules = [ ]
|
||||
check(t, cfg)
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg := writeAndParseConfig(t, `
|
||||
cors:
|
||||
enabled: true
|
||||
credentials: true
|
||||
allowed_headers:
|
||||
- Depth
|
||||
allowed_hosts:
|
||||
- http://localhost:8080
|
||||
allowed_methods:
|
||||
- GET
|
||||
exposed_headers:
|
||||
- Content-Length
|
||||
- Content-Range`, ".yml")
|
||||
require.NoError(t, cfg.Validate())
|
||||
|
||||
require.True(t, cfg.CORS.Enabled)
|
||||
require.True(t, cfg.CORS.Credentials)
|
||||
require.EqualValues(t, []string{"Content-Length", "Content-Range"}, cfg.CORS.ExposedHeaders)
|
||||
require.EqualValues(t, []string{"Depth"}, cfg.CORS.AllowedHeaders)
|
||||
require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts)
|
||||
require.EqualValues(t, []string{"GET"}, cfg.CORS.AllowedMethods)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user