test: harden admin acceptance fixtures and transport retries

This commit is contained in:
2026-09-24 04:38:38 +08:00
parent f5e53db24f
commit eb8662070a
6 changed files with 74 additions and 24 deletions
@@ -537,28 +537,36 @@ function findIsolatedInbound(context) {
}
async function multipartRequest(context, path, data) {
const form = new FormData()
form.append('data', new Blob([JSON.stringify(data)], { type: 'application/json' }))
form.append('image', new Blob([WEIGHT_IMAGE], { type: 'image/png' }), 'carton-evidence.png')
try {
const response = await fetch(`${context.options.gateway}${path}`, {
method: 'POST',
headers: { Accept: 'application/json', Authorization: `Bearer ${context.token}` },
body: form,
signal: AbortSignal.timeout(60_000),
})
const content = await response.text()
let body = null
try { body = content ? JSON.parse(content) : null } catch { /* Preserve only a bounded error string. */ }
return {
method: 'POST', path, http: response.status, body,
raw: body === null ? content.slice(0, 500) : null,
requestId: body?.requestId || response.headers.get('x-request-id'),
traceId: body?.traceId || response.headers.get('x-trace-id'),
let lastResponse = null
for (let attempt = 1; attempt <= 3; attempt += 1) {
const form = new FormData()
form.append('data', new Blob([JSON.stringify(data)], { type: 'application/json' }))
form.append('image', new Blob([WEIGHT_IMAGE], { type: 'image/png' }), 'carton-evidence.png')
try {
const response = await fetch(`${context.options.gateway}${path}`, {
method: 'POST',
headers: { Accept: 'application/json', Authorization: `Bearer ${context.token}` },
body: form,
signal: AbortSignal.timeout(60_000),
})
const content = await response.text()
let body = null
try { body = content ? JSON.parse(content) : null } catch { /* Preserve only a bounded error string. */ }
lastResponse = {
method: 'POST', path, http: response.status, body,
raw: body === null ? content.slice(0, 500) : null,
requestId: body?.requestId || response.headers.get('x-request-id'),
traceId: body?.traceId || response.headers.get('x-trace-id'),
}
if (lastResponse.http !== 0 || attempt === 3) return lastResponse
} catch (error) {
lastResponse = { method: 'POST', path, http: 0, body: null,
raw: error instanceof Error ? error.message : String(error), requestId: null, traceId: null }
if (attempt === 3) return lastResponse
}
} catch (error) {
return { method: 'POST', path, http: 0, body: null, raw: error instanceof Error ? error.message : String(error), requestId: null, traceId: null }
await new Promise((resolve) => setTimeout(resolve, 400 * attempt))
}
return lastResponse
}
async function requestWithHeaders(context, method, path, body, extraHeaders) {