2024-05-11 19:36:33 -04:00
|
|
|
package cache
|
2021-02-07 23:26:05 -05:00
|
|
|
|
|
|
|
|
import (
|
2026-11-23 20:20:14 -08:00
|
|
|
"context"
|
2021-02-07 23:26:05 -05:00
|
|
|
"fmt"
|
2021-07-20 20:00:58 -04:00
|
|
|
"io"
|
2021-02-07 23:26:05 -05:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2026-11-23 20:20:14 -08:00
|
|
|
"strings"
|
2021-02-07 23:26:05 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
2026-11-23 23:42:23 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/tests"
|
2022-07-26 16:47:16 -04:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2021-02-07 23:26:05 -05:00
|
|
|
. "github.com/onsi/gomega"
|
2026-11-23 23:42:23 -05:00
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
2021-02-07 23:26:05 -05:00
|
|
|
)
|
|
|
|
|
|
2024-05-11 19:36:33 -04:00
|
|
|
var _ = Describe("HTTPClient", func() {
|
2021-02-08 10:14:29 -05:00
|
|
|
Context("GET", func() {
|
2024-05-11 19:36:33 -04:00
|
|
|
var chc *HTTPClient
|
2021-02-07 23:26:05 -05:00
|
|
|
var ts *httptest.Server
|
|
|
|
|
var requestsReceived int
|
2021-02-08 10:14:29 -05:00
|
|
|
var header string
|
2021-02-07 23:26:05 -05:00
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2026-06-05 18:06:52 -04:00
|
|
|
requestsReceived = 0
|
|
|
|
|
header = ""
|
2021-02-07 23:26:05 -05:00
|
|
|
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
requestsReceived++
|
2021-02-08 10:14:29 -05:00
|
|
|
header = r.Header.Get("head")
|
2021-02-07 23:26:05 -05:00
|
|
|
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
|
|
|
|
|
}))
|
2024-05-11 19:36:33 -04:00
|
|
|
chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
|
2021-02-07 23:26:05 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("caches repeated requests", func() {
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ := http.NewRequest(http.MethodGet, ts.URL+"?name=doe", nil)
|
2021-02-07 23:26:05 -05:00
|
|
|
resp, err := chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-07-20 20:00:58 -04:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-07 23:26:05 -05:00
|
|
|
Expect(string(body)).To(Equal("Hello, [doe]"))
|
|
|
|
|
Expect(requestsReceived).To(Equal(1))
|
|
|
|
|
|
|
|
|
|
// Same request
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ = http.NewRequest(http.MethodGet, ts.URL+"?name=doe", nil)
|
2021-02-07 23:26:05 -05:00
|
|
|
resp, err = chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-07-20 20:00:58 -04:00
|
|
|
body, err = io.ReadAll(resp.Body)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-07 23:26:05 -05:00
|
|
|
Expect(string(body)).To(Equal("Hello, [doe]"))
|
|
|
|
|
Expect(requestsReceived).To(Equal(1))
|
|
|
|
|
|
|
|
|
|
// Different request
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ = http.NewRequest(http.MethodGet, ts.URL, nil)
|
2021-02-07 23:26:05 -05:00
|
|
|
resp, err = chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-07-20 20:00:58 -04:00
|
|
|
body, err = io.ReadAll(resp.Body)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-07 23:26:05 -05:00
|
|
|
Expect(string(body)).To(Equal("Hello, []"))
|
|
|
|
|
Expect(requestsReceived).To(Equal(2))
|
2021-02-08 10:14:29 -05:00
|
|
|
|
|
|
|
|
// Different again (same as before, but with header)
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ = http.NewRequest(http.MethodGet, ts.URL, nil)
|
2021-02-08 10:14:29 -05:00
|
|
|
r.Header.Add("head", "this is a header")
|
|
|
|
|
resp, err = chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-07-20 20:00:58 -04:00
|
|
|
body, err = io.ReadAll(resp.Body)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-08 10:14:29 -05:00
|
|
|
Expect(string(body)).To(Equal("Hello, []"))
|
2021-02-08 10:36:07 -05:00
|
|
|
Expect(header).To(Equal("this is a header"))
|
2021-02-08 10:14:29 -05:00
|
|
|
Expect(requestsReceived).To(Equal(3))
|
2021-02-07 23:26:05 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("expires responses after TTL", func() {
|
|
|
|
|
requestsReceived = 0
|
2024-05-11 19:36:33 -04:00
|
|
|
chc = NewHTTPClient(http.DefaultClient, 10*time.Millisecond)
|
2021-02-07 23:26:05 -05:00
|
|
|
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ := http.NewRequest(http.MethodGet, ts.URL+"?name=doe", nil)
|
2021-02-07 23:26:05 -05:00
|
|
|
_, err := chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-07 23:26:05 -05:00
|
|
|
Expect(requestsReceived).To(Equal(1))
|
|
|
|
|
|
|
|
|
|
// Wait more than the TTL
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
// Same request
|
2026-11-17 20:58:17 -05:00
|
|
|
r, _ = http.NewRequest(http.MethodGet, ts.URL+"?name=doe", nil)
|
2021-02-07 23:26:05 -05:00
|
|
|
_, err = chc.Do(r)
|
2026-11-16 23:38:07 -05:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-02-07 23:26:05 -05:00
|
|
|
Expect(requestsReceived).To(Equal(2))
|
|
|
|
|
})
|
2026-11-23 23:42:23 -05:00
|
|
|
|
|
|
|
|
It("passes the caller's context values, but not its cancellation", func() {
|
|
|
|
|
type ctxKey struct{}
|
|
|
|
|
fake := &tests.FakeHttpClient{Res: http.Response{StatusCode: http.StatusOK, ProtoMajor: 1, ProtoMinor: 1, Body: io.NopCloser(strings.NewReader("ok"))}}
|
|
|
|
|
chc = NewHTTPClient(fake, consts.DefaultHttpClientTimeOut)
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.WithValue(GinkgoT().Context(), ctxKey{}, "value"))
|
|
|
|
|
cancel()
|
|
|
|
|
r, _ := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
|
|
|
|
|
_, err := chc.Do(r)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
sent := fake.SavedRequest.Context()
|
|
|
|
|
Expect(sent.Value(ctxKey{})).To(Equal("value"))
|
|
|
|
|
Expect(sent.Err()).To(Succeed())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("does not log request headers", func() {
|
|
|
|
|
l, hook := test.NewNullLogger()
|
|
|
|
|
prevLogger, prevLevel := log.SetDefaultLogger(l), log.CurrentLevel()
|
|
|
|
|
log.SetLevel(log.LevelTrace)
|
|
|
|
|
DeferCleanup(func() {
|
|
|
|
|
log.SetDefaultLogger(prevLogger)
|
|
|
|
|
log.SetLevel(prevLevel)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r, _ := http.NewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
|
r.Header.Set("Authorization", "Bearer secret-token")
|
|
|
|
|
_, err := chc.Do(r)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
|
|
Expect(hook.AllEntries()).ToNot(BeEmpty())
|
|
|
|
|
for _, e := range hook.AllEntries() {
|
|
|
|
|
line, _ := e.String()
|
|
|
|
|
Expect(line).ToNot(ContainSubstring("secret-token"))
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-02-07 23:26:05 -05:00
|
|
|
})
|
2026-11-23 20:20:14 -08:00
|
|
|
|
|
|
|
|
Context("non-GET", func() {
|
|
|
|
|
var chc *HTTPClient
|
|
|
|
|
var ts *httptest.Server
|
|
|
|
|
var requestsReceived int
|
|
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
|
requestsReceived = 0
|
|
|
|
|
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
requestsReceived++
|
|
|
|
|
if requestsReceived == 1 {
|
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, _ = fmt.Fprint(w, "ok")
|
|
|
|
|
}))
|
|
|
|
|
chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
|
ts.Close()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("sends every request to the server, so an error reply is not replayed", func() {
|
|
|
|
|
r, _ := http.NewRequest(http.MethodPost, ts.URL, strings.NewReader("listen"))
|
|
|
|
|
resp, err := chc.Do(r)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(resp.StatusCode).To(Equal(http.StatusBadGateway))
|
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
// Same request
|
|
|
|
|
r, _ = http.NewRequest(http.MethodPost, ts.URL, strings.NewReader("listen"))
|
|
|
|
|
resp, err = chc.Do(r)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(resp.StatusCode).To(Equal(http.StatusOK))
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
Expect(string(body)).To(Equal("ok"))
|
|
|
|
|
Expect(requestsReceived).To(Equal(2))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("keeps the caller's context", func() {
|
|
|
|
|
ctx, cancel := context.WithCancel(GinkgoT().Context())
|
|
|
|
|
cancel()
|
|
|
|
|
r, _ := http.NewRequestWithContext(ctx, http.MethodPost, ts.URL, strings.NewReader("listen"))
|
|
|
|
|
_, err := chc.Do(r)
|
|
|
|
|
Expect(err).To(MatchError(context.Canceled))
|
|
|
|
|
Expect(requestsReceived).To(Equal(0))
|
|
|
|
|
})
|
|
|
|
|
})
|
2021-02-07 23:26:05 -05:00
|
|
|
})
|