mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-20 00:18:55 +00:00
Merge pull request #6836 from r266-tech/fix-proxy-expiry-json-range
fix(proxies): reject malformed list responses
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { get } = vi.hoisted(() => ({ get: vi.fn() }))
|
||||
vi.mock('@/api/client', () => ({ apiClient: { get } }))
|
||||
|
||||
import { getAll, getAllWithCount, list } from '@/api/admin/proxies'
|
||||
|
||||
describe.each([
|
||||
{ name: 'paginated list', load: () => list(), wrap: (items: unknown[]) => ({ items, total: items.length, pages: 1 }) },
|
||||
{ name: 'account selector', load: getAll, wrap: (items: unknown[]) => items },
|
||||
{ name: 'account selector with counts', load: getAllWithCount, wrap: (items: unknown[]) => items },
|
||||
])('$name', ({ load, wrap }) => {
|
||||
beforeEach(() => { get.mockReset() })
|
||||
|
||||
it.each(['', undefined, null, {}, { items: null }, { items: {} }])(
|
||||
'rejects an empty or malformed successful response: %j',
|
||||
async (data) => {
|
||||
get.mockResolvedValue({ data })
|
||||
await expect(load()).rejects.toThrow('Invalid proxy list response')
|
||||
},
|
||||
)
|
||||
|
||||
it.each([{ items: [] }, { items: [{ id: 1, name: 'valid proxy' }] }])('preserves valid rows: %j', async ({ items }) => {
|
||||
const data = wrap(items)
|
||||
get.mockResolvedValue({ data })
|
||||
await expect(load()).resolves.toBe(data)
|
||||
})
|
||||
})
|
||||
@@ -15,6 +15,12 @@ import type {
|
||||
AdminDataImportResult
|
||||
} from '@/types'
|
||||
|
||||
function assertProxyArray(value: unknown): asserts value is Proxy[] {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error('Invalid proxy list response')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all proxies with pagination
|
||||
* @param page - Page number (default: 1)
|
||||
@@ -44,6 +50,7 @@ export async function list(
|
||||
},
|
||||
signal: options?.signal
|
||||
})
|
||||
assertProxyArray(data?.items)
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -53,6 +60,7 @@ export async function list(
|
||||
*/
|
||||
export async function getAll(): Promise<Proxy[]> {
|
||||
const { data } = await apiClient.get<Proxy[]>('/admin/proxies/all')
|
||||
assertProxyArray(data)
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -64,6 +72,7 @@ export async function getAllWithCount(): Promise<Proxy[]> {
|
||||
const { data } = await apiClient.get<Proxy[]>('/admin/proxies/all', {
|
||||
params: { with_count: 'true' }
|
||||
})
|
||||
assertProxyArray(data)
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user