/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/tests/server/responseMeta.test.ts
244 строки
6 KB
Alex / KATT
chore: test refactor (#6958)
24 сен 2025, 08:03
Не верифицирован
24 сен 2025, 08:03
466a924
Код
Авторство
О чём код?
import type { IncomingMessage, ServerResponse } from 'http'; import { testServerAndClientResource } from '@trpc/client/__tests__/testClientResource'; import { initTRPC } from '@trpc/server'; import fetch from 'node-fetch'; test('set custom headers in beforeEnd', async () => { const onError = vi.fn(); interface Context { req: IncomingMessage; res: ServerResponse<IncomingMessage>; } const t = initTRPC.context<Context>().create(); const appRouter = t.router({ ['public.q']: t.procedure.query(({}) => { return 'public endpoint'; }), nonCachedEndpoint: t.procedure.query(({}) => { return 'not cached endpoint'; }), }); await using ctx = testServerAndClientResource(appRouter, { server: { onError, responseMeta({ ctx, paths, type, errors }) { // assuming you have all your public routes with the keyword `public` in them const allPublic = paths?.every((path) => path.includes('public')); // checking that no procedures errored const allOk = errors.length === 0; // checking we're doing a query request const isQuery = type === 'query'; if (ctx?.res && allPublic && allOk && isQuery) { // cache request for 1 day + revalidate once every second const ONE_DAY_IN_SECONDS = 60 * 60 * 24; return { headers: new Headers([ [ 'cache-control', `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`, ], ]), }; } return {}; }, }, }); { const res = await fetch(`${ctx.httpUrl}/public.q`); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "public endpoint", }, } `); expect(res.headers.get('cache-control')).toMatchInlineSnapshot( `"s-maxage=1, stale-while-revalidate=86400"`, ); } { const res = await fetch(`${ctx.httpUrl}/nonCachedEndpoint`); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "not cached endpoint", }, } `); expect(res.headers.get('cache-control')).toBeNull(); } }); test('cookie headers', async () => { const onError = vi.fn(); interface Context { req: IncomingMessage; res: ServerResponse<IncomingMessage>; } const t = initTRPC.context<Context>().create(); const appRouter = t.router({ cookieEndpoint: t.procedure.query(() => { return 'cookie endpoint'; }), }); await using ctx = testServerAndClientResource(appRouter, { server: { onError, responseMeta() { return { headers: new Headers([ ['Set-Cookie', 'a=b'], ['Set-Cookie', 'b=c'], ]), }; }, }, }); { const res = await fetch(`${ctx.httpUrl}/cookieEndpoint`); expect(res.headers.get('set-cookie')).toMatchInlineSnapshot(` "a=b, b=c" `); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "cookie endpoint", }, } `); } }); describe('deprecated headers object', () => { test('set custom headers in beforeEnd', async () => { const onError = vi.fn(); interface Context { req: IncomingMessage; res: ServerResponse<IncomingMessage>; } const t = initTRPC.context<Context>().create(); const appRouter = t.router({ ['public.q']: t.procedure.query(({}) => { return 'public endpoint'; }), nonCachedEndpoint: t.procedure.query(({}) => { return 'not cached endpoint'; }), }); await using ctx = testServerAndClientResource(appRouter, { server: { onError, responseMeta({ ctx, paths, type, errors }) { // assuming you have all your public routes with the keyword `public` in them const allPublic = paths?.every((path) => path.includes('public')); // checking that no procedures errored const allOk = errors.length === 0; // checking we're doing a query request const isQuery = type === 'query'; if (ctx?.res && allPublic && allOk && isQuery) { // cache request for 1 day + revalidate once every second const ONE_DAY_IN_SECONDS = 60 * 60 * 24; return { headers: { 'cache-control': `s-maxage=1, stale-while-revalidate=${ONE_DAY_IN_SECONDS}`, }, }; } return {}; }, }, }); { const res = await fetch(`${ctx.httpUrl}/public.q`); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "public endpoint", }, } `); expect(res.headers.get('cache-control')).toMatchInlineSnapshot( `"s-maxage=1, stale-while-revalidate=86400"`, ); } { const res = await fetch(`${ctx.httpUrl}/nonCachedEndpoint`); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "not cached endpoint", }, } `); expect(res.headers.get('cache-control')).toBeNull(); } }); test('cookie headers', async () => { const onError = vi.fn(); interface Context { req: IncomingMessage; res: ServerResponse<IncomingMessage>; } const t = initTRPC.context<Context>().create(); const appRouter = t.router({ cookieEndpoint: t.procedure.query(() => { return 'cookie endpoint'; }), }); await using ctx = testServerAndClientResource(appRouter, { server: { onError, responseMeta() { return { headers: { 'Set-Cookie': ['a=b', 'b=c'], }, }; }, }, }); { const res = await fetch(`${ctx.httpUrl}/cookieEndpoint`); expect(res.headers.get('set-cookie')).toMatchInlineSnapshot(` "a=b, b=c" `); expect(await res.json()).toMatchInlineSnapshot(` Object { "result": Object { "data": "cookie endpoint", }, } `); } }); });