1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 04:51:56 +00:00

add the stale release

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-12 23:41:20 +03:00
parent 535fb6dc0d
commit 2a4ce876b6
793 changed files with 188 additions and 52415 deletions

6
cache/browser.go vendored
View File

@@ -4,8 +4,8 @@ import (
"strconv"
"time"
"github.com/kataras/iris/v12/cache/client"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/cache/client"
"github.com/kataras/iris/context"
)
// CacheControlHeaderValue is the header value of the
@@ -111,7 +111,7 @@ var ETag = func(ctx *context.Context) {
// Cache304 sends a `StatusNotModified` (304) whenever
// the "If-Modified-Since" request header (time) is before the
// time.Now() + expiresEvery (always compared to their UTC values).
// Use this `cache#Cache304` instead of the "github.com/kataras/iris/v12/cache" or iris.Cache
// Use this `cache#Cache304` instead of the "github.com/kataras/iris/cache" or iris.Cache
// for better performance.
// Clients that are compatible with the http RCF (all browsers are and tools like postman)
// will handle the caching.

102
cache/browser_test.go vendored
View File

@@ -1,102 +0,0 @@
package cache_test
import (
"strconv"
"testing"
"time"
"github.com/kataras/iris/v12/cache"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/httptest"
)
func TestNoCache(t *testing.T) {
app := iris.New()
app.Get("/", cache.NoCache, func(ctx iris.Context) {
ctx.WriteString("no_cache")
})
// tests
e := httptest.New(t, app)
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("no_cache")
r.Header(context.CacheControlHeaderKey).Equal(cache.CacheControlHeaderValue)
r.Header(cache.PragmaHeaderKey).Equal(cache.PragmaNoCacheHeaderValue)
r.Header(cache.ExpiresHeaderKey).Equal(cache.ExpiresNeverHeaderValue)
}
func TestStaticCache(t *testing.T) {
// test change the time format, which is not recommended but can be done.
app := iris.New().Configure(iris.WithTimeFormat("02 Jan 2006 15:04:05 GMT"))
cacheDur := 30 * (24 * time.Hour)
var expectedTime time.Time
app.Get("/", cache.StaticCache(cacheDur), func(ctx iris.Context) {
expectedTime = time.Now()
ctx.WriteString("static_cache")
})
// tests
e := httptest.New(t, app)
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("static_cache")
r.Header(cache.ExpiresHeaderKey).Equal(expectedTime.Add(cacheDur).Format(app.ConfigurationReadOnly().GetTimeFormat()))
cacheControlHeaderValue := "public, max-age=" + strconv.Itoa(int(cacheDur.Seconds()))
r.Header(context.CacheControlHeaderKey).Equal(cacheControlHeaderValue)
}
func TestCache304(t *testing.T) {
// t.Parallel()
app := iris.New()
expiresEvery := 4 * time.Second
app.Get("/", cache.Cache304(expiresEvery), func(ctx iris.Context) {
ctx.WriteString("send")
})
// handlers
e := httptest.New(t, app)
// when 304, content type, content length and if ETagg is there are removed from the headers.
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("")
// 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")
// now without header, it should continue to the handler itself as well.
r = e.GET("/").Expect().Status(httptest.StatusOK)
r.Body().Equal("send")
}
func TestETag(t *testing.T) {
// t.Parallel()
app := iris.New()
n := "_"
app.Get("/", cache.ETag, func(ctx iris.Context) {
ctx.WriteString(n)
n += "_"
})
// the first and last test writes the content with status OK without cache,
// the rest tests the cache headers and status 304 and return, so body should be "".
e := httptest.New(t, app)
r := e.GET("/").Expect().Status(httptest.StatusOK)
r.Header("ETag").Equal("/") // test if header set.
r.Body().Equal("_")
e.GET("/").WithHeader("ETag", "/").WithHeader("If-None-Match", "/").Expect().
Status(httptest.StatusNotModified).Body().Equal("") // 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("__")
}

8
cache/cache.go vendored
View File

@@ -9,8 +9,8 @@ Example code:
import (
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/cache"
"github.com/kataras/iris"
"github.com/kataras/iris/cache"
)
func main(){
@@ -30,8 +30,8 @@ package cache
import (
"time"
"github.com/kataras/iris/v12/cache/client"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/cache/client"
"github.com/kataras/iris/context"
)
// Cache accepts the cache expiration duration.

215
cache/cache_test.go vendored
View File

@@ -1,215 +0,0 @@
package cache_test
import (
"fmt"
"net/http"
"sync/atomic"
"testing"
"time"
"github.com/kataras/iris/v12/cache"
"github.com/kataras/iris/v12/cache/client"
"github.com/kataras/iris/v12/cache/client/rule"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/iris-contrib/httpexpect/v2"
"github.com/kataras/iris/v12/httptest"
)
var (
cacheDuration = 2 * time.Second
expectedBodyStr = "Imagine it as a big message to achieve x20 response performance!"
)
type testError struct {
expected int
got uint32
}
func (h *testError) Error() string {
return fmt.Sprintf("expected the main handler to be executed %d times instead of %d", h.expected, h.got)
}
func runTest(e *httpexpect.Expect, path string, counterPtr *uint32, expectedBodyStr string, nocache string) error {
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(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)
counter := atomic.LoadUint32(counterPtr)
if counter > 1 {
// n should be 1 because it doesn't changed after the first call
return &testError{1, counter}
}
time.Sleep(cacheDuration)
// cache should be cleared now
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
time.Sleep(cacheDuration / 5)
// let's call again , the cache should be saved
e.GET(path).Expect().Status(http.StatusOK).Body().Equal(expectedBodyStr)
counter = atomic.LoadUint32(counterPtr)
if counter != 2 {
return &testError{2, counter}
}
// 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)
counter = atomic.LoadUint32(counterPtr)
if counter != 4 {
return &testError{4, counter}
}
if nocache != "" {
// test the NoCache, first sleep to pass the cache expiration,
// second add to the cache with a valid request and response
// third, do it with the "/nocache" path (static for now, pure test design) given by the consumer
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
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
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)
counter = atomic.LoadUint32(counterPtr)
if counter != 6 {
return &testError{6, counter}
}
// but now check for the No
}
return nil
}
func TestClientNoCache(t *testing.T) {
app := iris.New()
var n uint32
app.Get("/", cache.Handler(cacheDuration), func(ctx *context.Context) {
atomic.AddUint32(&n, 1)
ctx.Write([]byte(expectedBodyStr))
})
app.Get("/nocache", cache.Handler(cacheDuration), func(ctx *context.Context) {
client.NoCache(ctx) // <----
atomic.AddUint32(&n, 1)
ctx.Write([]byte(expectedBodyStr))
})
e := httptest.New(t, app)
if err := runTest(e, "/", &n, expectedBodyStr, "/nocache"); err != nil {
t.Fatalf(t.Name()+": %v", err)
}
}
func TestCache(t *testing.T) {
app := iris.New()
var n uint32
app.Use(cache.Handler(cacheDuration))
app.Get("/", func(ctx *context.Context) {
atomic.AddUint32(&n, 1)
ctx.Write([]byte(expectedBodyStr))
})
var (
n2 uint32
expectedBodyStr2 = "This is the other"
)
app.Get("/other", func(ctx *context.Context) {
atomic.AddUint32(&n2, 1)
ctx.Write([]byte(expectedBodyStr2))
})
e := httptest.New(t, app)
if err := runTest(e, "/", &n, expectedBodyStr, ""); err != nil {
t.Fatalf(t.Name()+": %v", err)
}
if err := runTest(e, "/other", &n2, expectedBodyStr2, ""); err != nil {
t.Fatalf(t.Name()+" other: %v", err)
}
}
// 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)
// }
func TestCacheValidator(t *testing.T) {
app := iris.New()
var n uint32
h := func(ctx *context.Context) {
atomic.AddUint32(&n, 1)
ctx.Write([]byte(expectedBodyStr))
}
validCache := cache.Cache(cacheDuration)
app.Get("/", validCache.ServeHTTP, h)
managedCache := cache.Cache(cacheDuration)
managedCache.AddRule(rule.Validator([]rule.PreValidator{
func(ctx *context.Context) bool {
// should always invalid for cache, don't bother to go to try to get or set cache
return ctx.Request().URL.Path != "/invalid"
},
}, nil))
managedCache2 := cache.Cache(cacheDuration)
managedCache2.AddRule(rule.Validator(nil,
[]rule.PostValidator{
func(ctx *context.Context) bool {
// it's passed the Claim and now Valid checks if the response contains a header of "DONT"
return ctx.ResponseWriter().Header().Get("DONT") == ""
},
},
))
app.Get("/valid", validCache.ServeHTTP, h)
app.Get("/invalid", managedCache.ServeHTTP, h)
app.Get("/invalid2", managedCache2.ServeHTTP, func(ctx *context.Context) {
atomic.AddUint32(&n, 1)
ctx.Header("DONT", "DO not cache that response even if it was claimed")
ctx.Write([]byte(expectedBodyStr))
})
e := httptest.New(t, app)
// execute from cache the next time
e.GET("/valid").Expect().Status(http.StatusOK).Body().Equal(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)
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
counter = atomic.LoadUint32(&n)
if counter != 3 {
// n should be 1 because it doesn't changed after the first call
t.Fatalf("%s: %v", t.Name(), &testError{3, counter})
}
}

View File

@@ -6,10 +6,10 @@ import (
"net/http"
"time"
"github.com/kataras/iris/v12/cache/cfg"
"github.com/kataras/iris/v12/cache/client/rule"
"github.com/kataras/iris/v12/cache/uri"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/cache/cfg"
"github.com/kataras/iris/cache/client/rule"
"github.com/kataras/iris/cache/uri"
"github.com/kataras/iris/context"
)
// ClientHandler is the client-side handler

View File

@@ -4,9 +4,9 @@ import (
"sync"
"time"
"github.com/kataras/iris/v12/cache/client/rule"
"github.com/kataras/iris/v12/cache/entry"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/cache/client/rule"
"github.com/kataras/iris/cache/entry"
"github.com/kataras/iris/context"
)
func init() {

View File

@@ -1,7 +1,7 @@
package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/context"
)
// chainedRule is a Rule with next Rule

View File

@@ -1,7 +1,7 @@
package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/context"
)
// Conditional is a Rule witch adds a predicate in order to its methods to execute

