1.1.23 • Published 8 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-restconst 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: objector 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
8 years ago
1.1.22
8 years ago
1.1.21
8 years ago
1.0.21
8 years ago
1.0.20
8 years ago
1.0.19
8 years ago
0.0.19
8 years ago
0.0.18
8 years ago
0.0.17
8 years ago
0.0.16-d
8 years ago
0.0.16
8 years ago
0.0.15-d
8 years ago
0.0.15
8 years ago
0.0.14-d
8 years ago
0.0.14
8 years ago
0.0.13-d
8 years ago
0.0.13
8 years ago
0.0.12-d
8 years ago
0.0.12
8 years ago
0.0.11
8 years ago
0.0.10
8 years ago
0.0.9
8 years ago
0.0.8
8 years ago
0.0.7
8 years ago
0.0.6
8 years ago
0.0.5
8 years ago