Merge pull request 'fix: set DestroySession MaxAge to -1 instead of -1*time.Second (closes #39)' (#50) from clawbot/upaas:fix/destroy-session-maxage into main
Reviewed-on: #50
This commit is contained in:
commit
07ac71974c
@ -10,7 +10,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
@ -269,7 +268,7 @@ func (svc *Service) DestroySession(
|
|||||||
return fmt.Errorf("failed to get session: %w", err)
|
return fmt.Errorf("failed to get session: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Options.MaxAge = -1 * int(time.Second)
|
session.Options.MaxAge = -1
|
||||||
|
|
||||||
saveErr := session.Save(request, respWriter)
|
saveErr := session.Save(request, respWriter)
|
||||||
if saveErr != nil {
|
if saveErr != nil {
|
||||||
|
|||||||
@ -369,3 +369,38 @@ func TestAuthenticate(testingT *testing.T) {
|
|||||||
assert.ErrorIs(t, err, auth.ErrInvalidCredentials)
|
assert.ErrorIs(t, err, auth.ErrInvalidCredentials)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDestroySessionMaxAge(testingT *testing.T) {
|
||||||
|
testingT.Parallel()
|
||||||
|
|
||||||
|
testingT.Run("sets MaxAge to exactly -1", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
svc, cleanup := setupTestService(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
recorder := httptest.NewRecorder()
|
||||||
|
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
|
||||||
|
err := svc.DestroySession(recorder, request)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Check the Set-Cookie header to verify MaxAge is -1 (immediate expiry).
|
||||||
|
// With MaxAge = -1, the cookie should have Max-Age=0 in the HTTP header
|
||||||
|
// (per http.Cookie semantics: negative MaxAge means delete now).
|
||||||
|
cookies := recorder.Result().Cookies()
|
||||||
|
require.NotEmpty(t, cookies, "expected a Set-Cookie header")
|
||||||
|
|
||||||
|
found := false
|
||||||
|
|
||||||
|
for _, c := range cookies {
|
||||||
|
if c.MaxAge < 0 {
|
||||||
|
found = true
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.True(t, found, "expected a cookie with negative MaxAge (deletion)")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user