Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
{ | ||
"name": "hapi-mock", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"description": "A simple HAPI plug-in for mocking endpoints", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -84,7 +84,10 @@ # hapi-mock | ||
body: 'case 13', | ||
headers: { | ||
'x-mock-foo': 'bar', | ||
}, | ||
}]; | ||
``` | ||
`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`. | ||
`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 `{}`). | ||
@@ -91,0 +94,0 @@ 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. |
@@ -43,10 +43,14 @@ const Boom = require('@hapi/boom'); | ||
type = 'text/plain', | ||
body = 'mocked', | ||
body = 'mocked!', | ||
headers: headersRes = {}, | ||
} = mock; | ||
return h | ||
.response(body) | ||
.type(type) | ||
.code(code) | ||
.header(headerName, true) | ||
.takeover(); | ||
const response = h.response(body); | ||
response.type(type); | ||
response.code(code); | ||
response.header(headerName, true); | ||
Object.entries(headersRes).forEach(([key, value]) => { | ||
response.header(key, value); | ||
}); | ||
response.takeover(); | ||
return response; | ||
} catch (error) { | ||
@@ -53,0 +57,0 @@ server.log(['error'], error); |
@@ -178,3 +178,3 @@ const Path = require('path'); | ||
it('should not mock routes / pass on to handler', async () => { | ||
it('should not mock routes / pass on to real handler', async () => { | ||
const res = await server.inject({ | ||
@@ -197,6 +197,8 @@ method: 'GET', | ||
expect(res.statusCode).to.be.equal(418); | ||
expect(res.headers['content-type']).to.be.equal('text/plain; charset=utf-8'); | ||
expect(res.payload).to.be.equal('mocked!'); | ||
expect(listener.handlers.called).to.equal(false); | ||
}); | ||
it('should mock routes / find case by headers', async () => { | ||
it('should mock routes / find case by headers / set header', async () => { | ||
const res = await server.inject({ | ||
@@ -210,3 +212,6 @@ method: 'GET', | ||
}); | ||
expect(res.statusCode).to.be.equal(200); | ||
expect(res.headers['content-type']).to.be.equal('text/plain; charset=utf-8'); | ||
expect(res.payload).to.be.equal('case 13'); | ||
expect(res.headers['x-mock-foo']).to.be.equal('bar'); | ||
expect(listener.handlers.called).to.equal(false); | ||
@@ -224,2 +229,3 @@ }); | ||
expect(res.statusCode).to.be.equal(200); | ||
expect(res.headers['content-type']).to.be.equal('application/json; charset=utf-8'); | ||
expect(JSON.parse(res.payload)).to.be.deep.equal({ bar: true }); | ||
@@ -226,0 +232,0 @@ expect(listener.handlers.called).to.equal(false); |
@@ -15,2 +15,5 @@ module.exports = [{ | ||
body: 'case 13', | ||
headers: { | ||
'x-mock-foo': 'bar', | ||
}, | ||
}]; |
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
13264
329
97