oesmith / puffing-billy

A rewriting web proxy for testing interactions between your browser and external sites. Works with ruby + rspec.
MIT License
656 stars 170 forks source link

Before handle request method #273

Closed le0pard closed 4 years ago

le0pard commented 4 years ago

Hello.

I have feature proposal, which very valuable for my use cases. My proposal to add new config variable before_handle_request, which provide ability to define method to normalize, filter, cleanup request before pass it to handlers. Use cases:

Filter sensitive information before store to cache store

If you request from JS code contain some sensitive information, you can remove them or repalce to some dump info by this example

c.before_handle_request = proc { |method, url, headers, body|
  filtered_body = JSON.stringify(replace_secret_data(JSON.parse(body)))
  [method, url, headers, filtered_body]
}

Normalize POST body before store to cache and fetch from cache

Some libs, like braintree.js generate for each request UUID and add this info POST body. This doesn't allow to reuse the same stored cache, because cache key and comparison based on body content. This function allow to replace this dynamic info inside POST body (normalization):

c.before_handle_request = proc { |method, url, headers, body|
  [method, url, headers, normalize_body(body)]
}
le0pard commented 4 years ago

Looks like capybara-webkit cannot build on CI :(

ronwsmith commented 4 years ago

@le0pard Thanks for the contribution! If you are able to fix the CI issue, please feel free, or I will in a couple weeks when I have time.

le0pard commented 4 years ago

@ronwsmith hope this will increase speed to accept my PR :)

P.S. You can ask Travis CI support to migrate this repo from travis-ci.org to travis-ci.com - https://docs.travis-ci.com/user/migrate/open-source-repository-migration

ronwsmith commented 4 years ago

@le0pard Awesome, thanks! I'll try to get it in this weekend when I'm back home.

le0pard commented 4 years ago

@ronwsmith hello. How are you? You like disappear from github...

ronwsmith commented 4 years ago

I'm back @le0pard, thanks for your patience!

ronwsmith commented 4 years ago

Released in v2.2.0

le0pard commented 4 years ago

Big thanks @ronwsmith