( payload: HttpExecutePayload, )
| 380 | } |
| 381 | |
| 382 | async function executeHttpRequest( |
| 383 | payload: HttpExecutePayload, |
| 384 | ): Promise<HttpExecuteResult> { |
| 385 | const variables = resolveEnvironmentVariables(payload.environmentId) |
| 386 | const interpolated = interpolateRequest(payload.request, variables) |
| 387 | |
| 388 | const headersWithAuth = applyAuth(interpolated.auth, interpolated.headers) |
| 389 | const headersObj = toHeadersObject(headersWithAuth) |
| 390 | const built = buildBody( |
| 391 | interpolated.bodyType, |
| 392 | interpolated.body, |
| 393 | interpolated.formData, |
| 394 | ) |
| 395 | |
| 396 | const hasContentType = Object.keys(headersObj).some( |
| 397 | k => k.toLowerCase() === 'content-type', |
| 398 | ) |
| 399 | if (!hasContentType && built.contentType) { |
| 400 | headersObj['Content-Type'] = built.contentType |
| 401 | } |
| 402 | |
| 403 | let finalUrl: string |
| 404 | try { |
| 405 | finalUrl = buildUrl(interpolated.url, interpolated.query) |
| 406 | } |
| 407 | catch (error) { |
| 408 | return { |
| 409 | status: null, |
| 410 | statusText: '', |
| 411 | headers: [], |
| 412 | body: '', |
| 413 | bodyKind: 'text', |
| 414 | durationMs: 0, |
| 415 | sizeBytes: 0, |
| 416 | truncated: false, |
| 417 | error: error instanceof Error ? error.message : 'Invalid URL', |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | const timeoutMs = payload.timeoutMs ?? DEFAULT_TIMEOUT_MS |
| 422 | const controller = new AbortController() |
| 423 | const timer = setTimeout(() => controller.abort(), timeoutMs) |
| 424 | const startedAt = Date.now() |
| 425 | const startedAtPerf = performance.now() |
| 426 | |
| 427 | try { |
| 428 | const response = await undiciRequest(finalUrl, { |
| 429 | method: interpolated.method, |
| 430 | headers: headersObj, |
| 431 | body: built.body as Dispatcher.DispatchOptions['body'], |
| 432 | signal: controller.signal, |
| 433 | maxRedirections: 5, |
| 434 | ...(payload.skipCertificateVerification |
| 435 | ? { dispatcher: insecureCertificateDispatcher } |
| 436 | : {}), |
| 437 | }) |
| 438 | |
| 439 | const { buffer, sizeBytes, truncated } = await readBodyCapped( |
no test coverage detected