-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-server.js
74 lines (60 loc) · 2.3 KB
/
test-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
const https = require("https");
const express = require("express");
const caMaker = require("./cacerts.js");
const url = require("url");
const assert = require("assert");
const fetch = require("node-fetch");
const simpleCertService = require("./simple-cert-service.js");
const test = async function () {
const {
listener: certServiceListener,
dnsServer,
port: certServicePort,
ca
} = await simpleCertService();
try {
https.globalAgent = new https.Agent({ ca: ca });
const certResponse = await fetch(
`https://localhost:${certServicePort}/certificates?domain=my-test-server.com&version=2`, {
agent: https.globalAgent
});
console.log("response from cert server>", certResponse.status);
assert.ok(certResponse.status === 200);
const certObject = await certResponse.json();
// Now get the parts and start a service with the new cert
const {ca:myCa, pkcs12, pkcs12password} = certObject;
const myTlsApp = express();
myTlsApp.get("/test", function (req, res) {
res.send("<h1>Hello World!</h1>");
});
// Apparently we can't use the base64 that forge generates, so do this:
const buf = Buffer.from(pkcs12, "base64");
// Now tls opts based on just the PKCS12 data and the passphrase
const myTlsOpts = {
pfx: buf,
passphrase: pkcs12password
};
const testTlsServerPort = 8444;
const myTlsListener = https.createServer(myTlsOpts, myTlsApp)
.listen(testTlsServerPort);
// Now connect to that service
try {
const opts = { agent: new https.Agent({ca:myCa}) };
const response = await fetch(`https://localhost:${testTlsServerPort}/test`);
console.log("test server fetch status>", response.status);
assert.ok(response.status === 200);
const testBody = await response.text();
console.log("test server fetch body>", testBody);
assert.deepStrictEqual(testBody, "<h1>Hello World!</h1>");
}
finally {
myTlsListener.close();
}
}
finally {
certServiceListener.close();
dnsServer.close();
}
};
test().then();
// End