XuqmGroup-RNSDK/packages/bugcollect/tests/http-interceptor.test.ts
2026-07-18 08:59:40 +08:00

33 行
1.1 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { HttpInterceptor } from '../src/interceptor/HttpInterceptor'
test('HTTP interception removes host, query and request contents before reporting', async () => {
const originalFetch = globalThis.fetch
const reports: Array<{ error: unknown; metadata?: Record<string, unknown> }> = []
globalThis.fetch = async () => new Response(null, { status: 500 })
try {
HttpInterceptor.start((error, metadata) => reports.push({ error, metadata }))
await globalThis.fetch('https://api.example.com/am/user?phone=13800000000&token=secret', {
body: JSON.stringify({ userId: 'secret-user' }),
headers: { authorization: 'secret-authorization' },
method: 'POST',
})
} finally {
HttpInterceptor.stop()
globalThis.fetch = originalFetch
}
assert.equal(reports.length, 1)
assert.deepEqual(reports[0]?.metadata, {
method: 'POST',
path: '/am/user',
status: 500,
type: 'api_error',
})
const serialized = JSON.stringify(reports)
assert.doesNotMatch(serialized, /example\.com|13800000000|token|secret|authorization|userId/)
})