/
githubmirror
/
ydb-embedded-ui
Обзор
Документация
Войти
/
githubmirror
/
ydb-embedded-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/services/api/__test__/streaming.test.ts
117 строк
3 KB
Konstantin Kozlov
fix: send database query param for post requests (#4168)
27 июл 2026, 17:47
Не верифицирован
27 июл 2026, 17:47
4ee8923
Код
Авторство
О чём код?
jest.mock('../../../store', () => ({ backend: undefined, clusterName: undefined, })); jest.mock('@mjackson/multipart-parser', () => ({ parseMultipart: jest.fn(async () => undefined), })); import {CSRF_TOKEN_HEADER_NAME} from '../base'; import {StreamingAPI} from '../streaming'; function createStreamingApi(csrfTokenGetter: () => string | undefined = () => undefined) { return new StreamingAPI( {config: {withCredentials: true}}, { singleClusterMode: true, proxyMeta: false, useRelativePath: false, csrfTokenGetter, }, ); } function createStreamQueryOptions() { return { onStreamDataChunk: jest.fn(), onQueryResponseChunk: jest.fn(), onSessionChunk: jest.fn(), }; } function createFetchResponseMock() { return { ok: true, body: {}, headers: { get: jest.fn(() => null), }, } as unknown as Response; } describe('StreamingAPI requests', () => { const originalFetch = global.fetch; afterEach(() => { global.fetch = originalFetch; jest.restoreAllMocks(); }); test('uses manually set CSRF token when cookie getter returns empty value', async () => { const api = createStreamingApi(); api.setCSRFToken('manual-token'); const fetchMock = jest.fn<Promise<Response>, [string, RequestInit]>(async () => Promise.resolve(createFetchResponseMock()), ); global.fetch = fetchMock as typeof fetch; await api.streamQuery( { base64: false, } as never, createStreamQueryOptions(), ); const headers = fetchMock.mock.calls[0][1].headers; expect(headers).toBeInstanceOf(Headers); expect((headers as Headers).get(CSRF_TOKEN_HEADER_NAME)).toBe('manual-token'); }); test('uses current cookie getter token before manually set fallback', async () => { const api = createStreamingApi(() => 'cookie-token'); api.setCSRFToken('manual-token'); const fetchMock = jest.fn<Promise<Response>, [string, RequestInit]>(async () => Promise.resolve(createFetchResponseMock()), ); global.fetch = fetchMock as typeof fetch; await api.streamQuery( { base64: false, } as never, createStreamQueryOptions(), ); const headers = fetchMock.mock.calls[0][1].headers; expect(headers).toBeInstanceOf(Headers); expect((headers as Headers).get(CSRF_TOKEN_HEADER_NAME)).toBe('cookie-token'); }); test('sends database in both URL parameters and body for streaming queries', async () => { const api = createStreamingApi(); const fetchMock = jest.fn<Promise<Response>, [string, RequestInit]>(async () => Promise.resolve(createFetchResponseMock()), ); global.fetch = fetchMock as typeof fetch; await api.streamQuery( { database: '/Root/test', base64: false, }, createStreamQueryOptions(), ); const [url, request] = fetchMock.mock.calls[0]; expect(new URL(url, 'http://localhost').searchParams.get('database')).toBe('/Root/test'); expect(JSON.parse(request.body as string)).toEqual( expect.objectContaining({ database: '/Root/test', }), ); }); });