Skip to content

Commit

Permalink
fix: rules parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jul 22, 2024
1 parent 732cf5e commit 947b163
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ username = "basic"
password = "basic"
scope = "/basic"
modify = false
rules = [ ]
rules = []
`

cfg := writeAndParseConfig(t, content, ".toml")
Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion lib/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ func (r *Rule) Validate() error {
}
r.Regexp = rp
r.Path = ""
r.Regex = false
}

return nil
}

// 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)
}

Expand Down

0 comments on commit 947b163

Please sign in to comment.