Skip to content

Example of using a proxy in front of Prism for programatic example selection

License

Notifications You must be signed in to change notification settings

agilepathway/custom-headers-prism

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

custom-headers-prism

Example of using a proxy in front of Prism for programatic example selection

Use cases

One use case is when you are testing a web application which connects to an API. Using a proxy as middleware between the web application and the API allows programatic control to specify a particular example or HTTP response from a Prism mock server of the API, without needing to modify the web application's API endpoint for each testing scenario. Instead all you need to do is specify the proxy server as the API endpoint.

Prerequisites to run the example

Codespace

An easy way to run the example is to create a free GitHub Codespace, as that will give you an instant development environment in the cloud. All you need is a GitHub account and a web browser (or VS Code).

Running the example

  1. Install Prism:

    npm install -g @stoplight/prism-cli

  2. Install node-http-proxy:

    npm install http-proxy --save

  3. Start a Prism mock server based on our example Petstore OpenAPI spec:

    prism mock petstore.oas3.yaml

    This OpenAPI spec is an exact copy of the petstore example on Prism's own GitHub repo. The only modification was to remove the security section, as that allows us to make a plain HTTP request to Prism (e.g. http://127.0.0.1:4010/pets/123) without needing to provide authorization headers.

  4. Start a Proxy server:

    node proxy.js

    The proxy server is a stand-alone node-http-proxy server, an exact copy of their stand-alone proxy server with proxy request header re-writing example. The only modifications were to point the proxy server to Prism, and to rewrite the Prefer header so that Prism serves up the example we specify there.

  5. Request a pet directly from Prism, and you will see you get the first example in the OpenAPI spec back (Fluffy the cat)

    curl http://127.0.0.1:4010/pets/123

  6. Request a pet from the proxy, and you will see you get the example specified in the proxy server back (Sharik the dog)

    curl http://127.0.0.1:5050/pets/123

That's all there is to it. Going back to the example use case above, it would be trivial to startup and teardown a proxy server for individual test cases, programatically specifying which Prism example or response code should be returned each time.

About

Example of using a proxy in front of Prism for programatic example selection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published