Open Source Express Server for mocked services. Mock your services and test as you wish
You have to have node js installed in your computer. 'https://github.com/nodesource/distributions/blob/master/README.md'
- download or clone from the https://github.com/kwar0715/mockableExpress
- open the project
- Install yarn 'https://linuxize.com/post/how-to-install-yarn-on-ubuntu-18-04/'
- run
yarn install
- run the project by using below commands
- yarn
yarn start
or on custom portyarn start 80
- docker command
- build
docker build . -t <name>
- run
docker run -p 80:80 <name>
(if run in detached mode use-d
)
- goto mocakble express url
Default Port is 3000
- when you run the server first, You should change the default password. type username as
user
and password as12345678
- reset password
- enjoy :-)
we use pm2 process manager to run mockableExpress in detached mode.
- install
npm install pm2 -g
- Export Variables
export HOST='your host'
export API_PORT='your port'
- start mockable express
#goto mockable express root
pm2 start index.js
Mockable express is providing token based authentication for endpoints,
- click on the menu icon on right side and create token
- create new domain and path,
- in the path creation view, enable the Authorization check box
- Add authentication to your header.
- Keep token safe.
- You can pass your true data as url parameters and make the custom responses
As an example your url can be like below one
<host>/users/:userID
<host>/users/?userID=123456
{
"userID":"123456"
}
then your response can be customized by the url paramter
{
Id : {{userID}},
Name: "Name"
}
then Id can be changed according to the userID parameter
You can use your own database to save and retrive your mocked data temporarly.
{
#save("<your key >","<your value>")#
}
save with parameters,
you are passing a value as URL, Query, Body parameter. you can add you parameter as below,
url : /users/:id/:value
{
#save("{{id}}","{{value}}")#
}
{
value: #get("<your key >")#
}{
}
you are passing a value as URL, Query, Body parameter. you can retrieve your data as below,
url : /users/:id
{
value: #get("{{id}}")#
}
#random(minimum,maximum,number of decimal points)#
you can create a random number between 0 and 10 and round off to 2 digits.
#random(0,10,2)#
#del("<your key >")#
you are passing a value as URL, Query, Body parameter. you can retrieve your data as below,
url : /users/:id
#del("{{id}}")#
You can return difference response according to a condition.
#if("value1",<operator>,"value2"){
response
}endif
value1 and value2 are compaired by the operator what you are using, if the value1 and value2 are satisfied by the comparison, so only the below response will be returened.
Operator | Discription |
---|---|
= | equal to |
!= | not equal |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
*< | starts With |
*> | ends With |
Ex:- suppose you need to send A if userId equals to 10, unless you need to send B as the response
url : /users/:id
#if("{{id}}",=,"10"){
A
}endif
B
you can add json object in the response
#if("{{id}}",=,"10"){
{
value: "A"
}
}endif
{
value: "B"
}
You can generate your response from iterative manner. if you want to repeate something you can use this syntax.
#for("<Count>"){
<Repeatable part>
}endfor
suppose you need to pring "Hello" 10 times,
#for("10"){
Hello
}endfor
then your response will be
HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLO
add to json response
{
"value": "#for("10"){
Hello
}endfor"
}
You can make the response according to the array of comma seperated response. your body, query parameter can be a comma seperated array or string,
body param:
{
"arr": [1,2,3,4,5]
}
or
{
"arr": "1,2,3,4,5"
}
query param:
<host>/path?arr=1,2,3,4,5
usage can be
#foreach("[1,2,3,4,5]","ele"){
{{ele}}
}endforeach
element iterative element and you can use any name.
result will be
[1,2,3,4,5]
you can use this in json object.
{
"value":#foreach("[1,2,3,4,5]","ele"){
{{ele}}
}endforeach
}
so the response can be,
{
"value": [
1,
2,
3,
4,
5
]
}
You can set your response status in your code.
#setStatus(400)#
You can use this in if conditions.
#if("{{id}}",=,"10"){
#setStatus(400)#
A
}endif
B
If you send id=10 so you will receive status 400 A
Response.
unless you will get the common status you saved by the UI.
You can define a constants in the response body. you can only pass strings and digits for the constant.
!<variable name>=<value>!
!<variable name>
suppose you need to make a constant called data and value on data should be printed twise.
!data=hello!
!data
!data
You can comment on your reponse.
/*
comment string
*/
You can set environment variables here. click on more button.
#env("VARIABLE_NAME")#
You can use javascript language for logic development. Write your logic and run it.
#prog{
var count = {{count}}
return JSON.stringify({
"returnValue": count + 1
});
}endprog
{
"hello": #prog_value#
}
Mockable express is using node-fetch library for execute http communication.
#prog{
await fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
return 'OK';
}endprog
{
"hello": #prog_value#
}
#prog{
const variableValue = "#env("ENV")#"
return 'OK';
}endprog
{
"hello": #prog_value#
}
We use below libraries for extend usability of code.
- lodash = https://lodash.com/
- moment = https://momentjs.com/
- path = https://nodejs.org/docs/latest/api/path.html
- underscore = https://underscorejs.org/
- faker = https://www.npmjs.com/package/Faker
#prog{
const value = lodash.add(1,2);
const time = moment()
const underscore = underscore.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; })
const data = faker.name.findName()
}endprog
You can create endpoints programmatically from upload endpoint. so first you have to enable the upload functionality. goto ** Menu Bar -> Enable Upload**
url : <host>/admin/upload
method : post
headers : Content-Type : application/json
body :{
[
{
"domainName":"testDomain1",
"pathName":"testPath1",
"pathUrl":"/testPath1",
"pathDescription": "testPath1",
"header":{},
"pathMethod":"post",
"pathStatus": 200,
"body":"testPath1"
},
{
"domainName":"testDomain2",
"pathName":"testPath2",
"pathUrl":"/testPath2",
"pathDescription": "testPath1",
"header":{},
"pathMethod":"get",
"pathStatus": 200,
"body":"#if(\"{{id}}\",=,\"10\"){{\"value\": \"A\"}}endif{\"value\": \"B\"}"
},
{
"domainName":"testDomain3",
"pathName":"testPath3",
"pathUrl":"/testPath3",
"pathDescription": "testPath3",
"header":{},
"pathMethod":"get",
"pathStatus": 200,
"body": "{\"messsage\":\"hello\"}"
}
]
}
You can change your response by query parameter without do any if else syntax. this is enabled only for upload method.
[
{
"domainName": "testDomain1",
"pathName": "testPath1",
"pathUrl": "/testPath1",
"pathDescription": "testDiscripion",
"header":{},
"pathMethod": "GET",
"pathStatus": 200,
"query": {
"parameter":"status",
"value":"test1",
"operator":"!=", // default is =. this is optional
"body":{
"message":"hello"
}
},
"body": "#Query"
}
]
#Query segment will replaced by the filtered query.
"parameters": [ // one query parameter
{
"condition": "templete", // parameter condition
"value": "templete2" // parameter value that will pass from the above parameter to show
}
]
Templete 1 will received for testDomain1/testPath1?templete=templete1 Templete 2 will received for testDomain1/testPath1?templete=templete2
- Query params should not contain '+' charactor. is it is necessary, it should be changed in the conditions as ' '
Ex:-
Url: /test?count = 10+10
#if("{{count}}",=,"10 10"){
{
value: "Query params are 10+10"
}
}endif
You can use mockable express serer as mocked web socket server. go to socket tab and connect your web socket client to Socket URL. Send your message to connected client
- Node JS : https://nodejs.org/en/
- Express Server : https://expressjs.com/
- EJS : https://ejs.co/
- JSON-DB : https://github.com/Belphemur/node-json-db
- Winston Logger : https://github.com/winstonjs/winston
- Bootstrap : https://getbootstrap.com/
- Jquery : https://jquery.com/
- Kolitha Warnakulasooriya
- Pravinda Perera
- Sameera Jayawardana
- Kasun Kodithuwakku