This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
188 lines (160 loc) · 5.94 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (c) 2015 Yahoo Inc. */
/*jshint node: true */
/**
* This is a node server used to mock a web service layer
* @author R.Cocetta
*/
"use strict";
var path = require('path'),
express = require('express'),
app = express(),
url = require('url'),
util = require("util"),
log = util.log,
getMergeableRes = require('./libs/mergeable_res.js'),
ext_libs = {},
_ = require('lodash'),
parent = module.parent.parent || module.parent,
embedded = !!module.parent.parent,
dirname = path.dirname(parent.filename),
mockUtils = require("./utils.js")(dirname);
/**
* Used to handle mock endpoint that extend other mocks.
* @method handleExtendEndpoint
*/
function handleExtendEndpoint(req, res, cfgItem, key, method, cfg) {
var qsOverride = {},
new_res = getMergeableRes(res, function (statusCode, data) {
var result;
if (typeof data === "string") {
data = JSON.parse(data);
}
result = _.merge(data, cfgItem.obj);
serveObject(req, res, res.statusCode, data);
}),
qsParamName = cfg.qsParam || "tc";
qsOverride[qsParamName] = cfgItem.extendFrom;
handleRequest(key, cfg, method, qsOverride, req, new_res);
}
/**
* Serves the static file associated with a key
* @param {object} req The request obj
* @param {object} res The response object
* @param {object} cfgItem the Config item associated with the enpoint you're treating now
* @param {string} key A key in the mockResponses object
*/
function serveStaticFileForKey(req, res, cfgItem, key, method) {
var statusCode = cfgItem.statusCode || 200;
log("[Mockaccino] Serving static file" + __dirname + cfgItem.path);
mockUtils.serveStaticFile(req, res, cfgItem.path, statusCode);
}
/**
* Serves the static file associated with a key
* @param {object} req The request obj
* @param {object} res The response object
* @param {object} cfgItem the Config item associated with the enpoint you're treating now
* @param {string} key A key in the mockResponses object
*/
function serveObject(req, res, statusCode, object) {
log("[Mockaccino] Serving object");
res.send(statusCode, object);
}
/**
* Loads the external libs
* @method loadExternalLibs
* @param {object} cfg the config object
*/
function loadExternalLibs(cfg) {
Object.keys(cfg.ext_libs).forEach(function (key) {
ext_libs[key] = require(dirname + cfg.ext_libs[key]);
});
}
/**
* Returns an object describing the behaviour for a single request
* @method getBehaviourForRequest
* @param {object} cfg the full config
* @param {object} qs the parse querystring
* @param {string} key the route key
* @param {string} method the HTTP method
* @return {object} an object containing {cfgItem, key, method, fn}
*/
function getBehaviourForRequest(req, res, cfg, qs, key, method) {
var qsParam = cfg.queryStringParam,
specConfig,
mockResponses = cfg.mockResponses;
if ((qs) && (qsParam) && (qs[qsParam]) && (mockResponses[qs[qsParam]]) && (mockResponses[qs[qsParam]][key]) && (mockResponses[qs[qsParam]][key][method])) {
//if there is a defined behaviour for a specific query string parameter, and that parameter is defined
specConfig = getBehaviourFor(cfg, key, qs[qsParam], method);
} else {
specConfig = getBehaviourFor(cfg, key, "default", method);
}
return specConfig;
}
/**
* Returns thje specific behaviour for an endpoint
* @method getBehaviourFor
* @param {object} cfg the full config
* @param {string} key the route key
* @param {[type]} qsVal The Query string value
* @param {string} method the HTTP method
* @return {object} an object containing {cfgItem, key, method, fn}
*/
function getBehaviourFor(cfg, key, qsVal, method) {
var mockResponses = cfg.mockResponses[qsVal],
cfgItem = mockResponses[key][method],
resObject = {
"cfgItem": cfgItem,
"key": key,
"method": method
},
fn;
if (cfgItem.type === "staticFile") {
fn = serveStaticFileForKey;
} else if (cfgItem.type === "function") {
fn = mockUtils.walkJSONDot(ext_libs, cfgItem.fn);
} else if (cfgItem.type === "extend") {
fn = handleExtendEndpoint;
}
resObject.fn = fn;
return resObject;
}
/**
* Handles a request
* @method handleRequest
* @param {string} key the route key
* @param {object} cfg the full config
* @param {string} method the HTTP method
* @param {object} req The Express req
* @param {object} res The Express res
* @param {object} qsOverride Used to override the querystring
*/
function handleRequest(key, cfg, method, qsOverride, req, res) {
var qs = qsOverride || url.parse(req.url, true).query,
behaviour = getBehaviourForRequest(req, res, cfg, qs, key, method);
behaviour.fn(req, res, behaviour.cfgItem, behaviour.key, method, cfg);
}
function getMockserver(cfg) {
var mockResponses;
if (!cfg) {
throw "FATAL: Mockaccino needs a config object to work";
}
mockResponses = cfg.mockResponses;
//loads the libraries passed in the config
loadExternalLibs(cfg);
if (!embedded) {
app.use(express.bodyParser());
}
app.use(express.logger());
app.configure(function () {
log("[Mockaccino] Creating routes");
// for each line in the mockResponses configuration, creates a route
// that either serves a file or runs a function
Object.keys(mockResponses.default).forEach(function (key) {
Object.keys(mockResponses.default[key]).forEach(function (method) {
app[method](key, handleRequest.bind(this, key, cfg, method, null));
});
});
});
return app;
}
module.exports = getMockserver;