|
1 | 1 | import { handleIsr, type IsrCacheEnv } from '@void/isr/runtime'; |
2 | 2 | import { memoryAdapter } from 'better-auth/adapters/memory'; |
3 | 3 | import { Hono } from 'hono'; |
| 4 | +import { etag } from 'hono/etag'; |
4 | 5 | import { describe, expect, it } from 'vite-plus/test'; |
5 | 6 | import { createVoidAuthRuntime } from '../../src/runtime/better-auth-shared.ts'; |
6 | 7 |
|
@@ -91,3 +92,100 @@ describe('real anonymous Better Auth lookup cache policy', () => { |
91 | 92 | await expect(response.json()).resolves.toBeNull(); |
92 | 93 | }); |
93 | 94 | }); |
| 95 | + |
| 96 | +describe('real authenticated Better Auth session cache policy', () => { |
| 97 | + async function signedInApp(routeHeaders: Record<string, string>, { withEtag = false } = {}) { |
| 98 | + const database = memoryAdapter({ user: [], session: [], account: [], verification: [] }); |
| 99 | + const runtime = createVoidAuthRuntime('sqlite', async () => {}); |
| 100 | + const app = new Hono(); |
| 101 | + app.use(async (c, next) => runtime.runVoidAuthMiddleware(c, next, options, database)); |
| 102 | + app.on(['GET', 'POST'], '/api/auth/*', (c) => |
| 103 | + runtime.handleVoidAuthRequest(c, options, database), |
| 104 | + ); |
| 105 | + if (withEtag) { |
| 106 | + app.use('/api/report', etag()); |
| 107 | + } |
| 108 | + app.get('/api/report', (c) => c.text('report', 200, routeHeaders)); |
| 109 | + const signUp = await app.request( |
| 110 | + 'https://app.test/api/auth/sign-up/email', |
| 111 | + { |
| 112 | + method: 'POST', |
| 113 | + headers: { 'content-type': 'application/json' }, |
| 114 | + body: JSON.stringify({ |
| 115 | + name: 'Cache user', |
| 116 | + email: 'cache-user@example.test', |
| 117 | + password: 'correct-horse-battery-staple', |
| 118 | + }), |
| 119 | + }, |
| 120 | + env, |
| 121 | + ); |
| 122 | + expect(signUp.status).toBe(200); |
| 123 | + const cookie = signUp.headers |
| 124 | + .getSetCookie() |
| 125 | + .map((value) => value.split(';')[0]) |
| 126 | + .join('; '); |
| 127 | + const session = await app.request( |
| 128 | + 'https://app.test/api/auth/get-session', |
| 129 | + { headers: { cookie } }, |
| 130 | + env, |
| 131 | + ); |
| 132 | + await expect(session.json()).resolves.toMatchObject({ |
| 133 | + user: { email: 'cache-user@example.test' }, |
| 134 | + }); |
| 135 | + return { app, cookie }; |
| 136 | + } |
| 137 | + |
| 138 | + it.each(['private, no-cache', 'no-store', 'Private, No-Cache, max-age=60', 'private,\tno-cache'])( |
| 139 | + 'keeps a no-store or private, no-cache route policy: %s', |
| 140 | + async (policy) => { |
| 141 | + const { app, cookie } = await signedInApp({ 'cache-control': policy, etag: 'W/"report-1"' }); |
| 142 | + const response = await app.request( |
| 143 | + 'https://app.test/api/report', |
| 144 | + { headers: { cookie } }, |
| 145 | + env, |
| 146 | + ); |
| 147 | + expect(response.status).toBe(200); |
| 148 | + expect(response.headers.get('cache-control')).toBe(policy); |
| 149 | + expect(response.headers.has('pragma')).toBe(false); |
| 150 | + expect(response.headers.get('etag')).toBe('W/"report-1"'); |
| 151 | + }, |
| 152 | + ); |
| 153 | + |
| 154 | + it.each([ |
| 155 | + null, |
| 156 | + 'public, max-age=60', |
| 157 | + 'no-cache', |
| 158 | + 'private="set-cookie", max-age=60', |
| 159 | + 'private', |
| 160 | + 'private, max-age=60', |
| 161 | + 'private, no-cache="set-cookie"', |
| 162 | + 'no-cache="x,private,y", public, max-age=600', |
| 163 | + 'no-cache="x,no-store,y", public, max-age=600', |
| 164 | + 'no-cache="a\\",no-store,b", public, max-age=600', |
| 165 | + ])('applies the session lookup no-store to a route policy of %s', async (policy) => { |
| 166 | + const { app, cookie } = await signedInApp(policy ? { 'cache-control': policy } : {}); |
| 167 | + const response = await app.request('https://app.test/api/report', { headers: { cookie } }, env); |
| 168 | + expect(response.headers.get('cache-control')).toBe('no-store'); |
| 169 | + expect(response.headers.get('pragma')).toBe('no-cache'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('answers a signed-in repeat request with 304 through etag()', async () => { |
| 173 | + const { app, cookie } = await signedInApp( |
| 174 | + { 'cache-control': 'private, no-cache' }, |
| 175 | + { withEtag: true }, |
| 176 | + ); |
| 177 | + const first = await app.request('https://app.test/api/report', { headers: { cookie } }, env); |
| 178 | + expect(first.status).toBe(200); |
| 179 | + const tag = first.headers.get('etag'); |
| 180 | + expect(tag).toBeTruthy(); |
| 181 | + const repeat = await app.request( |
| 182 | + 'https://app.test/api/report', |
| 183 | + { headers: { cookie, 'if-none-match': tag! } }, |
| 184 | + env, |
| 185 | + ); |
| 186 | + expect(repeat.status).toBe(304); |
| 187 | + expect(repeat.headers.get('cache-control')).toBe('private, no-cache'); |
| 188 | + expect(repeat.headers.has('pragma')).toBe(false); |
| 189 | + expect(repeat.headers.get('etag')).toBe(tag); |
| 190 | + }); |
| 191 | +}); |
0 commit comments