mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 00:38:33 +02:00
feat(rules): add post-request rules system with response manipulation (#160)
* Add comprehensive post-request rules support for response phase * Enable response body, status, and header manipulation via set commands * Refactor command handlers to support both request and response phases * Implement response modifier system for post-request template execution * Support response-based rule matching with status and header checks * Add comprehensive benchmarks for matcher performance * Refactor authentication and proxying commands for unified error handling * Support negated conditions with ! * Enhance error handling, error formatting and validation * Routes: add `rule_file` field with rule preset support * Environment variable substitution: now supports variables without `GODOXY_` prefix * new conditions: * `on resp_header <key> [<value>]` * `on status <status>` * new commands: * `require_auth` * `set resp_header <key> <template>` * `set resp_body <template>` * `set status <code>` * `log <level> <path> <template>` * `notify <level> <provider> <title_template> <body_template>`
This commit is contained in:
@@ -49,9 +49,9 @@ func TestParser(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "regex_escaped",
|
||||
input: `foo regex(\b\B\s\S\w\W\d\D\$\.)`,
|
||||
input: `foo regex(\b\B\s\S\w\W\d\D\$\.\(\)\{\}\|\?\"\')`,
|
||||
subject: "foo",
|
||||
args: []string{`regex(\b\B\s\S\w\W\d\D\$\.)`},
|
||||
args: []string{`regex(\b\B\s\S\w\W\d\D\$\.\(\)\{\}\|\?"')`},
|
||||
},
|
||||
{
|
||||
name: "quote inside argument",
|
||||
@@ -71,6 +71,12 @@ func TestParser(t *testing.T) {
|
||||
subject: "foo",
|
||||
args: []string{"glob(\"`/**/to/path`\")"},
|
||||
},
|
||||
{
|
||||
name: "complex_regex",
|
||||
input: `path !regex("^(_next/static|_next/image|favicon.ico).*$")`,
|
||||
subject: "path",
|
||||
args: []string{`!regex("^(_next/static|_next/image|favicon.ico).*$")`},
|
||||
},
|
||||
{
|
||||
name: "chaos",
|
||||
input: `error 403 "Forbidden "foo" "bar""`,
|
||||
@@ -170,6 +176,53 @@ func TestParser(t *testing.T) {
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("negated", func(t *testing.T) {
|
||||
test := `!error 403 "Forbidden"`
|
||||
subject, args, err := parse(test)
|
||||
expect.NoError(t, err)
|
||||
expect.Equal(t, subject, "!error")
|
||||
expect.Equal(t, args, []string{"403", "Forbidden"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestFullParse(t *testing.T) {
|
||||
input := `
|
||||
- name: login page
|
||||
on: path /login
|
||||
do: pass
|
||||
- name: require auth
|
||||
on: path !regex("^(_next/static|_next/image|favicon.ico).*$")
|
||||
do: require_auth
|
||||
- name: redirect to login
|
||||
on: status 401 | status 403
|
||||
do: proxy /login
|
||||
- name: proxy to backend
|
||||
on: path glob("/api/v1/*")
|
||||
do: proxy http://localhost:8999/
|
||||
- name: proxy to backend (old /auth)
|
||||
on: path glob("/auth/*")
|
||||
do: proxy http://localhost:8999/api/v1/`
|
||||
|
||||
var rules Rules
|
||||
err := parseRules(input, &rules)
|
||||
expect.NoError(t, err)
|
||||
expect.Equal(t, len(rules), 5)
|
||||
expect.Equal(t, rules[0].Name, "login page")
|
||||
expect.Equal(t, rules[0].On.String(), "path /login")
|
||||
expect.Equal(t, rules[0].Do.String(), "pass")
|
||||
expect.Equal(t, rules[1].Name, "require auth")
|
||||
expect.Equal(t, rules[1].On.String(), `path !regex("^(_next/static|_next/image|favicon.ico).*$")`)
|
||||
expect.Equal(t, rules[1].Do.String(), "require_auth")
|
||||
expect.Equal(t, rules[2].Name, "redirect to login")
|
||||
expect.Equal(t, rules[2].On.String(), "status 401 | status 403")
|
||||
expect.Equal(t, rules[2].Do.String(), "proxy /login")
|
||||
expect.Equal(t, rules[3].Name, "proxy to backend")
|
||||
expect.Equal(t, rules[3].On.String(), `path glob("/api/v1/*")`)
|
||||
expect.Equal(t, rules[3].Do.String(), "proxy http://localhost:8999/")
|
||||
expect.Equal(t, rules[4].Name, "proxy to backend (old /auth)")
|
||||
expect.Equal(t, rules[4].On.String(), `path glob("/auth/*")`)
|
||||
expect.Equal(t, rules[4].Do.String(), "proxy http://localhost:8999/api/v1/")
|
||||
}
|
||||
|
||||
func BenchmarkParser(b *testing.B) {
|
||||
|
||||
Reference in New Issue
Block a user