Skip to content

Commit

Permalink
feat(proxy): add a flag to skip request validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda Phelan committed Feb 2, 2022
1 parent d310b19 commit 71d04c8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 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('https://github.com/')} --validate-request=false`);

expect(createMultiProcessPrism).toHaveBeenLastCalledWith(
expect.objectContaining({ validateRequest: false, host: '0.0.0.0' })
);
});
18 changes: 14 additions & 4 deletions packages/cli/src/commands/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { pick } from 'lodash';
import { CommandModule } from 'yargs';
import { CommandModule, parsed } from 'yargs';
import { createMultiProcessPrism, CreateProxyServerOptions, createSingleProcessPrism } from '../util/createServer';
import sharedOptions from './sharedOptions';
import { runPrismAndSetupWatcher } from '../util/runner';
import { boolean } from 'fp-ts';

const proxyCommand: CommandModule = {
describe: 'Start a proxy server with the given document file',
Expand All @@ -24,18 +25,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 71d04c8

Please sign in to comment.