superagent-mocker icon indicating copy to clipboard operation
superagent-mocker copied to clipboard

Slowing down the response

Open avanderhoorn opened this issue 10 years ago • 11 comments
trafficstars

I'm looking at how we can simulate as much as possible what our real/production environment in offline development scenarios. Inline with this, we are fully faking out the server to be used during client dev work.

With this in mind, it would be nice if the api facilitated some way in which once could control when superagents end callback gets called? In my case I would call it from within a setTimeout.

avanderhoorn avatar Jul 31 '15 00:07 avanderhoorn

Pretty nice idea! Give me some time to figure out the way to make it out.

A avatar Jul 31 '15 13:07 A

Great!

avanderhoorn avatar Jul 31 '15 19:07 avanderhoorn

Yay! @avanderhoorn, is it ok for you?

var mock = require('superagent-mocker');

// set just number
mock.timeout = 100;

// Or function to get random
mock.timeout = function () {
  return Math.random() * 1e4 |0;
}

https://github.com/rambler-digital-solutions/superagent-mocker#timeout

A avatar Aug 02 '15 21:08 A

Yep that is nice! I had originally thought about it being configurable on an endpoint by endpoint basis so endpoints that take longer than other, etc could be accounted for but I'll take what I can get and having it work globally does save having to manually setup each one. I also like being able to pass in a function instead of just a int, so nice touch there. :shipit:

avanderhoorn avatar Aug 03 '15 00:08 avanderhoorn

@avanderhoorn do you want make pull request? :D

A avatar Aug 03 '15 20:08 A

Ya, might be up for it, how would you want the API to look?

avanderhoorn avatar Aug 03 '15 20:08 avanderhoorn

Awesome! What do you think about chained API like superagent?

require('superagent-mocker')
  .get('/topics/:id', function(req) {
    return {
      id: req.params.id,
      content: 'Hello World!'
    };
  })
  .timeout(100)
  // or
  .timeout(function() { return Math.random() * 1e4 |0; })

A avatar Aug 03 '15 22:08 A

btw, I can release new version with mock.timeout, but if you want to add ability to set timeout to each endpoint, I will wait your PR before

A avatar Aug 03 '15 22:08 A

@avanderhoorn published with 0.2.0

A avatar Oct 06 '15 21:10 A

Fantastic! Which commit # was it?

avanderhoorn avatar Oct 07 '15 03:10 avanderhoorn

9db610c0a6825fb69aad2a39ba7d97f3ea024602 but still without ability to setup it for endpoint. You can make PR for it.

A avatar Oct 07 '15 13:10 A