mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-27 01:12:48 +00:00
Some checks are pending
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Validate DB migrations (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Waiting to run
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
* There has been a report about navidrome hitting listenbrainz hard
and the listenbrainz guys wanting to be able to distinguish navidrome
* feat: apply Navidrome User-Agent to all outgoing HTTP requests
Add utils/httpclient, a shared http.Client factory whose transport sets
the User-Agent header (Navidrome/{version} - https://github.com/navidrome)
on any request that does not already have one, and use it at every place
the server builds an HTTP client: Last.fm, ListenBrainz and Deezer agents
and auth routers, insights collector, backgrounds handler, and the plugin
host HTTP service. Plugin-set User-Agent values are preserved. The
per-request header lines from the previous commit are superseded by the
transport.
---------
Co-authored-by: Deluan <deluan@navidrome.org>
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package httpclient_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/utils/httpclient"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("httpclient", func() {
|
|
var server *httptest.Server
|
|
var receivedUA string
|
|
|
|
BeforeEach(func() {
|
|
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedUA = r.Header.Get("User-Agent")
|
|
}))
|
|
DeferCleanup(server.Close)
|
|
})
|
|
|
|
Describe("New", func() {
|
|
It("sets the Navidrome User-Agent when the request has none", func() {
|
|
c := httpclient.New(time.Second)
|
|
resp, err := c.Get(server.URL)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
resp.Body.Close()
|
|
Expect(receivedUA).To(Equal(consts.HTTPUserAgent))
|
|
})
|
|
|
|
It("keeps a User-Agent already set by the caller", func() {
|
|
c := httpclient.New(time.Second)
|
|
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
req.Header.Set("User-Agent", "CustomAgent/1.0")
|
|
resp, err := c.Do(req)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
resp.Body.Close()
|
|
Expect(receivedUA).To(Equal("CustomAgent/1.0"))
|
|
})
|
|
|
|
It("applies the given timeout", func() {
|
|
c := httpclient.New(5 * time.Second)
|
|
Expect(c.Timeout).To(Equal(5 * time.Second))
|
|
})
|
|
})
|
|
|
|
Describe("NewTransport", func() {
|
|
It("uses the default transport when base is nil", func() {
|
|
c := &http.Client{Transport: httpclient.NewTransport(nil)}
|
|
resp, err := c.Get(server.URL)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
resp.Body.Close()
|
|
Expect(receivedUA).To(Equal(consts.HTTPUserAgent))
|
|
})
|
|
|
|
It("does not modify the original request", func() {
|
|
c := &http.Client{Transport: httpclient.NewTransport(nil)}
|
|
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
resp, err := c.Do(req)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
resp.Body.Close()
|
|
Expect(req.Header).ToNot(HaveKey("User-Agent"))
|
|
})
|
|
})
|
|
|
|
Describe("HTTPUserAgent", func() {
|
|
It("identifies Navidrome with version and project URL", func() {
|
|
Expect(consts.HTTPUserAgent).To(Equal("Navidrome/" + consts.Version + " - https://github.com/navidrome"))
|
|
})
|
|
})
|
|
})
|