1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

fix #2158 and more

This commit is contained in:
Gerasimos (Makis) Maropoulos
2023-07-08 02:08:18 +03:00
parent 757e7fe61b
commit 6add1ba49b
79 changed files with 551 additions and 467 deletions

16
cache/browser_test.go vendored
View File

@@ -22,7 +22,7 @@ func TestNoCache(t *testing.T) {
e := httptest.New(t, app)
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("no_cache")
r.Body().IsEqual("no_cache")
r.Header(context.CacheControlHeaderKey).Equal(cache.CacheControlHeaderValue)
r.Header(cache.PragmaHeaderKey).Equal(cache.PragmaNoCacheHeaderValue)
r.Header(cache.ExpiresHeaderKey).Equal(cache.ExpiresNeverHeaderValue)
@@ -42,7 +42,7 @@ func TestStaticCache(t *testing.T) {
// tests
e := httptest.New(t, app)
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("static_cache")
r.Body().IsEqual("static_cache")
r.Header(cache.ExpiresHeaderKey).Equal(expectedTime.Add(cacheDur).Format(app.ConfigurationReadOnly().GetTimeFormat()))
cacheControlHeaderValue := "public, max-age=" + strconv.Itoa(int(cacheDur.Seconds()))
@@ -64,15 +64,15 @@ func TestCache304(t *testing.T) {
insideCacheTimef := time.Now().Add(-expiresEvery).UTC().Format(app.ConfigurationReadOnly().GetTimeFormat())
r := e.GET("/").WithHeader(context.IfModifiedSinceHeaderKey, insideCacheTimef).Expect().Status(httptest.StatusNotModified)
r.Headers().NotContainsKey(context.ContentTypeHeaderKey).NotContainsKey(context.ContentLengthHeaderKey).NotContainsKey("ETag")
r.Body().Equal("")
r.Body().IsEqual("")
// continue to the handler itself.
cacheInvalidatedTimef := time.Now().Add(expiresEvery).UTC().Format(app.ConfigurationReadOnly().GetTimeFormat()) // after ~5seconds.
r = e.GET("/").WithHeader(context.LastModifiedHeaderKey, cacheInvalidatedTimef).Expect().Status(httptest.StatusOK)
r.Body().Equal("send")
r.Body().IsEqual("send")
// now without header, it should continue to the handler itself as well.
r = e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("send")
r.Body().IsEqual("send")
}
func TestETag(t *testing.T) {
@@ -91,12 +91,12 @@ func TestETag(t *testing.T) {
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Header("ETag").Equal("/") // test if header set.
r.Body().Equal("_")
r.Body().IsEqual("_")
e.GET("/").WithHeader("ETag", "/").WithHeader("If-None-Match", "/").Expect().
Status(httptest.StatusNotModified).Body().Equal("") // browser is responsible, no the test engine.
Status(httptest.StatusNotModified).Body().IsEqual("") // browser is responsible, no the test engine.
r = e.GET("/").Expect().Status(httptest.StatusOK)
r.Header("ETag").Equal("/") // test if header set.
r.Body().Equal("__")
r.Body().IsEqual("__")
}

26
cache/cache_test.go vendored
View File

@@ -33,9 +33,9 @@ func (h *testError) Error() string {
}
func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBodyStr string, nocache string) error {
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
time.Sleep(cacheDuration / 5) // lets wait for a while, cache should be saved and ready
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
counter := atomic.LoadUint32(counterPtr)
if counter > 1 {
// n should be 1 because it doesn't changed after the first call
@@ -44,10 +44,10 @@ func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBody
time.Sleep(cacheDuration)
// cache should be cleared now
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
time.Sleep(cacheDuration / 5)
// let's call again , the cache should be saved
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
counter = atomic.LoadUint32(counterPtr)
if counter != 2 {
return &testError{2, counter}
@@ -55,8 +55,8 @@ func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBody
// we have cache response saved for the path, we have some time more here, but here
// we will make the requestS with some of the deniers options
e.GET(path).WithHeader("max-age", "0").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).WithHeader("Authorization", "basic or anything").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).WithHeader("max-age", "0").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
e.GET(path).WithHeader("Authorization", "basic or anything").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
counter = atomic.LoadUint32(counterPtr)
if counter != 4 {
return &testError{4, counter}
@@ -69,19 +69,19 @@ func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBody
time.Sleep(cacheDuration)
// cache should be cleared now, this should work because we are not in the "nocache" path
e.GET("/").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr) // counter = 5
e.GET("/").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 5
time.Sleep(cacheDuration / 5)
// let's call the "nocache", the expiration is not passed so but the "nocache"
// route's path has the cache.NoCache so it should be not cached and the counter should be ++
e.GET(nocache).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr) // counter should be 6
e.GET(nocache).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter should be 6
counter = atomic.LoadUint32(counterPtr)
if counter != 6 { // 4 before, 5 with the first call to store the cache, and six with the no cache, again original handler executation
return &testError{6, counter}
}
// let's call again the path the expiration is not passed so it should be cached
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET(path).Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
counter = atomic.LoadUint32(counterPtr)
if counter != 6 {
return &testError{6, counter}
@@ -195,17 +195,17 @@ func TestCacheValidator(t *testing.T) {
e := httptest.New(t, app)
// execute from cache the next time
e.GET("/valid").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
time.Sleep(cacheDuration / 5) // lets wait for a while, cache should be saved and ready
e.GET("/valid").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
e.GET("/valid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr)
counter := atomic.LoadUint32(&n)
if counter > 1 {
// n should be 1 because it doesn't changed after the first call
t.Fatalf("%s: %v", t.Name(), &testError{1, counter})
}
// don't execute from cache, execute the original, counter should ++ here
e.GET("/invalid").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr) // counter = 2
e.GET("/invalid2").Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr) // counter = 3
e.GET("/invalid").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 2
e.GET("/invalid2").Expect().Status(http.StatusOK).Body().IsEqual(expectedBodyStr) // counter = 3
counter = atomic.LoadUint32(&n)
if counter != 3 {