Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mollweide committed Jun 28, 2016
2 parents 7ff4e8d + 40cb198 commit 23c7330
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 44 deletions.
45 changes: 45 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

## [0.8.1](https://github.com/smollweide/node-mock-server/issues?q=milestone%3A0.8.1)
- Fix: Validation: ignore "cannot read file" when response_schema.json is empty #19
- Fix: Validation: set minimum response delay #20
- Prettify logs

## [0.8.0](https://github.com/smollweide/node-mock-server/issues?q=milestone%3A0.8.0)
- More readable schema structure and faker DTO response functions

## [0.7.4](https://github.com/smollweide/node-mock-server/issues?q=milestone%3A0.7.4)
- Different success files for different params #14
- Fix: Preview not working with as expected with params in success.json #11

## [0.7.3](https://github.com/smollweide/node-mock-server/issues?q=milestone%3A0.7.3)
- Fix: First level calls are detected as invalid path #12

## [0.7.2](https://github.com/smollweide/node-mock-server/issues?q=milestone%3A0.7.2)
- Add dynamic path params to ejs object #9
- Detect empty dynamic path params and return error #8
- Store validation results in just one file #3

## 0.7.0
- DTO import and DTO to class converter

## 0.6.3
- Prettify logs and bug fixes

## 0.6.0
- Mock response validation

## 0.5.0
- Swagger import

## 0.4.0
- Allows explicit error messages and their status

## 0.3.0
- UI with group navigation

## 0.2.0
- Mock Server

