navidrome/utils/cached_http_client_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.5 KiB
Go
Raw Normal View History

package utils
2021-02-08 05:26:05 +01:00
import (
"fmt"
"io"
2021-02-08 05:26:05 +01:00
"net/http"
"net/http/httptest"
"time"
"github.com/navidrome/navidrome/consts"
2022-07-26 22:47:16 +02:00
. "github.com/onsi/ginkgo/v2"
2021-02-08 05:26:05 +01:00
. "github.com/onsi/gomega"
)
var _ = Describe("CachedHttpClient", func() {
2021-02-08 16:14:29 +01:00
Context("GET", func() {
2021-02-08 05:26:05 +01:00
var chc *CachedHTTPClient
var ts *httptest.Server
var requestsReceived int
2021-02-08 16:14:29 +01:00
var header string
2021-02-08 05:26:05 +01:00
BeforeEach(func() {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestsReceived++
2021-02-08 16:14:29 +01:00
header = r.Header.Get("head")
2021-02-08 05:26:05 +01:00
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
}))
chc = NewCachedHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
2021-02-08 05:26:05 +01:00
})
AfterEach(func() {
defer ts.Close()
})
It("caches repeated requests", func() {
r, _ := http.NewRequest("GET", ts.URL+"?name=doe", nil)
resp, err := chc.Do(r)
Expect(err).To(BeNil())
body, err := io.ReadAll(resp.Body)
2021-02-08 05:26:05 +01:00
Expect(err).To(BeNil())
Expect(string(body)).To(Equal("Hello, [doe]"))
Expect(requestsReceived).To(Equal(1))
// Same request
r, _ = http.NewRequest("GET", ts.URL+"?name=doe", nil)
resp, err = chc.Do(r)
Expect(err).To(BeNil())
body, err = io.ReadAll(resp.Body)
2021-02-08 05:26:05 +01:00
Expect(err).To(BeNil())
Expect(string(body)).To(Equal("Hello, [doe]"))
Expect(requestsReceived).To(Equal(1))
// Different request
r, _ = http.NewRequest("GET", ts.URL, nil)
resp, err = chc.Do(r)
Expect(err).To(BeNil())
body, err = io.ReadAll(resp.Body)
2021-02-08 05:26:05 +01:00
Expect(err).To(BeNil())
Expect(string(body)).To(Equal("Hello, []"))
Expect(requestsReceived).To(Equal(2))
2021-02-08 16:14:29 +01:00
// Different again (same as before, but with header)
r, _ = http.NewRequest("GET", ts.URL, nil)
r.Header.Add("head", "this is a header")
resp, err = chc.Do(r)
Expect(err).To(BeNil())
body, err = io.ReadAll(resp.Body)
2021-02-08 16:14:29 +01:00
Expect(err).To(BeNil())
Expect(string(body)).To(Equal("Hello, []"))
2021-02-08 16:36:07 +01:00
Expect(header).To(Equal("this is a header"))
2021-02-08 16:14:29 +01:00
Expect(requestsReceived).To(Equal(3))
2021-02-08 05:26:05 +01:00
})
It("expires responses after TTL", func() {
requestsReceived = 0
chc = NewCachedHTTPClient(http.DefaultClient, 10*time.Millisecond)
r, _ := http.NewRequest("GET", ts.URL+"?name=doe", nil)
_, err := chc.Do(r)
Expect(err).To(BeNil())
Expect(requestsReceived).To(Equal(1))
// Wait more than the TTL
time.Sleep(50 * time.Millisecond)
// Same request
r, _ = http.NewRequest("GET", ts.URL+"?name=doe", nil)
_, err = chc.Do(r)
Expect(err).To(BeNil())
Expect(requestsReceived).To(Equal(2))
})
})
})