lolly/internal/netutil/url_test.go
xfy 7a98a0b044 refactor: 抽取网络工具函数到 netutil 包,移除冗余代码
- 新增 internal/netutil 包,统一 IP 提取和 URL 解析函数
- proxy/websocket/middleware 使用 netutil 替代重复实现
- 移除 handler/sendfile 中未使用的 BufferPool 相关代码
- 移除 http3/adapter 中未使用的反向转换函数
- 提取 server.registerStaticHandler 函数改进代码结构
- 优化 access.go 锁范围,减少持锁时间

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-03 18:24:21 +08:00

147 lines
3.5 KiB
Go

package netutil
import "testing"
func TestParseTargetURL(t *testing.T) {
tests := []struct {
name string
targetURL string
addDefaultPort bool
wantAddr string
wantIsTLS bool
}{
// HTTP without port
{
name: "http without port, add default",
targetURL: "http://backend.example.com",
addDefaultPort: true,
wantAddr: "backend.example.com:80",
wantIsTLS: false,
},
{
name: "http without port, no default",
targetURL: "http://backend.example.com",
addDefaultPort: false,
wantAddr: "backend.example.com",
wantIsTLS: false,
},
// HTTPS without port
{
name: "https without port, add default",
targetURL: "https://api.example.com",
addDefaultPort: true,
wantAddr: "api.example.com:443",
wantIsTLS: true,
},
{
name: "https without port, no default",
targetURL: "https://api.example.com",
addDefaultPort: false,
wantAddr: "api.example.com",
wantIsTLS: true,
},
// HTTP with port
{
name: "http with port",
targetURL: "http://backend:8080",
addDefaultPort: true,
wantAddr: "backend:8080",
wantIsTLS: false,
},
// HTTPS with port
{
name: "https with port",
targetURL: "https://api:8443",
addDefaultPort: true,
wantAddr: "api:8443",
wantIsTLS: true,
},
// With path
{
name: "http with path",
targetURL: "http://backend:8080/api/v1",
addDefaultPort: false,
wantAddr: "backend:8080",
wantIsTLS: false,
},
{
name: "https with path",
targetURL: "https://api.example.com/v1/users",
addDefaultPort: true,
wantAddr: "api.example.com:443",
wantIsTLS: true,
},
// No protocol (treat as HTTP)
{
name: "no protocol",
targetURL: "backend:8080",
addDefaultPort: false,
wantAddr: "backend:8080",
wantIsTLS: false,
},
{
name: "no protocol, no port, add default",
targetURL: "backend",
addDefaultPort: true,
wantAddr: "backend:80",
wantIsTLS: false,
},
// IPv6 address
{
name: "ipv6 address",
targetURL: "http://[::1]:8080",
addDefaultPort: false,
wantAddr: "[::1]:8080",
wantIsTLS: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAddr, gotIsTLS := ParseTargetURL(tt.targetURL, tt.addDefaultPort)
if gotAddr != tt.wantAddr {
t.Errorf("ParseTargetURL() addr = %q, want %q", gotAddr, tt.wantAddr)
}
if gotIsTLS != tt.wantIsTLS {
t.Errorf("ParseTargetURL() isTLS = %v, want %v", gotIsTLS, tt.wantIsTLS)
}
})
}
}
func TestExtractHost(t *testing.T) {
tests := []struct {
name string
targetURL string
want string
}{
{
name: "http without port",
targetURL: "http://backend.example.com",
want: "backend.example.com:80",
},
{
name: "https without port",
targetURL: "https://api.example.com",
want: "api.example.com:443",
},
{
name: "http with port",
targetURL: "http://backend:8080",
want: "backend:8080",
},
{
name: "https with path",
targetURL: "https://api.example.com/v1/users",
want: "api.example.com:443",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ExtractHost(tt.targetURL); got != tt.want {
t.Errorf("ExtractHost() = %q, want %q", got, tt.want)
}
})
}
}