You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

browser_test.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package cache_test
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. "github.com/kataras/iris/v12/cache"
  7. "github.com/kataras/iris/v12"
  8. "github.com/kataras/iris/v12/context"
  9. "github.com/kataras/iris/v12/httptest"
  10. )
  11. func TestNoCache(t *testing.T) {
  12. app := iris.New()
  13. app.Get("/", cache.NoCache, func(ctx iris.Context) {
  14. ctx.WriteString("no_cache")
  15. })
  16. // tests
  17. e := httptest.New(t, app)
  18. r := e.GET("/").Expect().Status(httptest.StatusOK)
  19. r.Body().Equal("no_cache")
  20. r.Header(context.CacheControlHeaderKey).Equal(cache.CacheControlHeaderValue)
  21. r.Header(cache.PragmaHeaderKey).Equal(cache.PragmaNoCacheHeaderValue)
  22. r.Header(cache.ExpiresHeaderKey).Equal(cache.ExpiresNeverHeaderValue)
  23. }
  24. func TestStaticCache(t *testing.T) {
  25. // test change the time format, which is not recommended but can be done.
  26. app := iris.New().Configure(iris.WithTimeFormat("02 Jan 2006 15:04:05 GMT"))
  27. cacheDur := 30 * (24 * time.Hour)
  28. var expectedTime time.Time
  29. app.Get("/", cache.StaticCache(cacheDur), func(ctx iris.Context) {
  30. expectedTime = time.Now()
  31. ctx.WriteString("static_cache")
  32. })
  33. // tests
  34. e := httptest.New(t, app)
  35. r := e.GET("/").Expect().Status(httptest.StatusOK)
  36. r.Body().Equal("static_cache")
  37. r.Header(cache.ExpiresHeaderKey).Equal(expectedTime.Add(cacheDur).Format(app.ConfigurationReadOnly().GetTimeFormat()))
  38. cacheControlHeaderValue := "public, max-age=" + strconv.Itoa(int(cacheDur.Seconds()))
  39. r.Header(context.CacheControlHeaderKey).Equal(cacheControlHeaderValue)
  40. }
  41. func TestCache304(t *testing.T) {
  42. // t.Parallel()
  43. app := iris.New()
  44. expiresEvery := 4 * time.Second
  45. app.Get("/", cache.Cache304(expiresEvery), func(ctx iris.Context) {
  46. ctx.WriteString("send")
  47. })
  48. // handlers
  49. e := httptest.New(t, app)
  50. // when 304, content type, content length and if ETagg is there are removed from the headers.
  51. insideCacheTimef := time.Now().Add(-expiresEvery).UTC().Format(app.ConfigurationReadOnly().GetTimeFormat())
  52. r := e.GET("/").WithHeader(context.IfModifiedSinceHeaderKey, insideCacheTimef).Expect().Status(httptest.StatusNotModified)
  53. r.Headers().NotContainsKey(context.ContentTypeHeaderKey).NotContainsKey(context.ContentLengthHeaderKey).NotContainsKey("ETag")
  54. r.Body().Equal("")
  55. // continue to the handler itself.
  56. cacheInvalidatedTimef := time.Now().Add(expiresEvery).UTC().Format(app.ConfigurationReadOnly().GetTimeFormat()) // after ~5seconds.
  57. r = e.GET("/").WithHeader(context.LastModifiedHeaderKey, cacheInvalidatedTimef).Expect().Status(httptest.StatusOK)
  58. r.Body().Equal("send")
  59. // now without header, it should continue to the handler itself as well.
  60. r = e.GET("/").Expect().Status(httptest.StatusOK)
  61. r.Body().Equal("send")
  62. }
  63. func TestETag(t *testing.T) {
  64. // t.Parallel()
  65. app := iris.New()
  66. n := "_"
  67. app.Get("/", cache.ETag, func(ctx iris.Context) {
  68. ctx.WriteString(n)
  69. n += "_"
  70. })
  71. // the first and last test writes the content with status OK without cache,
  72. // the rest tests the cache headers and status 304 and return, so body should be "".
  73. e := httptest.New(t, app)
  74. r := e.GET("/").Expect().Status(httptest.StatusOK)
  75. r.Header("ETag").Equal("/") // test if header set.
  76. r.Body().Equal("_")
  77. e.GET("/").WithHeader("ETag", "/").WithHeader("If-None-Match", "/").Expect().
  78. Status(httptest.StatusNotModified).Body().Equal("") // browser is responsible, no the test engine.
  79. r = e.GET("/").Expect().Status(httptest.StatusOK)
  80. r.Header("ETag").Equal("/") // test if header set.
  81. r.Body().Equal("__")
  82. }