Skip to content

Commit

Permalink
Update tests and improve the Storing
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Jul 3, 2022
1 parent 4065f54 commit f3417e2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions plugins/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func DefaultSouinPluginCallback(
if r != nil {
rh := r.Header
if stale {
rfc.HitStaleCache(&rh, retriever.GetMatchedURL().TTL.Duration)
rfc.HitStaleCache(&rh)
} else {
rfc.HitCache(&rh, retriever.GetMatchedURL().TTL.Duration)
rfc.HitCache(&rh)
prometheus.Increment(prometheus.CachedResponseCounter)
}
sendAnyCachedResponse(rh, r, res)
Expand Down
6 changes: 6 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func TestMaxAge(t *testing.T) {
if resp2.Header.Get("Cache-Status") != "Souin; hit; ttl=59" {
t.Errorf("unexpected Cache-Status header %v", resp2.Header.Get("Cache-Status"))
}

time.Sleep(2 * time.Second)
resp3, _ := tester.AssertGetResponse(`http://localhost:9080/cache-max-age`, 200, "Hello, max-age!")
if resp3.Header.Get("Cache-Status") != "Souin; hit; ttl=57" {
t.Errorf("unexpected Cache-Status header %v", resp2.Header.Get("Cache-Status"))
}
}

func TestSMaxAge(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/traefik/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func DefaultSouinPluginCallback(
if r != nil {
rh := r.Header
if stale {
rfc.HitStaleCache(&rh, retriever.GetMatchedURL().TTL.Duration)
rfc.HitStaleCache(&rh)
} else {
rfc.HitCache(&rh, retriever.GetMatchedURL().TTL.Duration)
rfc.HitCache(&rh)
}
sendAnyCachedResponse(rh, r, res)

Expand Down
23 changes: 11 additions & 12 deletions plugins/traefik/vendor/github.com/darkweak/souin/rfc/cacheStatus.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/tyk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SouinResponseHandler(rw http.ResponseWriter, res *http.Response, _ *http.Re

if r != nil {
rh := r.Header
rfc.HitCache(&rh, retriever.GetMatchedURL().TTL.Duration)
rfc.HitCache(&rh)
r.Header = rh
for _, v := range []string{"Age", "Cache-Status"} {
h := r.Header.Get(v)
Expand Down
23 changes: 11 additions & 12 deletions rfc/cacheStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func ValidateCacheControl(r *http.Response) bool {
}

// HitCache set hit and manage age header too
func HitCache(h *http.Header, ttl time.Duration) {
manageAge(h, ttl)
func HitCache(h *http.Header) {
manageAge(h)
}

// HitStaleCache set hit and manage age header too
func HitStaleCache(h *http.Header, ttl time.Duration) {
manageAge(h, ttl)
func HitStaleCache(h *http.Header) {
manageAge(h)
h.Set("Cache-Status", h.Get("Cache-Status")+"; fwd=stale")
}

func manageAge(h *http.Header, ttl time.Duration) {
func manageAge(h *http.Header) {
utc1 := time.Now().UTC()
dh := h.Get("Date")
if dh == "" {
Expand All @@ -76,14 +76,13 @@ func manageAge(h *http.Header, ttl time.Duration) {
}

if h.Get(storedTTLHeader) != "" {
ttl, _ = time.ParseDuration(h.Get(storedTTLHeader))
ttl, _ := time.ParseDuration(h.Get(storedTTLHeader))
h.Del(storedTTLHeader)
cage := correctedInitialAge(utc1, utc2)
h.Set("Age", strconv.Itoa(cage))
ttlValue := strconv.Itoa(int(ttl.Seconds()) - cage)
h.Set("Cache-Status", "Souin; hit; ttl="+ttlValue)
}
cage := correctedInitialAge(utc1, utc2)
age := strconv.Itoa(cage)
h.Set("Age", age)
ttlValue := strconv.Itoa(int(ttl.Seconds()) - cage)
h.Set("Cache-Status", "Souin; hit; ttl="+ttlValue)
}

func setMalformedHeader(headers *http.Header, header string) {
Expand All @@ -94,7 +93,7 @@ func setMalformedHeader(headers *http.Header, header string) {
func SetCacheStatusEventually(resp *http.Response) *http.Response {
h := resp.Header
validateEmptyHeaders(&h)
manageAge(&h, 0)
manageAge(&h)

resp.Header = h
return resp
Expand Down
3 changes: 1 addition & 2 deletions rfc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ func (t *VaryTransport) SetCache(key string, resp *http.Response) {
}
if ma > t.ConfigurationURL.TTL.Duration {
ma = t.ConfigurationURL.TTL.Duration
} else {
resp.Header.Set(storedTTLHeader, ma.String())
}
resp.Header.Set(storedTTLHeader, ma.String())
if respBytes, err := httputil.DumpResponse(resp, true); e == nil && err == nil {
t.Provider.Set(key, respBytes, t.ConfigurationURL, ma)
}
Expand Down

0 comments on commit f3417e2

Please sign in to comment.