Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Marketto committed Aug 24, 2020
2 parents dec8ff6 + 5625461 commit 8d8ccc1
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 32 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ Instant Server for JSON and XML Mocks with support for REST structure, VERB spec
## Supported files for mocks
* **.json** => *resource*
* **.xml** => *resource*
* **.txt** => *resource*
* **.config.yml** => *yaml config for custom status, delay and headers*
* **.schema.json** => *request validator*

## Supported responses
* **JSON** => *Default*
* **XML** => *"Accept"*: ***"application/xml"***
* **TXT** => *"Accept"*: ***"text/plain"***

## Supported config (.config.yml)
* **status** *numeric*
Expand Down Expand Up @@ -329,6 +331,8 @@ npx mockettaro -r services


## Changelog
### 2.1.0
- Added support for txt (text/plain) files
### 2.0.0
- Introduced config.yml files for custom settings
- Added support for custom HTTP status code in config.yml files
Expand Down
2 changes: 1 addition & 1 deletion docs/Mockettaro.html
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ <h5>Parameters</h5>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/MockettaroProgram.html
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ <h5>Parameters</h5>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/PathRetriever.html
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ <h5>Parameters</h5>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/RequestValidator.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ <h5>Parameters</h5>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions docs/ResourceLoader.html
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ <h5>Parameters</h5>
<div class="form-group">
<label class="col-sm-3 control-label">Source</label>
<div class="col-sm-9">
<p class="form-control-static"><a href="resource-loader.class.js.html">resource-loader.class.js</a>, <a href="resource-loader.class.js.html#line-151">line 151</a></p>
<p class="form-control-static"><a href="resource-loader.class.js.html">resource-loader.class.js</a>, <a href="resource-loader.class.js.html#line-152">line 152</a></p>
</div>
</div>

Expand All @@ -468,7 +468,7 @@ <h5>Parameters</h5>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ <h1><span class="name">Globals</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
8 changes: 7 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ <h2>Supported files for mocks</h2>
<ul>
<li><strong>.json</strong> =&gt; <em>resource</em></li>
<li><strong>.xml</strong> =&gt; <em>resource</em></li>
<li><strong>.txt</strong> =&gt; <em>resource</em></li>
<li><strong>.config.yml</strong> =&gt; <em>yaml config for custom status, delay and headers</em></li>
<li><strong>.schema.json</strong> =&gt; <em>request validator</em></li>
</ul>
<h2>Supported responses</h2>
<ul>
<li><strong>JSON</strong> =&gt; <em>Default</em></li>
<li><strong>XML</strong> =&gt; <em>&quot;Accept&quot;</em>: <em><strong>&quot;application/xml&quot;</strong></em></li>
<li><strong>TXT</strong> =&gt; <em>&quot;Accept&quot;</em>: <em><strong>&quot;text/plain&quot;</strong></em></li>
</ul>
<h2>Supported config (.config.yml)</h2>
<ul>
Expand Down Expand Up @@ -352,6 +354,10 @@ <h4>URL to test</h4>
<li>http://localhost:8080/services/customer/anything GET, PUT</li>
</ul>
<h2>Changelog</h2>
<h3>2.1.0</h3>
<ul>
<li>Added support for txt (text/plain) files</li>
</ul>
<h3>2.0.0</h3>
<ul>
<li>Introduced config.yml files for custom settings</li>
Expand Down Expand Up @@ -487,7 +493,7 @@ <h2>AUTHOR</h2>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/list_class.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h4 id="ResourceLoader" class="class public"><a href="ResourceLoader.html"><span
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/mockettaro-program.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ <h1><span class="name">mockettaro-program.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/mockettaro.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ <h1><span class="name">mockettaro.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/path-retriever.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ <h1><span class="name">path-retriever.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/request-validator.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1><span class="name">request-validator.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
7 changes: 4 additions & 3 deletions docs/resource-loader.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h1><span class="name">resource-loader.class.js</span></h1>
* @memberof ResourceLoader
*/
static get TYPES() {
return ['json', 'xml'];
return ['json', 'xml', 'txt'];
}

/**
Expand Down Expand Up @@ -209,7 +209,8 @@ <h1><span class="name">resource-loader.class.js</span></h1>
},
json: input => {
return JSON.parse(input);
}
},
txt: input => input
};

logger.debug(`Content type ${fileType}`);
Expand Down Expand Up @@ -280,7 +281,7 @@ <h1><span class="name">resource-loader.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
13 changes: 9 additions & 4 deletions docs/type-handler.class.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,29 @@ <h1><span class="name">type-handler.class.js</span></h1>
static encodeResponseBody(req, res, next) {
const [type] = ResourceLoader.acceptedTypes(req);
if (req.resBody) {
req.resJsonBody = req.resBody;
req.rawResBody = req.resBody;
req.resConfig = req.resConfig || {};
req.resConfig.headers = req.resConfig.headers || {};

switch(type) {
case 'xml':
req.resBody = `&lt;?xml version="1.0" encoding="UTF-8" ?>${j2xParser.parse(req.resJsonBody)}`;
req.resBody = `&lt;?xml version="1.0" encoding="UTF-8" ?>${j2xParser.parse(req.rawResBody)}`;
Object.assign(req.resConfig.headers, {
'Content-Type': 'application/xml'
});
break;
case 'json':
req.resBody = JSON.stringify(req.resJsonBody);
req.resBody = JSON.stringify(req.rawResBody);
Object.assign(req.resConfig.headers, {
'Content-Type': 'application/json'
});
break;
//txt
default:
req.resBody = req.rawResBody;
Object.assign(req.resConfig.headers, {
'Content-Type': 'text/plain'
});
}
}

Expand All @@ -145,7 +150,7 @@ <h1><span class="name">type-handler.class.js</span></h1>
</div>
<footer>
<div class="copyright">FooDoc Copyright © 2016 The contributors to the JSDoc3 and FooDoc projects.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 12th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
<div class="generated-by">Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.5</a> on 24th Aug 2020 using the <a href="https://github.com/steveush/foodoc">FooDoc template</a>.</div>
</footer>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Expand Down
1 change: 1 addition & 0 deletions examples/mocks/cities/Berlin/parks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Großer Tiergarten
5 changes: 3 additions & 2 deletions lib/resource-loader.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ResourceLoader {
* @memberof ResourceLoader
*/
static get TYPES() {
return ['json', 'xml'];
return ['json', 'xml', 'txt'];
}