## 0.1.0
- Initial Version
- Specification UI
File renamed without changes.
6 changes: 3 additions & 3 deletions controller/AppController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
fs = require('fs'),
log = require('chip')(),
util = require('util'),
extend = util._extend,
Utils = require('../lib/Utils'),
Expand Down Expand Up @@ -58,7 +58,7 @@ AppController.prototype = extend(AppController.prototype, {
this.app = app;

if (!this.options.dirName) {
console.error('options.dirName is required (dirName: __dirname)');
log.error('options.dirName is required (dirName: __dirname)');
return;
}

Expand All @@ -68,7 +68,7 @@ AppController.prototype = extend(AppController.prototype, {

app.listen(options.port, function () {
if (process.env.NODE_ENV !== 'test') {
console.log('server started at port ' + options.port);
log.info('server started at port ' + options.port);
}
});

Expand Down
7 changes: 4 additions & 3 deletions controller/MockController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Utils = require('../lib/Utils'),
extend = require('util')._extend,
ejs = require('ejs'),
log = require('chip')(),
faker = require('faker'),
AppControllerSingleton = require('./AppController'),
appController = AppControllerSingleton.getInstance();
Expand Down Expand Up @@ -114,7 +115,7 @@ MockController.prototype = extend(MockController.prototype, {
});
outStr = ejs.render(responseFile, responseData);
} catch (err) {
console.log(err);
log.log(err);
}

if (outStr) {
Expand All @@ -123,7 +124,7 @@ MockController.prototype = extend(MockController.prototype, {
options.res.send(responseFile);
}
} catch (err) {
console.log(err);
log.log(err);
options.res.end();
}

Expand Down Expand Up @@ -326,7 +327,7 @@ MockController.prototype = extend(MockController.prototype, {
list = _this.readDir(path, ['.DS_Store']);
} catch (err) {
if (process.env.NODE_ENV !== 'test') {
console.log('Folder "' + path + '" not found!');
log.error('Folder "' + path + '" not found!');
}
}

Expand Down
3 changes: 2 additions & 1 deletion controller/ResponseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var Utils = require('../lib/Utils'),
util = require('util'),
extend = util._extend,
log = require('chip')(),
AppControllerSingleton = require('./AppController'),
appController = AppControllerSingleton.getInstance(),
GetResponse = require('../lib/GetResponse');
Expand Down Expand Up @@ -94,7 +95,7 @@ ResponseController.prototype = extend(ResponseController.prototype, {
});

if (process.env.NODE_ENV !== 'test') {
console.log('Expected response set. Linkable Url: ' + linkableUrl);
log.info('Expected response set. Linkable Url: ' + linkableUrl);
}

res.end();
Expand Down
6 changes: 4 additions & 2 deletions lib/SwaggerLog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

'use strict';

var log = require('chip')();

if (!_swaggerLog) {
var _swaggerLog = [];
}
Expand Down Expand Up @@ -44,8 +46,8 @@ function _consoleLog (msg) {
type = (type === 'neutral') ? 'log': type;
type = (type === 'success') ? 'log': type;

if (console && console[type]) {
console[type](msg.time + ': ' + _getWithoutHtml(msg.msg));
if (log && log[type]) {
log[type](msg.time + ': ' + _getWithoutHtml(msg.msg));
}
}

Expand Down
26 changes: 24 additions & 2 deletions lib/Utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var fs = require('fs'),
log = require('chip')(),
jQueryExtend = require('extend');

/**
Expand Down Expand Up @@ -184,7 +185,7 @@ Utils.prototype = {
list = fs.readdirSync(dir);
} catch (err) {
if (process.env.NODE_ENV !== 'test') {
console.log('Folder "' + err.path + '" not found!');
log.error('Folder "' + err.path + '" not found!');
}
}

Expand All @@ -209,7 +210,7 @@ Utils.prototype = {
try {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file){
var curPath = path + "/" + file;
var curPath = path + '/' + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
this.removeDir(curPath);
} else { // delete file
Expand Down Expand Up @@ -316,6 +317,27 @@ Utils.prototype = {
*/
typeOf: function (value) {
return (value instanceof Array) ? 'array' : typeof value;
},

/**
*
* @method cleanPath
* @param {Object} options
* @param {String} path
* @returns {String}
* @public
*/
cleanPath: function (options, path) {

if (typeof path !== 'string') {
return '';
}

if (typeof options !== 'object' || !options.restPath) {
return path;
}

return path.replace(options.restPath, '');
}

};
Expand Down
8 changes: 4 additions & 4 deletions lib/ValidatorDataSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ ValidatorDataSchema.prototype = extend(ValidatorDataSchema.prototype, {
*/
init: function (options) {

var _validation = {};
var _validation = {},
cleanedPath = this.cleanPath(options, options.filePathData);

options = extend(this._defaults, options || {});

Expand All @@ -57,7 +58,6 @@ ValidatorDataSchema.prototype = extend(ValidatorDataSchema.prototype, {

if (!this._data || typeof this._data !== 'object') {
this._isValid = false;
validatorLog.error('<code>options.data</code> can\'t be empty!');
}

this._mapSchema(this._schema);
Expand All @@ -70,13 +70,13 @@ ValidatorDataSchema.prototype = extend(ValidatorDataSchema.prototype, {
this.setMethodStore(this._path, _validation);

if (this._invalidCounter > 0) {
validatorLog.error('Mock data in file <code>' + options.filePathData + '</code> are invalid! ' +
validatorLog.error('Mock data in file <code>' + cleanedPath + '</code> are invalid! ' +
'<span class="label label-danger">' + this._invalidCounter + '</span> invalid values found.');

return false;
}

validatorLog.success('Mock data in file <code>' + options.filePathData + '</code> are valid!');
validatorLog.success('Mock data in file <code>' + cleanedPath + '</code> are valid!');
return true;
},

Expand Down
8 changes: 5 additions & 3 deletions lib/ValidatorLog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

'use strict';

var log = require('chip')();

if (!_validatorLog) {
var _validatorLog = [];
}
Expand Down Expand Up @@ -42,10 +44,10 @@ function _consoleLog (msg) {

type = msg.type;
type = (type === 'neutral') ? 'log': type;
type = (type === 'success') ? 'log': type;
type = (type === 'success') ? 'info': type;

if (console && console[type]) {
console[type](msg.time + ': ' + _getWithoutHtml(msg.msg));
if (log && log[type]) {
log[type](msg.time + ': ' + _getWithoutHtml(msg.msg));
}
}

Expand Down
30 changes: 24 additions & 6 deletions lib/ValidatorResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ ValidatorResponse.prototype = extend(ValidatorResponse.prototype, {

options = extend(this._defaults, options || {});



this._options = options;
this._serverOptions = serverOptions;
this._isValid = true;
Expand Down Expand Up @@ -77,8 +75,6 @@ ValidatorResponse.prototype = extend(ValidatorResponse.prototype, {
this._readFiles();
}

validatorLog.neutral('response validation for <code>' + this._pathMockData + '</code> started');

if (this._isValid) {
new ValidatorDataSchema({
schema: this._dataSchema,
Expand All @@ -99,13 +95,14 @@ ValidatorResponse.prototype = extend(ValidatorResponse.prototype, {
*/
_readFiles: function () {

var response;
var response,
cleanedPath = this._cleanPath(this._serverOptions, this._pathMockData);

if (this._isValid) {
try {
this._dataSchema = JSON.parse(this.readFile(this._pathSchema));
} catch (err) {
validatorLog.error('cannot read file <code>' + this._pathSchema + '</code>!');
validatorLog.success('Mock data in file <code>' + cleanedPath + '</code> are valid!');
this._isValid = false;
}

Expand All @@ -117,6 +114,27 @@ ValidatorResponse.prototype = extend(ValidatorResponse.prototype, {

this._dataExpected = response.get();
}
},

/**
*
* @method cleanPath
* @param {Object} options
* @param {String} path
* @returns {String}
* @private
*/
_cleanPath: function (options, path) {

if (typeof path !== 'string') {
return '';
}

if (typeof options !== 'object' || !options.restPath) {
return path;
}

return path.replace(options.restPath, '');
}

});
Expand Down
36 changes: 36 additions & 0 deletions lib/ValidatorResponses.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,47 @@ ValidatorResponses.prototype = extend(ValidatorResponses.prototype, {

this._options = options;
this._serverOptions = serverOptions;
this._prefFilePath = this._options.restPath + '/preferences.json';
this._tempPrefData = '';
this._setResponseDelay();
this._validationResponses = this._getValidationResponses(this._options.restPath);
this._runValidations(this._validationResponses);
this._restoreResponseDelay();

},

/**
* @method _restoreResponseDelay
* @private
*/
_restoreResponseDelay: function () {
this.writeFile(this._prefFilePath, this._tempPrefData);
},

/**
* @method _setResponseDelay
* @private
*/
_setResponseDelay: function () {

var prefData,
prefDataJson;

if (!this.existFile(this._prefFilePath)) {
this._tempPrefData = '{}';
return;
}

prefData = this.readFile(this._prefFilePath);
prefDataJson = JSON.parse(prefData);

this._tempPrefData = prefData;

prefDataJson.responseDelay = '0';

this.writeFile(this._prefFilePath, JSON.stringify(prefDataJson));
},

/**
* @method _runValidations
* @param {Array} validationResponses
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-mock-server",
"version": "0.8.0",
"version": "0.8.1",
"description": "File based Node API mock server",
"email": "[email protected]",
"author": "Simon Mollweide <[email protected]>",
Expand All @@ -27,14 +27,15 @@
"DTO",
"Swagger"
],
"engines" : {
"node" : ">=0.12.7"
"engines": {
"node": ">=0.12.7"
},
"preferGlobal": false,
"private": false,
"license": "MIT",
"dependencies": {
"body-parser": "^1.15.0",
"chip": "0.0.5",
"deasync": "^0.1.4",
"ejs": "^2.4.1",
"express": "^4.13.4",
Expand Down
Loading

0 comments on commit 23c7330

Please sign in to comment.