ray-template/__test__/axios/admin-headers.spec.ts
XiaoDaiGua-Ray bca3f3ff24
feat(admin): add user management and HTTP mock APIs
Add user CRUD, account-scoped navigation history and favorites.

Unify HTTP mocks, backend contracts and route configuration.

Reuse existing scrolling components and add Harness reuse guidance.
2026-11-05 17:21:59 +08:00

40 lines
1.3 KiB
TypeScript

import { APP_CATCH_KEY } from '@/app-config'
import { injectRequestHeaders } from '@/axios/axios-interceptor/request/plugins/request-headers'
import type { RequestInterceptorConfig } from '@/axios/types'
import { setStorage } from '@/utils/cache'
import { AxiosHeaders } from 'axios'
afterEach(() => {
localStorage.clear()
vi.unstubAllEnvs()
})
it('sends Bearer credentials only to the configured API origin and path', () => {
vi.stubEnv('VITE_API_BASE_URL', 'https://api.example.test/v1/')
setStorage(APP_CATCH_KEY.token, 'test-token', 'localStorage')
const config = (url: string): RequestInterceptorConfig => ({
url,
headers: new AxiosHeaders(),
})
const owned = config('https://api.example.test/v1/admin/users')
injectRequestHeaders(owned, '')
expect(owned.headers?.Authorization).toBe('Bearer test-token')
for (const url of [
'https://other.example.test/v1/admin/users',
'https://api.example.test/v10/admin/users',
]) {
const foreign = config(url)
injectRequestHeaders(foreign, '')
expect(foreign.headers?.Authorization).toBeUndefined()
}
const explicit = config('https://api.example.test/v1/admin/users')
explicit.headers = { authorization: 'Bearer supplied-token' }
injectRequestHeaders(explicit, '')
expect(explicit.headers.authorization).toBe('Bearer supplied-token')
})