fix: rules parsing

This commit is contained in:
Henrique Dias
2024-07-22 22:25:50 +02:00
parent 732cf5eff5
commit 947b163ea7
2 changed files with 30 additions and 2 deletions
+27
View File
@@ -168,3 +168,30 @@ cors:
require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts) require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts)
require.EqualValues(t, []string{"GET"}, cfg.CORS.AllowedMethods) 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
View File
@@ -31,6 +31,7 @@ func (r *Rule) Validate() error {
} }
r.Regexp = rp r.Regexp = rp
r.Path = "" r.Path = ""
r.Regex = false
} }
return nil return nil
@@ -38,7 +39,7 @@ func (r *Rule) Validate() error {
// Matches checks if [Rule] matches the given path. // Matches checks if [Rule] matches the given path.
func (r *Rule) Matches(path string) bool { func (r *Rule) Matches(path string) bool {
if r.Regex { if r.Regexp != nil {
return r.Regexp.MatchString(path) return r.Regexp.MatchString(path)
} }