Something went wrong. Try again.
forked from darkweak/souin
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677package context
import ( "context" "net/http" "net/http/httptest" "testing" "time"
"github.com/darkweak/souin/configurationtypes" "go.uber.org/zap")
func Test_TimeoutContext_SetupContext(t *testing.T) { dc := configurationtypes.DefaultCache{} c := testConfiguration{ defaultCache: &dc, } c.SetLogger(zap.NewNop().Sugar()) ctx := timeoutContext{}
ctx.SetupContext(&c) if ctx.timeoutBackend != defaultTimeoutBackend { t.Error("The timeout backend must be equal to the default timeout backend when no directives are given.") } if ctx.timeoutCache != defaultTimeoutCache { 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} ctx.SetupContext(&c) if ctx.timeoutBackend != time.Second { t.Error("The timeout backend must be equal to one second when a directive is given.") } if ctx.timeoutCache != defaultTimeoutCache { 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} ctx.SetupContext(&c) if ctx.timeoutBackend != defaultTimeoutBackend { t.Error("The timeout backend must be equal to 10 seconds when no directives are given.") } if ctx.timeoutCache != time.Second { t.Error("The timeout cache must be equal to one second when a directive is given.") }}
func Test_TimeoutContext_SetContext(t *testing.T) { dc := configurationtypes.DefaultCache{ Timeout: configurationtypes.Timeout{ Backend: configurationtypes.Duration{ Duration: time.Second, }, Cache: configurationtypes.Duration{ Duration: time.Millisecond, }, }, } c := testConfiguration{ defaultCache: &dc, } c.SetLogger(zap.NewNop().Sugar()) ctx := timeoutContext{} ctx.SetupContext(&c)
req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil) req = ctx.SetContext(req) if req.Context().Value(TimeoutCache).(time.Duration) != time.Millisecond { t.Error("The TimeoutCache context must be set in the request.") } if req.Context().Value(TimeoutCancel).(context.CancelFunc) == nil { t.Error("The TimeoutCancel context must be set and be a CancelFunc.") }}