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

4

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

@@ -53,4 +53,4 @@ "main": "src/index.js",

"@hapi/boom": "^7.4.2",
"@networkteam/eel": "^1.4.1"
"jexl": "^2.1.1"
}
}

@@ -53,3 +53,3 @@ # hapi-mock

method: 'GET',
path: '/example',
path: '/example/{id}',
options: {

@@ -69,6 +69,9 @@ plugins: {

The `file` option refers to a JS module (e.g., `cases.js`) containing your mock cases, e.g.,
```
```js
module.exports = [{
condition: "params.id == '4711'",
condition: 'params.id == "4711"',
code: 418,
}, {
condition: 'headers["x-mock-case"] == 13',
body: 'case 13',
}];

@@ -75,0 +78,0 @@ ```

const Boom = require('@hapi/boom');
const Path = require('path');
const { compile } = require('@networkteam/eel');
const jexl = require('jexl');
const { name } = require('../package.json');

@@ -16,6 +16,6 @@

const mockIt = headers[headerName];
const routeOptions = route.settings.plugins[name];
if (!mockIt) { // do not mock
return h.continue;
}
const routeOptions = route.settings.plugins[name];
if (!routeOptions) { // no mocks available

@@ -28,5 +28,12 @@ return Boom.badRequest('no mocks for this route');

const cases = require(location); // eslint-disable-line global-require, import/no-dynamic-require, max-len
const mock = cases.find(({ condition }) => compile(condition)({
const context = {
headers, params, query, payload, method, path,
}));
};
const mock = cases.find(({ condition }, idx) => {
try {
return jexl.evalSync(condition, context);
} catch (error) {
throw new Error(`error in condition [${idx}]: ${error.message}`);
}
});
if (!mock) {

@@ -33,0 +40,0 @@ return Boom.badRequest('no matching mock found');

@@ -117,3 +117,3 @@ const Path = require('path');

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

@@ -129,2 +129,27 @@ method: 'GET',

});
it('should mock routes / condition by headers', async () => {
const res = await server.inject({
method: 'GET',
url: '/test2/4712',
headers: {
'x-hapi-mock': true,
'x-mock-case': 13,
},
});
expect(res.payload).to.be.equal('case 13');
expect(listener.handlers.called).to.equal(false);
});
it('should mock routes / no mock found', async () => {
const res = await server.inject({
method: 'GET',
url: '/test2/4712',
headers: {
'x-hapi-mock': true,
},
});
expect(res.statusCode).to.be.equal(400);
expect(listener.handlers.called).to.equal(false);
});
});
module.exports = [{
condition: "params.id == '4711'",
condition: 'params.id == "4711"',
code: 418,
}, {
condition: 'headers["x-mock-case"] == 13',
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