forked from jhudson8/smocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hapi.js
168 lines (145 loc) · 4.5 KB
/
hapi.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
/**
* Exposes HAPI integration for the mock server
*/
var smocks = require('./lib');
var Hapi = require('hapi');
var _ = require('lodash');
var _inputs = {
boolean: require('./lib/admin/api/input-plugins/checkbox'),
text: require('./lib/admin/api/input-plugins/text'),
select: require('./lib/admin/api/input-plugins/select'),
multiselect: require('./lib/admin/api/input-plugins/multiselect')
};
module.exports = {
toPlugin: function(pluginOptions, smocksOptions) {
pluginOptions = pluginOptions || {};
smocksOptions = smocksOptions || {};
smocks.initOptions = smocksOptions;
smocks._sanityCheckRoutes();
options = smocks._sanitizeOptions(smocksOptions);
var register = function (server, options, next) {
function _next (err) {
if (err) {
next(err);
} else {
configServer(server);
next();
}
}
if (pluginOptions.onRegister) {
pluginOptions.onRegister(server, options, _next);
} else {
_next();
}
};
return register;
},
start: function(hapiOptions, smocksOptions) {
if (!smocks.id()) {
throw new Error('You must set an id value for the smocks instance... smocks.id("my-project")');
}
hapiOptions = hapiOptions || {};
var hapiServerOptions = hapiOptions.server;
var hapiConnectionOptions = hapiOptions.connection;
if (!hapiServerOptions && !hapiConnectionOptions) {
hapiConnectionOptions = hapiOptions;
}
smocksOptions = smocks._sanitizeOptions(smocksOptions || {});
smocks.initOptions = smocksOptions;
smocks._sanityCheckRoutes();
if (!hapiConnectionOptions.routes) {
hapiConnectionOptions.routes = { cors: true };
}
var server = new Hapi.Server(hapiServerOptions);
server.connection(hapiConnectionOptions);
configServer(server);
server.start(function(err) {
if (err) {
console.error(err.message);
process.exit(1);
}
});
console.log('started smocks server on ' + hapiConnectionOptions.port + '. visit http://localhost:' + hapiConnectionOptions.port + '/_admin to configure');
return {
server: server,
start: function(options) {
self.start(options);
}
};
}
};
function wrapReply(request, reply, plugins) {
var rtn = function() {
var response = reply.apply(this, arguments);
if (smocks.state.onResponse) {
smocks.state.onResponse(request, response);
}
_.each(plugins, function(plugin) {
if (plugin.onResponse) {
plugin.onResponse(request, response);
}
});
return response;
};
_.each(['continue', 'file', 'view', 'close', 'proxy', 'redirect'], function(key) {
rtn[key] = function() {
reply[key].apply(reply, arguments);
};
});
return rtn;
}
function configServer(server) {
// set the input types on the smocks object
smocks.input = function(type, options) {
_inputs[type] = options;
};
smocks.inputs = {
get: function() {
return _inputs;
}
};
var _routes = smocks.routes.get();
var _plugins = smocks.plugins.get();
_.each(_routes, function(route) {
if (route.hasVariants()) {
var connection = server;
if (route.connection()) {
connection = server.select(route.connection());
}
connection.route({
method: route.method(),
path: route.path(),
config: route.config(),
handler: function(request, reply) {
if (!smocks.state.isInitialized(request)) {
_.each(_routes, function(route) {
route.resetRouteVariant(request);
route.resetSelectedInput(request);
});
smocks.plugins.resetInput(request);
smocks.state.onInitialized && smocks.state.onInitialized(request);
}
if (smocks.state.onRequest) {
smocks.state.onRequest(request, reply);
}
var pluginIndex = 0;
function handlePlugins() {
var plugin = _plugins[pluginIndex++];
if (plugin) {
if (plugin.onRequest) {
plugin.onRequest.call(smocks._executionContext(request, route, plugin), request, reply, handlePlugins);
} else {
handlePlugins();
}
} else {
reply = wrapReply(request, reply, _plugins);
route._handleRequest.call(route, request, reply);
}
}
handlePlugins();
}
});
}
}, this);
require('./lib/admin')(server, smocks);
}