Skip to content

Commit

Permalink
feat: add test case for WithDiscardHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui committed Nov 17, 2023
1 parent a2f9e2b commit fedf110
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
42 changes: 39 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,55 @@ const prefixKey = "#prefix#"
func TestPrefixKey(t *testing.T) {

memoryStore := persist.NewMemoryStore(1 * time.Minute)
cacheURIMiddleware := CacheByRequestPath(
cachePathMiddleware := CacheByRequestPath(
memoryStore,
3*time.Second,
WithPrefixKey(prefixKey),
)

requestPath := "/cache"

w1 := mockHttpRequest(cacheURIMiddleware, requestPath, true)
w1 := mockHttpRequest(cachePathMiddleware, requestPath, true)

err := memoryStore.Delete(prefixKey + requestPath)
require.NoError(t, err)

w2 := mockHttpRequest(cacheURIMiddleware, requestPath, true)
w2 := mockHttpRequest(cachePathMiddleware, requestPath, true)
assert.NotEqual(t, w1.Body, w2.Body)
}

func TestWithDiscardHeaders(t *testing.T) {
const headerKey = "RandKey"

memoryStore := persist.NewMemoryStore(1 * time.Minute)
cachePathMiddleware := CacheByRequestPath(
memoryStore,
3*time.Second,
WithDiscardHeaders([]string{
headerKey,
}),
)

_, engine := gin.CreateTestContext(httptest.NewRecorder())

engine.GET("/cache", cachePathMiddleware, func(c *gin.Context) {
c.Header(headerKey, fmt.Sprintf("rand:%d", rand.Int()))
c.String(http.StatusOK, "value")
})

testRequest := httptest.NewRequest(http.MethodGet, "/cache", nil)

{
testWriter := httptest.NewRecorder()
engine.ServeHTTP(testWriter, testRequest)
headers1 := testWriter.Header()
assert.NotEqual(t, headers1.Get(headerKey), "")
}

{
testWriter := httptest.NewRecorder()
engine.ServeHTTP(testWriter, testRequest)
headers2 := testWriter.Header()
assert.Equal(t, headers2.Get(headerKey), "")
}
}
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func WithoutHeader() Option {
}
}

func DiscardHeaders(headers []string) Option {
func WithDiscardHeaders(headers []string) Option {
return func(c *Config) {
c.discardHeaders = headers
}
Expand Down

0 comments on commit fedf110

Please sign in to comment.