- 新增 internal/sitecode 包:FromRequest 头优先解析、FromQueryThenHeader 探测接口专用、ExtractHost/IsLocalHost 提取 Host 域名 - 替换 auth/order/listing/chat/notification/withdrawal/wallet/distribution 八模块 17 处各自内联的解析逻辑,统一优先级与 TrimSpace 行为 - 修复 notification 漏 TrimSpace、探测接口会话头压过显式 query、 GetPublicSiteInfo 把 localhost Host 当站点域名查库等不一致 - 顺手清理 auth 中无引用的 extractHost 死代码 - gofmt 修复 withdrawal/presenter、chat/file/payment 测试四个历史遗留格式问题
101 lines
3.4 KiB
Go
101 lines
3.4 KiB
Go
package sitecode
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func newTestContext(method, target string, headers map[string]string) *gin.Context {
|
|
gin.SetMode(gin.TestMode)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
req := httptest.NewRequest(method, target, nil)
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
c.Request = req
|
|
return c
|
|
}
|
|
|
|
func TestFromRequest(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
target string
|
|
header map[string]string
|
|
want string
|
|
}{
|
|
{name: "仅头", target: "/api/listings", header: map[string]string{HeaderName: "site_a"}, want: "site_a"},
|
|
{name: "旧头兜底", target: "/api/wallet/balance", header: map[string]string{LegacyHeaderName: "site_b"}, want: "site_b"},
|
|
{name: "query兜底", target: "/api/wallet/balance?distribution_site_code=site_c", header: nil, want: "site_c"},
|
|
{name: "短query兜底", target: "/api/orders?site_code=site_d", header: nil, want: "site_d"},
|
|
{name: "头优先于query", target: "/api/orders?site_code=query_site", header: map[string]string{HeaderName: "header_site"}, want: "header_site"},
|
|
{name: "权威头优先于旧头", target: "/api/chats", header: map[string]string{HeaderName: "new_site", LegacyHeaderName: "old_site"}, want: "new_site"},
|
|
{name: "空白字符修剪", target: "/api/orders?site_code=%20%20site_e%20%20", header: nil, want: "site_e"},
|
|
{name: "主站为空", target: "/api/orders", header: nil, want: ""},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := FromRequest(newTestContext("GET", tc.target, tc.header))
|
|
if got != tc.want {
|
|
t.Fatalf("FromRequest() = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFromQueryThenHeader(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
target string
|
|
header map[string]string
|
|
want string
|
|
}{
|
|
{name: "code参数优先", target: "/distribution/site-info?code=probe_site", header: map[string]string{HeaderName: "session_site"}, want: "probe_site"},
|
|
{name: "site_code参数优先", target: "/distribution/site-info?site_code=probe2", header: map[string]string{HeaderName: "session"}, want: "probe2"},
|
|
{name: "无query时回落头", target: "/distribution/site-info", header: map[string]string{HeaderName: "session"}, want: "session"},
|
|
{name: "全空", target: "/distribution/site-info", header: nil, want: ""},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := FromQueryThenHeader(newTestContext("GET", tc.target, tc.header))
|
|
if got != tc.want {
|
|
t.Fatalf("FromQueryThenHeader() = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractHost(t *testing.T) {
|
|
cases := []struct {
|
|
raw string
|
|
want string
|
|
}{
|
|
{raw: "shop.example.com:8080", want: "shop.example.com"},
|
|
{raw: "shop.example.com", want: "shop.example.com"},
|
|
{raw: "Shop.Example.COM", want: "shop.example.com"},
|
|
{raw: "[::1]:5189", want: "::1"},
|
|
{raw: " shop.example.com ", want: "shop.example.com"},
|
|
{raw: "", want: ""},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := ExtractHost(tc.raw); got != tc.want {
|
|
t.Errorf("ExtractHost(%q) = %q, want %q", tc.raw, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsLocalHost(t *testing.T) {
|
|
for _, local := range []string{"localhost", "127.0.0.1", "::1"} {
|
|
if !IsLocalHost(local) {
|
|
t.Errorf("IsLocalHost(%q) 应为 true", local)
|
|
}
|
|
}
|
|
for _, remote := range []string{"shop.example.com", "0.0.0.0", ""} {
|
|
if IsLocalHost(remote) {
|
|
t.Errorf("IsLocalHost(%q) 应为 false", remote)
|
|
}
|
|
}
|
|
}
|