Skip to content

Commit

Permalink
fix(ci): Unit tests for beego & gin, Traefik and Tyk sync
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Jul 20, 2024
1 parent d37840b commit 04f5fb6
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions pkg/api/souin.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (s *SouinAPI) listKeys(search string) []string {
var storageToInfiniteTTLMap = map[string]time.Duration{
"BADGER": types.OneYearDuration,
"ETCD": types.OneYearDuration,
"GO-REDIS": 0,
"NUTS": 0,
"OLRIC": types.OneYearDuration,
"OTTER": types.OneYearDuration,
Expand Down
4 changes: 2 additions & 2 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ func (s *SouinBaseHandler) Upstream(
}
}

_, cacheControl := s.SurrogateKeyStorer.GetSurrogateControl(customWriter.Header())
headerName, cacheControl := s.SurrogateKeyStorer.GetSurrogateControl(customWriter.Header())
if cacheControl == "" {
// customWriter.Header().Set(headerName, s.DefaultMatchedUrl.DefaultCacheControl)
customWriter.Header().Set(headerName, s.DefaultMatchedUrl.DefaultCacheControl)
}

err := s.Store(customWriter, rq, requestCc, cachedKey)
Expand Down
2 changes: 1 addition & 1 deletion plugins/beego/souin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_SouinBeegoPlugin_Middleware(t *testing.T) {
req.Header = http.Header{}
web.BeeApp.Handlers.ServeHTTP(res, req)

if res.Result().Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-example.com-/handled; detail=DEFAULT" {
if res.Result().Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-example.com-/handled" {
t.Error("The response must contain a Cache-Status header with the stored directive.")
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/gin/souin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_SouinGinPlugin_Process(t *testing.T) {
}

r.ServeHTTP(res2, c.Request)
if res2.Result().Header.Get("Cache-Status") != "Souin; hit; ttl=4; key=GET-http-example.com-/handled; detail=DEFAULTh" {
if res2.Result().Header.Get("Cache-Status") != "Souin; hit; ttl=4; key=GET-http-example.com-/handled; detail=DEFAULT" {
t.Error("The response must contain a Cache-Status header with the hit and ttl directives.")
}
if res2.Result().Header.Get("Age") != "1" {
Expand Down
8 changes: 4 additions & 4 deletions plugins/traefik/override/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n

if response != nil && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) {
if validator.ResponseETag != "" && validator.Matched {
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, "DEFAULT")
customWriter.Headers = response.Header
if validator.NotModified {
customWriter.statusCode = http.StatusNotModified
Expand Down Expand Up @@ -440,7 +440,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n

return err
}
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, "DEFAULT")
if !modeContext.Strict || rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
customWriter.Headers = response.Header
customWriter.statusCode = response.StatusCode
Expand All @@ -458,7 +458,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n
}
if nil != response && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, "DEFAULT")

responseCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control"))
if responseCc.StaleWhileRevalidate > 0 {
Expand Down Expand Up @@ -502,7 +502,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n

if customWriter.statusCode == http.StatusNotModified {
if !validator.Matched {
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, "DEFAULT")
customWriter.statusCode = response.StatusCode
customWriter.Headers = response.Header
_, _ = io.Copy(customWriter.Buf, response.Body)
Expand Down

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.

6 changes: 4 additions & 2 deletions plugins/tyk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {
defer s.bufPool.Put(bufPool)
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
var storerName string
var fresh, stale *http.Response
finalKey := cachedKey
if rq.Context().Value(context.Hashed).(bool) {
Expand All @@ -199,14 +200,15 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {
fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator)

if fresh != nil || stale != nil {
storerName = currentStorer.Name()
fmt.Printf("Found at least one valid response in the %s storage\n", currentStorer.Name())
break
}
}

if fresh != nil && (!modeContext.Strict || rfc.ValidateCacheControl(fresh, requestCc)) {
response := fresh
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, storerName)
if rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
for hn, hv := range response.Header {
rw.Header().Set(hn, strings.Join(hv, ", "))
Expand All @@ -220,7 +222,7 @@ func SouinRequestHandler(rw http.ResponseWriter, baseRq *http.Request) {

if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, storerName)

responseCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control"))
if responseCc.StaleIfError > 0 {
Expand Down
6 changes: 4 additions & 2 deletions plugins/tyk/override/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,15 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques
customWriter := NewCustomWriter(rq, rw, bufPool)
if !requestCc.NoCache {
validator := rfc.ParseRequest(rq)
var storerName string
var response *http.Response
var fresh, stale *http.Response
finalKey := cachedKey
if rq.Context().Value(context.Hashed).(bool) {
finalKey = fmt.Sprint(xxhash.Sum64String(finalKey))
}
for _, currentStorer := range s.Storers {
storerName = currentStorer.Name()
fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator)

if fresh != nil || stale != nil {
Expand All @@ -289,7 +291,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques

if rfc.ValidateCacheControl(response, requestCc) {
response := fresh
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, storerName)
if rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil {
customWriter.Headers = response.Header
customWriter.statusCode = response.StatusCode
Expand All @@ -303,7 +305,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques

if nil != response && rfc.ValidateCacheControl(response, requestCc) {
addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader))
rfc.SetCacheStatusHeader(response)
rfc.SetCacheStatusHeader(response, storerName)

responseCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control"))
if responseCc.StaleWhileRevalidate > 0 {
Expand Down

0 comments on commit 04f5fb6

Please sign in to comment.