mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:08:31 +02: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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if req.Form == nil {
|
||||||
|
if err := req.ParseForm(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
return getValueByKeyAtIndex(req.Form, key, index)
|
return getValueByKeyAtIndex(req.Form, key, index)
|
||||||
},
|
},
|
||||||
VarPostForm: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
VarPostForm: func(args []string, w *ResponseModifier, req *http.Request) (string, error) {
|
||||||
@@ -50,6 +55,11 @@ var dynamicVarSubsMap = map[string]dynamicVarGetter{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if req.Form == nil {
|
||||||
|
if err := req.ParseForm(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
return getValueByKeyAtIndex(req.PostForm, key, index)
|
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) {
|
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
|
// NOTE: do not use Header.Get or http.CanonicalHeaderKey here, respect to user input
|
||||||
if values, ok := values[key]; ok && index < len(values) {
|
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
|
// ignore unknown header or index out of range
|
||||||
return "", nil
|
return "", nil
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/yusing/godoxy/internal/route/routes"
|
"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 },
|
VarRequestAddr: func(req *http.Request) string { return req.Host },
|
||||||
VarRequestPath: func(req *http.Request) string { return req.URL.Path },
|
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() },
|
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") },
|
VarRequestContentType: func(req *http.Request) string { return req.Header.Get("Content-Type") },
|
||||||
VarRequestContentLen: func(req *http.Request) string { return strconv.FormatInt(req.ContentLength, 10) },
|
VarRequestContentLen: func(req *http.Request) string { return strconv.FormatInt(req.ContentLength, 10) },
|
||||||
VarRemoteHost: func(req *http.Request) string {
|
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()) },
|
VarRespContentLen: func(resp *ResponseModifier) string { return strconv.Itoa(resp.ContentLength()) },
|
||||||
VarRespStatusCode: func(resp *ResponseModifier) string { return strconv.Itoa(resp.StatusCode()) },
|
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) {
|
func TestExpandVars(t *testing.T) {
|
||||||
// Create a comprehensive test request
|
// Create a comprehensive test request with form data
|
||||||
testRequest := httptest.NewRequest("POST", "https://example.com:8080/api/users?param1=value1¶m2=value2#fragment", nil)
|
formData := url.Values{}
|
||||||
testRequest.Header.Set("Content-Type", "application/json")
|
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("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.ContentLength = 12345
|
||||||
testRequest.RemoteAddr = "192.168.1.100:54321"
|
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
|
// Create response modifier with headers
|
||||||
testResponseModifier := NewResponseModifier(httptest.NewRecorder())
|
testResponseModifier := NewResponseModifier(httptest.NewRecorder())
|
||||||
@@ -235,7 +251,7 @@ func TestExpandVars(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "req_uri",
|
name: "req_uri",
|
||||||
input: "$req_uri",
|
input: "$req_uri",
|
||||||
want: "/api/users?param1=value1¶m2=value2",
|
want: "/api/users?param1=value1¶m2=value2#fragment",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "req_host",
|
name: "req_host",
|
||||||
@@ -255,7 +271,7 @@ func TestExpandVars(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "req_content_type",
|
name: "req_content_type",
|
||||||
input: "$req_content_type",
|
input: "$req_content_type",
|
||||||
want: "application/json",
|
want: "application/x-www-form-urlencoded",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "req_content_length",
|
name: "req_content_length",
|
||||||
@@ -351,6 +367,68 @@ func TestExpandVars(t *testing.T) {
|
|||||||
input: "$arg(param3)",
|
input: "$arg(param3)",
|
||||||
want: "",
|
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
|
// Mixed variables
|
||||||
{
|
{
|
||||||
name: "mixed variables",
|
name: "mixed variables",
|
||||||
|
|||||||
Reference in New Issue
Block a user