Files
navidrome/utils/cache/cached_http_client_test.go
T
Deluan f8ed972e4c chore(lint): add the ctxvariant linter
ctxvariant (tools/ctxvariant, a golangci-lint module plugin built via .custom-gcl.yml) reports a call to F when
FContext or FWithContext takes the same arguments after a leading ctx, plus stdlib calls whose ctx variant is named
differently (http.Get, net.Listen/Dial, DB.Begin, ...). It replaces the migration-only forbidigo SQL rules.
Not noctx: it misses closures in package-level vars (every Ginkgo spec) and packages without a direct import.
Fixes 702 call sites: 658 in tests, 14 in server code, 30 in migrations. The migrations now use ctx-aware calls;
they already ran every other query with the same ctx, so their behavior is the same.
2026-11-17 20:19:10 -05:00

102 lines
2.8 KiB
Go

package cache
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"time"
"github.com/navidrome/navidrome/consts"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("HTTPClient", func() {
var ctx context.Context
BeforeEach(func() {
ctx = GinkgoT().Context()
})
Context("GET", func() {
var chc *HTTPClient
var ts *httptest.Server
var requestsReceived int
var header string
BeforeEach(func() {
requestsReceived = 0
header = ""
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestsReceived++
header = r.Header.Get("head")
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
}))
chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
})
AfterEach(func() {
defer ts.Close()
})
It("caches repeated requests", func() {
r, _ := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"?name=doe", nil)
resp, err := chc.Do(r)
Expect(err).ToNot(HaveOccurred())
body, err := io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("Hello, [doe]"))
Expect(requestsReceived).To(Equal(1))
// Same request
r, _ = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"?name=doe", nil)
resp, err = chc.Do(r)
Expect(err).ToNot(HaveOccurred())
body, err = io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("Hello, [doe]"))
Expect(requestsReceived).To(Equal(1))
// Different request
r, _ = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
resp, err = chc.Do(r)
Expect(err).ToNot(HaveOccurred())
body, err = io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("Hello, []"))
Expect(requestsReceived).To(Equal(2))
// Different again (same as before, but with header)
r, _ = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
r.Header.Add("head", "this is a header")
resp, err = chc.Do(r)
Expect(err).ToNot(HaveOccurred())
body, err = io.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("Hello, []"))
Expect(header).To(Equal("this is a header"))
Expect(requestsReceived).To(Equal(3))
})
It("expires responses after TTL", func() {
requestsReceived = 0
chc = NewHTTPClient(http.DefaultClient, 10*time.Millisecond)
r, _ := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"?name=doe", nil)
_, err := chc.Do(r)
Expect(err).ToNot(HaveOccurred())
Expect(requestsReceived).To(Equal(1))
// Wait more than the TTL
time.Sleep(50 * time.Millisecond)
// Same request
r, _ = http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"?name=doe", nil)
_, err = chc.Do(r)
Expect(err).ToNot(HaveOccurred())
Expect(requestsReceived).To(Equal(2))
})
})
})