2026-03-19 16:42:45 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-19 15:39:51 +08:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|