Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (89 loc) · 3.11 KB
/
Copy pathindex.js
File metadata and controls
122 lines (89 loc) · 3.11 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
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
import os from 'os'
import path from 'path'
import fs from 'fs-extra'
import test from 'tape'
import bent from 'bent'
import hostname from '@small-tech/cross-platform-hostname'
import https from '../index.js'
const getHttpsString = bent('GET', 'string')
const DEFAULT_SETTINGS_PATH = path.join(os.homedir(), '.small-tech.org', 'https')
const CUSTOM_SETTINGS_PATH = path.join(DEFAULT_SETTINGS_PATH, 'test')
function setup () {
fs.removeSync(path.join(DEFAULT_SETTINGS_PATH, 'global', 'staging'))
fs.removeSync(CUSTOM_SETTINGS_PATH)
}
async function testLocalAndGlobalServer (t, customSettingsPath = false) {
const localOptions = customSettingsPath ? {} : {settingsPath: CUSTOM_SETTINGS_PATH}
const globalOptions = {
staging: true,
domains: [hostname]
}
if (customSettingsPath) { globalOptions.settingsPath = CUSTOM_SETTINGS_PATH }
// Test local server
const localServer = https.createServer(localOptions, (request, response) => {
response.end('local server ok')
})
await new Promise((resolve, reject) => {
localServer.listen(443, () => {
resolve()
})
})
const localServerResponse = await getHttpsString('https://localhost')
t.strictEquals(localServerResponse, 'local server ok', 'local server response is as expected')
localServer.close()
await new Promise((resolve, reject) => {
localServer.on('close', () => {
resolve()
})
})
// Test global server
const globalServer = https.createServer(globalOptions, (request, response) => { response.end('global server ok') })
await new Promise((resolve, reject) => {
globalServer.listen(443, () => {
resolve()
})
})
const globalServerResponse = await getHttpsString(`https://${hostname}`)
t.strictEquals(globalServerResponse, 'global server ok', 'global server response is as expected')
globalServer.close()
await new Promise((resolve, reject) => {
globalServer.on('close', () => {
resolve()
})
})
}
test('@small-tech/https', async t => {
setup()
//
// Test using default settings path.
//
// Test initial run (provisioning).
await testLocalAndGlobalServer(t)
// Test subsequent run (certificate material provided from disk).
await testLocalAndGlobalServer(t)
//
// Test using custom settings path.
//
// Test initial run (provisioning).
await testLocalAndGlobalServer(t, /* customSettingsPath = */ true)
// Test subsequent run (certificate material provided from disk).
await testLocalAndGlobalServer(t, /* customSettingsPath = */ true)
// Test alternative function signature (no options object).
const localServer = https.createServer((request, response) => {
response.end('local server listener set ok')
})
await new Promise((resolve, reject) => {
localServer.listen(443, () => {
resolve()
})
})
const localServerResponse = await getHttpsString('https://localhost')
t.strictEquals(localServerResponse, 'local server listener set ok', 'alternative function signature works as expected')
localServer.close()
await new Promise((resolve, reject) => {
localServer.on('close', () => {
resolve()
})
})
t.end()
})