assaf / node-replay

When API testing slows you down: record and replay HTTP responses like a boss
http://documentup.com/assaf/node-replay
MIT License
522 stars 107 forks source link

Building tools with Replay #54

Open jugglinmike opened 9 years ago

jugglinmike commented 9 years ago

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.

assaf commented 9 years ago

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

jugglinmike commented 9 years ago

@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?

assaf commented 9 years ago

:+1: