faux-jax icon indicating copy to clipboard operation
faux-jax copied to clipboard

Feature Request - request.continue()

Open kumavis opened this issue 9 years ago • 7 comments

I only want to intercept requests that match a certain pattern. If the requests don't match the pattern it would be nice to have an easy way to get the request to continue normally.

kumavis avatar Feb 14 '16 20:02 kumavis

this is what i ended up doing to recreate the request

function continueRequestNormally(req){
  var xhr = new XHR()
  // set target url and method
  xhr.open(req.requestMethod, req.requestURL, req.async)
  // set headers
  Object.keys(req.requestHeaders || {}).forEach(function(headerKey){
    xhr.setRequestHeader(headerKey, req.requestHeaders[headerKey])
  })
  // send and call completion handler
  if (req.async) {
    xhr.onload = copyResult
    xhr.send(req.requestBody)
  } else {
    xhr.send(req.requestBody)
    copyResult()
  }

  function copyResult() {
    var headers = extractResponseHeaders(xhr.getAllResponseHeaders())
    req.respond(xhr.status, headers, xhr.response)
  }
}

function extractResponseHeaders(rawHeaders){
  var headers = {}
  var headerKeyValues = rawHeaders.split('\r\n').filter(Boolean)
  headerKeyValues.forEach(function(keyValue){
    var data = keyValue.split(': ')
    headers[data[0]] = data[1]
  })
  return headers
}

kumavis avatar Feb 15 '16 05:02 kumavis

I would be happy to take a PR that adds a request.continue(), seems legit

vvo avatar Feb 15 '16 08:02 vvo

Ultimately I guess we should avoid adding any non-standard methods to XMLHttpRequest, we'd rather add the methods to for example fauxJax.respond(req, ..) but that's maybe extra work. And would require refactoring also/documentation.

Tell me what you think, I would be happy to have you as a new contributor :)

vvo avatar Feb 15 '16 08:02 vvo

yeah i think something like fauxJax.performRequest(req) makes the most sense. but yeah tests and doc and stuff...

kumavis avatar Feb 15 '16 18:02 kumavis

So yes would take a PR that adds request.continue, for now

vvo avatar Feb 16 '16 15:02 vvo

do you have some utility already that does extractResponseHeaders to spec? maybe you only move in the other direction. I just threw this together without tests...

kumavis avatar Feb 16 '16 18:02 kumavis

Huge :+1:

Although, this might be more challenging for a universal ™ solution.

I tried to add a bypass method to https://github.com/moll/node-mitm , but got stuck after I couldn't access some of the requests details. mitm has it only in the socket layer.... I need to buff up on some more of node's http(s) libs to get a better grasp.

ponelat avatar Feb 29 '16 12:02 ponelat