Skip to content

Commit

Permalink
Merge pull request #1980 from craicoverflow/add-request-validation-by…
Browse files Browse the repository at this point in the history
…pass-option
  • Loading branch information
Daniel A. White authored Feb 2, 2022
2 parents f873dbb + 46211f0 commit 1fa9334
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/getting-started/03-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ curl -v -X POST http://localhost:4010/pet/

The response body contains the found output violations.

In some cases you may want to skip request validation. A common scenario would be where you want to check if your HTTP handlers validate the request appropriately.
Prism will validate all requests by default, but you can skip this by setting the flag to `--validate-request` flag to `false`.

```bash
prism proxy examples/petstore.oas2.yaml https://petstore.swagger.io/v2 --errors --validate-request false
```

```bash
curl -v -X POST http://localhost:4010/pet/ -d '{"name"": "Skip", "species": 100}'

< HTTP/1.1 422 Unprocessable Entity
{"statusCode": 400, "message": "Pet 'species' field should be a string, got integer", "code": "PET-ERROR-400"}
```

<!-- theme: info -->

> Server definitions (OAS3) and Host + BasePath (OAS2) are ignored. You need to manually specify the upstream URL when invoking Prism.
Expand Down
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 @@ -71,3 +71,11 @@ describe.each<{ 0: string; 1: string; 2: unknown }>([
expect(createMultiProcessPrism).toHaveBeenLastCalledWith(expect.objectContaining({ errors: true }));
});
});

test(`starts proxy server with default validate-request option to be overriden`, () => {
parser.parse(`proxy /path/to -m -h 0.0.0.0 ${new URL('http://github.com/')} --validate-request=false`);

expect(createMultiProcessPrism).toHaveBeenLastCalledWith(
expect.objectContaining({ validateRequest: false, host: '0.0.0.0' })
);
});
15 changes: 12 additions & 3 deletions packages/cli/src/commands/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ const proxyCommand: CommandModule = {
throw new Error(`Invalid upstream URL provided: ${value}`);
}
})
.options(sharedOptions),
.options({
...sharedOptions,
'validate-request': {
description: 'Validate incoming HTTP requests.',
boolean: true,
default: true,
},
}),
handler: parsedArgs => {
parsedArgs.validateRequest = parsedArgs['validate-request'];
const p: CreateProxyServerOptions = pick(
(parsedArgs as unknown) as CreateProxyServerOptions,
parsedArgs as unknown as CreateProxyServerOptions,
'dynamic',
'cors',
'host',
'port',
'document',
'multiprocess',
'upstream',
'errors'
'errors',
'validateRequest'
);

const createPrism = p.multiprocess ? createMultiProcessPrism : createSingleProcessPrism;
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ async function createPrismServerWithLogger(options: CreateBaseServerOptions, log
throw new Error('No operations found in the current file.');
}

const validateRequest = isProxyServerOptions(options) ? options.validateRequest : true;
const shared: Omit<IHttpConfig, 'mock'> = {
validateRequest: true,
validateRequest,
validateResponse: true,
checkSecurity: true,
errors: false,
Expand Down Expand Up @@ -141,6 +142,7 @@ type CreateBaseServerOptions = {
export interface CreateProxyServerOptions extends CreateBaseServerOptions {
dynamic: false;
upstream: URL;
validateRequest: boolean;
}

export type CreateMockServerOptions = CreateBaseServerOptions;
Expand Down

0 comments on commit 1fa9334

Please sign in to comment.