Stuntman
Building from source
Prerequisites
- pnpm package manager
- nvm node version manager (optional)
nvm use
pnpm install --frozen-lockfile
pnpm build
Start server
pnpm stuntman
Configuration
Stuntman uses config
You can create config/default.json
with settings of your liking matching ServerConfig
type
Running as a package
Install with package manager of your choice
npm install @stuntman/server
yarn add @stuntman/server
pnpm add @stuntman/server
Run from bin
stuntman
yarn stuntman
node ./node_modules/.bin/stuntman
Run programatically
import { Mock } from '../mock';
import { serverConfig } from '@stuntman/shared';
const mock = new Mock(serverConfig);
mock.start();
Point domain to localhost
Add some domains with .stuntman
suffix (or .stuntmanhttp
/ .stuntmanhttps
depending where you want to direct the traffic in proxy mode) to your /etc/hosts
for example
127.0.0.1 www.example.com.stuntman
Try in browser
go to your browser and visit http://www.example.com.stuntman:2015/
to see the proxied page
for local playground you can also use http://www.example.com.localhost:2015
Take a look at client
Take a look at ./src/clientTestExample.ts
, you can use it to set up some rules
Mind the scope of Stuntman.RemotableFunction
like matches
, modifyRequest
, modifyResponse
.
Stuntman.RemotableFunction.localFn
contains the function, but since it'll be executed on a remote mock server it cannot access any variables outside it's body. In order to pass variable values into the function use Stuntman.RemotableFunction.variables
for example:
matches: {
localFn: (req) => {
return /http:\/\/[^/]+\/somepath$/.test(req.url) && req.url.includes(`?someparam=${myVar}`);
},
localVariables: { myVar: 'myValue' },
}
You can build the rules using fluentish ruleBuilder
import { Client } from './apiClient';
import { ruleBuilder } from './ruleBuilder';
const client = new Client();
const uniqueQaUserEmail = 'unique_qa_email@example.com';
const rule = ruleBuilder()
.limitedUse(2)
.onRequestToHostname('example.com')
.withSearchParam('user', uniqueQaUserEmail)
.mockResponse({
localFn: (req) => {
if (JSON.parse(req.body).email !== uniqueQaUserEmail) {
return {
status: 500,
};
}
return { status: 201 };
},
localVariables: { uniqueQaUserEmail },
});
client.addRule(rule).then((x) => console.log(x));
Take a look at PoC of WebGUI
....just don't look to closely, it's very much incomplete and hacky