mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 18:57:04 +02:00
fix(test): correct test expectations and logic
Httptest and similar callers often leave Host unset; fall back to URL for scheme, host, port, and addr substitution. jsonstore drops the IsTest load short-circuit and duplicate loadNS map registration; tests isolate storesPath. Skip MaxMind background updates when IsTest. Tests restore APISkipOriginCheck, use app-scoped OIDC state cookies, attach route context in middleware helpers, and use locked buffers for concurrent log capture.
This commit is contained in:
@@ -42,7 +42,8 @@ func TestWithConsumedRouteOverlaysReturnsNewRequestWhenOverlayIsPresent(t *testi
|
||||
}
|
||||
|
||||
type fakeMiddlewareHTTPRoute struct {
|
||||
name string
|
||||
name string
|
||||
targetURL *nettypes.URL
|
||||
}
|
||||
|
||||
func (r fakeMiddlewareHTTPRoute) Key() string { return r.name }
|
||||
@@ -54,7 +55,7 @@ func (r fakeMiddlewareHTTPRoute) MarshalZerologObject(*zerolog.Event) {}
|
||||
func (r fakeMiddlewareHTTPRoute) ProviderName() string { return "" }
|
||||
func (r fakeMiddlewareHTTPRoute) GetProvider() types.RouteProvider { return nil }
|
||||
func (r fakeMiddlewareHTTPRoute) ListenURL() *nettypes.URL { return nil }
|
||||
func (r fakeMiddlewareHTTPRoute) TargetURL() *nettypes.URL { return nil }
|
||||
func (r fakeMiddlewareHTTPRoute) TargetURL() *nettypes.URL { return r.targetURL }
|
||||
func (r fakeMiddlewareHTTPRoute) HealthMonitor() types.HealthMonitor { return nil }
|
||||
func (r fakeMiddlewareHTTPRoute) SetHealthMonitor(types.HealthMonitor) {}
|
||||
func (r fakeMiddlewareHTTPRoute) References() []string { return nil }
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
"github.com/yusing/goutils/http/reverseproxy"
|
||||
)
|
||||
|
||||
@@ -161,6 +162,10 @@ func newMiddlewaresTest(middlewares []*Middleware, args *testArgs) (*TestResult,
|
||||
|
||||
req := httptest.NewRequest(args.reqMethod, args.reqURL.String(), args.bodyReader())
|
||||
maps.Copy(req.Header, args.headers)
|
||||
req = routes.WithRouteContext(req, fakeMiddlewareHTTPRoute{
|
||||
name: "test-upstream",
|
||||
targetURL: args.upstreamURL,
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
||||
@@ -56,9 +56,15 @@ var staticReqVarSubsMap = map[string]reqVarGetter{
|
||||
if req.TLS != nil {
|
||||
return "https"
|
||||
}
|
||||
if req.URL != nil && req.URL.Scheme != "" {
|
||||
return req.URL.Scheme
|
||||
}
|
||||
return "http"
|
||||
},
|
||||
VarRequestHost: func(req *http.Request) string {
|
||||
if req.Host == "" && req.URL != nil {
|
||||
return req.URL.Hostname()
|
||||
}
|
||||
reqHost, _, err := net.SplitHostPort(req.Host)
|
||||
if err != nil {
|
||||
return req.Host
|
||||
@@ -66,10 +72,21 @@ var staticReqVarSubsMap = map[string]reqVarGetter{
|
||||
return reqHost
|
||||
},
|
||||
VarRequestPort: func(req *http.Request) string {
|
||||
if req.Host == "" && req.URL != nil {
|
||||
return req.URL.Port()
|
||||
}
|
||||
_, reqPort, _ := net.SplitHostPort(req.Host)
|
||||
return reqPort
|
||||
},
|
||||
VarRequestAddr: func(req *http.Request) string { return req.Host },
|
||||
VarRequestAddr: func(req *http.Request) string {
|
||||
if req.Host != "" {
|
||||
return req.Host
|
||||
}
|
||||
if req.URL != nil {
|
||||
return req.URL.Host
|
||||
}
|
||||
return ""
|
||||
},
|
||||
VarRequestPath: func(req *http.Request) string { return req.URL.Path },
|
||||
VarRequestQuery: func(req *http.Request) string { return req.URL.RawQuery },
|
||||
VarRequestURL: func(req *http.Request) string { return req.URL.String() },
|
||||
|
||||
Reference in New Issue
Block a user