View File

@@ -1,9 +1,9 @@
package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/context"
"github.com/kataras/iris/v12/cache/ruleset"
"github.com/kataras/iris/cache/ruleset"
)
// The HeaderPredicate should be alived on each of $package/rule BUT GOLANG DOESN'T SUPPORT type alias and I don't want to have so many copies around

View File

@@ -1,7 +1,7 @@
package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/context"
)
type notSatisfiedRule struct{}

View File

@@ -1,6 +1,6 @@
package rule
import "github.com/kataras/iris/v12/context"
import "github.com/kataras/iris/context"
// Rule a superset of validators
type Rule interface {

View File

@@ -1,7 +1,7 @@
package rule
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/context"
)
type satisfiedRule struct{}

View File

@@ -1,6 +1,6 @@
package rule
import "github.com/kataras/iris/v12/context"
import "github.com/kataras/iris/context"
// Validators are introduced to implement the RFC about cache (https://tools.ietf.org/html/rfc7234#section-1.1).

View File

@@ -1,10 +1,10 @@
package client
import (
"github.com/kataras/iris/v12/cache/cfg"
"github.com/kataras/iris/v12/cache/client/rule"
"github.com/kataras/iris/v12/cache/ruleset"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/cache/cfg"
"github.com/kataras/iris/cache/client/rule"
"github.com/kataras/iris/cache/ruleset"
"github.com/kataras/iris/context"
)
// DefaultRuleSet is a list of the default pre-cache validators

View File

@@ -3,7 +3,7 @@ package entry
import (
"time"
"github.com/kataras/iris/v12/cache/cfg"
"github.com/kataras/iris/cache/cfg"
)
// Entry is the cache entry

View File

@@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/kataras/iris/v12/cache/cfg"
"github.com/kataras/iris/cache/cfg"
)
// URIBuilder is the requested url builder