UNPKG

http-svc

Version:

A HTTP request service for browser and node.js

189 lines (169 loc) 5.53 kB
/** * 测试 paramsSerializer 功能 */ import { HttpService, BUILT_IN_MIDDLEWARE } from '../' import { HttpSvcTestFetch } from './base' const encodeKV = (k: string, v: string) => { return `${encodeURIComponent(k)}=${encodeURIComponent(v)}` } describe('ParamsSerializer Tests', () => { test('使用自定义 paramsSerializer 函数', async () => { const customSerializer = (params: Record<string, unknown>) => { return Object.keys(params) .map((key) => `custom_${key}=${params[key]}`) .join('&') } const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: customSerializer }) .request({ url: '/api/test', method: 'GET', params: { foo: 'bar', baz: 'qux' } }) expect(result.request?.url).toContain('custom_foo=bar') expect(result.request?.url).toContain('custom_baz=qux') }) test('使用 ParamsSerializerOptions 配置 - brackets 格式', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { arrayFormat: 'brackets' } }) .request({ url: '/api/test', method: 'GET', params: { items: ['a', 'b', 'c'] } }) expect(result.request?.url).toContain(encodeKV('items[]', 'a')) expect(result.request?.url).toContain(encodeKV('items[]', 'b')) expect(result.request?.url).toContain(encodeKV('items[]', 'c')) }) test('使用 ParamsSerializerOptions 配置 - indices 格式', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { arrayFormat: 'indices' } }) .request({ url: '/api/test', method: 'GET', params: { items: ['a', 'b', 'c'] } }) expect(result.request?.url).toContain(encodeKV('items[0]', 'a')) expect(result.request?.url).toContain(encodeKV('items[1]', 'b')) expect(result.request?.url).toContain(encodeKV('items[2]', 'c')) }) test('使用 ParamsSerializerOptions 配置 - repeat 格式', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { arrayFormat: 'repeat' } }) .request({ url: '/api/test', method: 'GET', params: { item: ['a', 'b', 'c'] } }) const url = result.request?.url || '' const itemMatches = url.match(/item=\w/g) expect(itemMatches).toHaveLength(3) expect(url).toContain('item=a') expect(url).toContain('item=b') expect(url).toContain('item=c') }) test('使用 ParamsSerializerOptions 配置 - comma 格式', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { arrayFormat: 'comma' } }) .request({ url: '/api/test', method: 'GET', params: { items: ['a', 'b', 'c'] } }) expect(result.request?.url).toContain(encodeKV('items', 'a,b,c')) }) test('使用 ParamsSerializerOptions 配置 - encode: false', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { encode: false } }) .request({ url: '/api/test', method: 'GET', params: { query: 'hello world' } }) expect(result.request?.url).toContain('query=hello world') }) test('POST 请求使用 paramsSerializer 处理 form 数据', async () => { const customSerializer = (params: Record<string, unknown>) => { return Object.keys(params) .map((key) => `form_${key}=${params[key]}`) .join('&') } const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: customSerializer }) .request({ url: '/api/test', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: { username: 'test', password: '123' } }) expect(result.request?.data).toBe('form_username=test&form_password=123') }) test('处理复杂对象和数组', async () => { const result = await new HttpService({ fetch: new HttpSvcTestFetch({ code: 0 }) }) .with('DEBUG', true) .with(BUILT_IN_MIDDLEWARE.BODY, { paramsSerializer: { arrayFormat: 'indices' } }) .request({ url: '/api/test', method: 'GET', params: { simple: 'value', array: ['a', 'b'], stringDate: '2023-01-01', numberValue: 123 } }) const url = result.request?.url || '' expect(url).toContain('simple=value') expect(url).toContain(encodeKV('array[0]', 'a')) expect(url).toContain(encodeKV('array[1]', 'b')) expect(url).toContain('stringDate=') expect(url).toContain('numberValue=') }) })