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.

Impossible to mock redirects?

See original GitHub issue

I’m trying to mock a redirect structure as follows:

    nock('http://www.example.com')
      .get('/some-link-1')
      .reply(301, undefined, {
        Location: 'http://www.example.com/some-link-2'
      })
      .get('/some-link-2')
      .reply(302, undefined, {
        Location: 'http://www.example.com/some-link-3'
      })
      .get('/some-link-3')
      .reply(200, 'link');

    require('request')({
      uri: 'http://www.example.com/some-link-1',
      followRedirect : false
    }, function (err, res, body) {
      console.log(res.statusCode, body);
    });

This prints a 200 'link'. If I try to do this on an unmocked url that redirects, I get a proper 3xx code. Is there a way to do this with nock?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pgtecommented, Jan 26, 2015

@gajus this is the wrong syntax for specifying reply headers. Please see the docs for the proper syntax.

This test works for me:

'use strict';

var assert = require('assert');
var test    = require('tap').test;
var mikealRequest = require('request');
var nock    = require('../');

test("follows refirects", function(t) {
  nock('http://redirecter.com')
    .get('/YourAccount')
    .reply(302, undefined, {
        'Location': 'http://redirecter.com/Login'
    })
    .get('/Login')
    .reply(200, 'Here is the login page');

  mikealRequest('http://redirecter.com/YourAccount', function(err, res, body) {
    if (err) {
      throw err;
    }

    assert.equal(res.statusCode, 200);

    assert.equal(body, 'Here is the login page');
    t.end();
  });

});
0reactions
lock[bot]commented, Sep 14, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest: Trying to mock nested functions and redirects in jest, but ...
In the test file, I am trying to mock logout function. However it is executing someObj.lock function. someObj is not availabe to tests/main.js ......
Read more >
Redirecting Mock service's operation - SmartBear Community
Hello. I have a question: is it possible to create a mock service, based on local WSDL, with some operations implemented as a...
Read more >
Mocking and testing for RedirectToAction - MSDN - Microsoft
When I do that, the constraint always fails. Rhino Mocks says that null is indeed passed to the Response.Redirect method. There's is little...
Read more >
mock trial redirect examination
I've been coaching mock trial for about 12 years now, and every single year, I find that redirect examinations are the last thing...
Read more >
Redirect experiments: Test two URLs in Optimizely
Redirect experiments, or split tests, allow you to compare two ... as little delay as possible before a visitor can reach and load...
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