ray-template/__test__/workspace/http-client.spec.ts
XiaoDaiGua-Ray 911c403434
feat: refine admin workflows for v6.0.3
Improve theme consistency, keyboard navigation, and reusable frontend components.
Harden fullstack initialization and expose scoped lint and Vitest commands.
2026-12-01 11:04:13 +08:00

148 lines
4.9 KiB
TypeScript

// @vitest-environment node
import { createHttpClient } from '@ray-template/http'
import RequestCanceler from '@ray-template/http/RequestCanceler'
import type { AppRawRequestConfig } from '@ray-template/http/types'
import { AxiosError } from 'axios'
import type { AxiosAdapter, AxiosResponse } from 'axios'
it('compares repeated form fields, URL parameters and uploaded file identity when cancelling duplicates', () => {
const canceler = new RequestCanceler()
const submit = (data: FormData | URLSearchParams) => {
const config: AppRawRequestConfig = { url: '/upload', method: 'post', data }
canceler.removePendingRequest(config)
canceler.addPendingRequest(config)
return config.signal!
}
const first = submit(new URLSearchParams('x=1&x=2'))
const second = submit(new URLSearchParams('x=2&x=1'))
expect(first.aborted).toBe(false)
submit(new URLSearchParams('x=1&x=2'))
expect(first.aborted).toBe(true)
expect(second.aborted).toBe(false)
const form = (text: string, file: File) => {
const body = new FormData()
body.append('tag', text)
body.append('file', file)
return body
}
const file = new File(['a'], 'same.txt')
const uploaded = submit(form('first', file))
const anotherText = submit(form('second', file))
const anotherFile = submit(form('first', new File(['b'], 'same.txt')))
expect(uploaded.aborted).toBe(false)
submit(form('first', file))
expect(uploaded.aborted).toBe(true)
expect(anotherText.aborted).toBe(false)
expect(anotherFile.aborted).toBe(false)
canceler.cancelAllRequest()
expect(anotherFile.aborted).toBe(true)
})
it('keeps independent binary uploads pending and cancels repeats of the same instance', async () => {
const pending: Array<() => void> = []
const adapter: AxiosAdapter = (config) =>
new Promise((resolve, reject) => {
pending.push(() =>
resolve({
data: 'ok',
status: 200,
statusText: 'OK',
headers: {},
config,
}),
)
config.signal?.addEventListener?.('abort', () =>
reject(new AxiosError('aborted', 'ERR_CANCELED', config)),
)
})
const { request, canceler } = createHttpClient({ axios: { adapter } })
const upload = (data: unknown) =>
request.post('/binary', data).catch((error: AxiosError) => error.code)
const bodies = [
new Blob(['a']),
new Blob(['b']),
new File(['a'], 'same.bin'),
new File(['b'], 'same.bin'),
new Uint8Array([1]).buffer,
new Uint8Array([2]).buffer,
new Uint8Array([1]),
new Uint8Array([1]),
new DataView(new ArrayBuffer(1)),
new DataView(new ArrayBuffer(1)),
new ReadableStream(),
new ReadableStream(),
]
const uploads = bodies.map(upload)
await vi.waitFor(() => expect(pending).toHaveLength(bodies.length))
expect(canceler.getPendingCount()).toBe(bodies.length)
const repeated = upload(bodies[0])
await vi.waitFor(() => expect(pending).toHaveLength(bodies.length + 1))
expect(await uploads[0]).toBe('ERR_CANCELED')
expect(canceler.getPendingCount()).toBe(bodies.length)
pending.forEach((finish) => finish())
expect(await Promise.all([...uploads.slice(1), repeated])).toEqual(
bodies.map(() => 'ok'),
)
expect(canceler.getPendingCount()).toBe(0)
})
it('isolates pending requests by client and leaves caller-owned signals intact', async () => {
const pending: Array<{ signal: unknown; finish: () => void }> = []
const adapter: AxiosAdapter = (config) =>
new Promise<AxiosResponse>((resolve, reject) => {
pending.push({
signal: config.signal,
finish: () =>
resolve({
data: 'ok',
status: 200,
statusText: 'OK',
headers: {},
config,
}),
})
config.signal?.addEventListener?.('abort', () =>
reject(new AxiosError('aborted', 'ERR_CANCELED', config)),
)
})
const first = createHttpClient({ axios: { adapter } })
const second = createHttpClient({ axios: { adapter } })
const original = first.request
.get('/same')
.catch((error: AxiosError) => error.code)
const other = second.request.get('/same')
await vi.waitFor(() => expect(pending).toHaveLength(2))
const replacement = first.request.get('/same')
await vi.waitFor(() => expect(pending).toHaveLength(3))
expect(await original).toBe('ERR_CANCELED')
expect(first.canceler.getPendingCount()).toBe(1)
expect(second.canceler.getPendingCount()).toBe(1)
pending[1].finish()
pending[2].finish()
expect(await Promise.all([other, replacement])).toEqual(['ok', 'ok'])
const own = new AbortController()
const supplied = first.request.get('/custom', { signal: own.signal })
await vi.waitFor(() => expect(pending).toHaveLength(4))
expect(pending[3].signal).toBe(own.signal)
first.canceler.cancelAllRequest()
expect(own.signal.aborted).toBe(false)
pending[3].finish()
await expect(supplied).resolves.toBe('ok')
})