mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
fix: rules parsing
This commit is contained in:
+28
-1
@@ -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)
|
||||
}
|
||||
|
||||
+2
-1
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user