Usage
- Build simple fake server with routing, params, static content
- GET, POST, PUT, DELETE, supported methods, status, bodies etc
Install
npm install -SD json-fake-server || npm i -g json-fake-server
Example
base usage example
const fakeServer = const model = port: 9090 api: method: "GET" path: "/" response: "Hello world" const server = // open browser// url 'http://localhost:9090/
mocha test example
const fakeServer = const fetch = const expect = const model = "port": 8888 "api": "method": "GET" "path": "/user" "response": "user_name": "test user" "method": "POST" "path": "/user" "response": "created": true
Example from command line
./test.json
json-fake-server -m ./test.json
Model Structure
- Endpoint Model Object
- HTTP methods
- Authorization
- Url params
- Queries
- HTML static
- Request body assertion
- Several server nodes in one environment
Endpoint Model Object
const APIModelObject = "method": "GET" // required field, http methods: GET, POST, PUT, DELETE "path": "/example/:param1/:param2" // required field, params can be presented here "status": 200 // status in option field, default is 200 "authorization": // if full server model inludes authorization property, this will take part // in our endpoint response "unauthorized": // this property will be used as body for respose if request does not have credentials, // unauthorized is optional, default is {unauthorized: 'unauthorized'} "foo": "bar" // status what will be used, it is optional, default is 401 "status": 401 // "token":"testToken" // "params_response": // params_response is optional, it required if you want to work with "response": // properties of this object shoulb be equal params declaration in url "allParamsAreEqual": // for example our path is "/example/:param1/:param2" "param1": "success" // params_response object includes properties : param1 and param2 "param2": "success" // object param should have propertie "value" what will uses as a assertion // for example curl http://localhost:8888/example/testFirst/someItemWhatNotExists // response will be from param1 object - { "testId": "testFirst" } "param1": // if all params value equal our request url params we will get general response "value": "testFirst" // from params_response object or it it is not exists "response": // responses from params objects will be merged "testId": "testFirst" "param1": "value": "testSecond" "response": "testId": "testSecond" "request_body_equal": // this property will work with PUT, POST, DELETE, PATCH only "status": 404 "not_equal_response": // this field is optional, default is {"data": "invalid request"} "success": false "expected_body": // request body should equal expected_body property "username": "test_" "password": "test_pass" "response": // response is option field, default is {ok: 'OK'} "example": "example GET"
HTTP methods
const fakeServer = const fetch = const model = "port": 8081 "api": "method": "GET" "path": "/example" "response": "example": "example GET" "method": "POST" "path": "/example" "response": "example": "example POST" "method": "DELETE" "path": "/example" "response": "example": "example DELETE" "method": "PUT" "path": "/example" "response": "example": "example PUT" const server = { const postData = await // {example: "example POST"} const getData = await // {example: "example GET"} const putData = await // {example: "example PUT"} const deleteData = await // {example: "example DELETE"}}
Authorization
const fakeServer = const fetch = const authorizationInApiObj = "unauthorized": // this property will be used as body for respose "foo": "bar" // // "status": 401 // this property will be used as unsuccess status if token is not equal "token":"testToken" // to this toke property value const model = "port": 8081 "authorization": "type": "headers" "api": "method": "GET" "path": "/example" "response": "example": "example GET" // default properties are // unauthorized : {unauthorized: 'unauthorized'} // status : 401 "authorization": authorizationInApiObj const server = { const withoutTokenData = await // {foo: "bar"} const withTokenData = await // {example: "example GET"}}
Params
const fakeServer = const fetch = const model = "port": "8081" "api": "method": "GET" // after : name of param shoulb be used in params_response object // lets check :user "path": "/user/:user/id/:id" "params_response": "id": "value": "testId" "response": "testId": "testId" // user // if user will contain /user/testUser/id/:id // we will get next response from user object "user": "value": "testUser" "response": "user": "testId" // if we have full uquals between params // we will get general response - response property from params_response object // in this case we heed // http://localhost:8081/user/testUser/id/testId "response": "full_params_equal": "username": "test user1" "password": "test password" // this response will be used in other cases // as example http://localhost:8081/user/unknown/id/unknown "response": "example": "example GET" { const defaultGetData = await // {"example": "example GET"} console const fullPramsEqual = await // {"full_params_equal": { // "username": "test user1", // "password": "test password" // }} console const userEqualParamEqual = await // {"user": "testId"} console const idEqualParamEqual = await // {"testId": "testId"} console}
Default response
Full params equal response
Partial equal param user
Partial equal param id
Queries
const fakeServer = const fetch = const model_obj = "port": "8081" "api": "method": "GET" "path": "/test" "response": "testOne": 1 "testTwo": 2 "testThree": 3 "testFour": 4 const model_array = "port": "8082" "api": "method": "GET" "path": "/test" "response": "testOne": 1 "testTwo": 2 "testThree": 3 "testFour": 4 "testOne": 1 "testTwo": 2 "testThree": 3 "testFour": 4 "testOne": 1 "testTwo": 2 "testThree": 3 "testFour": 4 { server_obj = server_array = const query_resp_obj = await // {"testOne":1,"testTwo":2} console const query_resp_array = await // [{"testOne":1,"testTwo":2},{"testOne":1,"testTwo":2},{"testOne":1,"testTwo":2}] console}
HTML
const fakeServer = const fetch = const path = const indexHtml = pathconst model = "port": "8081" "api": "method": "GET" "path": "/" "response": indexHtml {const indexHtmlText = await // <html lang="en"> // <head> // <meta charset="UTF-8"> // <meta name="viewport" content="width=device-width, initial-scale=1.0"> // <meta http-equiv="X-UA-Compatible" content="ie=edge"> // <title>Document</title> // </head> // <body> // TEST FAKE SERVER // <div>A</div> // <div>B</div> // <div>C</div> // <div>D</div> // <div>E</div> // </body> // </html> console}
Request body assertion
const fakeServer = const fetch = const model_obj = "port": "8081" "debug": true // if this prop exists log will show all results in console, defailt is false "api": "method": "POST" "path": "/test" "request_body_equal": "status": 404 "not_equal_response": // this field is optional, default is {"data": "invalid request"} "success": false "expected_body": "username": "test_" "password": "test_pass" "response": "success": true const serser = { const body_equal_success = await // {"success":true} console const body_not_equal = await // {"success": false} console serser}
Several server nodes in one environment
const fakeServer = const fetch = const model_entry_point = "port": 8081 "api": "method": "GET" "path": "/user" "response_from_url": // if this property exists this endpoint will try to use it as main "status": 201 // response status "method": "GET" // method what will use for request for other HTTP service "url": "http://localhost:8888/userData" // URL to other service endpoint "merge_with": // if this property exists response from URL will merged with this property value // for example from http://localhost:8888/userData we will get {user: "exists"} // after merge with this property value {user: "exists", part_from_entrypoint: "entry point"} // so after request to http://localhost:8081/user // we will get {user: "exists", part_from_entrypoint: "entry point"} "part_from_entrypoint": "entry point" const model_user = "port": 8888 "api": "method": "GET" "path": "/userData" "response": "part_from_user_service": "user_profile": "username": "some username" "postal_code": 3212654 const entry = const userSerice = { const getData = await // { // part_from_user_service: // { user_profile: { username: 'some username', postal_code: 3212654 } }, // part_from_entrypoint: 'entry point' // } console entry userSerice}