/**
Expand Down Expand Up @@ -136,7 +136,8 @@ class ResourceLoader {
},
json: input => {
return JSON.parse(input);
}
},
txt: input => input
};

logger.debug(`Content type ${fileType}`);
Expand Down
11 changes: 8 additions & 3 deletions lib/type-handler.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,29 @@ class TypeHandler {
static encodeResponseBody(req, res, next) {
const [type] = ResourceLoader.acceptedTypes(req);
if (req.resBody) {
req.resJsonBody = req.resBody;
req.rawResBody = req.resBody;
req.resConfig = req.resConfig || {};
req.resConfig.headers = req.resConfig.headers || {};

switch(type) {
case 'xml':
req.resBody = `<?xml version="1.0" encoding="UTF-8" ?>${j2xParser.parse(req.resJsonBody)}`;
req.resBody = `<?xml version="1.0" encoding="UTF-8" ?>${j2xParser.parse(req.rawResBody)}`;
Object.assign(req.resConfig.headers, {
'Content-Type': 'application/xml'
});
break;
case 'json':
req.resBody = JSON.stringify(req.resJsonBody);
req.resBody = JSON.stringify(req.rawResBody);
Object.assign(req.resConfig.headers, {
'Content-Type': 'application/json'
});
break;
//txt
default:
req.resBody = req.rawResBody;
Object.assign(req.resConfig.headers, {
'Content-Type': 'text/plain'
});
}
}

Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mockettaro",
"version": "2.0.0",
"version": "2.1.0",
"description": "Node Rest Mock Server",
"main": "index.js",
"scripts": {
Expand All @@ -18,10 +18,18 @@
"keywords": [
"mock",
"server",
"rest",
"json",
"xml",
"txt",
"yml",
"yaml",
"es6",
"rest",
"delay",
"status",
"statusCode",
"header",
"headers",
"cache"
],
"author": "Marco Ricupero <marco.ricupero@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectVersion=2.0.0
sonar.projectVersion=2.1.0
sonar.sourceEncoding=UTF-8
sonar.sources=./
sonar.exclusions=**/node_modules/**,**/examples/**,**/test/**,**/coverage/**,**/docs/**
Expand Down
33 changes: 29 additions & 4 deletions test/mockettaro-rest.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { chai, moment } = require('./test.utils');
const { chai, moment, expect } = require('./test.utils');
const fastXmlParser = require('fast-xml-parser');

describe('Mockettaro REST resources', ()=>{
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Mockettaro REST resources', ()=>{
});
});

describe('Rome/San Paolo/shops', () => {
describe('Accept types', () => {
describe('No specific accept type', () => {
it('Should return 200 and a json array of shops', done => {
chai.request(server).get('/test/cities/Rome/San Paolo/shops')
Expand Down Expand Up @@ -206,9 +206,9 @@ describe('Mockettaro REST resources', ()=>{
body.should.be.an('object').that.own.property('shop');
const { shop } = body;
shop.should.be.an('object').that.own.property('name');
shop.name.should.be.equals('Figaro');
shop.name.should.equals('Figaro');
shop.should.be.an('object').that.own.property('type');
shop.type.should.be.equals('Barber shop');
shop.type.should.equals('Barber shop');
done();
})
.catch(err => {
Expand All @@ -217,6 +217,31 @@ describe('Mockettaro REST resources', ()=>{
});
});
});

describe('Accepting text', () => {
const request = require('request');
it('Should return 200 and a string', async () => {
const resource = '/test/cities/Berlin/parks';
const { headers, body, statusCode } = await (new Promise((resolve, reject) => request.get({
url: `http://localhost:${testPort}${resource}`,
headers: {
'Accept': 'text/plain'
}
}, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve( { ...response, body });
}
})));
expect(statusCode).to.be.equal(200);
headers['content-type'].split('; ').should.contain('text/plain');
headers.should.not.own.property('cached-response');
headers.should.not.own.property('cached-config');

expect(body).to.be.a('string').that.equals('Großer Tiergarten');
});
});
});

describe('Delayed resource', () => {
Expand Down

0 comments on commit 8d8ccc1

Please sign in to comment.