Skip to content

Commit

Permalink
feat(chore): storages externalization refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Jun 23, 2024
1 parent 40af3c2 commit 7b06c36
Show file tree
Hide file tree
Showing 964 changed files with 1,815 additions and 278,785 deletions.
2 changes: 2 additions & 0 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ type URL struct {

// CacheProvider config
type CacheProvider struct {
// Found to determine if we can use that storage.
Found bool `json:"found" yaml:"found"`
// URL to connect to the storage system.
URL string `json:"url" yaml:"url"`
// Path to the configuration file.
Expand Down
44 changes: 40 additions & 4 deletions context/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,50 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

type testConfiguration struct {
urls map[string]configurationtypes.URL
defaultCache *configurationtypes.DefaultCache
cacheKeys configurationtypes.CacheKeys
}

func (*testConfiguration) GetUrls() map[string]configurationtypes.URL {
return nil
}
func (*testConfiguration) GetPluginName() string {
return ""
}
func (t *testConfiguration) GetDefaultCache() configurationtypes.DefaultCacheInterface {
return t.defaultCache
}
func (*testConfiguration) GetAPI() configurationtypes.API {
return configurationtypes.API{}
}
func (*testConfiguration) GetLogLevel() string {
return ""
}
func (*testConfiguration) GetLogger() *zap.Logger {
return zap.NewNop()
}
func (*testConfiguration) SetLogger(*zap.Logger) {
return
}
func (*testConfiguration) GetYkeys() map[string]configurationtypes.SurrogateKeys {
return nil
}
func (*testConfiguration) GetSurrogateKeys() map[string]configurationtypes.SurrogateKeys {
return nil
}
func (t *testConfiguration) GetCacheKeys() configurationtypes.CacheKeys {
return t.cacheKeys
}

func Test_CacheContext_SetupContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := cacheContext{}
Expand All @@ -23,7 +59,7 @@ func Test_CacheContext_SetupContext(t *testing.T) {
t.Error("The context must be equal to Souin.")
}

c.DefaultCache.CacheName = "Something"
c.defaultCache.CacheName = "Something"
ctx.SetupContext(&c)
if ctx.cacheName != "Something" {
t.Error("The context must be equal to Something.")
Expand Down
12 changes: 5 additions & 7 deletions context/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

func Test_GraphQLContext_SetupContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := graphQLContext{}

ctx.SetupContext(&c)
if ctx.custom {
t.Error("The context must not be custom if no allowed HTTP verbs are set in the configuration.")
}

c.DefaultCache.AllowedHTTPVerbs = []string{http.MethodGet}
c.defaultCache.AllowedHTTPVerbs = []string{http.MethodGet}
ctx.SetupContext(&c)
if !ctx.custom {
t.Error("The context must be custom if at least one allowed HTTP verb is set in the configuration.")
Expand All @@ -33,8 +31,8 @@ func Test_GraphQLContext_SetupContext(t *testing.T) {

func Test_GraphQLContext_SetContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := graphQLContext{custom: true}
Expand Down
11 changes: 5 additions & 6 deletions context/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (

"github.com/caddyserver/caddy/v2"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
)

func Test_KeyContext_SetupContext(t *testing.T) {
ctx := keyContext{}
ctx.SetupContext(&configuration.Configuration{
DefaultCache: &configurationtypes.DefaultCache{
ctx.SetupContext(&testConfiguration{
defaultCache: &configurationtypes.DefaultCache{
Key: configurationtypes.Key{},
},
})
Expand All @@ -37,14 +36,14 @@ func Test_KeyContext_SetupContext(t *testing.T) {
},
},
}
ctx.SetupContext(&configuration.Configuration{
DefaultCache: &configurationtypes.DefaultCache{
ctx.SetupContext(&testConfiguration{
defaultCache: &configurationtypes.DefaultCache{
Key: configurationtypes.Key{
DisableHost: true,
DisableMethod: true,
},
},
CacheKeys: m,
cacheKeys: m,
})

if !ctx.disable_host {
Expand Down
13 changes: 6 additions & 7 deletions context/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

func Test_MethodContext_SetupContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := methodContext{}
Expand All @@ -23,7 +22,7 @@ func Test_MethodContext_SetupContext(t *testing.T) {
t.Error("The context must not be custom if no allowed HTTP verbs are set in the configuration.")
}

c.DefaultCache.AllowedHTTPVerbs = []string{http.MethodGet}
c.defaultCache.AllowedHTTPVerbs = []string{http.MethodGet}
ctx.SetupContext(&c)
if !ctx.custom {
t.Error("The context must be custom if at least one allowed HTTP verb is set in the configuration.")
Expand All @@ -32,12 +31,12 @@ func Test_MethodContext_SetupContext(t *testing.T) {

func Test_MethodContext_SetContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := methodContext{}
c.DefaultCache.AllowedHTTPVerbs = []string{http.MethodGet, http.MethodHead}
c.defaultCache.AllowedHTTPVerbs = []string{http.MethodGet, http.MethodHead}
ctx.SetupContext(&c)

req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil)
Expand Down
15 changes: 7 additions & 8 deletions context/mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

func Test_ModeContext_SetupContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := ModeContext{}
Expand All @@ -21,35 +20,35 @@ func Test_ModeContext_SetupContext(t *testing.T) {
t.Error("The context must be strict and must not bypass either response or request.")
}

c.DefaultCache.Mode = "bypass"
c.defaultCache.Mode = "bypass"
ctx = ModeContext{}
ctx.SetupContext(&c)
if !ctx.Bypass_request || !ctx.Bypass_response || ctx.Strict {
t.Error("The context must bypass either response and request.")
}

c.DefaultCache.Mode = "bypass_request"
c.defaultCache.Mode = "bypass_request"
ctx = ModeContext{}
ctx.SetupContext(&c)
if !ctx.Bypass_request || ctx.Bypass_response || ctx.Strict {
t.Error("The context must bypass request only.")
}

c.DefaultCache.Mode = "bypass_response"
c.defaultCache.Mode = "bypass_response"
ctx = ModeContext{}
ctx.SetupContext(&c)
if ctx.Bypass_request || !ctx.Bypass_response || ctx.Strict {
t.Error("The context must bypass response only.")
}

c.DefaultCache.Mode = "strict"
c.defaultCache.Mode = "strict"
ctx = ModeContext{}
ctx.SetupContext(&c)
if ctx.Bypass_request || ctx.Bypass_response || !ctx.Strict {
t.Error("The context must be strict.")
}

c.DefaultCache.Mode = "default_value"
c.defaultCache.Mode = "default_value"
ctx = ModeContext{}
ctx.SetupContext(&c)
if ctx.Bypass_request || ctx.Bypass_response || !ctx.Strict {
Expand Down
15 changes: 7 additions & 8 deletions context/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"time"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

func Test_TimeoutContext_SetupContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := timeoutContext{}
Expand All @@ -28,7 +27,7 @@ func Test_TimeoutContext_SetupContext(t *testing.T) {
t.Error("The timeout cache must be equal to the default timeout cache when no directives are given.")
}

c.DefaultCache.Timeout.Backend = configurationtypes.Duration{Duration: time.Second}
c.defaultCache.Timeout.Backend = configurationtypes.Duration{Duration: time.Second}
ctx.SetupContext(&c)
if ctx.timeoutBackend != time.Second {
t.Error("The timeout backend must be equal to one second when a directive is given.")
Expand All @@ -37,8 +36,8 @@ func Test_TimeoutContext_SetupContext(t *testing.T) {
t.Error("The timeout cache must be equal to the default timeout cache when no directives are given.")
}

c.DefaultCache.Timeout = configurationtypes.Timeout{}
c.DefaultCache.Timeout.Cache = configurationtypes.Duration{Duration: time.Second}
c.defaultCache.Timeout = configurationtypes.Timeout{}
c.defaultCache.Timeout.Cache = configurationtypes.Duration{Duration: time.Second}
ctx.SetupContext(&c)
if ctx.timeoutBackend != defaultTimeoutBackend {
t.Error("The timeout backend must be equal to 10 seconds when no directives are given.")
Expand All @@ -59,8 +58,8 @@ func Test_TimeoutContext_SetContext(t *testing.T) {
},
},
}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
ctx := timeoutContext{}
Expand Down
13 changes: 6 additions & 7 deletions context/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
)

Expand All @@ -18,8 +17,8 @@ func Test_GetContext(t *testing.T) {

func Test_Context_Init(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
co := GetContext()
Expand All @@ -29,8 +28,8 @@ func Test_Context_Init(t *testing.T) {

func Test_Context_SetContext(t *testing.T) {
dc := configurationtypes.DefaultCache{}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
co := GetContext()
Expand All @@ -56,8 +55,8 @@ func Test_Context_SetBaseContext(t *testing.T) {
dc := configurationtypes.DefaultCache{
CacheName: "Dummy",
}
c := configuration.Configuration{
DefaultCache: &dc,
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
co := GetContext()
Expand Down
Loading

0 comments on commit 7b06c36

Please sign in to comment.