-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dfebe4
commit 1622d42
Showing
3 changed files
with
23 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,9 +81,17 @@ func TestVerifyToken(t *testing.T) { | |
func TestVerifyTokenCaching(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
|
||
callCount := 0 | ||
// Create a middleware to track call count | ||
var callCount int | ||
countMiddleware := func(c *gin.Context) { | ||
if c.Writer.Header().Get("X-Token-Cache") == "MISS" { | ||
callCount++ | ||
t.Logf("Mock server called. Count: %d", callCount) | ||
} | ||
c.Next() | ||
} | ||
|
||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
callCount++ | ||
profile := Profile{ | ||
ID: "123", | ||
Email: "[email protected]", | ||
|
@@ -106,6 +114,7 @@ func TestVerifyTokenCaching(t *testing.T) { | |
c.Next() | ||
}) | ||
r.Use(VerifyToken(os.Getenv("TOKEN_CACHE_EXPIRY"))) | ||
r.Use(countMiddleware) // Add the count middleware | ||
r.GET("/test", func(c *gin.Context) { | ||
c.Status(http.StatusOK) | ||
}) | ||
|
@@ -115,22 +124,31 @@ func TestVerifyTokenCaching(t *testing.T) { | |
req.Header.Set("Authorization", "Bearer valid_token") | ||
resp := httptest.NewRecorder() | ||
r.ServeHTTP(resp, req) | ||
t.Logf("Request made. Status: %d", resp.Code) | ||
} | ||
|
||
t.Log("Making first request") | ||
makeRequest() // First request, should call the mock server | ||
if callCount != 1 { | ||
t.Errorf("Expected 1 call to mock server, got %d", callCount) | ||
} | ||
|
||
// time.Sleep(100 * time.Millisecond) // Small delay | ||
|
||
t.Log("Making second request") | ||
makeRequest() // Second request, should use cache | ||
if callCount != 1 { | ||
t.Errorf("Expected still 1 call to mock server, got %d", callCount) | ||
} | ||
|
||
t.Log("Waiting for cache to expire") | ||
time.Sleep(3 * time.Second) // Wait for cache to expire | ||
|
||
t.Log("Making third request") | ||
makeRequest() // Third request, should call the mock server again | ||
if callCount != 2 { | ||
t.Errorf("Expected 2 calls to mock server, got %d", callCount) | ||
} | ||
|
||
t.Logf("Final call count: %d", callCount) | ||
} |