diff --git a/lib/config_test.go b/lib/config_test.go index 8306e0e..1c688a3 100644 --- a/lib/config_test.go +++ b/lib/config_test.go @@ -133,7 +133,7 @@ username = "basic" password = "basic" scope = "/basic" modify = false -rules = [ ] +rules = [] ` cfg := writeAndParseConfig(t, content, ".toml") @@ -168,3 +168,30 @@ cors: require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts) require.EqualValues(t, []string{"GET"}, cfg.CORS.AllowedMethods) } + +func TestConfigRules(t *testing.T) { + content := ` +auth: false +scope: / +modify: true +rules: + - path: '^.+\.js$' + regex: true + modify: true + - path: /public/access/ + regex: false + modify: true` + + cfg := writeAndParseConfig(t, content, ".yaml") + require.NoError(t, cfg.Validate()) + + require.Len(t, cfg.Rules, 2) + + require.Empty(t, cfg.Rules[0].Path) + require.NotNil(t, cfg.Rules[0].Regexp) + require.True(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.js")) + require.False(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.ts")) + + require.NotEmpty(t, cfg.Rules[1].Path) + require.Nil(t, cfg.Rules[1].Regexp) +} diff --git a/lib/permissions.go b/lib/permissions.go index bd27c60..ddf66d9 100644 --- a/lib/permissions.go +++ b/lib/permissions.go @@ -31,6 +31,7 @@ func (r *Rule) Validate() error { } r.Regexp = rp r.Path = "" + r.Regex = false } return nil @@ -38,7 +39,7 @@ func (r *Rule) Validate() error { // Matches checks if [Rule] matches the given path. func (r *Rule) Matches(path string) bool { - if r.Regex { + if r.Regexp != nil { return r.Regexp.MatchString(path) }