MCPcopy Index your code
hub / github.com/kataras/iris / TestCacheValidator

Function TestCacheValidator

cache/cache_test.go:156–215  ·  view source on GitHub ↗

This works but we have issue on golog.SetLevel and get golog.Level on httptest.New when tests are running in parallel and the loggers are used. TODO: Fix it on golog repository or here, we'll see. func TestCacheHandlerParallel(t *testing.T) { t.Parallel() TestCache(t) }

(t *testing.T)

Source from the content-addressed store, hash-verified

154// }
155
156func TestCacheValidator(t *testing.T) {
157 app := iris.New()
158 var n uint32
159
160 h := func(ctx *context.Context) {
161 atomic.AddUint32(&n, 1)
162 ctx.Write([]byte(expectedBodyStr))
163 }
164
165 validCache := cache.Handler(cacheDuration)
166 app.Get("/", validCache, h)
167
168 managedCache := cache.Cache(cache.MaxAge(cacheDuration))
169 managedCache.AddRule(rule.Validator([]rule.PreValidator{
170 func(ctx *context.Context) bool {
171 // should always invalid for cache, don't bother to go to try to get or set cache
172 return ctx.Request().URL.Path != "/invalid"
173 },
174 }, nil))
175
176 managedCache2 := cache.Cache(cache.MaxAge(cacheDuration))
177 managedCache2.AddRule(rule.Validator(nil,
178 []rule.PostValidator{
179 func(ctx *context.Context) bool {
180 // it's passed the Claim and now Valid checks if the response contains a header of "DONT"
181 return ctx.ResponseWriter().Header().Get("DONT") == ""
182 },
183 },
184 ))
185
186 app.Get("/valid", validCache, h)
187
188 app.Get("/invalid", managedCache.ServeHTTP, h)
189 app.Get("/invalid2", managedCache2.ServeHTTP, func(ctx *context.Context) {
190 atomic.AddUint32(&n, 1)
191 ctx.Header("DONT", "DO not cache that response even if it was claimed")
192 ctx.Write([]byte(expectedBodyStr))
193 })
194
195 e := httptest.New(t, app)
196
197 // execute from cache the next time
198 e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
199 time.Sleep(cacheDuration / 5) // lets wait for a while, cache should be saved and ready
200 e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
201 counter := atomic.LoadUint32(&n)
202 if counter > 1 {
203 // n should be 1 because it doesn't changed after the first call
204 t.Fatalf("%s: %v", t.Name(), &testError{1, counter})
205 }
206 // don't execute from cache, execute the original, counter should ++ here
207 e.GET("/invalid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 2
208 e.GET("/invalid2").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 3
209
210 counter = atomic.LoadUint32(&n)
211 if counter != 3 {
212 // n should be 1 because it doesn't changed after the first call
213 t.Fatalf("%s: %v", t.Name(), &testError{3, counter})

Callers

nothing calls this directly

Calls 15

HandlerFunction · 0.92
CacheFunction · 0.92
MaxAgeFunction · 0.92
ValidatorFunction · 0.92
NewFunction · 0.92
NewMethod · 0.80
RequestMethod · 0.80
ResponseWriterMethod · 0.80
StatusMethod · 0.80
FatalfMethod · 0.80
GetMethod · 0.65
BodyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…