question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nock.recorder don't log redirection (302) with axios

See original GitHub issue

What is the expected behavior? nock seems to record 302 redirections ( I’ve seen an unit test with super agent, working )

What is the actual behavior? nock doesn’t record the redirection when called by axios (only the first call recorded)

Possible solution

How to reproduce the issue Here is a test I wrote in test_recorder.js :

const axios = require('axios')

test('test 302 on axios', t => {
  const exampleText = '<html><body>example</body></html>'

  const server = http.createServer((request, response) => {
    switch (require('url').parse(request.url).pathname) {
      case '/':
        response.writeHead(302, { Location: '/abc' })
        break
      case '/abc':
        response.write(exampleText)
        break
    }
    response.end()
  })
  t.once('end', () => server.close())

  nock.restore()
  nock.recorder.clear()
  t.equal(nock.recorder.play().length, 0)

  nock.recorder.rec({
    dont_print: true,
    output_objects: true,
  })

  server.listen(async () => {
    let resp, resp2

    try {
      resp = await axios.get(`http://localhost:${server.address().port}`)
    } catch (e) {
      t.ok(false)
    }

    t.ok(resp)
    t.ok(resp.headers)
    t.strictEqual(resp.data, exampleText)

    nock.restore()
    const recorded = nock.recorder.play()
    nock.recorder.clear()
    nock.activate()

    t.equal(recorded.length, 2)
    const nocks = nock.define(recorded)

    try {
      resp2 = axios.get(
        `http://localhost:${server.address().port}`,
      )

      t.strictEqual(resp2.text, exampleText)
      nocks.forEach(nock => nock.done())

      t.end()
    } catch (e) {
      t.ok(false)
    }
  })
})

Does the bug have a test case? not for the moment

Versions tested with last npm, last béta, and from github

Software Version(s)
Node V11.10.0

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
simlucommented, Dec 1, 2021

We just ran into this as well (moving from the deprecated request library to axios). So would very much appreciate if this old ticket could be picked up again! Cheers

1reaction
mastermattcommented, Jun 28, 2020

I didn’t dig into this, but I was curios if this was still an issue. I can confirm on Node 14.2, Nock 13.0, and Axios 0.19.2, this is still not working 😢

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
nock.recorder don't log redirection (302) with axios.
Read more >
Nock Reply With 302 And 404 Throws An Httperror - ADocLib
Javascript queries related to axios.interceptors.request example.axios ... issue on Mar 18 2019.nock.recorder don't log redirection 302 with axios #1479.
Read more >
Axios is not passing cookies upon 302 redirect - Stack Overflow
I'm trying to implement an admin request submission class in node.js. There are two API calls, one is Login and the second is...
Read more >
nock - npm
Global no match event. Nock Back. Setup. Options. Usage. Options. Example. Modes. Common issues. Axios; Memory issues with Jest.
Read more >
node-fetch Alternatives - Node.js HTTP | LibHunt
10.0 9.7 L3 node-fetch VS axios ... 8.7 8.4 L4 node-fetch VS Nock ... :dash: Simple yet powerful file-based mock server with recording...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found