DEV: Add a user agent to all HTTP requests that Discourse makes. (#31555)

This change standardises the `User-Agent` header that Discourse will send when talking to other sites.

`Discourse.user_agent` is now the authority on what the user agent value should be. For Onebox requests, this changes the user agent from their existing value to match the new value (unless overridden).

For all other requests, `Net::HTTPHeader` is monkey-patched to add a default `User-Agent` header when one hasn't been provided.
This commit is contained in:
Gary Pendergast
2025-03-03 16:32:25 +11:00
committed by GitHub
parent 8325d42e56
commit 8615fc6cbb
13 changed files with 71 additions and 19 deletions

View File

@ -194,6 +194,16 @@ RSpec.describe Discourse do
end
end
describe "#user_agent" do
it "returns a user agent string" do
stub_const(Discourse::VERSION, :STRING, "1.2.3") do
Discourse.stubs(:git_version).returns("123456")
expect(Discourse.user_agent).to eq("Discourse/1.2.3-123456; +https://www.discourse.org/")
end
end
end
describe "#site_contact_user" do
fab!(:admin)
fab!(:another_admin) { Fabricate(:admin) }