This is a HTTP mocking server based on RoboHydra framework.
npm i -g robohydra
npm i --save-dev robohydra-mock-server
Create a robohydra.json
configuration file:
{
"plugins": [
"admin-extras",
"forward-proxy",
"swagger-mock",
"user-agent-summoner"
],
"pluginLoadPaths": [
"node_modules/robohydra-mock-server/plugins"
],
"summoner": {
"hydraPickerPlugin": "user-agent-summoner"
}
}
Then run a RoboHydra server:
robohydra robohydra.json
Connections will be accepted on http://localhost:3000/.
Let's add a some rules to our RoboHydra proxy. For example, let's it proxy connections to http://robohydra.org/.
curl -X POST \
--data-urlencode newHeadType=proxy \
--data-urlencode newProxyHeadMountPath=/robohydra.org/ \
--data-urlencode newProxyHeadProxyTo=http://robohydra.org/ \
--data-urlencode newProxyHeadSetHostHeader=on \
http://localhost:3000/robohydra-admin/head/create
Check that it works:
curl --proxy http://localhost:3000 http://robohydra.org/
It should output HTML code.
And any other URLs have been rejected:
curl --proxy http://localhost:3000 http://rkn.gov.ru/
It should output a Not Found
.
import MockServerClient = require('robohydra-mock-server/client');
import request = require('request');
/* create a client */
const roboHydraUrl = 'http://localhost:3000/';
const mockServerClient = new MockServerClient({ host: roboHydraUrl });
/* set expectations */
// proxy connections
const devServerProxy = () =>
mockServerClient.when({ url: `http://robohydra.org/` }).proxy();
// fake respose
const fakeServerError = () =>
mockServerClient
.when({ url: `http://robohydra.org/fake` })
.respose({ status: 500, body: '(Fake) Internal Server Error' });
const useIt = () =>
const r = request.defaults({'proxy': roboHydraUrl});
r.get(`http://robohydra.org/`)
.on('response', (resp) => console.log(resp.statusCode) ); // 200
r.get(`http://robohydra.org/fake`)
.on('response', (resp) => console.log(resp.statusCode) ); // 500
const removeAllExpectations = () => mockServerClient.reset()
Promise.all([devServerProxy(), fakeServerError()])
.then(useIt)
.then(removeAllExpectations);
- Add support for HTTPS.
- Add server-side validators.
- Add Swagger mocking head.
- Add support for multiple sessions.