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

+1 -1
View File
@@ -127,7 +127,7 @@ func FetchLatest(ctx context.Context, c *http.Client) (*Release, error) {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
httpidentity.ApplyRequired(req)
httpidentity.Apply(req)
resp, err := c.Do(req)
if err != nil {
+4 -2
View File
@@ -59,7 +59,7 @@ func TestFetchLatestMissingRelease(t *testing.T) {
func TestUpdateUserAgent(t *testing.T) {
for _, tc := range []struct{ env, want string }{
{"", "Go-http-client/1.1"},
{"", ""},
{"Captured-UA/1.0", "Captured-UA/1.0"},
} {
t.Run(tc.env, func(t *testing.T) {
@@ -67,7 +67,9 @@ func TestUpdateUserAgent(t *testing.T) {
calls := 0
client := &http.Client{Transport: releaseTransport(func(req *http.Request) (*http.Response, error) {
calls++
if got := req.UserAgent(); got != tc.want {
if got := req.UserAgent(); tc.want == "" && !strings.HasPrefix(got, "Mozilla/5.0 ") {
t.Errorf("random UA missing: %q", got)
} else if tc.want != "" && got != tc.want {
t.Errorf("User-Agent = %q, want %q", got, tc.want)
}
body := `{"tag_name":"v0.3.17"}`
+1 -1
View File
@@ -231,7 +231,7 @@ func get(ctx context.Context, c *http.Client, rawURL string) (io.ReadCloser, err
if err := checkURL(req.URL); err != nil {
return nil, err
}
httpidentity.ApplyRequired(req)
httpidentity.Apply(req)
resp, err := c.Do(req)
if err != nil {
return nil, err