BedrockStreaming / superagent-mock

superagent plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL
MIT License
170 stars 46 forks source link

Mocking an error and using retry() #93

Open techandsoftware opened 4 years ago

techandsoftware commented 4 years ago

I'm trying to do the following

  1. Get a fixture to return a server error (500)
  2. Use retry(3) on the superagent
  3. See that the fixture is called 3 times, and the get request ultimately fails.

I've written a small test script, from what it looks like the failure happens immediately after the exeption even though the retry callback is called 3 times afterwards.

Here's my script

var request = require('superagent');
var config = [{
  pattern: 'http://localhost/500/devices',
  fixtures: (match, params) => {
    let err = new Error(500);
    err.status = 500;
    throw err;
  },
  get: (match, data) => {
      return data;
  }
}];

var superagentMock = require('superagent-mock')(request, config);

request
  .get('http://localhost/500/devices')
  .retry(3, retryCallback)
  .then(res => {
    console.log('completed');
    console.log(res);
  })
  .catch(err => {
    console.log('failed');
    console.log(err);
  });

function retryCallback(err, res) {
  console.debug('__retrying');
  return true;
}

Output is failed retrying retrying __retrying

Is there an error with how superagent-mock interacts with retry or am I approaching this the wrong way?

fdubost commented 4 years ago

Hi, did you test to throw the error in the get method instead in the fixtures?

techandsoftware commented 4 years ago

Yes, the result is the same.

fdubost commented 4 years ago

I think the problem is with superagent-mock that don't handle superagent's retry method. We don't use the retry feature of superagent, but feel free to open a PR to fix this bug 😉

techandsoftware commented 4 years ago

I'll try and work it out :)