XuqmGroup-Vue3SDK/__tests__/unit/core/http.test.ts
XuqmGroup 49796f51ae docs(testing): 添加测试文档和修复多个平台SDK问题
- 新增 BUG_TRACKER.md 记录已修复和开放的bug
- 新增 TEST_EXECUTION_2026-04-30.md 自动化测试执行报告
- 新增 TEST_PROGRESS.md 测试进度跟踪文档
- 修复 Android SDK connectedCheck 内存不足问题
- 修复 Android sample-app CAMERA 权限 lint 失败
- 修复 Android UpdateSDK longVersionCode minSdk lint 失败
- 修复 RN Chat Demo Jest 无法解析本地 SDK 源码包
- 修复 Python Server SDK 回调消息解析与顶层导出错误
- 修复 Vue3 SDK package exports 条件顺序警告
- 修复 im-service 群消息不进入会话聚合列表问题
- 修复 im-service 对外时间字段单位不一致问题
- 修复 RN SDK 历史消息 upsert 丢失回推状态问题
- 修复 Android 黑名单操作静默失败问题
- 修复 AppSecret 调用无鉴权安全问题
- 修复 IM Token 无过期信息问题
- 修复 RN SDK 草稿同步服务端数据污染问题
- 修复 Vue3 SDK 撤回编辑后依赖 WS 刷新延迟问题
- 修复 im-service 消息摘要不支持媒体类型问题
2026-04-30 16:54:10 +08:00

117 行
4.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { configureHttp, http } from '../../../src/core/http'
describe('core/http', () => {
beforeEach(() => {
configureHttp('https://api.test.com', () => 'test_token')
vi.restoreAllMocks()
})
it('should configure base URL and token getter', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: { id: 1 }, message: 'ok' }),
} as unknown as Response)
await http.get('/api/test')
expect(fetch).toHaveBeenCalledWith(
'https://api.test.com/api/test',
expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
Authorization: 'Bearer test_token',
'Content-Type': 'application/json',
}),
})
)
})
it('should build URL with query parameters', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: [], message: 'ok' }),
} as unknown as Response)
await http.get('/api/list', { page: 0, size: 20, keyword: 'hello' })
const url = (fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string
expect(url).toContain('page=0')
expect(url).toContain('size=20')
expect(url).toContain('keyword=hello')
})
it('should omit null, undefined and empty query values', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: [], message: 'ok' }),
} as unknown as Response)
await http.get('/api/list', { a: null, b: undefined, c: '', d: 'value' })
const url = (fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string
expect(url).not.toContain('a=')
expect(url).not.toContain('b=')
expect(url).not.toContain('c=')
expect(url).toContain('d=value')
})
it('should send POST with JSON body', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: { id: 1 }, message: 'ok' }),
} as unknown as Response)
const body = { name: 'test' }
await http.post('/api/create', body)
expect(fetch).toHaveBeenCalledWith(
'https://api.test.com/api/create',
expect.objectContaining({
method: 'POST',
body: JSON.stringify(body),
})
)
})
it('should throw when response code is not 200', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 500, data: null, message: 'Server Error' }),
} as unknown as Response)
await expect(http.get('/api/error')).rejects.toThrow('Server Error')
})
it('should handle Date query parameters as ISO string', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: [], message: 'ok' }),
} as unknown as Response)
const date = new Date('2026-04-30T00:00:00.000Z')
await http.get('/api/list', { createdAt: date })
const url = (fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string
expect(decodeURIComponent(url)).toContain('createdAt=2026-04-30T00:00:00.000Z')
})
it('should strip trailing slash from base URL', async () => {
configureHttp('https://api.test.com/', () => 'token')
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: [], message: 'ok' }),
} as unknown as Response)
await http.get('/api/test')
const url = (fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string
expect(url).toBe('https://api.test.com/api/test')
})
it('should handle PUT and DELETE methods', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
json: vi.fn().mockResolvedValue({ code: 200, data: null, message: 'ok' }),
} as unknown as Response)
await http.put('/api/update', { name: 'new' })
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining('/api/update'),
expect.objectContaining({ method: 'PUT' })
)
await http.delete('/api/delete', { id: 1 })
expect(fetch).toHaveBeenCalledWith(
expect.stringContaining('/api/delete'),
expect.objectContaining({ method: 'DELETE' })
)
})
})