mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:08:31 +02:00
feat(rules): introduce block DSL, phase-based execution (#203)
* chore(deps): update submodule goutils * docs(http): remove default client from README.md * refactor(rules): introduce block DSL, phase-based execution, and flow validation - add block syntax parser/scanner with nested @blocks and elif/else support - restructure rule execution into explicit pre/post phases with phase flags - classify commands by phase and termination behavior - enforce flow semantics (default rule handling, dead-rule detection) - expand HTTP flow coverage with block + YAML parity tests and benches - refresh rules README/spec and update playground/docs integration - Default rules act as fallback handlers that execute only when no matching non-default rule exists in the pre phase - IfElseBlockCommand now returns early when a condition matches with a nil Do block, instead of falling through to else blocks - Add nil check for auth handler to allow requests when no auth is configured * fix(rules): buffer log output before writing to stdout/stderr * refactor(api/rules): remove IsResponseRule field from ParsedRule and related logic * docs(rules): update examples to use block syntax
This commit is contained in:
73
internal/route/rules/do_blocks_test.go
Normal file
73
internal/route/rules/do_blocks_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
httputils "github.com/yusing/goutils/http"
|
||||
)
|
||||
|
||||
func TestIfElseBlockCommandServeHTTP_UnconditionalNilDoNotFallsThrough(t *testing.T) {
|
||||
elseCalled := false
|
||||
cmd := IfElseBlockCommand{
|
||||
Ifs: []IfBlockCommand{
|
||||
{
|
||||
On: RuleOn{},
|
||||
Do: nil,
|
||||
},
|
||||
},
|
||||
Else: []CommandHandler{
|
||||
Handler{
|
||||
fn: func(_ *httputils.ResponseModifier, _ *http.Request, _ http.HandlerFunc) error {
|
||||
elseCalled = true
|
||||
return nil
|
||||
},
|
||||
phase: PhaseNone,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
rm := httputils.NewResponseModifier(w)
|
||||
|
||||
err := cmd.ServeHTTP(rm, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, elseCalled)
|
||||
}
|
||||
|
||||
func TestIfElseBlockCommandServeHTTP_ConditionalMatchedNilDoNotFallsThrough(t *testing.T) {
|
||||
elseCalled := false
|
||||
cmd := IfElseBlockCommand{
|
||||
Ifs: []IfBlockCommand{
|
||||
{
|
||||
On: RuleOn{
|
||||
checker: CheckFunc(func(_ *httputils.ResponseModifier, _ *http.Request) bool {
|
||||
return true
|
||||
}),
|
||||
},
|
||||
Do: nil,
|
||||
},
|
||||
},
|
||||
Else: []CommandHandler{
|
||||
Handler{
|
||||
fn: func(_ *httputils.ResponseModifier, _ *http.Request, _ http.HandlerFunc) error {
|
||||
elseCalled = true
|
||||
return nil
|
||||
},
|
||||
phase: PhaseNone,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
rm := httputils.NewResponseModifier(w)
|
||||
|
||||
err := cmd.ServeHTTP(rm, req, nil)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, elseCalled)
|
||||
}
|
||||
Reference in New Issue
Block a user