test.js
2.13 KB
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
'use strict';
//var httpsOptions = require('localhost.daplie.com-certificates').merge({});
var webrootPath = '/tmp/acme-challenge';
var challenge = require('./').create({ debug: true, webrootPath: webrootPath });
var opts = challenge.getOptions();
var domain = 'example.com';
var token = 'token-id';
var key = 'secret-key';
challenge.remove(opts, domain, token, function () {
// ignore error, if any
challenge.set(opts, domain, token, key, function (err) {
// if there's an error, there's a problem
if (err) { throw err; }
// throw new Error("manually check /tmp/acme-challenge");
challenge.get(opts, domain, token, function (err, _key) {
// if there's an error, there's a problem
if (err) { throw err; }
// should retrieve the key
if (key !== _key) {
throw new Error("FAIL: could not get key by token");
}
challenge.remove(opts, domain, token, function () {
// if there's an error, there's a problem
if (err) { throw err; }
challenge.get(opts, domain, token, function (err, _key) {
// error here is okay
// should NOT retrieve the key
if (_key) {
throw new Error("FAIL: should not get key");
}
console.info('[PASS] unit test');
});
});
});
});
});
function loopbackTest() {
var http = require('http');
var serveStatic = require('serve-static')(webrootPath, { dotfiles: 'allow' });
var finalhandler = require('finalhandler');
var server = http.createServer(function (req, res) {
// fail on purpose
//setTimeout(function () {
req.url = req.url.replace(/^\/\.well-known\/acme-challenge\//, '/');
serveStatic(req, res, finalhandler(req, res));
//}, 2000);
});
server.listen(0, function () {
var port = server.address().port;
opts.webrootPath = webrootPath;
opts.loopbackPort = port;
opts.loopbackTimeout = 500;
challenge.test(opts, 'localhost', 'foo', 'bar', function (err) {
server.close();
if (err) { console.error(err.stack); process.exit(1); return; }
console.info('[PASS] localhost loopback');
});
});
}
loopbackTest();