Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid redundantly calling em-http-request middleware
This appears to fix #899. em-http-request is such a ball of spaghetti that I have my doubts, but the tests are green. The patch is based on the observation that the em-http-request call sequence goes: 1. `HttpConnection#activate_connection` 2. `HttpClient#connection_completed` 3. `HttpClient#send_request` The `#connection_completed` method is normally responsible for calling the request middleware before passing the adjusted headers/body to `#send_request` (cf. https://github.com/igrigorik/em-http-request/blob/34919d760b940dfd27159bbebce5c4dac9845eb6/lib/em-http/client.rb#L53-L62). But webmock needs the request signature all the way back in `#activate_connection`, which we want to compute using the middleware-adjusted headers/body. So instead, we let `#activate_connection` handle building the request signature, whose result will be memoized. Then when we monkey patch `#connection_completed` to look up the headers/body from that signature without recomputing the values. This required some tweaking to `#build_request_signature`, where it was actually failing to follow the same algorithm as em-http-request. Basically it just means calling em-http-request's internal `#build_request` method + making sure not to clobber the `headers` local variable (which was probably not intentional in the first place).
- Loading branch information