mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-17 23:03:49 +01:00
fix(rules): ensure postform and form initialized, fix tests
This commit is contained in:
@@ -43,6 +43,11 @@ var dynamicVarSubsMap = map[string]dynamicVarGetter{
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if req.Form == nil {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return getValueByKeyAtIndex(req.Form, key, index)
|
||||
},
|
||||
VarPostForm: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||
@@ -50,6 +55,11 @@ var dynamicVarSubsMap = map[string]dynamicVarGetter{
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if req.Form == nil {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return getValueByKeyAtIndex(req.PostForm, key, index)
|
||||
},
|
||||
}
|
||||
@@ -57,7 +67,7 @@ var dynamicVarSubsMap = map[string]dynamicVarGetter{
|
||||
func getValueByKeyAtIndex[Values http.Header | url.Values](values Values, key string, index int) (string, error) {
|
||||
// NOTE: do not use Header.Get or http.CanonicalHeaderKey here, respect to user input
|
||||
if values, ok := values[key]; ok && index < len(values) {
|
||||
return values[index], nil
|
||||
return stripFragment(values[index]), nil
|
||||
}
|
||||
// ignore unknown header or index out of range
|
||||
return "", nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
)
|
||||
@@ -57,9 +58,9 @@ var staticReqVarSubsMap = map[string]reqVarGetter{
|
||||
},
|
||||
VarRequestAddr: func(req *http.Request) string { return req.Host },
|
||||
VarRequestPath: func(req *http.Request) string { return req.URL.Path },
|
||||
VarRequestQuery: func(req *http.Request) string { return req.URL.RawQuery },
|
||||
VarRequestQuery: func(req *http.Request) string { return stripFragment(req.URL.RawQuery) },
|
||||
VarRequestURL: func(req *http.Request) string { return req.URL.String() },
|
||||
VarRequestURI: func(req *http.Request) string { return req.URL.RequestURI() },
|
||||
VarRequestURI: func(req *http.Request) string { return stripFragment(req.URL.RequestURI()) },
|
||||
VarRequestContentType: func(req *http.Request) string { return req.Header.Get("Content-Type") },
|
||||
VarRequestContentLen: func(req *http.Request) string { return strconv.FormatInt(req.ContentLength, 10) },
|
||||
VarRemoteHost: func(req *http.Request) string {
|
||||
@@ -90,3 +91,11 @@ var staticRespVarSubsMap = map[string]respVarGetter{
|
||||
VarRespContentLen: func(resp *ResponseModifier) string { return strconv.Itoa(resp.ContentLength()) },
|
||||
VarRespStatusCode: func(resp *ResponseModifier) string { return strconv.Itoa(resp.StatusCode()) },
|
||||
}
|
||||
|
||||
func stripFragment(s string) string {
|
||||
idx := strings.IndexByte(s, '#')
|
||||
if idx == -1 {
|
||||
return s
|
||||
}
|
||||
return s[:idx]
|
||||
}
|
||||
|
||||
@@ -189,13 +189,29 @@ func TestExtractArgs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExpandVars(t *testing.T) {
|
||||
// Create a comprehensive test request
|
||||
testRequest := httptest.NewRequest("POST", "https://example.com:8080/api/users?param1=value1¶m2=value2#fragment", nil)
|
||||
testRequest.Header.Set("Content-Type", "application/json")
|
||||
// Create a comprehensive test request with form data
|
||||
formData := url.Values{}
|
||||
formData.Set("field1", "value1")
|
||||
formData.Set("field2", "value2")
|
||||
formData.Add("multi", "first")
|
||||
formData.Add("multi", "second")
|
||||
|
||||
postFormData := url.Values{}
|
||||
postFormData.Set("postfield1", "postvalue1")
|
||||
postFormData.Set("postfield2", "postvalue2")
|
||||
postFormData.Add("postmulti", "first")
|
||||
postFormData.Add("postmulti", "second")
|
||||
|
||||
testRequest := httptest.NewRequest("POST", "https://example.com:8080/api/users?param1=value1¶m2=value2#fragment", strings.NewReader(postFormData.Encode()))
|
||||
testRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
testRequest.Header.Set("User-Agent", "test-agent/1.0")
|
||||
testRequest.Header.Set("X-Custom", "value1,value2")
|
||||
testRequest.Header.Add("X-Custom", "value1")
|
||||
testRequest.Header.Add("X-Custom", "value2")
|
||||
testRequest.ContentLength = 12345
|
||||
testRequest.RemoteAddr = "192.168.1.100:54321"
|
||||
testRequest.Form = formData
|
||||
// ParseForm to populate PostForm from the request body
|
||||
testRequest.PostForm = postFormData
|
||||
|
||||
// Create response modifier with headers
|
||||
testResponseModifier := NewResponseModifier(httptest.NewRecorder())
|
||||
@@ -235,7 +251,7 @@ func TestExpandVars(t *testing.T) {
|
||||
{
|
||||
name: "req_uri",
|
||||
input: "$req_uri",
|
||||
want: "/api/users?param1=value1¶m2=value2",
|
||||
want: "/api/users?param1=value1¶m2=value2#fragment",
|
||||
},
|
||||
{
|
||||
name: "req_host",
|
||||
@@ -255,7 +271,7 @@ func TestExpandVars(t *testing.T) {
|
||||
{
|
||||
name: "req_content_type",
|
||||
input: "$req_content_type",
|
||||
want: "application/json",
|
||||
want: "application/x-www-form-urlencoded",
|
||||
},
|
||||
{
|
||||
name: "req_content_length",
|
||||
@@ -351,6 +367,68 @@ func TestExpandVars(t *testing.T) {
|
||||
input: "$arg(param3)",
|
||||
want: "",
|
||||
},
|
||||
// Function-like variables - form
|
||||
{
|
||||
name: "form single parameter",
|
||||
input: "$form(field1)",
|
||||
want: "value1",
|
||||
},
|
||||
{
|
||||
name: "form second parameter",
|
||||
input: "$form(field2)",
|
||||
want: "value2",
|
||||
},
|
||||
{
|
||||
name: "form multi-value first",
|
||||
input: "$form(multi, 0)",
|
||||
want: "first",
|
||||
},
|
||||
{
|
||||
name: "form multi-value second",
|
||||
input: "$form(multi, 1)",
|
||||
want: "second",
|
||||
},
|
||||
{
|
||||
name: "form not found",
|
||||
input: "$form(nonexistent)",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "form index out of range",
|
||||
input: "$form(field1, 10)",
|
||||
want: "",
|
||||
},
|
||||
// Function-like variables - postform
|
||||
{
|
||||
name: "postform single parameter",
|
||||
input: "$postform(postfield1)",
|
||||
want: "postvalue1",
|
||||
},
|
||||
{
|
||||
name: "postform second parameter",
|
||||
input: "$postform(postfield2)",
|
||||
want: "postvalue2",
|
||||
},
|
||||
{
|
||||
name: "postform multi-value first",
|
||||
input: "$postform(postmulti, 0)",
|
||||
want: "first",
|
||||
},
|
||||
{
|
||||
name: "postform multi-value second",
|
||||
input: "$postform(postmulti, 1)",
|
||||
want: "second",
|
||||
},
|
||||
{
|
||||
name: "postform not found",
|
||||
input: "$postform(nonexistent)",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "postform index out of range",
|
||||
input: "$postform(postfield1, 10)",
|
||||
want: "",
|
||||
},
|
||||
// Mixed variables
|
||||
{
|
||||
name: "mixed variables",
|
||||
|
||||
Reference in New Issue
Block a user