npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jordanwalsh23/postman-local-mock-server

v0.2.4

Published

Provides the ability to run Postman collections as a local mock server.

Downloads

493

Readme

Postman Local Mock Server

npm version

This project brings Postman's collection mocking capability locally enabling you to create mock servers quickly and run tests against these.

Quick Start

npm install -g @jordanwalsh23/postman-local-mock-server
postman-local -c path/to/collection.json -p 8080

Capabilities

  • Create a local mock server by supplying a Postman Collection.
  • Customizable TCP Port number for your mock server.
  • Supports the x-mock-response-name and x-mock-response-code headers to specify the response you want returned by either name or status code.
  • Supports the x-mock-match-request-headers header to match only the responses that contain a specific header.
  • Supports the x-mock-match-request-body header to match responses on POST/PATCH/PUT requests.
  • Full support for Postman's dynamic variables in example responses.
  • Support for wildcard variables in response examples.
  • Support for TLS enabled servers by supplying key/certificate.
  • Supports a local cache for performance testing.

CLI Options

--collection, -c : Path to your collection file
--port, -p       : Port you would like to use to host the server 0-65535
--key            : Path to Private Key file for TLS protected servers
--cert           : Path to Certificate file for TLS protected servers
--debug, -d      : Print debug statements to console when running.
--cacheTTL       : The time to keep responses in cache - see apicache for options.

Using in your project

  1. Run npm install @jordanwalsh23/postman-local-mock-server
  2. Add the dependency to your project and start the server.
const PostmanLocalMockServer = require('@jordanwalsh23/postman-local-mock-server');

let options = {
  port = 3555
}

//Create the collection object.
options.collection = JSON.parse(fs.readFileSync('./test/test-collection.json', 'utf8'));

//Create a new server
let server = new PostmanLocalMockServer(options);

//Start the server
server.start();

//Run some requests against your server
axios.get(`http://localhost:3555`).then(res => {
  //do something with the mocked response.
});

//Stop the server
server.stop();
  1. The server will now have endpoints available that match your specified collection.

Caching

This project includes a local cache that can be enabled via the CLI with the --cacheTTL flag or as an object when starting your server e.g.

//Start the server
server.start({
    cache: true,
    cacheOptions: {
        defaultDuration: "500ms"
    }
})

The defaultDuration and cacheTTL parameters must either specify a number of milliseconds, or use plain text english. Some valid examples:

defaultDuration: "500ms" 
defaultDuration: "1 minute" 
defaultDuration: "5 minutes" 
defaultDuration: "1 hour" 
defaultDuration: "1 day" 

Clearing the cache

Restarting the server will automatically clear the in-memory cache, however if you want to clear the cache while the server is still running you can use the following endpoint:

DELETE http://localhost:<PORT>/cache

204 No Content

Unfortunately this will clash if there is an endpoint in your supplied collection with the path /cache. In that instance you can fork a copy of the source code and change this path manually.

Known Issues/Limitations

Tests/Prerequest scripts are not executed

This emulates the endpoints of a collection and the associated example responses. It does not invoke the pre-request or test scripts within a request.

As such, any requests that are reliant on variables (either collection/environment or global) will not work in this library.

Responses for the same path will take the first available

If your collection has the same path (e.g. /api/products) available multiple times, the first response defined will be the one returned by default - regardless of whether this is a successful or error response code.

There are several ways to overcome this:

  • Use the x-mock-response-name header on your requests to name the mock response you want returned.
  • Use the x-mock-response-code header on your requests to specify the response code (e.g. 200, 404) you want returned.

If you still cannot get the server to return your specific response, create an issue on this repo with the collection supplied and we'll try to replicate.

Request Matching algorithm differs from official Postman algorithm

  • This library uses a scoring based algorithm that mirrors closely, but does not fully match the official algorithm
  • Differences include:
    • If x-mock-response-code is used and multiple items are found the algorithm will just return the first item instead of prioritising the 2xx response.
    • If the document distance algorithm is used as no other matches can be found, the first result that meets the criteria is returned instead of calculating all responses and returning the highest score.

Contributions

Contributions are welcome on this repo. Submit issues or PRs and these will be reviewed for merging.

License

See the LICENSE file.