Skip to content

Commit

Permalink
bug(overrider): unmocked HTTPS request with partial interceptor match (
Browse files Browse the repository at this point in the history
…#1664)

Fixes: #1422

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 fixes and 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.
  • Loading branch information
mastermatt committed Aug 14, 2019
1 parent 11dba99 commit 870584b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/request_overrider.js
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
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 => {
// 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)
}
)
})
})

0 comments on commit 870584b

Please sign in to comment.