Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(overrider): unmocked HTTPS request with partial interceptor match #1664

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

const debug = require('debug')('nock.request_overrider')
const { EventEmitter } = require('events')
const { IncomingMessage, ClientRequest } = require('http')
const {
IncomingMessage,
ClientRequest,
request: originalHttpRequest,
} = require('http')
const { request: originalHttpsRequest } = require('https')
const _ = require('lodash')
const propagate = require('propagate')
const timers = require('timers')
Expand Down Expand Up @@ -253,7 +258,11 @@ function RequestOverrider(req, options, interceptors, remove) {
)

if (allowUnmocked && req instanceof ClientRequest) {
const newReq = new ClientRequest(options)
const newReq =
options.proto === 'https'
? originalHttpsRequest(options)
: originalHttpRequest(options)

propagate(newReq, req)
// We send the raw buffer as we received it, not as we interpreted it.
newReq.end(requestBodyBuffer)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_allow_unmocked_https.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,38 @@ test('allow unmocked option works with https', t => {
.end()
})
})

test('allow unmocked option works with https for a partial match', t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a link here to the issue?

// The `allowUnmocked` option is processed in two places. Once in the intercept when there
// are no interceptors that come close to matching the request. And again in the overrider when
// there are interceptors that partially match, eg just path, but don't completely match.
// This explicitly tests the later case in the overrider by making an HTTPS request for a path
// that has an interceptor but fails to match the query constraint.

function middleware(request, response) {
response.writeHead(201)
response.write('foo')
response.end()
}

ssl.startServer(middleware, function(error, server) {
t.error(error)

const { port } = server.address()
const origin = `https://localhost:${port}`

nock(origin, { allowUnmocked: true })
.get('/foo')
.query({ foo: 'bar' })
.reply(418)

// no query so wont match the interceptor
got(`${origin}/foo`, { rejectUnauthorized: false }).then(
({ body, statusCode }) => {
t.is(statusCode, 201)
t.is(body, 'foo')
server.close(t.end)
}
)
})
})