Skip to content

Commit

Permalink
cache middleware: adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Feb 6, 2024
1 parent c29b0c8 commit b980d9e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 57 deletions.
1 change: 0 additions & 1 deletion deserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func TestConvertSQLNullString(t *testing.T) {
v := convertSQLNullString("test")
require.Equal(t, "test", v.Interface().(sql.NullString).String)
})

}

func TestConvertSQLNullBool(t *testing.T) {
Expand Down
205 changes: 149 additions & 56 deletions middleware/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,86 +23,179 @@ func baseController(ctx *fuego.ContextNoBody) (testStruct, error) {
}

func TestCache(t *testing.T) {
s := fuego.NewServer()
t.Run("cache with several configs panics", func(t *testing.T) {
s := fuego.NewServer()

fuego.Get(s, "/without-cache", baseController)
require.Panics(t, func() {
fuego.Use(s, New(Config{}, Config{}))
})
})

fuego.Use(s, New(Config{}))
t.Run("cache with custom Key", func(t *testing.T) {
s := fuego.NewServer()
fuego.Use(s, New(Config{
Key: func(r *http.Request) string { return "custom_key" },
}))
fuego.Get(s, "/with-cache", baseController)
})

fuego.Get(s, "/with-cache", baseController)
fuego.Post(s, "/cant-be-cached-because-not-get", baseController)
t.Run("cache with base config", func(t *testing.T) {
s := fuego.NewServer()

t.Run("Answer once", func(t *testing.T) {
r := httptest.NewRequest("GET", "/without-cache", nil)
w := httptest.NewRecorder()
fuego.Get(s, "/without-cache", baseController)

s.Mux.ServeHTTP(w, r)
fuego.Use(s, New(Config{}))

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})
fuego.Get(s, "/with-cache", baseController)
fuego.Post(s, "/cant-be-cached-because-not-get", baseController)

t.Run("Answer twice without cache", func(t *testing.T) {
r := httptest.NewRequest("GET", "/without-cache", nil)
w := httptest.NewRecorder()
t.Run("Answer once", func(t *testing.T) {
r := httptest.NewRequest("GET", "/without-cache", nil)
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)
s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})

r = httptest.NewRequest("GET", "/without-cache", nil)
w = httptest.NewRecorder()
t.Run("Answer twice without cache", func(t *testing.T) {
r := httptest.NewRequest("GET", "/without-cache", nil)
w := httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)
s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

t.Run("Answer twice the same result with cache", func(t *testing.T) {
r := httptest.NewRequest("GET", "/with-cache", nil)
w := httptest.NewRecorder()
r = httptest.NewRequest("GET", "/without-cache", nil)
w = httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)
start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})

r = httptest.NewRequest("GET", "/with-cache", nil)
w = httptest.NewRecorder()
t.Run("Do not use cache when Cache-Control: no-cache", func(t *testing.T) {
r := httptest.NewRequest("GET", "/with-cache", nil)
w := httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed < waitTime)
s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

// The response is stored but we will not use it
r = httptest.NewRequest("GET", "/with-cache", nil)
r.Header.Set("Cache-Control", "no-cache")
w = httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)

t.Run("Cannot cache non GET requests", func(t *testing.T) {
r := httptest.NewRequest("POST", "/cant-be-cached-because-not-get", nil)
w := httptest.NewRecorder()
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})

s.Mux.ServeHTTP(w, r)
t.Run("Do not store to cache when Cache-Control: no-store", func(t *testing.T) {
s := fuego.NewServer()
fuego.Get(s, "/with-cache", baseController, New(Config{}))

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
r := httptest.NewRequest("GET", "/with-cache", nil)
r.Header.Set("Cache-Control", "no-store") // The response will not be stored
w := httptest.NewRecorder()

r = httptest.NewRequest("POST", "/cant-be-cached-because-not-get", nil)
w = httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

// The response is not stored, so the answer is slow
r = httptest.NewRequest("GET", "/with-cache", nil)
r.Header.Set("Cache-Control", "no-store")
w = httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

// Lets's try again without the no-store header
r = httptest.NewRequest("GET", "/with-cache", nil)
w = httptest.NewRecorder()

start = time.Now()
s.Mux.ServeHTTP(w, r)
elapsed = time.Since(start)
require.True(t, elapsed >= waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

// The response is stored, so the answer will be fast event with the no-store header
r = httptest.NewRequest("GET", "/with-cache", nil)
r.Header.Set("Cache-Control", "no-store")
w = httptest.NewRecorder()

start = time.Now()
s.Mux.ServeHTTP(w, r)
elapsed = time.Since(start)
require.True(t, elapsed < waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})

t.Run("Answer twice the same result with cache", func(t *testing.T) {
r := httptest.NewRequest("GET", "/with-cache", nil)
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

r = httptest.NewRequest("GET", "/with-cache", nil)
w = httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed < waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})

t.Run("Cannot cache non GET requests", func(t *testing.T) {
r := httptest.NewRequest("POST", "/cant-be-cached-because-not-get", nil)
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")

r = httptest.NewRequest("POST", "/cant-be-cached-because-not-get", nil)
w = httptest.NewRecorder()

start := time.Now()
s.Mux.ServeHTTP(w, r)
elapsed := time.Since(start)
require.True(t, elapsed >= waitTime)

require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), `{"Name":"test","Age":10}`+"\n")
})
})
}

Expand Down

0 comments on commit b980d9e

Please sign in to comment.