ijpiantanida / talkback

A simple HTTP proxy that records and playbacks requests
MIT License
283 stars 41 forks source link

Allow Req redirect follow instead of manual #77

Closed scmx closed 1 year ago

scmx commented 1 year ago

The default for a request has been to pass in redirect manual to fetch when doing real requests. This is sometimes problematic for an API that responds with a redirect to another url with the actual content that a tape should contain instead. With this change the redirect property can be changed to redirect follow (which is the normal default for fetch).

Usage:

const request = { url, method, body, headers, redirect: 'follow' }
requestHandler.handle(request)
ijpiantanida commented 1 year ago

Following redirects would hide the original intent of the host, which is not something talkback should be doing. Your app would otherwise behave differently when running against talkback vs the real server.

A better alternative is to rewrite the response redirect to point to talkback instead of the original server. This way the whole flow gets recorded as talkback tapes and used in future runs. Talkback cold provide an option to do this automatically, since it seems like a common case, but for now this can be done through the tapeDecorator option

function tapeDecorator(tape: Tape, context: MatchingContext) {
      let location = tape.res!.headers["location"]
      if (location && location[0]) {
        location = location[0]
        tape.res!.headers["location"] = [location.replace(proxiedHost, talkbackHost)]
      }

      return tape
}

Let me know if this doesn't solve your issue. Thanks!