Skip to content

Commit

Permalink
Merge branch 'main' into vsc#16_multi-jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
vasan-agrostar committed Jun 29, 2024
2 parents a2e3eba + 7542632 commit 2142a3d
Show file tree
Hide file tree
Showing 18 changed files with 850 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
with:
node-version: "20"
- name: prettier check
run: npx prettier -c ./src --config ./src/.prettierrc
run: npx prettier -c ./src ./tests --config ./.prettierrc
File renamed without changes.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [todo]
- Introduced automated tests

## 1.5.1
- Fixed regression (status check failures were not detected)

Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
silent: false,
testTimeout: 120*1000,
};
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/constructCurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { getStringValueIfDefined } from "./utils/typeUtils";
import { getParamsForUrl, getURL } from "./executeRequest";
import { RequestSpec } from "./models";

function replaceSingleQuotes<T>(value: T): T {
if (typeof value !== "string") return value;
return value.replace(/'/g, "%27") as T & string;
}

export function getCurlRequest(request: RequestSpec): string {
let curl: string = "curl";

Expand All @@ -11,13 +16,14 @@ export function getCurlRequest(request: RequestSpec): string {

// headers
if (request.httpRequest.headers !== undefined) {
for (const header in request.httpRequest.headers)
curl += ` -H '${header}: ${request.httpRequest.headers[header]}'`;
for (const header in request.httpRequest.headers) {
curl += ` -H '${replaceSingleQuotes(`${header}: ${request.httpRequest.headers[header]}`)}'`;
}
}

// body
if (request.httpRequest.body !== undefined)
curl += ` -d '${getStringValueIfDefined(request.httpRequest.body)}'`;
curl += ` -d '${replaceSingleQuotes(getStringValueIfDefined(request.httpRequest.body))}'`;

// options.follow
if (request.options.follow) curl += " -L";
Expand All @@ -29,10 +35,12 @@ export function getCurlRequest(request: RequestSpec): string {
if (request.options.showHeaders) curl += " -i";

// url w/ params
curl += ` '${getURL(
request.httpRequest.baseUrl,
request.httpRequest.url,
getParamsForUrl(request.httpRequest.params, request.options.rawParams),
curl += ` '${replaceSingleQuotes(
getURL(
request.httpRequest.baseUrl,
request.httpRequest.url,
getParamsForUrl(request.httpRequest.params, request.options.rawParams),
),
)}'`;

return curl;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
GotRequest,
TestResult,
SpecResult,
Tests,
} from "./models";

export { getRequestPositions, getAllRequestSpecs, getRequestSpec } from "./parseBundle";
Expand Down
7 changes: 4 additions & 3 deletions src/replaceVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export function replaceVariablesInRequest(request: RequestSpec, variables: Varia

type keyOfHttp = Exclude<keyof typeof request.httpRequest, "method">;
const httpPropertiesToReplace: string[] = ["baseUrl", "url", "params", "headers", "body"];
httpPropertiesToReplace.forEach((key) => {
const replacedData = replaceVariables(request.httpRequest[key as keyOfHttp], variables);
request.httpRequest[key as keyOfHttp] = replacedData.data;
httpPropertiesToReplace.forEach((prop) => {
const httpKey = prop as keyOfHttp;
const replacedData = replaceVariables(request.httpRequest[httpKey], variables);
request.httpRequest[httpKey] = replacedData.data;
undefs.push(...replacedData.undefinedVars);
});

Expand Down
29 changes: 23 additions & 6 deletions src/variableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ export function getEnvironments(bundleContent: string | undefined, varFileConten
return [...uniqueNames];
}

function replaceEnvironmentVariables(vars: Variables): Variables {
const PREFIX = "$env.";

const getVal = (val: any): any => {
if (typeof val !== "string" || !val.startsWith(PREFIX)) return val;

const envVarName = val.slice(PREFIX.length);
return envVarName in process.env ? process.env[envVarName] : val;
};

const replacedVars: Variables = {};
for (const key in vars) replacedVars[key] = getVal(vars[key]);

return replacedVars;
}

export function loadVariables(
envName: string | undefined,
bundleContent: string | undefined,
Expand All @@ -46,15 +62,16 @@ export function loadVariables(
if (!envName) return {};

const allBundleVariables = getBundleVariables(bundleContent);
const bundleVars = allBundleVariables.hasOwnProperty(envName) ? allBundleVariables[envName] : {};
const bundleVars: Variables = allBundleVariables[envName] ?? {};

let envVars = {};
const envVars: Variables = {};
varFileContents.forEach((fileContents) => {
const parsedData = YAML.parse(fileContents);
if (parsedData && isDict(parsedData[envName])) {
Object.assign(envVars, parsedData[envName]);
}
if (parsedData && isDict(parsedData[envName])) Object.assign(envVars, parsedData[envName]);
});

return Object.assign({}, envVars, bundleVars);
const basicVars = Object.assign({}, envVars, bundleVars);
const vars = replaceEnvironmentVariables(basicVars);

return vars;
}
9 changes: 9 additions & 0 deletions tests/bundles/auto-test.zzv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# to ensure that the module is reading from varfiles, we put some essential
# variables in the varfiles

default:
getUrl: /get
fooVar: bar
random:
getUrl: /random
fooVar: random
Loading

0 comments on commit 2142a3d

Please sign in to comment.