Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-mock

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-mock

A simple HAPI plug-in for mocking endpoints

  • 1.0.0-beta.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

hapi-mock

Hapi plugin for mocking endpoints.

Build Status Coverage Status node code style License Status

Tested with Node 12/13 and Hapi 19.

Install

npm install hapi-mock

Purpose

This plugin provides a simple way to mock your HAPI endpoints. It is experimental at this point of time.

Usage

Register the plugin with Hapi server like this:

const Hapi = require('@hapi/hapi');
const hapiMock = require('hapi-mock');

const server = new Hapi.Server({
  port: 3000,
});

const mock = {
  plugin: hapiMock,
  options: {
    baseDir: Path.join(__dirname, 'mocks'),
    validate: async (request) => ({ isValid: true }), // optional auth for mocks
  },
};

const provision = async () => {
  await server.register([mock]);
  // ...
  await server.start();
};

provision();

Your route configuration may look like this:

server.route({
  method: 'GET',
  path: '/example/{id}',
  options: {
    // ...
    plugins: {
      'hapi-mock': { // activate mocking for this endpoint
        file: './cases', // JS module relative to `baseDir`
      },
    },
  },
  handler: async (request, h) => {
    // ...
  }
});

The file option refers to a JS module (e.g., cases.js) containing your mock cases, e.g.,

module.exports = [{
  condition: 'params.id == "4711"',
  code: 418,
}, {
  condition: 'query.id == "foo"',
  type: 'application/json',
  body: {
    bar: true,
  },
}, {
  condition: 'headers["x-mock-case"] == 13',
  code: 200, // this is the default
  type: 'text/plain', // this is the default
  body: 'case 13',
  headers: {
    'x-mock-foo': 'bar',
  },
}];

condition is an expression that may refer to HAPI's route parameters headers, params, query, payload, method (lowercase), and path. The usual operators are supported (==, &&, ||, etc.). Response parameters of a mock can be code (default 200), type (default text/plain), body (default mocked!), and headers (default {}).

And finally, you need to set the HTTP header x-hapi-mock: true to a request to have a route use mocking rather than its real handler implementation.

You don't want to use this plug-in in production, of course. Have fun.

Keywords

FAQs

Package last updated on 29 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc