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 - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0-beta.2

build/src/index.d.ts

60

package.json
{
"name": "hapi-mock",
"version": "1.0.0",
"version": "2.0.0-beta.2",
"description": "A simple HAPI plug-in for mocking endpoints",
"main": "src/index.js",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
"scripts": {
"lint": "eslint . --ignore-path ./.eslintignore",
"test": "NODE_ENV=test nyc --reporter=lcov --reporter=text-summary mocha --exit --recursive test",
"coveralls": "nyc report --reporter=lcovonly && cat ./coverage/lcov.info | coveralls",
"preversion": "npm run lint && npm test"
"coveralls": "npm run test && cat ./coverage/lcov.info | coveralls",
"preversion": "npm run lint && npm test",
"build": "npm run build:clear && npm run build:copy && npm run build:ts",
"build:clear": "rm -rf ./build",
"build:copy": "mkdir -p build/src && cp src/index.d.ts build/src",
"build:ts": "tsc",
"lint": "npm run lint:ts",
"lint:ts": "eslint src/**/*.ts --ignore-path ./.eslintignore",
"lint:fix": "eslint src/**/*.ts --ignore-path ./.eslintignore --fix",
"test": "jest",
"test:all": "npm run lint && npm test",
"test:ci": "npm run lint && CI=true npm test",
"prepublishOnly": "npm run build && npm run test:all"
},

@@ -16,2 +26,5 @@ "repository": {

},
"files": [
"build/src/**"
],
"keywords": [

@@ -33,25 +46,24 @@ "hapi",

},
"dependencies": {
"@hapi/boom": "^9.1.2"
},
"devDependencies": {
"@hapi/basic": "^6.0.0",
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"@hapi/hapi": "^20.1.2",
"@types/hapi__hapi": "^20.0.8",
"@types/jest": "^26.0.23",
"@types/mocha": "^8.2.2",
"@types/node": "^15.3.0",
"@typescript-eslint/eslint-plugin": "^4.24.0",
"coveralls": "^3.1.0",
"eslint": "^7.16.0",
"eslint": "^7.26.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-plugin-import": "^2.23.2",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-should-promised": "^2.0.0",
"hapi19": "npm:@hapi/hapi@^19.2.0",
"hapi20": "npm:@hapi/hapi@^20.0.3",
"joi": "^17.1.0",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"sinon": "^9.2.2",
"sinon-chai": "^3.5.0"
},
"dependencies": {
"@hapi/boom": "^9.1.1",
"jexl": "^2.3.0"
"jest": "^26.6.3",
"ts-jest": "^26.5.6",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
}
}

104

README.md
# hapi-mock
Hapi server plugin for mocking endpoints.
Hapi server plugin for adding mock behavior to endpoints.

@@ -9,9 +9,7 @@ [![Build Status](https://travis-ci.org/frankthelen/hapi-mock.svg?branch=master)](https://travis-ci.org/frankthelen/hapi-mock)

[![code style](https://img.shields.io/badge/code_style-airbnb-brightgreen.svg)](https://github.com/airbnb/javascript)
[![Types](https://img.shields.io/npm/types/rools.svg)](https://www.npmjs.com/package/rools)
[![License Status](http://img.shields.io/npm/l/hapi-mock.svg)]()
Tested with
Tested with Hapi 19/20 on Node 12/14/16.
* Hapi 20 on Node 12/14/15
* Hapi 19 on Node 12/14/15
## Install

@@ -25,8 +23,12 @@

This plugin provides a simple way to mock your HAPI endpoints.
This plugin provides a simple way to add mock behavior to endpoints.
It is *experimental* at this point of time.
## Usage
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:
```js

@@ -36,5 +38,3 @@ const Hapi = require('@hapi/hapi');

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

@@ -44,17 +44,12 @@ const mock = {

options: {
baseDir: Path.join(__dirname, 'mocks'),
validate: async (request) => ({ isValid: true }), // optional auth for mocks
triggerByHeader: true, // default
headerName: 'x-hapi-mock', // default
},
};
const provision = async () => {
await server.register([mock]);
// ...
await server.start();
};
provision();
await server.register(mock);
```
Your route configuration may look like this:
```js

@@ -67,26 +62,28 @@ server.route({

plugins: {
'hapi-mock': { // activate mocking for this endpoint
file: './cases', // JS module relative to `baseDir`
'hapi-mock': { // add mock behavior to this endpoint
mocks: [{
condition: ({ params }) => params.id === '4711',
code: 418,
}, ...],
},
},
},
handler: async (request, h) => {
// ...
}
handler: // ...
});
```
The `file` option refers to a JS module (e.g., `cases.js`) containing your mock cases, e.g.,
The `mocks` option can also refer to a separate module, e.g.,
```js
module.exports = [{
condition: 'params.id == "4711"',
condition: ({ params }) => params.id === '4711',
code: 418,
}, {
condition: 'query.id == "foo"',
condition: ({ query }) => query.id === 'foo',
type: 'application/json',
body: {
bar: true,
bar: 'qux',
},
}, {
condition: 'headers["x-mock-case"] == 13',
condition: ({ headers }) => headers['x-mock-case'] === '13',
code: 200, // this is the default

@@ -101,8 +98,47 @@ type: 'text/plain', // this is the default

`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 `{}`).
`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 `{}`).
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.
Have fun.
You don't want to use this plug-in in production, of course.
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.
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