mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 17:41:05 +01:00
- 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
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
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_UnconditionalNilDoFallsThrough(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.True(t, elseCalled)
|
|
}
|
|
|
|
func TestIfElseBlockCommandServeHTTP_ConditionalMatchedNilDoFallsThrough(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.True(t, elseCalled)
|
|
}
|