Comparing version 1.0.0-alpha.1 to 1.0.0-alpha.2
{ | ||
"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', | ||
}]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9473
208
85
+ Addedjexl@^2.1.1
+ Added@babel/runtime@7.26.0(transitive)
+ Addedjexl@2.3.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removed@networkteam/eel@^1.4.1
- Removed@networkteam/eel@1.5.2(transitive)
- Removedcommander@2.20.3(transitive)
- Removeddiscontinuous-range@1.0.0(transitive)
- Removedmoo@0.5.2(transitive)
- Removednearley@2.20.1(transitive)
- Removedrailroad-diagrams@1.0.0(transitive)
- Removedrandexp@0.4.6(transitive)
- Removedret@0.1.15(transitive)