|
| 1 | +const test = require('tap').test |
| 2 | +const path = require('path') |
| 3 | +const fs = require('fs') |
| 4 | +const request = require('request') |
| 5 | +const httpServer = require('../lib/http-server') |
| 6 | +const promisify = require('util').promisify |
| 7 | + |
| 8 | +const requestAsync = promisify(request) |
| 9 | +const fsReadFile = promisify(fs.readFile) |
| 10 | + |
| 11 | +// Prevent errors from being swallowed |
| 12 | +process.on('uncaughtException', console.error) |
| 13 | + |
| 14 | +const root = path.join(__dirname, 'fixtures', 'root') |
| 15 | +const httpsOpts = { |
| 16 | + key: path.join(__dirname, 'fixtures', 'https', 'agent2-key.pem'), |
| 17 | + cert: path.join(__dirname, 'fixtures', 'https', 'agent2-cert.pem') |
| 18 | +} |
| 19 | + |
| 20 | +// Tests are grouped into those which can run together. The groups are given |
| 21 | +// their own port to run on and live inside a Promise. Tests are done when all |
| 22 | +// Promise test groups complete. |
| 23 | +test('proxy options', (t) => { |
| 24 | + new Promise((resolve) => { |
| 25 | + const server = httpServer.createServer({ |
| 26 | + root, |
| 27 | + robots: true, |
| 28 | + headers: { |
| 29 | + 'Access-Control-Allow-Origin': '*', |
| 30 | + 'Access-Control-Allow-Credentials': 'true' |
| 31 | + }, |
| 32 | + cors: true, |
| 33 | + corsHeaders: 'X-Test', |
| 34 | + ext: true, |
| 35 | + brotli: true, |
| 36 | + gzip: true |
| 37 | + }) |
| 38 | + server.listen(8080, async () => { |
| 39 | + try { |
| 40 | + |
| 41 | + // Another server proxies 8081 to 8080 |
| 42 | + const proxyServer = httpServer.createServer({ |
| 43 | + proxy: 'http://localhost:8080', |
| 44 | + root: path.join(__dirname, 'fixtures'), |
| 45 | + ssl: true, |
| 46 | + https: httpsOpts, |
| 47 | + proxyOptions: { |
| 48 | + secure: false |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + await new Promise((resolve) => { |
| 53 | + proxyServer.listen(8081, async () => { |
| 54 | + try { |
| 55 | + // Serve files from proxy root |
| 56 | + await requestAsync('https://localhost:8081/root/file', { rejectUnauthorized: false }).then(async (res) => { |
| 57 | + t.ok(res) |
| 58 | + t.equal(res.statusCode, 200) |
| 59 | + |
| 60 | + // File content matches |
| 61 | + const fileData = await fsReadFile(path.join(root, 'file'), 'utf8') |
| 62 | + t.equal(res.body.trim(), fileData.trim(), 'proxied root file content matches') |
| 63 | + }).catch(err => t.fail(err.toString())) |
| 64 | + |
| 65 | + // Proxy fallback |
| 66 | + await requestAsync('https://localhost:8081/file', { rejectUnauthorized: false }).then(async (res) => { |
| 67 | + t.ok(res) |
| 68 | + t.equal(res.statusCode, 200) |
| 69 | + |
| 70 | + // File content matches |
| 71 | + const fileData = await fsReadFile(path.join(root, 'file'), 'utf8') |
| 72 | + t.equal(res.body.trim(), fileData.trim(), 'proxy fallback root file content matches') |
| 73 | + }).catch(err => t.fail(err.toString())) |
| 74 | + } catch (err) { |
| 75 | + t.fail(err.toString()) |
| 76 | + } finally { |
| 77 | + proxyServer.close() |
| 78 | + resolve() |
| 79 | + } |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + } catch (err) { |
| 84 | + t.fail(err.toString()) |
| 85 | + } finally { |
| 86 | + server.close() |
| 87 | + resolve() |
| 88 | + } |
| 89 | + }) |
| 90 | + }) |
| 91 | + .then(() => t.end()) |
| 92 | + .catch(err => { |
| 93 | + t.fail(err.toString()) |
| 94 | + t.end() |
| 95 | + }) |
| 96 | +}) |
0 commit comments