fix: select random browser user agents for outbound HTTP

This commit is contained in:
ji ruo committed 2026-11-11 19:13:38 +08:00
1 parent 17c2fc5e08
commit 171db405dd
18 files changed
+205 -71

No files matched your search

+3 -4
View File
@@ -32,12 +32,11 @@ func TestProbeOneUserAgent(t *testing.T) {
t.Run(ua, func(t *testing.T) {
t.Setenv("ARTEX_HTTP_USER_AGENT", ua)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.UserAgent(); got != ua {
if got := r.UserAgent(); ua == "" && !strings.HasPrefix(got, "Mozilla/5.0 ") {
t.Errorf("random UA missing: %q", got)
} else if ua != "" && got != ua {
t.Errorf("User-Agent = %q, want %q", got, ua)
}
if _, present := r.Header["User-Agent"]; ua == "" && present {
t.Error("empty User-Agent present on wire")
}
fmt.Fprint(w, `{"ip":"203.0.113.7"}`)
}))
defer server.Close()
+5 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
@@ -23,10 +24,12 @@ func TestTargetHTTPIdentityOverrides(t *testing.T) {
t.Run(route+"/"+tc.name, func(t *testing.T) {
t.Setenv("ARTEX_HTTP_USER_AGENT", tc.env)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.UserAgent(); got != tc.want {
if got := r.UserAgent(); tc.name == "default" && !strings.HasPrefix(got, "Mozilla/5.0 ") {
t.Errorf("random UA missing: %q", got)
} else if tc.name != "default" && got != tc.want {
t.Errorf("User-Agent = %q, want %q", got, tc.want)
}
if _, present := r.Header["User-Agent"]; tc.want == "" && present {
if _, present := r.Header["User-Agent"]; tc.name == "explicit-empty" && present {
t.Error("empty User-Agent present on wire")
}
w.Write([]byte("ok"))