Skip to content

feat: Validate request option in Mock mode #2696

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

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
8 changes: 8 additions & 0 deletions packages/cli/src/commands/__tests__/commands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ test(`starts proxy server with default validate-request option to be overriden`,
expect.objectContaining({ validateRequest: false, host: '0.0.0.0' })
);
});

test(`starts mock server with default validate-request option to be overriden`, () => {
parser.parse(`mock /path/to -m -h 0.0.0.0 --validate-request=false`);

expect(createMultiProcessPrism).toHaveBeenLastCalledWith(
expect.objectContaining({ validateRequest: false, host: '0.0.0.0' })
);
});
4 changes: 3 additions & 1 deletion packages/cli/src/commands/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const mockCommand: CommandModule = {
}),
handler: async parsedArgs => {
parsedArgs.jsonSchemaFakerFillProperties = parsedArgs['json-schema-faker-fillProperties'];
const { multiprocess, dynamic, port, host, cors, document, errors, verboseLevel, ignoreExamples, seed, jsonSchemaFakerFillProperties } =
parsedArgs.validateRequest = parsedArgs['validate-request'];
const { multiprocess, dynamic, port, host, cors, document, errors, verboseLevel, ignoreExamples, seed, jsonSchemaFakerFillProperties, validateRequest } =
parsedArgs as unknown as CreateMockServerOptions;

const createPrism = multiprocess ? createMultiProcessPrism : createSingleProcessPrism;
Expand All @@ -59,6 +60,7 @@ const mockCommand: CommandModule = {
ignoreExamples,
seed,
jsonSchemaFakerFillProperties,
validateRequest,
};

await runPrismAndSetupWatcher(createPrism, options);
Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/commands/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ const proxyCommand: CommandModule = {
})
.options({
...sharedOptions,
'validate-request': {
description: 'Validate incoming HTTP requests.',
boolean: true,
default: true,
},
'upstream-proxy': {
description:
'If an http proxy is required to reach upstream, formatted as "{protocol}://[{user}[:{password}]@]{host}[:{port}]". eg "http://myUser:[email protected]:1234"',
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/commands/sharedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ const sharedOptions: Dictionary<Options> = {
default: false,
},

'validate-request': {
description: 'Validate incoming HTTP requests.',
boolean: true,
default: true,
},

verboseLevel: {
alias: 'v',
description: 'Turns on verbose logging.',
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async function createPrismServerWithLogger(options: CreateBaseServerOptions, log
throw new Error('No operations found in the current file.');
}

const validateRequest = isProxyServerOptions(options) ? options.validateRequest : true;
const validateRequest = options.validateRequest !== undefined ? options.validateRequest : true;
const shared = {
validateRequest,
validateResponse: true,
Expand Down Expand Up @@ -168,6 +168,7 @@ type CreateBaseServerOptions = {
ignoreExamples: boolean;
seed: string;
jsonSchemaFakerFillProperties: boolean;
validateRequest?: boolean;
};

export interface CreateProxyServerOptions extends CreateBaseServerOptions {
Expand Down
Loading