@testsmith/api-spector 0.0.5 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  const promises = require("fs/promises");
4
4
  const path = require("path");
5
5
  const undici = require("undici");
6
- const scriptRunner = require("./chunks/script-runner-CmdLWmKN.js");
6
+ const requestHandler = require("./chunks/request-handler-C1b53aHR.js");
7
7
  require("crypto");
8
8
  require("vm");
9
9
  require("dayjs");
@@ -150,7 +150,7 @@ async function loadEnvironments(workspace, dir) {
150
150
  }
151
151
  return envs;
152
152
  }
153
- async function executeRequest(req, collectionVars, envVars, globals, verbose) {
153
+ async function executeRequest(req, collectionVars, envVars, globals, verbose, tls) {
154
154
  const base = {
155
155
  requestId: req.id,
156
156
  name: req.name,
@@ -164,7 +164,7 @@ async function executeRequest(req, collectionVars, envVars, globals, verbose) {
164
164
  let updatedGlobals = { ...globals };
165
165
  let preScriptError;
166
166
  if (req.preRequestScript?.trim()) {
167
- const r = await scriptRunner.runScript(req.preRequestScript, {
167
+ const r = await requestHandler.runScript(req.preRequestScript, {
168
168
  envVars: { ...envVars },
169
169
  collectionVars: { ...collectionVars },
170
170
  globals: { ...globals },
@@ -175,41 +175,41 @@ async function executeRequest(req, collectionVars, envVars, globals, verbose) {
175
175
  updatedEnvVars = r.updatedEnvVars;
176
176
  updatedCollectionVars = r.updatedCollectionVars;
177
177
  updatedGlobals = r.updatedGlobals;
178
- scriptRunner.patchGlobals(r.updatedGlobals);
179
- await scriptRunner.persistGlobals();
178
+ requestHandler.patchGlobals(r.updatedGlobals);
179
+ await requestHandler.persistGlobals();
180
180
  if (verbose && r.consoleOutput.length) r.consoleOutput.forEach((l) => console.log(color(` [pre] ${l}`, C.gray)));
181
181
  if (r.error) console.error(color(` [pre-script error] ${r.error}`, C.red));
182
182
  }
183
- const vars = scriptRunner.mergeVars(updatedEnvVars, updatedCollectionVars, updatedGlobals, localVars);
184
- const resolvedUrl = scriptRunner.buildUrl(req.url, req.params, vars);
183
+ const vars = requestHandler.mergeVars(updatedEnvVars, updatedCollectionVars, updatedGlobals, localVars);
184
+ const resolvedUrl = requestHandler.buildUrl(req.url, req.params, vars);
185
185
  base.resolvedUrl = resolvedUrl;
186
186
  const start = Date.now();
187
187
  try {
188
188
  const authH = {};
189
189
  if (req.auth.type === "bearer") {
190
190
  let token = req.auth.token ?? "";
191
- if (!token && req.auth.tokenSecretRef) token = await scriptRunner.getSecret(req.auth.tokenSecretRef) ?? "";
192
- if (token) authH["Authorization"] = `Bearer ${scriptRunner.interpolate(token, vars)}`;
191
+ if (!token && req.auth.tokenSecretRef) token = await requestHandler.getSecret(req.auth.tokenSecretRef) ?? "";
192
+ if (token) authH["Authorization"] = `Bearer ${requestHandler.interpolate(token, vars)}`;
193
193
  }
194
194
  const headers = new undici.Headers();
195
195
  for (const h of req.headers) {
196
- if (h.enabled && h.key) headers.set(scriptRunner.interpolate(h.key, vars), scriptRunner.interpolate(h.value, vars));
196
+ if (h.enabled && h.key) headers.set(requestHandler.interpolate(h.key, vars), requestHandler.interpolate(h.value, vars));
197
197
  }
198
198
  for (const [k, v] of Object.entries(authH)) headers.set(k, v);
199
199
  let body;
200
200
  if (req.body.mode === "json" && req.body.json) {
201
- body = scriptRunner.interpolate(req.body.json, vars);
201
+ body = requestHandler.interpolate(req.body.json, vars);
202
202
  if (!headers.has("content-type")) headers.set("Content-Type", "application/json");
203
203
  } else if (req.body.mode === "raw" && req.body.raw) {
204
- body = scriptRunner.interpolate(req.body.raw, vars);
204
+ body = requestHandler.interpolate(req.body.raw, vars);
205
205
  if (!headers.has("content-type")) headers.set("Content-Type", req.body.rawContentType ?? "text/plain");
206
206
  } else if (req.body.mode === "graphql" && req.body.graphql) {
207
207
  const gql = req.body.graphql;
208
- const gqlBody = { query: scriptRunner.interpolate(gql.query, vars) };
208
+ const gqlBody = { query: requestHandler.interpolate(gql.query, vars) };
209
209
  const rawVars = gql.variables?.trim();
210
210
  if (rawVars) {
211
211
  try {
212
- gqlBody.variables = JSON.parse(scriptRunner.interpolate(rawVars, vars));
212
+ gqlBody.variables = JSON.parse(requestHandler.interpolate(rawVars, vars));
213
213
  } catch {
214
214
  }
215
215
  }
@@ -217,10 +217,12 @@ async function executeRequest(req, collectionVars, envVars, globals, verbose) {
217
217
  body = JSON.stringify(gqlBody);
218
218
  if (!headers.has("content-type")) headers.set("Content-Type", "application/json");
219
219
  }
220
+ const dispatcher = await requestHandler.buildDispatcher(void 0, tls);
220
221
  const fetchResp = await undici.fetch(resolvedUrl, {
221
222
  method: req.method,
222
223
  headers,
223
- body: ["GET", "HEAD"].includes(req.method) ? void 0 : body
224
+ body: ["GET", "HEAD"].includes(req.method) ? void 0 : body,
225
+ ...dispatcher ? { dispatcher } : {}
224
226
  });
225
227
  const responseBody = await fetchResp.text();
226
228
  const durationMs = Date.now() - start;
@@ -240,7 +242,7 @@ async function executeRequest(req, collectionVars, envVars, globals, verbose) {
240
242
  let consoleOutput = [];
241
243
  let postScriptError;
242
244
  if (req.postRequestScript?.trim()) {
243
- const r = await scriptRunner.runScript(req.postRequestScript, {
245
+ const r = await requestHandler.runScript(req.postRequestScript, {
244
246
  envVars: updatedEnvVars,
245
247
  collectionVars: updatedCollectionVars,
246
248
  globals: updatedGlobals,
@@ -250,8 +252,8 @@ async function executeRequest(req, collectionVars, envVars, globals, verbose) {
250
252
  testResults = r.testResults;
251
253
  consoleOutput = r.consoleOutput;
252
254
  postScriptError = r.error;
253
- scriptRunner.patchGlobals(r.updatedGlobals);
254
- await scriptRunner.persistGlobals();
255
+ requestHandler.patchGlobals(r.updatedGlobals);
256
+ await requestHandler.persistGlobals();
255
257
  if (verbose && r.consoleOutput.length) r.consoleOutput.forEach((l) => console.log(color(` [post] ${l}`, C.gray)));
256
258
  }
257
259
  const allPassed = testResults.every((t) => t.passed);
@@ -311,7 +313,7 @@ async function main() {
311
313
  console.error(color(`Error: could not read workspace file: ${wsPath}`, C.red));
312
314
  process.exit(1);
313
315
  }
314
- await scriptRunner.loadGlobals(wsDir);
316
+ await requestHandler.loadGlobals(wsDir);
315
317
  const collections = await loadCollections(workspace, wsDir);
316
318
  const environments = await loadEnvironments(workspace, wsDir);
317
319
  const env = envName ? environments.find((e) => e.name.toLowerCase() === envName.toLowerCase()) ?? null : null;
@@ -334,12 +336,14 @@ async function main() {
334
336
  const items = collectTagged(col.rootFolder, col.requests, col.collectionVariables ?? {}, filterTags);
335
337
  if (items.length === 0) continue;
336
338
  if (!firstColName) firstColName = col.name;
337
- const envVars = await scriptRunner.buildEnvVars(env);
338
- const globals = scriptRunner.getGlobals();
339
+ const envVars = await requestHandler.buildEnvVars(env);
340
+ const globals = requestHandler.getGlobals();
339
341
  console.log(color(` ┌ ${col.name}`, C.bold, C.white));
342
+ const workspaceTls = workspace.settings?.tls;
343
+ const effectiveTls = col.tls ? { ...workspaceTls, ...col.tls } : workspaceTls;
340
344
  let bailed = false;
341
345
  for (const item of items) {
342
- const result = await executeRequest(item.request, item.collectionVars, envVars, globals, verbose);
346
+ const result = await executeRequest(item.request, item.collectionVars, envVars, globals, verbose, effectiveTls);
343
347
  printResult(result, verbose);
344
348
  allResults.push(result);
345
349
  summary.total++;