forked from http-party/http-server
/
proxy-options.test.js
96 lines (86 loc) · 2.99 KB
/
proxy-options.test.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
const test = require('tap').test
const path = require('path')
const fs = require('fs')
const request = require('request')
const httpServer = require('../lib/http-server')
const promisify = require('util').promisify
const requestAsync = promisify(request)
const fsReadFile = promisify(fs.readFile)
// Prevent errors from being swallowed
process.on('uncaughtException', console.error)
const root = path.join(__dirname, 'fixtures', 'root')
const httpsOpts = {
key: path.join(__dirname, 'fixtures', 'https', 'agent2-key.pem'),
cert: path.join(__dirname, 'fixtures', 'https', 'agent2-cert.pem')
}
// Tests are grouped into those which can run together. The groups are given
// their own port to run on and live inside a Promise. Tests are done when all
// Promise test groups complete.
test('proxy options', (t) => {
new Promise((resolve) => {
const server = httpServer.createServer({
root,
robots: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true'
},
cors: true,
corsHeaders: 'X-Test',
ext: true,
brotli: true,
gzip: true
})
server.listen(8080, async () => {
try {
// Another server proxies 8081 to 8080
const proxyServer = httpServer.createServer({
proxy: 'http://localhost:8080',
root: path.join(__dirname, 'fixtures'),
ssl: true,
https: httpsOpts,
proxyOptions: {
secure: false
}
})
await new Promise((resolve) => {
proxyServer.listen(8081, async () => {
try {
// Serve files from proxy root
await requestAsync('https://localhost:8081/root/file', { rejectUnauthorized: false }).then(async (res) => {
t.ok(res)
t.equal(res.statusCode, 200)
// File content matches
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8')
t.equal(res.body.trim(), fileData.trim(), 'proxied root file content matches')
}).catch(err => t.fail(err.toString()))
// Proxy fallback
await requestAsync('https://localhost:8081/file', { rejectUnauthorized: false }).then(async (res) => {
t.ok(res)
t.equal(res.statusCode, 200)
// File content matches
const fileData = await fsReadFile(path.join(root, 'file'), 'utf8')
t.equal(res.body.trim(), fileData.trim(), 'proxy fallback root file content matches')
}).catch(err => t.fail(err.toString()))
} catch (err) {
t.fail(err.toString())
} finally {
proxyServer.close()
resolve()
}
})
})
} catch (err) {
t.fail(err.toString())
} finally {
server.close()
resolve()
}
})
})
.then(() => t.end())
.catch(err => {
t.fail(err.toString())
t.end()
})
})