Skip to content

Commit

Permalink
Add twig support
Browse files Browse the repository at this point in the history
  • Loading branch information
tejerka committed Dec 23, 2019
1 parent 4bb36cf commit 621f6da
Show file tree
Hide file tree
Showing 9 changed files with 641 additions and 571 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:10-alpine
FROM node:12-alpine

COPY ./app /usr/src/app

Expand All @@ -18,7 +18,7 @@ RUN rm -rf /usr/src/app/gui
EXPOSE 80
EXPOSE 4580

VOLUME /usr/src/app/mocks
#VOLUME /usr/src/app/mocks

WORKDIR /usr/src/app

Expand Down
72 changes: 36 additions & 36 deletions app/http-mock-server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const http = require('http');
const url = require('url');
const fs = require('fs');
const Velocity = require('velocityjs');
const sleep = require('sleep');
const path = require('path');
const mime = require('mime');
Expand All @@ -10,6 +9,7 @@ const uniqid = require('./uniqid');
const ValidatorsStack = require('./validators/ValidatorsStack');
const AnswererStack = require('./answerers/AnswererStack');
const VoterStack = require('./voters/VotersStack');
const TemplateEngineStack = require('./templating/TemplateEnginesStack');
const MaxCallsVoter = require('./voters/MaxCallsVoter');
const MethodVoter = require('./voters/MethodVoter');
const PathVoter = require('./voters/PathVoter');
Expand All @@ -22,6 +22,7 @@ class HttpMockServer {
this.app = app;
this.validatorsStack = new ValidatorsStack();
this.answererStack = new AnswererStack();
this.templateEngineStack = new TemplateEngineStack();
this.voterStack = new VoterStack()
.addVoter(new MaxCallsVoter())
.addVoter(new MethodVoter(this.validatorsStack))
Expand All @@ -47,7 +48,7 @@ class HttpMockServer {
let foundEndpoint = this.findEndpoint(req);

if (foundEndpoint !== null) {
HttpMockServer.writeResponse(
this.writeResponse(
req,
res,
foundEndpoint,
Expand All @@ -62,11 +63,10 @@ class HttpMockServer {
.listen(80);
}

static writeResponse(request, response, endpoint, endpointResponse) {
writeResponse(request, response, endpoint, endpointResponse) {
if (endpointResponse.delay) {
if (
typeof endpointResponse.delay === 'object' &&
endpointResponse.delay !== null
typeof endpointResponse.delay === 'object'
) {
const minDelay = endpointResponse.delay.min || 0;
const maxDelay = endpointResponse.delay.max || 10000;
Expand All @@ -79,35 +79,29 @@ class HttpMockServer {
}
}

response.writeHead(
endpointResponse.status || 200,
endpointResponse.headers
);
try {
const endpointResponseBody = HttpMockServer.getEndpointBody(endpoint, endpointResponse);
let ResponseBody = '';

let velocityOptions = endpointResponse.velocity;

if (typeof velocityOptions === 'boolean') {
velocityOptions = {
enabled: velocityOptions,
};
}
if (endpointResponseBody.type === 'plainText') {
ResponseBody = endpointResponseBody.value;
} else {
ResponseBody = this.templateEngineStack.run({
endpoint,
endpointResponse,
request,
body: endpointResponseBody.value,
bodyFilePath: endpointResponseBody.path
});
}

if (velocityOptions && velocityOptions.enabled) {
response.write(
Velocity.render(
HttpMockServer.getEndpointBody(endpoint, endpointResponse),
{
math: Math,
req: request,
endpoint: endpoint,
context: velocityOptions.context,
}
)
);
} else {
response.write(
HttpMockServer.getEndpointBody(endpoint, endpointResponse)
response.writeHead(
endpointResponse.status || 200,
endpointResponse.headers
);
response.write(ResponseBody);
} catch (e) {
console.log(e);
}
}

Expand Down Expand Up @@ -139,17 +133,17 @@ class HttpMockServer {
}

static getEndpointBody(endpoint, endpointResponse) {
let ResponseBody = endpointResponse.body;
let ResponseBody = JSON.parse(JSON.stringify(endpointResponse.body));

if (typeof ResponseBody === 'string') {
ResponseBody = {
type: 'plaintext',
value: ResponseBody
value: ResponseBody,
};
}

if (ResponseBody.type === 'plainText') {
return ResponseBody.value;
return ResponseBody;
}

if (ResponseBody.type === 'file') {
Expand All @@ -167,10 +161,16 @@ class HttpMockServer {
ResponseBody.value
);

return fs.readFileSync(
ResponseBody.path = bodyFilePath;

ResponseBody.value = fs.readFileSync(
bodyFilePath,
imageMimeTypes.indexOf(mime.getType(bodyFilePath)) === -1 ? 'utf8' : null
imageMimeTypes.indexOf(mime.getType(bodyFilePath)) === -1
? 'utf8'
: null
);

return ResponseBody;
}

return '';
Expand Down

0 comments on commit 621f6da

Please sign in to comment.