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

  • 2.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

hapi-mock

Hapi server plugin for adding mock behavior to endpoints.

Build Status Coverage Status node code style Types License Status

Tested with Hapi 19/20 on Node 12/14/16.

Install

npm install hapi-mock

Purpose

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

v2 is going away from jexl expressions towards es6 conditions. And it is rewritten in TypeScript.

Usage (ES6)

Register the plugin with Hapi server like this:

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

// ...

const mock = {
  plugin: hapiMock,
  options: {
    triggerByHeader: true, // default
    headerName: 'x-hapi-mock', // default
  },
};

await server.register(mock);

Your route configuration may look like this:

server.route({
  method: 'GET',
  path: '/example/{id}',
  options: {
    // ...
    plugins: {
      'hapi-mock': { // add mock behavior to this endpoint
        mocks: [{
          condition: ({ params }) => params.id === '4711',
          code: 418,
        }, ...],
      },
    },
  },
  handler: // ...
});

The mocks option can also refer to a separate module, e.g.,

module.exports = [{
  condition: ({ params }) => params.id === '4711',
  code: 418,
}, {
  condition: ({ query }) => query.id === 'foo',
  type: 'application/json',
  body: {
    bar: 'qux',
  },
}, {
  condition: ({ headers }) => 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 maps the Hapi request object to true for applying the mock case. Response parameters of a mock can be code (default 200 or 204), type (default text/plain), body (default empty), and headers (default {}).

Have fun.

Options

Registration Options

triggerByHeader (optional, boolean, default true) -- When to apply mocks (provided that an endpoint has mocks configured). If true, mocks are only applied when the request header x-hapi-mock is set (any value). If false mocks are always applied.

headerName (optional, string, default x-hapi-mock) -- As request header, it must be set to activate mocks (unless triggerByHeader is false). As response header, it tells which mock case was applied (if any).

continueIfNoneMatch (optional, boolean, default true) -- What should be done if mocks are configured but none is matching. If true, the request is passed on. If false, the response is status code 422 "Unprocessable Entity".

Route Options

mocks (required, Array) -- List of mock cases for the respective endpoint.

continueIfNoneMatch (optional, boolean, default is registration option continueIfNoneMatch) -- What should be done if mocks are configured but none is matching. If true, the request is passed on. If false, the response is status code 422 "Unprocessable Entity".

Mock Cases

title (required, string) -- A descriptive title of the mock case.

condition (required, function (request: Hapi.Request) => boolean) -- The condition when the mock case shall be applied.

code (optional, number, default 200 or 204) -- Status code.

type (optional, string, default text/plain) -- Response content-type.

body (optional, string or object) -- Response body.

headers (optional, object) -- Response headers.

Keywords

FAQs

Package last updated on 17 May 2021

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