node-replay icon indicating copy to clipboard operation
node-replay copied to clipboard

Building tools with Replay

Open jugglinmike opened this issue 10 years ago • 3 comments

I'm making a simple HTTP proxy for use in Selenium UI tests. Replay is great because it provides an automated way to cache the responses from an external service. Ideally, though, I would not have to globally stub out Node's "http" module to do this--I would like to programmatically feed requests to Replay. This would make it easier for others to consume the tool (the "http" module will continue to function as normal in their environment).

@assaf: would you have any interest in extending the API to support this? I think it would involve:

  1. Exposing the Replay constructor and documenting the path to its module
  2. Defining a method to handle requests, possibly Replay#request(requestObject).
  3. Re-using Replay#request from the src/replay/index.coffee file

#3 is mostly an optimization to limit code duplication. It's based on my limited understanding of the project internals, so it might be wrong. #1 is technically simple, but it requires publishing a previously-private API, so I would understand any reluctance to do so.

jugglinmike avatar Jan 09 '15 17:01 jugglinmike

Good idea. Keep in mind you'll need a different require if you don't want it to replace HTTP.request

assaf avatar Jan 11 '15 02:01 assaf

@assaf The way I'm thinking of implementing it, that precaution wouldn't be necessary. Loading the module at the top level would function exactly the same as it does today:

var replay = require('replay');

replay.ignore('github.com');
// etc...

...but users could reach into the replay module to get a reference to the constructor without side effects:

var Replay = require('replay/replay');
var replay = new Replay();
var http = require('http');
var proxy;

replay.ignore('github.com');

// (HTTP proxy setup logic omitted)

proxy.on('request', function(req, res) {
  replay.request(req, res);
});

// This is unaffected:
http.get('http://www.github.com', function(res) {
  console.log('Got response: ' + res.statusCode);
})

Does that sound right to you?

jugglinmike avatar Jan 11 '15 20:01 jugglinmike

:+1:

assaf avatar Jan 12 '15 01:01 assaf