2026-07-20 19:31:43 +08:00
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
import test from 'node:test'
|
|
|
|
|
|
|
|
|
|
import { apiRequest, configureHttp } from '../src/http'
|
2026-07-28 20:29:06 +08:00
|
|
|
import { _setUserInfo, initConfigFromRemote } from '../src/config'
|
2026-07-20 19:31:43 +08:00
|
|
|
|
|
|
|
|
test('apiRequest aborts a stalled request at the shared timeout boundary', async () => {
|
|
|
|
|
const originalFetch = globalThis.fetch
|
|
|
|
|
configureHttp({ baseUrl: 'https://platform.example.test' })
|
|
|
|
|
globalThis.fetch = async (_input, init) =>
|
|
|
|
|
new Promise((_resolve, reject) => {
|
|
|
|
|
init?.signal?.addEventListener(
|
|
|
|
|
'abort',
|
|
|
|
|
() => reject(init.signal?.reason ?? new Error('aborted')),
|
|
|
|
|
{ once: true },
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await assert.rejects(
|
|
|
|
|
() => apiRequest('/api/v1/rn/release-set/check', { skipAuth: true, timeoutMs: 5 }),
|
|
|
|
|
/Request timed out after 5ms/,
|
|
|
|
|
)
|
|
|
|
|
} finally {
|
|
|
|
|
globalThis.fetch = originalFetch
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-07-28 20:29:06 +08:00
|
|
|
|
|
|
|
|
test('configured requests use public identity headers without client HMAC headers', async () => {
|
|
|
|
|
const originalFetch = globalThis.fetch
|
|
|
|
|
let sentHeaders = new Headers()
|
|
|
|
|
initConfigFromRemote(
|
|
|
|
|
{ appKey: 'public-app-key', platformUrl: 'https://platform.example.test' },
|
|
|
|
|
{},
|
|
|
|
|
)
|
|
|
|
|
await _setUserInfo({ userId: 'user-1' })
|
|
|
|
|
globalThis.fetch = async (_input, init) => {
|
|
|
|
|
sentHeaders = new Headers(init?.headers)
|
|
|
|
|
return new Response(JSON.stringify({ data: { ok: true } }), {
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
status: 200,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await apiRequest('/config', { skipAuth: true })
|
|
|
|
|
assert.equal(sentHeaders.get('X-App-Key'), 'public-app-key')
|
|
|
|
|
assert.equal(sentHeaders.get('X-User-Id'), 'user-1')
|
|
|
|
|
assert.equal(sentHeaders.has('X-Signature'), false)
|
|
|
|
|
assert.equal(sentHeaders.has('X-Nonce'), false)
|
|
|
|
|
assert.equal(sentHeaders.has('X-Timestamp'), false)
|
|
|
|
|
} finally {
|
|
|
|
|
await _setUserInfo(null)
|
|
|
|
|
globalThis.fetch = originalFetch
|
|
|
|
|
}
|
|
|
|
|
})
|