feat(cli): add support for request variables (#4275)

feat: add support for request variables in the CLI
This commit is contained in:
James George
2024-08-26 06:51:29 -07:00
committed by GitHub
parent 10e8f4ef19
commit 33b0a54af1
8 changed files with 449 additions and 45 deletions

View File

@@ -335,6 +335,7 @@ describe("hopp test [options] <file_path_or_id>", () => {
"secret-envs-persistence-scripting-envs.json",
"environment"
);
const args = `test ${COLL_PATH} --env ${ENVS_PATH}`;
const { error } = await runCLI(args);
@@ -343,6 +344,45 @@ describe("hopp test [options] <file_path_or_id>", () => {
},
{ timeout: 20000 }
);
describe("Request variables", () => {
test("Picks active request variables and ignores inactive entries", async () => {
const COLL_PATH = getTestJsonFilePath(
"request-vars-coll.json",
"collection"
);
const args = `test ${COLL_PATH}`;
const { error } = await runCLI(args);
expect(error).toBeNull();
});
test("Supports the usage of request variables along with environment variables", async () => {
const env = {
...process.env,
secretBasicAuthUsernameEnvVar: "username",
secretBasicAuthPasswordEnvVar: "password",
};
const COLL_PATH = getTestJsonFilePath(
"request-vars-coll.json",
"collection"
);
const ENVS_PATH = getTestJsonFilePath(
"request-vars-envs.json",
"environment"
);
const args = `test ${COLL_PATH} --env ${ENVS_PATH}`;
const { error, stdout } = await runCLI(args, { env });
expect(stdout).toContain(
"https://echo.hoppscotch.io/********/********"
);
expect(error).toBeNull();
});
});
});
describe("Test `hopp test <file_path_or_id> --delay <delay_in_ms>` command:", () => {

View File

@@ -0,0 +1,188 @@
{
"v": 2,
"name": "Request variables",
"folders": [],
"requests": [
{
"v": "6",
"auth": {
"authType": "inherit",
"authActive": true
},
"body": {
"body": "{\n \"<<httpBodyRawKey>>\": \"<<httpBodyRawValue>>\"\n}",
"contentType": "application/json"
},
"name": "request-variables-basic-usage",
"method": "POST",
"params": [
{
"key": "<<queryParamKey>>",
"value": "<<queryParamValue>>",
"active": true
},
{
"key": "inactive-query-param-key",
"value": "<<inactiveQueryParamValue>>",
"active": true
}
],
"headers": [
{
"key": "<<customHeaderKey>>",
"value": "<<customHeaderValue>>",
"active": true
},
{
"key": "inactive-header-key",
"value": "<<inactiveHeaderValue>>",
"active": true
}
],
"endpoint": "<<url>>",
"testScript": "pw.test(\"Accounts for active request variables\", ()=> {\n pw.expect(pw.response.body.args[\"query-param-key\"]).toBe(\"query-param-value\");\n\n const data = JSON.parse(pw.response.body.data)\n\n pw.expect(data[\"http-body-raw-key\"]).toBe(\"http-body-raw-value\")\n\n pw.expect(pw.response.body.headers[\"custom-header-key\"]).toBe(\"custom-header-value\");\n});\n\npw.test(\"Ignores inactive request variables\", () => {\n pw.expect(pw.response.body.args[\"inactive-query-param-key\"]).toBe(\"\")\n pw.expect(pw.response.body.args[\"inactive-header-key\"]).toBe(undefined)\n})",
"preRequestScript": "",
"requestVariables": [
{
"key": "url",
"value": "https://echo.hoppscotch.io",
"active": true
},
{
"key": "method",
"value": "POST",
"active": true
},
{
"key": "httpBodyRawKey",
"value": "http-body-raw-key",
"active": true
},
{
"key": "httpBodyRawValue",
"value": "http-body-raw-value",
"active": true
},
{
"key": "customHeaderKey",
"value": "custom-header-key",
"active": true
},
{
"key": "customHeaderValue",
"value": "custom-header-value",
"active": true
},
{
"key": "queryParamKey",
"value": "query-param-key",
"active": true
},
{
"key": "queryParamValue",
"value": "query-param-value",
"active": true
},
{
"key": "inactiveQueryParamValue",
"value": "inactive-query-param-value",
"active": false
},
{
"key": "inactiveHeaderValue",
"value": "inactive-header-value",
"active": false
}
]
},
{
"v": "6",
"auth": {
"authType": "none",
"password": "<<password>>",
"username": "<<username>>",
"authActive": true
},
"body": {
"body": "{\n \"username\": \"<<username>>\",\n \"password\": \"<<password>>\"\n}",
"contentType": "application/json"
},
"name": "request-variables-alongside-environment-variables",
"method": "POST",
"params": [
{
"key": "method",
"value": "<<method>>",
"active": true
}
],
"headers": [
{
"key": "test-header-key",
"value": "<<testHeaderValue>>",
"active": true
}
],
"endpoint": "<<url>>/<<path>>",
"testScript": "pw.test(\"The first occurrence is picked for multiple request variable occurrences with the same key.\", () => {\n pw.expect(pw.response.body.args.method).toBe(\"post\");\n});\n\npw.test(\"Request variables support recursive resolution and pick values from secret environment variables\", () => {\n const { username, password } = JSON.parse(pw.response.body.data)\n\n pw.expect(username).toBe(\"username\")\n pw.expect(password).toBe(\"password\")\n\n})\n\npw.test(\"Resolves request variables that are clubbed together\", () => {\n pw.expect(pw.response.body.path).toBe(\"/username/password\")\n})\n\npw.test(\"Request variables are prioritised over environment variables\", () => {\n pw.expect(pw.response.body.headers.host).toBe(\"echo.hoppscotch.io\")\n})\n\npw.test(\"Environment variable is picked if the request variable under the same name is empty\", () => {\n pw.expect(pw.response.body.headers[\"test-header-key\"]).toBe(\"test-header-value\")\n})",
"preRequestScript": "",
"requestVariables": [
{
"key": "url",
"value": "https://echo.hoppscotch.io",
"active": true
},
{
"key": "username",
"value": "<<recursiveBasicAuthUsernameReqVar>>",
"active": true
},
{
"key": "recursiveBasicAuthUsernameReqVar",
"value": "<<secretBasicAuthUsernameEnvVar>>",
"active": true
},
{
"key": "password",
"value": "<<recursiveBasicAuthPasswordReqVar>>",
"active": true
},
{
"key": "recursiveBasicAuthPasswordReqVar",
"value": "<<secretBasicAuthPasswordEnvVar>>",
"active": true
},
{
"key": "method",
"value": "post",
"active": true
},
{
"key": "method",
"value": "get",
"active": true
},
{
"key": "method",
"value": "put",
"active": true
},
{
"key": "path",
"value": "<<username>>/<<password>>",
"active": true
},
{
"key": "testHeaderValue",
"value": "",
"active": true
}
]
}
],
"auth": {
"authType": "inherit",
"authActive": true
},
"headers": []
}

View File

@@ -0,0 +1,34 @@
{
"v": 1,
"id": "cm00r7kpb0006mbd2nq1560w6",
"name": "Request variables alongside environment variables",
"variables": [
{
"key": "url",
"value": "https://echo.hoppscotch.io",
"secret": false
},
{
"key": "secretBasicAuthPasswordEnvVar",
"secret": true
},
{
"key": "secretBasicAuthUsernameEnvVar",
"value": "username",
"secret": true
},
{
"key": "username",
"secret": true
},
{
"key": "password",
"secret": true
},
{
"key": "testHeaderValue",
"value": "test-header-value",
"secret": false
}
]
}