lolly/internal/netutil/url_test.go
xfy f2352ab9cc docs(config,stream,logging,handler,proxy,cache,server,ssl,middleware): 为核心模块添加详细 GoDoc 文档注释
- config: 为 Config 和所有子配置结构添加完整文档,包含使用示例和注意事项
- stream: 为负载均衡器和服务器添加详细的参数、返回值和功能说明
- logging: 为日志格式化和输出函数添加文档,说明支持的变量替换
- handler: 为路由器、静态文件和 sendfile 处理器添加文档
- proxy: 为健康检查器和代理功能添加完整文档
- cache/server/ssl/middleware: 补充相关模块的文档注释
- config.example.yaml: 添加可信代理配置、加密套件示例,更新压缩级别说明

Co-Authored-By: Claude <noreply@anthropic.com>
2026-04-07 15:36:09 +08:00

157 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package netutil 提供 URL 工具功能的测试。
//
// 该文件测试网络工具模块的 URL 相关功能,包括:
// - 目标 URL 解析
// - 默认端口添加
// - TLS 检测
// - 主机提取
//
// 作者xfy
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)
}
})
}
}