Skip to content

Commit 7aa7420

Browse files
committed
feat(dependencies): bump souin v1.7.5
1 parent 283ea9b commit 7aa7420

File tree

4 files changed

+177
-7
lines changed

4 files changed

+177
-7
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.22.1
44

55
require (
66
github.com/caddyserver/caddy/v2 v2.8.4
7-
github.com/darkweak/souin v1.7.0
8-
github.com/darkweak/storages/core v0.0.8
7+
github.com/darkweak/souin v1.7.5
8+
github.com/darkweak/storages/core v0.0.11
99
)
1010

1111
require (

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
109109
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
110110
github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns=
111111
github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654=
112-
github.com/darkweak/souin v1.7.0 h1:QeSxwHECzZPlYHTGYDw4xQ6EBJY94f/nfqW4BLc3YQ0=
113-
github.com/darkweak/souin v1.7.0/go.mod h1:XXmhB+QIiZ/lkESd1izzqCI7QWxmX0of01QA+xxgogc=
114-
github.com/darkweak/storages/core v0.0.8 h1:9e7rOxHiJwnvADDVCZ7LFRnUnOHGT+UMpNOFlR8BOiw=
115-
github.com/darkweak/storages/core v0.0.8/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
112+
github.com/darkweak/souin v1.7.5 h1:drNhZc0GhSbGcugiGfcYdLDTcx3DCZW6o13wwRj5o5Y=
113+
github.com/darkweak/souin v1.7.5/go.mod h1:PcP+hhvYOdqn4OmeScKKvit0TihYVYS1o154mhfWT/s=
114+
github.com/darkweak/storages/core v0.0.11 h1:IwvpAtkhOmxC5pIffJ8opW6erpTnIi5zqPveiAQs8ew=
115+
github.com/darkweak/storages/core v0.0.11/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
116116
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
117117
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
118118
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

httpcache.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type SouinCaddyMiddleware struct {
4747
Key configurationtypes.Key `json:"key,omitempty"`
4848
// Override the cache key generation matching the pattern.
4949
CacheKeys configurationtypes.CacheKeys `json:"cache_keys,omitempty"`
50+
// Configure the Nats cache storage.
51+
Nats configurationtypes.CacheProvider `json:"nats,omitempty"`
5052
// Configure the Nuts cache storage.
5153
Nuts configurationtypes.CacheProvider `json:"nuts,omitempty"`
5254
// Configure the Otter cache storage.
@@ -61,6 +63,8 @@ type SouinCaddyMiddleware struct {
6163
Timeout configurationtypes.Timeout `json:"timeout,omitempty"`
6264
// Time to live for a key, using time.duration.
6365
TTL configurationtypes.Duration `json:"ttl,omitempty"`
66+
// Configure the SimpleFS cache storage.
67+
SimpleFS configurationtypes.CacheProvider `json:"simplefs,omitempty"`
6468
// Time to live for a stale key, using time.duration.
6569
Stale configurationtypes.Duration `json:"stale,omitempty"`
6670
// Storage providers chaining and order.
@@ -90,7 +94,9 @@ func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
9094
if s.Configuration.GetDefaultCache() == nil {
9195
defaultCache := DefaultCache{
9296
Badger: s.Badger,
97+
Nats: s.Nats,
9398
Nuts: s.Nuts,
99+
SimpleFS: s.SimpleFS,
94100
Otter: s.Otter,
95101
Key: s.Key,
96102
DefaultCacheControl: s.DefaultCacheControl,
@@ -115,6 +121,10 @@ func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
115121
return nil
116122
}
117123

124+
func isProviderEmpty(c configurationtypes.CacheProvider) bool {
125+
return !c.Found
126+
}
127+
118128
// FromApp to initialize configuration from App structure.
119129
func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
120130
if s.Configuration.GetDefaultCache() == nil {
@@ -198,14 +208,16 @@ func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
198208
if dc.CacheName == "" {
199209
s.Configuration.DefaultCache.CacheName = appDc.CacheName
200210
}
201-
if !s.Configuration.DefaultCache.Distributed && !dc.Olric.Found && !dc.Redis.Found && !dc.Etcd.Found && !dc.Badger.Found && !dc.Nuts.Found && !dc.Otter.Found {
211+
if isProviderEmpty(dc.Badger) && isProviderEmpty(dc.Etcd) && isProviderEmpty(dc.Nats) && isProviderEmpty(dc.Nuts) && isProviderEmpty(dc.Olric) && isProviderEmpty(dc.Otter) && isProviderEmpty(dc.Redis) && isProviderEmpty(dc.SimpleFS) {
202212
s.Configuration.DefaultCache.Distributed = appDc.Distributed
203213
s.Configuration.DefaultCache.Olric = appDc.Olric
204214
s.Configuration.DefaultCache.Redis = appDc.Redis
205215
s.Configuration.DefaultCache.Etcd = appDc.Etcd
206216
s.Configuration.DefaultCache.Badger = appDc.Badger
217+
s.Configuration.DefaultCache.Nats = appDc.Nats
207218
s.Configuration.DefaultCache.Nuts = appDc.Nuts
208219
s.Configuration.DefaultCache.Otter = appDc.Otter
220+
s.Configuration.DefaultCache.SimpleFS = appDc.SimpleFS
209221
}
210222
if dc.Regex.Exclude == "" {
211223
s.Configuration.DefaultCache.Regex.Exclude = appDc.Regex.Exclude

httpcache_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,106 @@ func TestMustRevalidate(t *testing.T) {
602602
}
603603
}
604604

605+
type staleIfErrorHandler struct {
606+
iterator int
607+
}
608+
609+
func (t *staleIfErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
610+
if t.iterator > 0 {
611+
w.WriteHeader(http.StatusInternalServerError)
612+
return
613+
}
614+
615+
t.iterator++
616+
w.Header().Set("Cache-Control", "stale-if-error=86400")
617+
w.WriteHeader(http.StatusOK)
618+
_, _ = w.Write([]byte("Hello stale-if-error!"))
619+
}
620+
621+
func TestStaleIfError(t *testing.T) {
622+
tester := caddytest.NewTester(t)
623+
tester.InitServer(`
624+
{
625+
admin localhost:2999
626+
http_port 9080
627+
cache {
628+
ttl 5s
629+
stale 5s
630+
}
631+
}
632+
localhost:9080 {
633+
route /stale-if-error {
634+
cache
635+
reverse_proxy localhost:9085
636+
}
637+
}`, "caddyfile")
638+
639+
go func() {
640+
staleIfErrorHandler := staleIfErrorHandler{}
641+
_ = http.ListenAndServe(":9085", &staleIfErrorHandler)
642+
}()
643+
time.Sleep(time.Second)
644+
resp1, _ := tester.AssertGetResponse(`http://localhost:9080/stale-if-error`, http.StatusOK, "Hello stale-if-error!")
645+
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/stale-if-error`, http.StatusOK, "Hello stale-if-error!")
646+
647+
if resp1.Header.Get("Cache-Control") != "stale-if-error=86400" {
648+
t.Errorf("unexpected resp1 Cache-Control header %v", resp1.Header.Get("Cache-Control"))
649+
}
650+
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/stale-if-error" {
651+
t.Errorf("unexpected resp1 Cache-Status header %v", resp1.Header.Get("Cache-Status"))
652+
}
653+
if resp1.Header.Get("Age") != "" {
654+
t.Errorf("unexpected resp1 Age header %v", resp1.Header.Get("Age"))
655+
}
656+
657+
if resp2.Header.Get("Cache-Control") != "stale-if-error=86400" {
658+
t.Errorf("unexpected resp2 Cache-Control header %v", resp2.Header.Get("Cache-Control"))
659+
}
660+
if resp2.Header.Get("Cache-Status") != "Souin; hit; ttl=4; key=GET-http-localhost:9080-/stale-if-error; detail=DEFAULT" {
661+
t.Errorf("unexpected resp2 Cache-Status header %v", resp2.Header.Get("Cache-Status"))
662+
}
663+
if resp2.Header.Get("Age") != "1" {
664+
t.Errorf("unexpected resp2 Age header %v", resp2.Header.Get("Age"))
665+
}
666+
667+
time.Sleep(6 * time.Second)
668+
staleReq, _ := http.NewRequest(http.MethodGet, "http://localhost:9080/stale-if-error", nil)
669+
staleReq.Header = http.Header{"Cache-Control": []string{"stale-if-error=86400"}}
670+
resp3, _ := tester.AssertResponse(staleReq, http.StatusOK, "Hello stale-if-error!")
671+
672+
if resp3.Header.Get("Cache-Control") != "stale-if-error=86400" {
673+
t.Errorf("unexpected resp3 Cache-Control header %v", resp3.Header.Get("Cache-Control"))
674+
}
675+
if resp3.Header.Get("Cache-Status") != "Souin; hit; ttl=-2; key=GET-http-localhost:9080-/stale-if-error; detail=DEFAULT; fwd=stale; fwd-status=500" {
676+
t.Errorf("unexpected resp3 Cache-Status header %v", resp3.Header.Get("Cache-Status"))
677+
}
678+
if resp3.Header.Get("Age") != "7" {
679+
t.Errorf("unexpected resp3 Age header %v", resp3.Header.Get("Age"))
680+
}
681+
682+
resp4, _ := tester.AssertGetResponse(`http://localhost:9080/stale-if-error`, http.StatusOK, "Hello stale-if-error!")
683+
684+
if resp4.Header.Get("Cache-Status") != "Souin; hit; ttl=-2; key=GET-http-localhost:9080-/stale-if-error; detail=DEFAULT; fwd=stale; fwd-status=500" &&
685+
resp4.Header.Get("Cache-Status") != "Souin; hit; ttl=-3; key=GET-http-localhost:9080-/stale-if-error; detail=DEFAULT; fwd=stale; fwd-status=500" {
686+
t.Errorf("unexpected resp4 Cache-Status header %v", resp4.Header.Get("Cache-Status"))
687+
}
688+
689+
if resp4.Header.Get("Age") != "7" && resp4.Header.Get("Age") != "8" {
690+
t.Errorf("unexpected resp4 Age header %v", resp4.Header.Get("Age"))
691+
}
692+
693+
time.Sleep(6 * time.Second)
694+
resp5, _ := tester.AssertGetResponse(`http://localhost:9080/stale-if-error`, http.StatusInternalServerError, "")
695+
696+
if resp5.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; key=GET-http-localhost:9080-/stale-if-error; detail=UNCACHEABLE-STATUS-CODE" {
697+
t.Errorf("unexpected resp5 Cache-Status header %v", resp5.Header.Get("Cache-Status"))
698+
}
699+
700+
if resp5.Header.Get("Age") != "" {
701+
t.Errorf("unexpected resp5 Age header %v", resp5.Header.Get("Age"))
702+
}
703+
}
704+
605705
type testETagsHandler struct{}
606706

607707
const etagValue = "AAA-BBB"
@@ -1086,3 +1186,61 @@ func TestComplexQuery(t *testing.T) {
10861186
cacheChecker(caddyTester, "fields[]=id&pagination=true", 9)
10871187
cacheChecker(caddyTester, "fields[]=id&pagination=false", 9)
10881188
}
1189+
1190+
func TestBypassWithExpiresAndRevalidate(t *testing.T) {
1191+
tester := caddytest.NewTester(t)
1192+
tester.InitServer(`
1193+
{
1194+
debug
1195+
admin localhost:2999
1196+
http_port 9080
1197+
https_port 9443
1198+
cache {
1199+
ttl 5s
1200+
stale 5s
1201+
mode bypass
1202+
}
1203+
}
1204+
localhost:9080 {
1205+
route /bypass-with-expires-and-revalidate {
1206+
cache
1207+
header Expires 0
1208+
header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate"
1209+
respond "Hello, expires and revalidate!"
1210+
}
1211+
}`, "caddyfile")
1212+
1213+
respStored1, _ := tester.AssertGetResponse(`http://localhost:9080/bypass-with-expires-and-revalidate`, 200, "Hello, expires and revalidate!")
1214+
if respStored1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/bypass-with-expires-and-revalidate" {
1215+
t.Errorf("unexpected Cache-Status header value %v", respStored1.Header.Get("Cache-Status"))
1216+
}
1217+
if respStored1.Header.Get("Age") != "" {
1218+
t.Errorf("unexpected Age header %v", respStored1.Header.Get("Age"))
1219+
}
1220+
1221+
respStored2, _ := tester.AssertGetResponse(`http://localhost:9080/bypass-with-expires-and-revalidate`, 200, "Hello, expires and revalidate!")
1222+
if respStored2.Header.Get("Cache-Status") != "Souin; hit; ttl=4; key=GET-http-localhost:9080-/bypass-with-expires-and-revalidate; detail=DEFAULT" {
1223+
t.Errorf("unexpected Cache-Status header value %v", respStored2.Header.Get("Cache-Status"))
1224+
}
1225+
if respStored2.Header.Get("Age") == "" {
1226+
t.Error("Age header should be present")
1227+
}
1228+
1229+
time.Sleep(5 * time.Second)
1230+
respStored3, _ := tester.AssertGetResponse(`http://localhost:9080/bypass-with-expires-and-revalidate`, 200, "Hello, expires and revalidate!")
1231+
if respStored3.Header.Get("Cache-Status") != "Souin; hit; ttl=-1; key=GET-http-localhost:9080-/bypass-with-expires-and-revalidate; detail=DEFAULT; fwd=stale" {
1232+
t.Errorf("unexpected Cache-Status header value %v", respStored3.Header.Get("Cache-Status"))
1233+
}
1234+
if respStored3.Header.Get("Age") == "" {
1235+
t.Error("Age header should be present")
1236+
}
1237+
1238+
time.Sleep(5 * time.Second)
1239+
respStored4, _ := tester.AssertGetResponse(`http://localhost:9080/bypass-with-expires-and-revalidate`, 200, "Hello, expires and revalidate!")
1240+
if respStored4.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/bypass-with-expires-and-revalidate" {
1241+
t.Errorf("unexpected Cache-Status header value %v", respStored4.Header.Get("Cache-Status"))
1242+
}
1243+
if respStored4.Header.Get("Age") != "" {
1244+
t.Errorf("unexpected Age header %v", respStored4.Header.Get("Age"))
1245+
}
1246+
}

0 commit comments

Comments
 (0)