Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/faker support #803

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ REST Client allows you to send HTTP request and view the response in Visual Stud
+ `{{$processEnv [%]envVarName}}`
+ `{{$dotenv [%]variableName}}`
+ `{{$aadToken [new] [public|cn|de|us|ppe] [<domain|tenantId>] [aud:<domain|tenantId>]}}`
- Generate realistic looking fake data using the faker.js library
+ `{{$faker name.lastName}}` generates a random lastname
+ For full Faker API checkout https://github.com/Marak/faker.js/blob/master/Readme.md
- Easily create/update/delete environments and environment variables in setting file
- File variables can reference both custom and system variables
- Support environment switch
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@
"dayjs": "^1.8.20",
"dotenv": "^8.2.0",
"encodeurl": "^1.0.1",
"faker": "^5.5.1",
"filesize": "^3.3.0",
"fs-extra": "^5.0.0",
"got": "^9.6.0",
Expand Down
2 changes: 2 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const AzureActiveDirectoryVariableName = "$aadToken";
export const AzureActiveDirectoryDescription = "Prompts to sign in to Azure AD and adds the token to the request";
export const AzureActiveDirectoryV2TokenVariableName = "$aadV2Token";
export const AzureActiveDirectoryV2TokenDescription = "Prompts to sign in to Azure AD V2 and adds the token to the request";
export const FakerVariableName = "$faker";
export const FakerDescription = "Generate realistic fake data for testing";

/**
* NOTE: The client id represents an AAD app people sign in to. The client id is sent to AAD to indicate what app
Expand Down
1 change: 1 addition & 0 deletions src/models/httpVariableResolveResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const enum ResolveWarningMessage {
IncorrectHeaderName = 'No value is resolved for given header name',
IncorrectJSONPath = 'No value is resolved for given JSONPath',
IncorrectRandomIntegerVariableFormat = 'RandomInt system variable should follow format "{{$randomInt minInteger maxInteger}}"',
IncorrectFakerVariableFormat = 'Faker expression should follow format "$faker <subject>.<method>}}"',
IncorrectProcessEnvVariableFormat = 'ProcessEnv system variable should follow format "{{$processEnv envVarName}}"',
IncorrectTimestampVariableFormat = 'Timestamp system variable should follow format "{{$timestamp [integer y|M|w|d|h|m|s|ms]}}"',
IncorrectDotenvVariableFormat = 'Dotenv variable should follow format "{{$dotenv variableName}}"',
Expand Down
6 changes: 6 additions & 0 deletions src/utils/httpElementFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ export class HttpElementFactory {
null,
Constants.AzureActiveDirectoryV2TokenDescription,
new SnippetString(`{{$\${name:${Constants.AzureActiveDirectoryV2TokenVariableName.slice(1)}}}}`)));
originalElements.push(new HttpElement(
Constants.FakerVariableName,
ElementType.SystemVariable,
null,
Constants.FakerDescription,
new SnippetString(`{{$\${name:${Constants.FakerVariableName.slice(1)}} {1:expression}}}`)));

// add environment custom variables
const environmentVariables = await EnvironmentVariableProvider.Instance.getAll();
Expand Down
17 changes: 17 additions & 0 deletions src/utils/httpVariableProviders/systemVariableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as adal from 'adal-node';
import dayjs, { Dayjs, OpUnitType } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import * as dotenv from 'dotenv';
import faker from 'faker';
import * as fs from 'fs-extra';
import * as path from 'path';
import { Clipboard, commands, env, QuickPickItem, QuickPickOptions, TextDocument, Uri, window } from 'vscode';
Expand Down Expand Up @@ -38,6 +39,8 @@ export class SystemVariableProvider implements HttpVariableProvider {

private readonly aadRegex: RegExp = new RegExp(`\\s*\\${Constants.AzureActiveDirectoryVariableName}(\\s+(${Constants.AzureActiveDirectoryForceNewOption}))?(\\s+(ppe|public|cn|de|us))?(\\s+([^\\.]+\\.[^\\}\\s]+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))?(\\s+aud:([^\\.]+\\.[^\\}\\s]+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))?\\s*`);

private readonly fakerRegex: RegExp = new RegExp(`\\${Constants.FakerVariableName}\\s+(\\w+\\..+)`);

private readonly innerSettingsEnvironmentVariableProvider: EnvironmentVariableProvider = EnvironmentVariableProvider.Instance;
private static _instance: SystemVariableProvider;

Expand All @@ -60,6 +63,7 @@ export class SystemVariableProvider implements HttpVariableProvider {
this.registerDotenvVariable();
this.registerAadTokenVariable();
this.registerAadV2TokenVariable();
this.registerFakerVariable();
}

public readonly type: VariableType = VariableType.System;
Expand Down Expand Up @@ -292,6 +296,19 @@ export class SystemVariableProvider implements HttpVariableProvider {
return {value: token};
});
}

private registerFakerVariable() {
this.resolveFuncs.set(Constants.FakerVariableName, async name => {
const groups = this.fakerRegex.exec(name);
if (groups !== null && groups.length === 2) {
const [, expression] = groups;
return { value: (faker.fake("{{" + expression + "}}")) };
}

return { warning: ResolveWarningMessage.IncorrectFakerVariableFormat };
});
}

private async resolveSettingsEnvironmentVariable(name: string) {
if (await this.innerSettingsEnvironmentVariableProvider.has(name)) {
const { value, error, warning } = await this.innerSettingsEnvironmentVariableProvider.get(name);
Expand Down