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-alpha.3 to 1.0.0-beta.1

2

package.json
{
"name": "hapi-mock",
"version": "1.0.0-alpha.3",
"version": "1.0.0-beta.1",
"description": "A simple HAPI plug-in for mocking endpoints",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -55,2 +55,3 @@ # hapi-mock

options: {
// ...
plugins: {

@@ -62,3 +63,3 @@ 'hapi-mock': { // activate mocking for this endpoint

},
handler: function (request, h) {
handler: async (request, h) => {
// ...

@@ -75,3 +76,11 @@ }

}, {
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',

@@ -81,3 +90,3 @@ }];

`condition` may refer to HAPI's route parameters `headers`, `params`, `query`, `payload`, `method`, and `path`.
`condition` may refer to HAPI's route parameters `headers`, `params`, `query`, `payload`, `method` (lowercase), and `path`.
The result parameters of a mock case can be `code`, `type`, and `body`.

@@ -88,2 +97,2 @@

You don't want to use this plug-in in production, of course.
Experimental. Have fun.
Have fun.
const Path = require('path');
const Hapi = require('@hapi/hapi');
const Joi = require('@hapi/joi');
const hapiAuthBasic = require('@hapi/basic');

@@ -61,2 +62,7 @@ const chai = require('chai');

auth: false,
validate: {
params: {
id: Joi.string().required(),
},
},
},

@@ -73,2 +79,7 @@ handler: () => {

auth: false,
validate: {
params: {
id: Joi.string().required(),
},
},
plugins: {

@@ -90,2 +101,7 @@ 'hapi-mock': {

auth: false,
validate: {
params: {
id: Joi.string().required(),
},
},
plugins: {

@@ -102,2 +118,22 @@ 'hapi-mock': {

};
const route4 = {
method: 'GET',
path: '/test4',
options: {
auth: false,
validate: {
query: {
id: Joi.string().required(),
},
},
plugins: {
'hapi-mock': {
},
},
},
handler: () => {
listener.handlers();
return 'ok';
},
};
const mock = {

@@ -112,3 +148,3 @@ plugin: hapiMock,

server.auth.default('simple');
await server.route([route1, route2, route3]);
await server.route([route1, route2, route3, route4]);
await server.start();

@@ -156,3 +192,3 @@ return server;

it('should mock routes / find condition by params', async () => {
it('should mock routes / find case by params', async () => {
const res = await server.inject({

@@ -169,3 +205,3 @@ method: 'GET',

it('should mock routes / find condition by headers', async () => {
it('should mock routes / find case by headers', async () => {
const res = await server.inject({

@@ -183,2 +219,15 @@ method: 'GET',

it('should mock routes / find case by query / json response', async () => {
const res = await server.inject({
method: 'GET',
url: '/test4?id=foo',
headers: {
'x-hapi-mock': true,
},
});
expect(res.statusCode).to.be.equal(200);
expect(JSON.parse(res.payload)).to.be.deep.equal({ bar: true });
expect(listener.handlers.called).to.equal(false);
});
it('should mock routes / no mock found', async () => {

@@ -185,0 +234,0 @@ const res = await server.inject({

@@ -5,4 +5,12 @@ module.exports = [{

}, {
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',
}];
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