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

mock-n-roll

v0.1.0

Published

Configurable server for proxing and mocking REST

Downloads

7

Readme

mock-n-roll

Configurable server for proxing and mocking REST.

Motivation

  • You don't need mock-n-roll for mocking service layers in your unit tests, use Sinion.
  • You don't need mock-n-roll for mocking http requests in your integration tests, use network interceptors - Nock is cool.
  • When doing blackbox testing of your client side code, you may want to use mock-n-roll.

Demo

Below a short recorded demo, it should you give first impression about tool.

asciicast

Example

Let's imagine that you have REST service which implements following call:

GET https://jsonplaceholder.typicode.com/posts/1 200

Response:
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

And you would like to change response body and status code for it as well.

Something like this:

GET https://jsonplaceholder.typicode.com/posts/1 404

Response:
{
  "msg": "Requested post wasn't found!"
}

As mentioned above you can use nock but in some cases you need a proxy that additionaly can change responses.

Mock-n-roll works as usual proxy that redirects all not mocked requests to target, and otherwise if mock found it will be used to return appropriate response to caller.

Usage

First of all you need to define configuration file for your proxy server.

// mock-n-roll.conf.js - default name for config file

module.exports = {
  httpPort: 8080,
  httpsPort: 8443,
  prefix: '',
  target: 'https://jsonplaceholder.typicode.com',
  options: {
    key: fs.readFileSync(`${__dirname}/ssl.key`, 'utf-8'),
    cert: fs.readFileSync(`${__dirname}/ssl.cert`, 'utf-8')
  }
};
  • httpPort - defines port on which HTTP will be served.
  • httpsPort - defines port on which HTTPS will be served.
  • target - host to which requests will be proxied.
  • prefix - prefix which will be used for proxy (for example, proxy only requests under /api/v1).
  • options - contains only string values of key and cert for HTTPS, if options not defined, then server will run only HTTP.

By default config file will be searched in current working directory from which node process was started.

Easily you can start server using existed runner:

./bin/mock-n-roll --config ./my-custom-config.js --debug

A little bit about runner options:

  • debug - run server with debug messages.
  • config - custom path to config file, if not specified then default mock-n-roll.conf.js will be used.

After you should see something like this:

$ ./bin/mock-n-roll -c ./example/mock-n-roll.conf.js
info: Mock-n-Roll started...
info: HTTP  : 8080
info: HTTPS : 8443
info: Target: https://jsonplaceholder.typicode.com
info: Prefix: [none]

Now you mock-n-roll server is started and you can send requests to 'localhost' and it will redirect it to 'jsonplaceholder.typicode.com'.

Use /mocks route to configure your mocks.

POST - add new mock, or override existing.

POST http://localhost:8080/mocks 201

Payload:
{
  "path": "/posts/1",
  "code": 404,
  "body": {
    "msg": "Requested post wasn't found!"
  }
}

GET - returns all mocked routes.

GET http://localhost:8080/mocks 200

Response:
{
  "/posts/1": {
    "body": {
      "msg": "Requested post was not found!"
    },
    "code": 404
  }
};

DELETE - delete all existing mocked routes.

DELETE http://localhost:8080/mocks 204

For more convinent usage you can use existing REST client to access mock-n-roll server.

const MocknRollClient = require('mock-n-roll').client;

const client = new MocknRollClient({ host: 'localhost', port: 8080 });

const mock = {
  path: '/posts/1',
  code: 404,
  body: {
    msg: 'Requested post was not found!'
  }
};

client.add(mock, (error, response, body) => {
    // do something in callback
});

client.get((error, response, body) => {
    // do something in callback
});

client.delete((error, response, body) => {
    // do something in callback
});

Also you could look at the tests, it should give you additional information how you could run and use mock-n-roll.

License

Copyright 2016

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.