Skip to content

Commit

Permalink
fix: added support for testing header modification with http-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGilleran committed Mar 26, 2019
1 parent 9e9e65d commit e1a47c5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
/// like to change request.path in mid-flight.
options.path = req.path

// similarly, node-http-proxy will modify headers in flight, so we have to put the headers back
// into options
const reqHeaders = req.getHeaders ? req.getHeaders() : req._headers;
if (Object.keys(reqHeaders).length > 0) {
options.headers = req.getHeaders ? req.getHeaders() : req._headers;
}

// fixes #976
options.protocol = `${options.proto}:`

Expand Down
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"got": "^9.4.0",
"http-proxy": "^1.17.0",
"hyperquest": "^2.1.3",
"isomorphic-fetch": "^2.2.0",
"lolex": "^3.0.0",
Expand Down
41 changes: 41 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const restify = require('restify-clients')
const hyperquest = require('hyperquest')
const got = require('got')
const nock = require('..')
const httpProxy = require('http-proxy')

require('./cleanup_after_each')()

Expand Down Expand Up @@ -1711,3 +1712,43 @@ test('teardown', t => {
t.deepEqual(leaks, [], 'No leaks')
t.end()
})

test('works when headers removed by http-proxy', t => {
const serviceScope = nock('http://service', {
badheaders: ['authorization'],
})
.get('/endpoint')
.reply(200)

const proxy = httpProxy.createProxyServer({
prependUrl: false,
})

proxy.on('proxyReq', (proxyReq, req, res) => {
proxyReq.removeHeader('authorization')
})

const server = http.createServer((request, response) => {
proxy.web(request, response, { target: 'http://service' })
})

server.listen(() => {
const options = {
uri: `http://localhost:${server.address().port}/endpoint`,
method: 'GET',
headers: {
authorization: 'blah',
},
}

mikealRequest(options, (err, resp, body) => {
if (err) {
t.error(err)
} else {
t.equal(200, resp.statusCode)
serviceScope.done()
server.close(t.end)
}
})
})
})

0 comments on commit e1a47c5

Please sign in to comment.