1.1.23 • Published 7 years ago
mock-backend-rest v1.1.23
#Fork and improve
Usage
- You can start fake server
- Mock some response (GET, POST, PUT, DELETE), if you need big response - can read it from JSON file
- Stop fake server
Install
npm install -SD mock-backend-rest
const FakeServer = require('mock-backend-rest');
const fakeServer = new FakeServer(8085);
fakeServer.port = 8085; //default port is 4000
fakeServer.get('/foo', './index.json'); //path to json file what will be response
fakeServer.post('/bar', {LOL: 'LOL'}); //
fakeServer.get('http://lol.com', { WEBLIUM_HTTP: 'WEBLIUM_HTTP' });
fakeServer.del('/foo', {LOL: 'LOL'});
fakeServer.put('/bar', {LOL: 'LOL'});
fakeServer.post('/xxx', { LOL: 'LOL' }, {error: 'SUPER CUSTOM ERROR'}, true, {a: 'a'});
fakeServer.start();
console.log(fakeServer.getGetResult('/foo'));
//output { called: false, callCount: 0, method: 'GET' }
//curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:8085/bar
//two times use curl and after
//curl -d '{"a": "a"}' -H "Content-Type: application/json" -X POST http://localhost:8085/xxx
//response will be {"LOL":"LOL"}
//curl -d '{"a": "1"}' -H "Content-Type: application/json" -X POST http://localhost:8085/xxx
//response will be {"error":"SUPER CUSTOM ERROR"}
//curl -d '{"a": "1"}' -H "Content-Type: application/json" -X POST http://localhost:8085/xxx
//response will be {"error":"SUPER CUSTOM ERROR"}
setTimeout(() => {
console.log(fakeServer.getPostResult('/bar'));
const callResult = fakeServer.getPostResult('/bar')
callResult.calledWithArg({key1:"value1", key2:"value2"}) // true
callResult.calledWithArg({key1:"value1"}) // false
fakeServer.stop();
fakeServer.restore();
}, 15000);
//{ calledArgs:
// [ { key1: 'value1', key2: 'value2' },
// { key1: 'value1', key2: 'value2' } ],
// called: true,
// callCount: 2,
// method: 'POST' }
methods | args |
---|---|
constructor(port, responseFormat) | port, any or number , default is 4000 , string 'text' or 'json' (default json) |
get(path, response, errorResponse, assertRequestBody, requestBody) | path: string example: '/foo'; response: object or string - path to json file or string response, three last args is optiona, if you want own response error errorResponse object , assertRequestBody bool if true your response body will be assert equal with last arg requestBody object |
post(path, response, errorResponse, assertRequestBody, requestBody) | path: string example: '/foo'; response: object or string - path to json file or string response, three last args is optiona, if you want own response error errorResponse object , assertRequestBody bool if true your response body will be assert equal with last arg requestBody object |
del(path, response, errorResponse, assertRequestBody, requestBody) | path: string example: '/foo'; response: object or string - path to json file or string response, three last args is optiona, if you want own response error errorResponse object , assertRequestBody bool if true your response body will be assert equal with last arg requestBody object |
put(path, response, errorResponse, assertRequestBody, requestBody) | path: string example: '/foo'; response: object or string - path to json file or string response, three last args is optiona, if you want own response error errorResponse object , assertRequestBody bool if true your response body will be assert equal with last arg requestBody object |
start() | any args |
getDelResult(path) | path: string example '/foo', if server dont have action for this path return empty obj |
getPutResult(path) | path: string example '/foo', if server dont have action for this path return empty obj |
getGetResult(path) | path: string example '/foo', if server dont have action for this path return empty obj |
getPostResult(path) | path: string example '/foo', if server dont have action for this path return empty obj |
stop() | any args, if server not started - will get message, after stop you can get actions results etc |
restore() | any args, server to initial conditions, if server runned it method stop it |
calledWithArg(arg) | called from result of action, arg object return true if you call this path with arg |
don`t need any dependencies
Improvement plan
- Stop FakeServer
- Mock request for any url (partly) (make for http, and https)
- Read response fron any file, and any format
1.1.23
7 years ago
1.1.22
7 years ago
1.1.21
7 years ago
1.0.21
7 years ago
1.0.20
7 years ago
1.0.19
7 years ago
0.0.19
7 years ago
0.0.18
7 years ago
0.0.17
7 years ago
0.0.16-d
7 years ago
0.0.16
7 years ago
0.0.15-d
7 years ago
0.0.15
7 years ago
0.0.14-d
7 years ago
0.0.14
7 years ago
0.0.13-d
7 years ago
0.0.13
7 years ago
0.0.12-d
7 years ago
0.0.12
7 years ago
0.0.11
7 years ago
0.0.10
7 years ago
0.0.9
7 years ago
0.0.8
7 years ago
0.0.7
7 years ago
0.0.6
7 years ago
0.0.5
7 years ago