-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainTest.js
376 lines (325 loc) · 9.65 KB
/
mainTest.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"use strict";
var turnOnDebugLogging = false;
var WebComponent = require("../index.js");
var Component = require("component-class");
var assert = require("chai").assert;
var request = require('supertest');
var dummyComponentManager = {
registerType: function() {},
getType: function() {},
register: function() {},
get: function(name) {
if (name === "logger") return dummyLogger;
if (name === "cert-manager") return dummyCertManager;
},
clear: function() {},
config: function() {},
init: function() {},
shutdown: function() {},
componentList: new Map(),
typeList: new Map()
};
var dummyLogger = {
create: function() {
return new Proxy(function() {}, {
get: function() {
return function(...msg) {
if (turnOnDebugLogging) console.log(...msg);
};
},
});
}
};
var fs = require("fs");
var dummyCertManager = {
config: function() {
return {
cert: fs.readFileSync("test/helpers/certs/cert.pem", "utf8"),
key: fs.readFileSync("test/helpers/certs/key.pem", "utf8")
};
}
};
describe("web component", function() {
it("can be created", function() {
var wc = new WebComponent(dummyComponentManager);
assert.instanceOf(wc, Component);
});
it("can init and shutdown", function() {
var wc = new WebComponent(dummyComponentManager);
var res = wc.init();
assert.isUndefined(res);
res = wc.shutdown();
assert.isUndefined(res);
});
});
describe("web component routes", function() {
var wc;
beforeEach(function() {
wc = new WebComponent(dummyComponentManager);
wc.defaultPort = 8080; // probably not running as root, so don't use port 80
});
afterEach(function() {
wc.shutdown();
});
it("can serve static data", function(done) {
wc.init();
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
request(wc.app)
.get("/index.html")
.expect(200, "<html>hi</html>", function(err) {
if (err) throw err;
done();
});
});
it("can add static routes before init", function(done) {
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
wc.init();
request(wc.app)
.get("/index.html")
.expect(200, "<html>hi</html>", function(err) {
if (err) throw err;
done();
});
});
it("can serve dynamic data", function(done) {
wc.init();
wc.addDynamic({
path: "/",
method: "get",
fn: function(req, res) {
res.send("hi");
}
});
request(wc.app)
.get("/")
.expect(200, "hi", function(err) {
if (err) throw err;
done();
});
});
it("can add dynamic routes before init", function(done) {
wc.addDynamic({
path: "/",
method: "get",
fn: function(req, res) {
res.send("hi");
}
});
wc.init();
request(wc.app)
.get("/")
.expect(200, "hi", function(err) {
if (err) throw err;
done();
});
});
it("can serve multiple static and dynamic routes");
it("can redirect to https", function(done) {
wc.setRedirect({
destProtocol: "https"
});
wc.init();
request("http://localhost:8080")
.get("/")
.expect(301)
.expect("Location", "https://localhost/")
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("can redirect after init", function(done) {
wc.init();
wc.setRedirect({
destProtocol: "https"
});
request("http://localhost:8080")
.get("/")
.expect(301)
.expect("Location", "https://localhost/")
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("can redirect to https:port", function(done) {
wc.setRedirect({
destProtocol: "https",
destPort: 8443
});
wc.init();
request("http://localhost:8080")
.get("/")
.expect(301)
.expect("Location", "https://localhost:8443/")
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("can redirect to new host", function(done) {
wc.setRedirect({
destHost: "google.com",
destProtocol: "https",
destUrl: "/"
});
wc.init();
request("http://localhost:8080")
.get("/google")
.expect(301)
.expect("Location", "https://google.com/")
.end(function(err) {
if (err) throw err;
done();
});
});
it("can serve static and redirect everything else", function(done) {
wc.addStatic({
path: "/static",
dir: "test/helpers/static"
});
wc.setRedirect({
destProtocol: "https"
});
wc.init();
request("http://localhost:8080")
.get("/static/index.html")
.expect(200, "<html>hi</html>")
.end((err) => {
if (err) throw err;
request("http://localhost:8080")
.get("/foo.html")
.expect(301)
.expect("Location", "https://localhost/foo.html")
.end((err) => {
if (err) throw err;
done();
});
});
});
it("redirects based on source host", function(done) {
wc.setRedirect({
destProtocol: "https",
matchHost: /localhost/
});
wc.init();
request("http://localhost:8080")
.get("/")
.expect(301)
.expect("Location", "https://localhost/")
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("doesn't redirect if source host doesn't match", function(done) {
wc.setRedirect({
destProtocol: "https",
matchHost: /google\.com/
});
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
wc.init();
request("http://localhost:8080")
.get("/")
.expect(200, "<html>hi</html>")
.end(function(err, res) {
if (err) throw err;
done();
});
});
it("redirects based on source url");
it("redirects based on source function");
it("can serve a route and redirect everything else");
it("can set port", function(done) {
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
wc.setPort(7777);
wc.init();
request("http://localhost:7777")
.get("/index.html")
.expect(200, "<html>hi</html>", function(err) {
if (err) throw err;
done();
});
});
it("can get port", function() {
var ret = wc.getPort();
assert.isUndefined(ret);
wc.setPort(7777);
ret = wc.getPort();
assert.isNumber(ret);
assert.strictEqual(ret, 7777);
});
it("throws if setting port after server started", function() {
wc.init();
assert.throws(function() {
wc.setPort(7777);
}, Error, "can't set port after server has started");
});
it("throws on bad port number");
it("can set domain", function() {
wc.setDomain("example.com");
});
it("can get domain", function() {
var ret = wc.getDomain();
assert.isUndefined(ret);
wc.setDomain("example.com");
ret = wc.getDomain();
assert.strictEqual(ret, "example.com");
});
it("throws if setting domain after server started", function() {
wc.init();
assert.throws(function() {
wc.setDomain("example.com");
}, Error, "can't set domain after server has started");
});
it("can set https", function(done) {
this.slow(150);
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
wc.setHttps(true);
wc.setPort(8443);
// for the self signed cert...
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
wc.init();
request("https://localhost:8443")
.get("/index.html")
.expect(200, "<html>hi</html>")
.end(function(err) {
if (err) throw err;
done();
});
});
it("can get protocol", function() {
wc.addStatic({
path: "/",
dir: "test/helpers/static"
});
var ret = wc.getProtocol();
assert.strictEqual(ret, "http");
wc.setHttps(true);
ret = wc.getProtocol();
assert.strictEqual(ret, "https");
});
it("https throws if called after init");
it("can run two instances at the same time");
it("can set body parser to JSON");
it("can set body parser to raw");
it("can set body parser to text");
it("can set body parser to url encoded");
it("throws if POST and no body parser (?)");
it("can set session");
});