MCPcopy
hub / github.com/appleboy/gin-jwt / testTokenPersistenceAcrossRequests

Function testTokenPersistenceAcrossRequests

auth_jwt_redis_test.go:414–454  ·  view source on GitHub ↗
(t *testing.T, r *gin.Engine)

Source from the content-addressed store, hash-verified

412}
413
414func testTokenPersistenceAcrossRequests(t *testing.T, r *gin.Engine) {
415 // Login and get refresh token
416 w := httptest.NewRecorder()
417 req, _ := http.NewRequestWithContext(
418 context.Background(),
419 "POST",
420 "/login",
421 strings.NewReader(`{"username":"`+testAdmin+`","password":"`+testAdmin+`"}`),
422 )
423 req.Header.Set("Content-Type", "application/json")
424 r.ServeHTTP(w, req)
425
426 var loginResp map[string]any
427 err := parseJSON(w.Body.String(), &loginResp)
428 require.NoError(t, err)
429
430 refreshToken := loginResp["refresh_token"].(string)
431
432 // Simulate some time passing and multiple refresh requests
433 for i := 0; i < 3; i++ {
434 time.Sleep(10 * time.Millisecond) // Small delay to simulate real usage
435
436 w = httptest.NewRecorder()
437 req, _ = http.NewRequestWithContext(
438 context.Background(),
439 "POST",
440 "/refresh",
441 strings.NewReader(fmt.Sprintf(`{"refresh_token":"%s"}`, refreshToken)),
442 )
443 req.Header.Set("Content-Type", "application/json")
444 r.ServeHTTP(w, req)
445
446 assert.Equal(t, 200, w.Code, fmt.Sprintf("refresh %d should succeed", i+1))
447
448 // Update refresh token for next iteration
449 var refreshResp map[string]any
450 err := parseJSON(w.Body.String(), &refreshResp)
451 require.NoError(t, err, fmt.Sprintf("should parse refresh response %d", i+1))
452 refreshToken = refreshResp["refresh_token"].(string)
453 }
454}
455
456func testRedisStoreOperations(t *testing.T, middleware *GinJWTMiddleware) {
457 // Verify that Redis store is being used

Calls 2

parseJSONFunction · 0.85
SetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…