feat(rewriter): add redirect(), get_cookie(), add_cookie() methods to rule engine (#36)
Some checks are pending
Cadoles/bouncer/pipeline/pr-develop Build started...
Some checks are pending
Cadoles/bouncer/pipeline/pr-develop Build started...
This commit is contained in:
@ -2,6 +2,7 @@ package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
@ -185,6 +186,134 @@ func TestDelRequestHeaders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddRequestCookie(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Cookie map[string]any
|
||||
Check func(t *testing.T, tc TestCase, req *http.Request)
|
||||
ShouldFail bool
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{
|
||||
Cookie: map[string]any{
|
||||
"name": "test",
|
||||
},
|
||||
Check: func(t *testing.T, tc TestCase, req *http.Request) {
|
||||
cookie, err := req.Cookie(tc.Cookie["name"].(string))
|
||||
if err != nil {
|
||||
t.Errorf("%+v", errors.WithStack(err))
|
||||
return
|
||||
}
|
||||
|
||||
if e, g := tc.Cookie["name"], cookie.Name; e != g {
|
||||
t.Errorf("cookie.Name: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
Cookie: map[string]any{
|
||||
"name": "foo",
|
||||
"value": "test",
|
||||
},
|
||||
Check: func(t *testing.T, tc TestCase, req *http.Request) {
|
||||
cookie, err := req.Cookie(tc.Cookie["name"].(string))
|
||||
if err != nil {
|
||||
t.Errorf("%+v", errors.WithStack(err))
|
||||
return
|
||||
}
|
||||
|
||||
if e, g := tc.Cookie["name"], cookie.Name; e != g {
|
||||
t.Errorf("cookie.Name: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
|
||||
if e, g := tc.Cookie["value"], cookie.Value; e != g {
|
||||
t.Errorf("cookie.Value: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for idx, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("Case_%d", idx), func(t *testing.T) {
|
||||
type Vars struct {
|
||||
NewCookie map[string]any `expr:"new_cookie"`
|
||||
}
|
||||
|
||||
engine := createRuleEngine[Vars](t,
|
||||
rule.WithExpr(addRequestCookieFunc()),
|
||||
rule.WithRules(
|
||||
`add_cookie(ctx, vars.new_cookie)`,
|
||||
),
|
||||
)
|
||||
|
||||
req, err := http.NewRequest("GET", "http://example.net", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
vars := Vars{
|
||||
NewCookie: tc.Cookie,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = WithRequest(ctx, req)
|
||||
|
||||
if _, err := engine.Apply(ctx, vars); err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
if tc.ShouldFail {
|
||||
t.Error("engine.Apply() should have failed")
|
||||
}
|
||||
|
||||
if tc.Check != nil {
|
||||
tc.Check(t, tc, req)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestCookie(t *testing.T) {
|
||||
type Vars struct {
|
||||
CookieName string `expr:"cookieName"`
|
||||
}
|
||||
|
||||
engine := createRuleEngine[Vars](t,
|
||||
rule.WithExpr(getRequestCookieFunc()),
|
||||
rule.WithRules(
|
||||
"let cookie = get_cookie(ctx, vars.cookieName); cookie.value",
|
||||
),
|
||||
)
|
||||
|
||||
req, err := http.NewRequest("GET", "http://example.net", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
vars := Vars{
|
||||
CookieName: "foo",
|
||||
}
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: vars.CookieName,
|
||||
Value: "bar",
|
||||
}
|
||||
|
||||
req.AddCookie(cookie)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = WithRequest(ctx, req)
|
||||
|
||||
results, err := engine.Apply(ctx, vars)
|
||||
if err != nil {
|
||||
t.Fatalf("%+v", errors.WithStack(err))
|
||||
}
|
||||
|
||||
if e, g := cookie.Value, results[0]; e != g {
|
||||
t.Errorf("result[0]: expected '%v', got '%v'", e, g)
|
||||
}
|
||||
}
|
||||
|
||||
func createRuleEngine[V any](t *testing.T, funcs ...rule.OptionFunc) *rule.Engine[V] {
|
||||
engine, err := rule.NewEngine[V](funcs...)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user