Files
elasticsearch-js/test/acceptance/proxy.test.js
github-actions[bot] 4c1095d805 [Backport 7.x] Added proxy support (#1276)
Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
2020-08-03 11:41:20 +02:00

150 lines
4.4 KiB
JavaScript

'use strict'
// We are using self-signed certificates
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
const { test } = require('tap')
const { Client } = require('../../index')
const {
buildProxy: {
createProxy,
createSecureProxy,
createServer,
createSecureServer
}
} = require('../utils')
test('http-http proxy support', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const client = new Client({
node: `http://${server.address().address}:${server.address().port}`,
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})
test('http-https proxy support', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const client = new Client({
node: `https://${server.address().address}:${server.address().port}`,
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})
test('https-http proxy support', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const client = new Client({
node: `http://${server.address().address}:${server.address().port}`,
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})
test('https-https proxy support', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const client = new Client({
node: `https://${server.address().address}:${server.address().port}`,
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})
test('http basic authentication', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
proxy.authenticate = function (req, fn) {
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
}
const client = new Client({
node: `http://${server.address().address}:${server.address().port}`,
proxy: `http://hello:world@${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})
test('https basic authentication', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => {
t.strictEqual(req.url, '/_cluster/health')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
proxy.authenticate = function (req, fn) {
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
}
const client = new Client({
node: `https://${server.address().address}:${server.address().port}`,
proxy: `http://hello:world@${proxy.address().address}:${proxy.address().port}`
})
const response = await client.cluster.health()
t.deepEqual(response.body, { hello: 'world' })
server.close()
proxy.close()
})