Skip to content

Commit

Permalink
Merge d6e5184 into 2831f62
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGilleran committed May 17, 2019
2 parents 2831f62 + d6e5184 commit 50e6a4b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 44 deletions.
4 changes: 4 additions & 0 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ 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
options.headers = req.getHeaders()

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

Expand Down
65 changes: 23 additions & 42 deletions package-lock.json

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

88 changes: 86 additions & 2 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ test('emits error if https route is missing', t => {
t.equal(
err.message.trim(),
`Nock: No match for request ${JSON.stringify(
{ method: 'GET', url: 'https://example.test/abcdef892932' },
{
method: 'GET',
url: 'https://example.test/abcdef892932',
headers: {},
},
null,
2
)}`
Expand Down Expand Up @@ -473,7 +477,11 @@ test('emits error if https route is missing', t => {
t.equal(
err.message.trim(),
`Nock: No match for request ${JSON.stringify(
{ method: 'GET', url: 'https://example.test:123/dsadsads' },
{
method: 'GET',
url: 'https://example.test:123/dsadsads',
headers: {},
},
null,
2
)}`
Expand Down Expand Up @@ -1293,3 +1301,79 @@ test('teardown', t => {
t.deepEqual(leaks, [], 'No leaks')
t.end()
})

/*
* This test imitates a feature of node-http-proxy (https://github.com/nodejitsu/node-http-proxy) -
* modifying headers for an in-flight request by modifying them.
*/
test('works when headers removed by http-proxy', t => {
// Set up a nock that will fail if it gets an "authorization" header.
const serviceScope = nock('http://service', {
badheaders: ['authorization'],
})
.get('/endpoint')
.reply(200)

// Create a server to act as our reverse proxy.
const server = http.createServer((request, response) => {
// Make a request to the nock instance with the same request that came in.
const proxyReq = http.request({
host: 'service',
// Get the path from the incoming request and pass it through
path: `/${request.url
.split('/')
.slice(1)
.join('/')}`,
headers: request.headers,
})

// When we connect, remove the authorization header (node-http-proxy uses this event to do it)
proxyReq.on('socket', function() {
proxyReq.removeHeader('authorization')
})

proxyReq.on('response', proxyRes => {
proxyRes.pipe(response)
})

proxyReq.on('error', error => {
t.error(error)
t.end()
})

proxyReq.end()
})

server
.listen(() => {
// Now that the server's started up, make a request to it with an authorization header.
const req = http.request(
{
hostname: 'localhost',
path: '/endpoint',
port: server.address().port,
method: 'GET',
headers: {
authorization: 'blah',
},
},
res => {
// If we get a request, all good :)
t.equal(200, res.statusCode)
serviceScope.done()
server.close(t.end)
}
)

req.on('error', error => {
t.error(error)
t.end()
})

req.end()
})
.on('error', error => {
t.error(error)
t.end()
})
})

0 comments on commit 50e6a4b

Please sign in to comment.