nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: appleboy/gin-jwt
Pull request #249 (2020-06-05 08:47:33)

calculate cookie max-age using TimeFunc
fixes https://github.com/appleboy/gin-jwt/issues/248


Patch:
diff --git a/auth_jwt.go b/auth_jwt.go
index 51f272d..60553b0 100644
--- a/auth_jwt.go
+++ b/auth_jwt.go
@@ -461,7 +461,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
 	// set cookie
 	if mw.SendCookie {
 		expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
-		maxage := int(expireCookie.Unix() - time.Now().Unix())
+		maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())
 		c.SetCookie(
 			mw.CookieName,
 			tokenString,


Test patch:
diff --git a/auth_jwt_test.go b/auth_jwt_test.go
index 79c431a..5c648ee 100644
--- a/auth_jwt_test.go
+++ b/auth_jwt_test.go
@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"net/http/httptest"
 	"reflect"
+	"strings"
 	"testing"
 	"time"
 
@@ -200,6 +201,8 @@ func TestMissingAuthenticatorForLoginHandler(t *testing.T) {
 func TestLoginHandler(t *testing.T) {
 
 	// the middleware to test
+	cookieName := "jwt"
+	cookieDomain := "example.com"
 	authMiddleware, err := New(&GinJWTMiddleware{
 		Realm: "test zone",
 		Key:   key,
@@ -236,7 +239,10 @@ func TestLoginHandler(t *testing.T) {
 				"cookie":  cookie,
 			})
 		},
-		SendCookie: true,
+		SendCookie:   true,
+		CookieName:   cookieName,
+		CookieDomain: cookieDomain,
+		TimeFunc:     func() time.Time { return time.Now().Add(time.Duration(5) * time.Minute) },
 	})
 
 	assert.NoError(t, err)
@@ -269,19 +275,16 @@ func TestLoginHandler(t *testing.T) {
 		})
 
 	r.POST("/login").
-		SetCookie(gofight.H{
-			"jwt": "jwt",
-		}).
 		SetJSON(gofight.D{
 			"username": "admin",
 			"password": "admin",
 		}).
 		Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
 			message := gjson.Get(r.Body.String(), "message")
-			cookie := gjson.Get(r.Body.String(), "cookie")
 			assert.Equal(t, "login successfully", message.String())
 			assert.Equal(t, http.StatusOK, r.Code)
-			assert.Equal(t, "jwt", cookie.String())
+			assert.True(t, strings.HasPrefix(r.HeaderMap.Get("Set-Cookie"), "jwt="))
+			assert.True(t, strings.HasSuffix(r.HeaderMap.Get("Set-Cookie"), "; Path=/; Domain=example.com; Max-Age=3600"))
 		})
 }