simple-json-replay-server
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
{ | ||
"name": "simple-json-replay-server", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"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.", | ||
@@ -8,3 +8,3 @@ "main": "main.js", | ||
"start": "node src/main.js", | ||
"test": "mocha" | ||
"test": "mocha --recursive" | ||
}, | ||
@@ -34,7 +34,8 @@ "engines": { | ||
"dependencies": { | ||
"body-parser": "^1.17.1", | ||
"chokidar": "^1.6.1", | ||
"express": "^4.15.0", | ||
"glob": "^7.1.1", | ||
"underscore": "^1.8.3", | ||
"chokidar": "^1.6.1", | ||
"rx": "^4.1.0" | ||
"rx": "^4.1.0", | ||
"underscore": "^1.8.3" | ||
}, | ||
@@ -41,0 +42,0 @@ "devDependencies": { |
@@ -107,3 +107,3 @@ # 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/) | ||
However, if more than one mock data match the same filtering criteria, we will not guarantee which one will return. | ||
However, if more than one mock data match the same number of filtering criteria (for query/body, each key is consider as seperate criteria), we will not guarantee which one will return. | ||
@@ -115,3 +115,4 @@ | ||
method | http methods | Yes | Default as **get** | ||
query | a json map with key value pairs | Yes | Default as **undefined**. You can think about this is a filtering logic. As long as you defined a key-value, it will only allow request which contains this query parameter and same value to pass through. | ||
query | key value pairs | Yes | Default as **undefined**. You can think about this is a filtering logic. As long as you defined a key-value, it will only allow request which contains this query parameter and same value to pass through. | ||
body | a json map | Yes | Default as **undefined**. you can have partial values in multiple layers, it will only try to match partial branch of the value till the end. So far, only support **json body** (application/json) & **form-urlencoded** (application/x-www-form-urlencoded). | ||
@@ -216,4 +217,27 @@ ### ✦ Response | ||
Although you can use **grunt-contrib-connect** with some magic middleware settings to make it work, | ||
I would suggest you to use **grunt-connect-proxy** plug-in which kind simplify the configurations. | ||
You can find more details in here: <https://github.com/drewzboto/grunt-connect-proxy> | ||
~~To Be Continue~~ | ||
``` | ||
grunt.initConfig({ | ||
connect: { | ||
server: { | ||
options: { | ||
port: 9000, | ||
hostname: 'localhost' | ||
}, | ||
proxies: [ | ||
{ | ||
context: '/api', | ||
host: '127.0.0.1', | ||
port: 8008, | ||
https: false | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
``` | ||
@@ -9,2 +9,3 @@ var express = require('express'); | ||
var watcher = require('./watcher'); | ||
var bodyParser = require('body-parser'); | ||
var app = express(); | ||
@@ -14,5 +15,6 @@ | ||
//it is not a production environment, performance impact is negligible. | ||
app.set('json spaces', 4); | ||
initialBodyParsers(app); | ||
var options = optionParser.parseArguments(); | ||
@@ -44,3 +46,3 @@ | ||
error: 'Can not find matching mock data', | ||
req: _.pick(req, 'path', 'method', 'query', 'cookies', 'headers') | ||
req: _.pick(req, 'path', 'method', 'query', 'body') //'cookies', 'headers' | ||
}); | ||
@@ -55,2 +57,14 @@ } | ||
//start watching the changes. | ||
watcher.startWatching(options.folder); | ||
//it says there might be potential cross site scripting attack if we declare global body parsers for all requests. | ||
//However, since the mock server is really nothing but a replay with hardcoded data, this is not a concern for us. | ||
function initialBodyParsers(app) { | ||
// parse application/json | ||
app.use(bodyParser.json()); | ||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
} | ||
function response(res, mockDataConfig) { | ||
@@ -62,3 +76,1 @@ res.header("Content-Type", "application/json") | ||
//start watching the changes. | ||
watcher.startWatching(options.folder); |
var _ = require('underscore'); | ||
var util = require('./util'); | ||
@@ -7,4 +8,5 @@ var NOT_MATCH = -999999; | ||
var path = request.path.toLowerCase(); | ||
var query = request.query || {}; | ||
var headers = request.headers || {}; | ||
var query = request.query; | ||
var body = request.body; | ||
var headers = request.headers; | ||
var method = request.method; | ||
@@ -31,17 +33,16 @@ | ||
var score = 0; | ||
var queryScore = util.partialContains(query, mockDataConfig.request.query); | ||
_.each(mockDataConfig.request.query,function(value, key){ | ||
if(queryScore < 0) { | ||
return; | ||
} | ||
if(query[key] != undefined ) { //if key exists | ||
if(query[key] == value) { | ||
score += 1000; | ||
} else { | ||
score = NOT_MATCH; | ||
} | ||
} else { | ||
score = NOT_MATCH; | ||
} | ||
}); | ||
var bodyScore = util.partialContains(body, mockDataConfig.request.body); | ||
if(bodyScore < 0) { | ||
return; | ||
} | ||
var score = queryScore + bodyScore; | ||
if(score >= bestScore) { | ||
@@ -48,0 +49,0 @@ bestScore = score; |
@@ -0,0 +0,0 @@ var glob = require("glob"); //load many files and filtering with some rules at one shot |
@@ -0,1 +1,3 @@ | ||
var _ = require('underscore'); | ||
function print(message, option) { | ||
@@ -15,3 +17,37 @@ process.stdout.write("[SJRS] "); | ||
//Check if a value in a deep branch of a map exists in another map object | ||
function partialContains(fullObject, partialObject) { | ||
if(!partialObject || _.isEmpty(partialObject)) { | ||
return 0; | ||
} | ||
if(!fullObject) { | ||
return -1; | ||
} | ||
var match = 0; | ||
//use _.find because we want to break from the loop if anything not match. | ||
//_.each will not be able to break completely. | ||
_.find(_.keys(partialObject), function(key) { | ||
var value = partialObject[key]; | ||
var fullObjectValue = fullObject[key]; | ||
if((typeof value === "object") && (typeof fullObjectValue === "object")) { | ||
match = partialContains(fullObjectValue, value); | ||
if(match < 0) { | ||
return true; | ||
} | ||
} else if(fullObjectValue != value) { | ||
match = -1; | ||
return true; | ||
} | ||
match++; | ||
}); | ||
return match; | ||
} | ||
exports.partialContains = partialContains; | ||
exports.printVersion = printVersion; | ||
exports.print = print; |
@@ -0,0 +0,0 @@ var chokidar = require('chokidar'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
37250
20
685
241
6
1
+ Addedbody-parser@^1.17.1