Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nock/nock
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v13.0.6
Choose a base ref
...
head repository: nock/nock
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v13.0.7
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Jan 30, 2021

  1. fix(recorder): escape single quotes in path of default output (#2137)

    Because the default output of the recorder uses single quotes, any such
    quotes in the path would generate invalid Javascript.
    mastermatt authored Jan 30, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    0f7b52e View commit details
Showing with 26 additions and 0 deletions.
  1. +4 −0 lib/recorder.js
  2. +22 −0 tests/test_recorder.js
4 changes: 4 additions & 0 deletions lib/recorder.js
Original file line number Diff line number Diff line change
@@ -110,6 +110,10 @@ function generateRequestAndResponse({
const queryStr = req.path.slice(queryIndex + 1)
queryObj = querystring.parse(queryStr)
}

// Escape any single quotes in the path as the output uses them
path = path.replace(/'/g, `\\'`)

// Always encode the query parameters when recording.
const encodedQueryObj = {}
for (const key in queryObj) {
22 changes: 22 additions & 0 deletions tests/test_recorder.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ const servers = require('./servers')

require('./setup')

// TODO: the guts of this file should be wrapped in a `describe`.
// These before and afters run for every test in the repo under Mocha.
let globalCount
beforeEach(() => {
globalCount = Object.keys(global).length
@@ -1148,6 +1150,26 @@ it('removes query params from the path and puts them in query()', done => {
})
})

// https://github.com/nock/nock/issues/2136
it('escapes single quotes in the path', async () => {
const { origin } = await servers.startHttpServer((request, response) => {
response.writeHead(200)
response.end()
})

nock.restore()
nock.recorder.clear()
expect(nock.recorder.play()).to.be.empty()

nock.recorder.rec(true)

await got(`${origin}/foo'bar'baz`)

const recorded = nock.recorder.play()
expect(recorded).to.have.lengthOf(1)
expect(recorded[0]).to.be.a('string').and.include(`.get('/foo\\'bar\\'baz')`)
})

it('respects http.request() consumers', done => {
const requestListener = (req, res) => {
res.write('foo')