- 分离 Request 的配置态与执行态,修复二次 Do、raw 模式网络配置失效和 body 来源互斥问题 - 新增 starnet trace 抽象,补齐 DNS/连接/TLS/重试事件,并优化动态 transport 缓存与代理解析路径 - 收紧非法代理为 fail-fast,多目标目标回退仅限幂等请求,修复 Host/TLS/SNI 等语义边界 - 补充防御性拷贝、专项回归测试、本地代理/TLS 用例与 README 行为说明
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package starnet
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestWithRawRequestNil(t *testing.T) {
|
|
_, err := NewRequest("http://example.com", "GET", WithRawRequest(nil))
|
|
if err == nil {
|
|
t.Fatal("expected error when WithRawRequest(nil)")
|
|
}
|
|
}
|
|
|
|
func TestSetHeadersDefensiveCopy(t *testing.T) {
|
|
req := NewSimpleRequest("http://example.com", "GET")
|
|
headers := http.Header{
|
|
"X-Test": []string{"v1"},
|
|
}
|
|
|
|
req.SetHeaders(headers)
|
|
headers.Set("X-Test", "v2")
|
|
|
|
if got := req.GetHeader("X-Test"); got != "v1" {
|
|
t.Fatalf("header mutated by external map change: got=%q want=%q", got, "v1")
|
|
}
|
|
}
|
|
|
|
func TestSetQueriesDefensiveCopy(t *testing.T) {
|
|
req := NewSimpleRequest("http://example.com", "GET")
|
|
queries := map[string][]string{
|
|
"k": []string{"v1"},
|
|
}
|
|
|
|
req.SetQueries(queries)
|
|
queries["k"][0] = "v2"
|
|
queries["k"] = append(queries["k"], "v3")
|
|
|
|
got := req.config.Queries["k"]
|
|
if len(got) != 1 || got[0] != "v1" {
|
|
t.Fatalf("queries mutated by external map change: got=%v want=[v1]", got)
|
|
}
|
|
}
|
|
|
|
func TestSetFormDataDefensiveCopy(t *testing.T) {
|
|
req := NewSimpleRequest("http://example.com", "POST")
|
|
form := map[string][]string{
|
|
"name": []string{"alice"},
|
|
}
|
|
|
|
req.SetFormData(form)
|
|
form["name"][0] = "bob"
|
|
form["name"] = append(form["name"], "carol")
|
|
|
|
got := req.config.Body.FormData["name"]
|
|
if len(got) != 1 || got[0] != "alice" {
|
|
t.Fatalf("form data mutated by external map change: got=%v want=[alice]", got)
|
|
}
|
|
}
|
|
|
|
func TestWithBodyDefensiveCopy(t *testing.T) {
|
|
body := []byte("hello")
|
|
|
|
req, err := NewRequest("http://example.com", "POST", WithBody(body))
|
|
if err != nil {
|
|
t.Fatalf("NewRequest() error: %v", err)
|
|
}
|
|
|
|
body[0] = 'j'
|
|
if string(req.config.Body.Bytes) != "hello" {
|
|
t.Fatalf("body mutated by external slice change: got=%q want=%q", string(req.config.Body.Bytes), "hello")
|
|
}
|
|
}
|
|
|
|
func TestWithFormDataDefensiveCopy(t *testing.T) {
|
|
form := map[string][]string{
|
|
"name": []string{"alice"},
|
|
}
|
|
|
|
req, err := NewRequest("http://example.com", "POST", WithFormData(form))
|
|
if err != nil {
|
|
t.Fatalf("NewRequest() error: %v", err)
|
|
}
|
|
|
|
form["name"][0] = "bob"
|
|
form["name"] = append(form["name"], "carol")
|
|
got := req.config.Body.FormData["name"]
|
|
if len(got) != 1 || got[0] != "alice" {
|
|
t.Fatalf("form data mutated by external map change: got=%v want=[alice]", got)
|
|
}
|
|
}
|
|
|
|
func TestSetCustomIPDefensiveCopy(t *testing.T) {
|
|
ips := []string{"1.1.1.1", "8.8.8.8"}
|
|
req := NewSimpleRequest("http://example.com", "GET").SetCustomIP(ips)
|
|
|
|
ips[0] = "9.9.9.9"
|
|
if got := req.config.DNS.CustomIP[0]; got != "1.1.1.1" {
|
|
t.Fatalf("custom ip mutated by external slice change: got=%q want=%q", got, "1.1.1.1")
|
|
}
|
|
}
|
|
|
|
func TestSetCustomDNSDefensiveCopy(t *testing.T) {
|
|
servers := []string{"8.8.8.8", "1.1.1.1"}
|
|
req := NewSimpleRequest("http://example.com", "GET").SetCustomDNS(servers)
|
|
|
|
servers[0] = "9.9.9.9"
|
|
if got := req.config.DNS.CustomDNS[0]; got != "8.8.8.8" {
|
|
t.Fatalf("custom dns mutated by external slice change: got=%q want=%q", got, "8.8.8.8")
|
|
}
|
|
}
|