|
1 | 1 | import { describe, expect, expectTypeOf, it } from "vitest"; |
2 | 2 | import { createAuthClient } from "../../client"; |
| 3 | +import { parseSetCookieHeader } from "../../cookies"; |
3 | 4 | import { getTestInstance } from "../../test-utils/test-instance"; |
4 | 5 | import type { BetterAuthOptions } from "../../types"; |
5 | 6 | import { admin } from "../admin"; |
@@ -81,6 +82,37 @@ describe("Custom Session Plugin Tests", async () => { |
81 | 82 | }); |
82 | 83 | }); |
83 | 84 |
|
| 85 | + it("should not double-encode session cookie during get-session refresh", async () => { |
| 86 | + const { headers } = await signInWithTestUser(); |
| 87 | + const signedInCookie = headers.get("cookie"); |
| 88 | + const signedInSessionToken = signedInCookie?.match( |
| 89 | + /better-auth\.session_token=([^;]+)/, |
| 90 | + )?.[1]; |
| 91 | + expect(signedInSessionToken).toBeDefined(); |
| 92 | + |
| 93 | + let refreshedSessionToken: string | undefined; |
| 94 | + await client.getSession({ |
| 95 | + fetchOptions: { |
| 96 | + headers, |
| 97 | + onResponse(context) { |
| 98 | + const setCookies = context.response.headers.getSetCookie(); |
| 99 | + for (const cookieStr of setCookies) { |
| 100 | + const parsed = parseSetCookieHeader(cookieStr); |
| 101 | + const token = parsed.get("better-auth.session_token")?.value; |
| 102 | + if (token) { |
| 103 | + refreshedSessionToken = token; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + expect(refreshedSessionToken).toBeDefined(); |
| 112 | + expect(refreshedSessionToken).toBe(signedInSessionToken); |
| 113 | + expect(refreshedSessionToken).not.toContain("%25"); |
| 114 | + }); |
| 115 | + |
84 | 116 | it("should return the custom session for multi-session", async () => { |
85 | 117 | const headers = new Headers(); |
86 | 118 | const testUser = { |
|
0 commit comments