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

Function runTest

cache/cache_test.go:35–94  ·  view source on GitHub ↗
(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBodyStr string, nocache string)

Source from the content-addressed store, hash-verified

33}
34
35func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBodyStr string, nocache string) error {
36 e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
37 time.Sleep(cacheDuration / 5) // lets wait for a while, cache should be saved and ready
38 e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
39 counter := atomic.LoadUint32(counterPtr)
40 if counter > 1 {
41 // n should be 1 because it doesn't changed after the first call
42 return &testError{1, counter}
43 }
44 time.Sleep(cacheDuration)
45
46 // cache should be cleared now
47 e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
48 time.Sleep(cacheDuration / 5)
49 // let's call again , the cache should be saved
50 e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
51 counter = atomic.LoadUint32(counterPtr)
52 if counter != 2 {
53 return &testError{2, counter}
54 }
55
56 // we have cache response saved for the path, we have some time more here, but here
57 // we will make the requestS with some of the deniers options
58 e.GET(path).WithHeader("max-age", "0").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
59 e.GET(path).WithHeader("Authorization", "basic or anything").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
60 counter = atomic.LoadUint32(counterPtr)
61 if counter != 4 {
62 return &testError{4, counter}
63 }
64
65 if nocache != "" {
66 // test the NoCache, first sleep to pass the cache expiration,
67 // second add to the cache with a valid request and response
68 // third, do it with the "/nocache" path (static for now, pure test design) given by the consumer
69 time.Sleep(cacheDuration)
70
71 // cache should be cleared now, this should work because we are not in the "nocache" path
72 e.GET("/").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 5
73 time.Sleep(cacheDuration / 5)
74
75 // let's call the "nocache", the expiration is not passed so but the "nocache"
76 // route's path has the cache.NoCache so it should be not cached and the counter should be ++
77 e.GET(nocache).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter should be 6
78 counter = atomic.LoadUint32(counterPtr)
79 if counter != 6 { // 4 before, 5 with the first call to store the cache, and six with the no cache, again original handler executation
80 return &testError{6, counter}
81 }
82
83 // let's call again the path the expiration is not passed so it should be cached
84 e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
85 counter = atomic.LoadUint32(counterPtr)
86 if counter != 6 {
87 return &testError{6, counter}
88 }
89
90 // but now check for the No
91 }
92

Callers 2

TestClientNoCacheFunction · 0.85
TestCacheFunction · 0.85

Calls 2

StatusMethod · 0.80
BodyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…