simple-json-replay-server
Advanced tools
Comparing version 0.0.13 to 0.1.0
{ | ||
"name": "simple-json-replay-server", | ||
"version": "0.0.13", | ||
"version": "0.1.0", | ||
"description": "A simple json replay server which can be used for standalone frontend web application (angular especially) when development. Simply put the url & parameters & response json data, then hit the url in browser or from your web application, whenever it matches the url and parameters, it will return the best match json data.", | ||
@@ -36,3 +36,3 @@ "main": "main.js", | ||
"author": { | ||
"name": "Jeff Duan", | ||
"name": "Anhua Duan", | ||
"email": "realdah@gmail.com" | ||
@@ -39,0 +39,0 @@ }, |
# Simple JSON Replay Server ❤️ [![Build Status](https://travis-ci.org/realdah/simple-json-replay-server.svg?branch=master)](https://travis-ci.org/realdah/simple-json-replay-server) [![npm version](https://badge.fury.io/js/simple-json-replay-server.svg)](https://badge.fury.io/js/simple-json-replay-server) [![Node version](https://img.shields.io/node/v/simple-json-replay-server.svg?style=flat)](http://nodejs.org/download/) | ||
Pefect companion with single page application development, and unit mock testing. Especially designed for angularjs 1 and 2 & reactjs. | ||
Pefect companion with single page application development, and unit mock testing. Especially designed for angularjs 1 and 2 & reactjs. Zero Code invasion to your production code. | ||
@@ -15,12 +15,9 @@ ## It is not a Restful Json Server | ||
* **Decoupled with production code** This json replay server will run only side by side in local development/testing environment on demand, **Zero** code change required in your production code. | ||
* **Simple command** to launch it with optional port and folder configuration. | ||
* **Supre Easy to create & maintain data** Create a pure json file in your replay data folder and you should get it right away, no programming is required. | ||
* **Advanced features** to allow you set more matching rules including query parameters, method, header, cookie etc. | ||
* **Advanced features** to allow you set more matching rules including query parameters, method, header, body, cookie etc. | ||
* **Straight-foward rules** configured in the file, they are almost self explaining. | ||
* **Fast & Predictable**, Once configured, it will response immediately and consitently. | ||
* **Fast & Predictable**, Once configured, it will response immediately and consistently. | ||
* **Simple but still Powerful**, You can configure different rules to simulating different responses to cover different scenarios for the same service call, such as paginiation, error, failure etc. | ||
## Sampe Use Cases | ||
https://github.com/realdah/simple-json-replay-server/wiki/Some-use-case-scenarios-for-demonstrating-how-to-use-this-tool | ||
@@ -50,7 +47,4 @@ ## Node Dependency | ||
Create a json file, eg. **_example.json_** inside of **_app_mock_** folder, you can create **any layer of sub folders to organize your mock data files**. The mock server will only look for files ending with ".json" in app_mock folder recursively. | ||
Create below json files, put them inside of **_app_mock_** folder, you can create **any layer of sub folders to organize your mock data files**. The mock server will only look for files ending with ".json" in app_mock folder recursively. | ||
Once start replay server, you can hit <http://localhost:8008/test?param1=value1%20to%20be%20matched¶m2=value2%20to%20be%20matched> to see the result. | ||
> Please note: you are able to config a different port number if it conflicts. | ||
@@ -60,11 +54,27 @@ | ||
``` | ||
example.json | ||
example-data1.json | ||
{ | ||
"comment" : "You are free to add comment for this json mock response, comment filed is optional", | ||
"request" : { | ||
"path": "test", | ||
"method" : "get" | ||
}, | ||
"response" : { | ||
"status" : 200, | ||
"data" : { | ||
"message" : "You get this message when you hit http://localhost:8008/test or http://localhost:8008/test?anyParameter=anyValue&anyParameter2=anyValue2 as long as you don't include 'param1=value1'" | ||
} | ||
} | ||
} | ||
example-data2.json | ||
{ | ||
"comment" : "", | ||
"request" : { | ||
"path": "test", | ||
"method" : "get", | ||
"query" : { | ||
"param1" : "value1 to be matched", | ||
"param2" : "value2 to be matched" | ||
"param1" : "value1" | ||
} | ||
@@ -75,3 +85,3 @@ }, | ||
"data" : { | ||
"message" : "you made it!" | ||
"message" : "You get this message when you hit http://localhost:8008/test¶m1=value1 or with additional parameters as long as you have 'param1=value1'" | ||
} | ||
@@ -82,3 +92,12 @@ } | ||
So, you could imagine that you can config json data with multiple level of filtering conditions. thus, you could possible to define a default mock response for a use case, then define more strict rules to return another mock data based on parameter/body/header changes. | ||
**Some Sample Use Cases** | ||
https://github.com/realdah/simple-json-replay-server/wiki/Some-use-case-scenarios-for-demonstrating-how-to-use-this-tool | ||
**Some More Mock Configurations Examples** | ||
https://github.com/realdah/simple-json-replay-server/tree/master/app_mock | ||
### ✦ **Start the replay server** | ||
@@ -138,5 +157,5 @@ | ||
html | a string | Yes | When you define **html** as response, please note that you should not define **data**, if both **data** & **html** are defined, it will consider only **data**. | ||
file | a json object map | Yes | it support two keys, **filePath** the file that you want user to download, it should be a relative path to config file including original file name, **downloadFilename** is the file name that you want browser to use when download. please refer to https://github.com/realdah/simple-json-replay-server/wiki/How-to-mock-a-file-download for details. | ||
## Integrate with your development work flow | ||
@@ -188,6 +207,6 @@ | ||
``` | ||
"start": "ng serve --proxy-config proxy.conf.json", | ||
"startWithMock": "ng serve --proxy-config proxy.conf.json", | ||
``` | ||
now run it with **npm start** | ||
now run it with **npm startWithMock** | ||
@@ -262,1 +281,10 @@ #### ► Directly Use Webpack | ||
0.0.12 | support new response type as **html** | ||
0.1.0 | support file download, new response type as **file** | ||
--- | ||
## License | ||
[MIT](http://opensource.org/licenses/MIT) | ||
var express = require('express'); | ||
var _ = require('underscore'); | ||
var path = require('path'); | ||
@@ -32,4 +33,4 @@ var util = require("./util"); | ||
if(delay != 0) { | ||
setTimeout(function() { | ||
if (delay != 0) { | ||
setTimeout(function () { | ||
response(res, mockDataConfig); | ||
@@ -69,12 +70,30 @@ }, delay); | ||
if(mockDataConfig.response.data) { //json data | ||
if (mockDataConfig.response.data) { //json data | ||
res.header("Content-Type", "application/json") | ||
.status(mockDataConfig.response.status) | ||
.json(mockDataConfig.response.data); | ||
} else { //assume it is html data | ||
.status(mockDataConfig.response.status) | ||
.json(mockDataConfig.response.data); | ||
} else if (mockDataConfig.response.html) { | ||
res.header("Content-Type", "text/html") | ||
.status(mockDataConfig.response.status) | ||
.send(mockDataConfig.response.html); | ||
.status(mockDataConfig.response.status) | ||
.send(mockDataConfig.response.html); | ||
} else if (mockDataConfig.response.file) { | ||
const config = mockDataConfig.response.file; | ||
const filePath = path.dirname(mockDataConfig.filePath) + path.sep + config.path; | ||
const downloadFilename = config.downloadFilename || path.basename(config.path); | ||
res.download(filePath, downloadFilename , function(err) { | ||
if(err) { | ||
res.header("Content-Type", "application/json").status(404).json( | ||
{ | ||
error: 'Can not find any download file - [' + filePath + '], please check your mock data configuration file - [' + mockDataConfig.filePath + '], your download file path should be relative to your mock configuration file.' | ||
}); | ||
} | ||
}); | ||
} else { | ||
res.header("Content-Type", "application/json").status(404).json( | ||
{ | ||
warning: 'You forget to configure the response type in ' + mockDataConfig.filePath + ', please refer document - https://github.com/realdah/simple-json-replay-server.' | ||
}); | ||
} | ||
} | ||
@@ -40,2 +40,3 @@ var glob = require("glob"); //load many files and filtering with some rules at one shot | ||
var mockDataConfig = JSON.parse(fs.readFileSync(filePath, 'utf8')); | ||
mockDataConfig.filePath = filePath; | ||
buildMappings(mockDataConfig); | ||
@@ -42,0 +43,0 @@ }); |
@@ -12,4 +12,4 @@ var chokidar = require('chokidar'); | ||
//Only reload the files until user stops changing files after 800 milliseconds, to avoid repeat loading when saving/copying multiple files. | ||
var changedFiles = Rx.Observable.fromEvent(dirWatcher, 'change').debounce(function (x) { return Rx.Observable.timer(800); }); | ||
//Only reload the files until user stops changing files after 1500 milliseconds, to avoid repeat loading when saving/copying multiple files. | ||
var changedFiles = Rx.Observable.fromEvent(dirWatcher, 'change').debounce(function (x) { return Rx.Observable.timer(1500); }); | ||
@@ -16,0 +16,0 @@ var subscription = changedFiles.subscribe( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57746
24
837
283