/
githubmirror
/
trpc
Обзор
Документация
Войти
/
githubmirror
/
trpc
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/tests/server/methodOverride.test.ts
138 строк
3 KB
Alex / KATT
chore: test refactor (#6958)
24 сен 2025, 08:03
Не верифицирован
24 сен 2025, 08:03
466a924
Код
Авторство
О чём код?
import { testServerAndClientResource } from '@trpc/client/__tests__/testClientResource'; import { waitError } from '@trpc/server/__tests__/waitError'; import { httpBatchLink, httpLink } from '@trpc/client'; import type { HTTPLinkBaseOptions } from '@trpc/client/links/internals/httpUtils'; import { initTRPC } from '@trpc/server'; import type { inferRouterRootTypes } from '@trpc/server/unstable-core-do-not-import'; import fetch from 'node-fetch'; import { afterEach, test } from 'vitest'; import { z } from 'zod'; const t = initTRPC.create(); const router = t.router({ q: t.procedure .input( z.object({ who: z.string().nullish(), }), ) .query((opts) => `hello ${opts.input?.who ?? 'world'}`), m: t.procedure .input( z.object({ who: z.string().nullish(), }), ) .mutation((opts) => `hello ${opts.input?.who ?? 'world'}`), }); async function startServer(opts: { linkOptions: Partial< HTTPLinkBaseOptions<inferRouterRootTypes<typeof router>> >; batch?: boolean; allowMethodOverride: boolean; }) { return testServerAndClientResource(router, { server: { allowMethodOverride: opts.allowMethodOverride, }, client(clientOpts) { return { links: [ opts.batch ? httpBatchLink({ url: clientOpts.httpUrl, fetch: fetch as any, ...opts.linkOptions, }) : httpLink({ url: clientOpts.httpUrl, fetch: fetch as any, ...opts.linkOptions, }), ], }; }, }); } test('client: sends query as POST when methodOverride=POST', async () => { await using t = await startServer({ linkOptions: { methodOverride: 'POST', }, allowMethodOverride: true, }); expect( await t.client.q.query({ who: 'test1', }), ).toBe('hello test1'); }); test('client/server: e2e batched query as POST', async () => { await using t = await startServer({ linkOptions: { methodOverride: 'POST', }, batch: true, allowMethodOverride: true, }); expect( await Promise.all([ t.client.q.query({ who: 'test1', }), t.client.q.query({ who: 'test2', }), t.client.m.mutate({ who: 'test3', }), ]), ).toMatchInlineSnapshot(` Array [ "hello test1", "hello test2", "hello test3", ] `); }); test('server: rejects method override from client when not enabled on the server', async () => { await using t = await startServer({ allowMethodOverride: false, linkOptions: { methodOverride: 'POST', }, }); const err = await waitError(() => t.client.q.query({ who: 'test1', }), ); expect(err).toMatchInlineSnapshot( `[TRPCClientError: Unsupported POST-request to query procedure at path "q"]`, ); }); test('cannot use method overriding with mutations', async () => { await using t = await startServer({ allowMethodOverride: true, linkOptions: {}, }); const err = await waitError(() => { // @ts-expect-error - testing invalid usage return t.client.m.query({ who: 'test1', }); }); expect(err).toMatchInlineSnapshot( `[TRPCClientError: Unsupported GET-request to mutation procedure at path "m"]`, ); });