Skip to content

Commit

Permalink
fix: avoid false negatives due to fixture conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 15, 2018
1 parent 5405660 commit ab9e637
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/additions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = fixtureAdditions

const parseUrl = require('url').parse

const mapValuesDeep = require('./map-values-deep')

function fixtureAdditions (state, {id, fixture}) {
Expand All @@ -13,6 +15,15 @@ function fixtureAdditions (state, {id, fixture}) {
})

fixture.headers['content-length'] = String(calculateBodyLength(fixture.response))

// We make the fixtures id part of the hostname to avoid conflicts with other
// mocks loaded with nock as they are global for the same process. Looking up
// fixtures by unique hostname is more efficient, too.
const {protocol, hostname, port} = parseUrl(fixture.scope)
const newHostame = `fixtures-${id}.${hostname}`
fixture.scope = `${protocol}//${newHostame}:${port}`
fixture.reqheaders.host = newHostame

return fixture
}

Expand Down
7 changes: 6 additions & 1 deletion lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ const validateRequest = require('./request-validation-middleware')

function proxy (state, {target}) {
const middleware = express.Router()
const hostname = urlParse(target).hostname
const {protocol, hostname} = urlParse(target)

middleware.use(`/${hostname}/:fixturesId`, validateRequest.bind(null, state), httpProxyMiddleware({
target: target,
router (req) {
const fixturesId = req.path.split('/')[2]
const target = `${protocol}//fixtures-${fixturesId}.${hostname}`
return target
},
changeOrigin: true,
logLevel: state.logLevel,
pathRewrite: {
Expand Down

0 comments on commit ab9e637

Please sign in to comment.