Skip to content

Commit

Permalink
Merge pull request #1 from delvedor/send-header
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Jul 17, 2020
2 parents 8699c01 + 8696f31 commit 1195dc4
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class HttpProxyAgent extends http.Agent {
host: this.proxy.hostname,
port: this.proxy.port,
path: `${options.host}:${options.port}`,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close' },
agent: false
}

if (this.proxy.username != null && this.proxy.password != null) {
const base64 = Buffer.from(`${this.proxy.username}:${this.proxy.password}`).toString('base64')
requestOptions.headers = { 'proxy-authorization': `Basic ${base64}` }
requestOptions.headers['proxy-authorization'] = `Basic ${base64}`
}

const request = (this.proxy.protocol === 'http:' ? http : https).request(requestOptions)
Expand Down Expand Up @@ -62,12 +63,13 @@ class HttpsProxyAgent extends https.Agent {
host: this.proxy.hostname,
port: this.proxy.port,
path: `${options.host}:${options.port}`,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close' },
agent: false
}

if (this.proxy.username != null && this.proxy.password != null) {
const base64 = Buffer.from(`${this.proxy.username}:${this.proxy.password}`).toString('base64')
requestOptions.headers = { 'proxy-authorization': `Basic ${base64}` }
requestOptions.headers['proxy-authorization'] = `Basic ${base64}`
}

const request = (this.proxy.protocol === 'http:' ? http : https).request(requestOptions)
Expand Down
76 changes: 76 additions & 0 deletions test/http-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,82 @@ test('Basic', async t => {
proxy.close()
})

test('Connection header (keep-alive)', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'keep-alive')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Connection header (close)', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'close')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpProxyAgent({
keepAlive: false,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Proxy authentication', async t => {
const server = await createServer()
const proxy = await createProxy()
Expand Down
76 changes: 76 additions & 0 deletions test/http-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,82 @@ test('Basic', async t => {
proxy.close()
})

test('Connection header (keep-alive)', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'keep-alive')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Connection header (close)', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'close')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpsProxyAgent({
keepAlive: false,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Proxy authentication', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
Expand Down
76 changes: 76 additions & 0 deletions test/https-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,82 @@ test('Basic', async t => {
proxy.close()
})

test('Connection header (keep-alive)', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'keep-alive')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Connection header (close)', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'close')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpProxyAgent({
keepAlive: false,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Proxy authentication', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
Expand Down
76 changes: 76 additions & 0 deletions test/https-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,82 @@ test('Basic', async t => {
proxy.close()
})

test('Connection header (keep-alive)', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'keep-alive')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Connection header (close)', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
t.is(req.headers.connection, 'close')
fn(null, true)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpsProxyAgent({
keepAlive: false,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

test('Proxy authentication', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
Expand Down

0 comments on commit 1195dc4

Please sign in to comment.