xolvio / meteor-http-interceptor

Intercepts HTTP calls and allows fake implementations to take over entire domains. Used for testing.
13 stars 5 forks source link

Question: Intercepting Client Side Calls #6

Open workflow opened 8 years ago

workflow commented 8 years ago

What is a good general way to stub client side external API calls?

Example:

Client side image upload to a service like Cloudinary or Filepicker.io Using jquery on the client to POST to an external API like https://api.cloudinary.com/v1_1/FakeKeyToPreventConsoleWarning/auto/upload

How would you stub that? Try to intercept the call (like http-interceptor does on the server side) by overloading jQuery.ajax and other potential sources of external api calls? Or prevent the call being made in the first place via some logic in the main app code (problem: cluttering). Or maybe something entirely more ingenious?

Thx in advance, would love to have your thoughts on this.

samhatoum commented 8 years ago

The way I intended to do was to create a browser proxy, using node-http-proxy and then set the Webdriver remote capabilities to use the proxy.

With access to both the request/response on the server, you can chose to either let the request go through, or you can intercept it and redirect it to to a stub of your choice.

Neat eh?

I would love a PR that implements this ;)

The other alternative is to use the host file on your OS and take over the domain you want to test. It's a bit more clunky but it's a useful technique if you have control over a dedicated e2e test environment

samhatoum commented 8 years ago

This is how you can set the proxy object see here and here

workflow commented 8 years ago

Neat! I set up a proxy and it seems to be running fine with a couple of limitations (lurking below).

Questions

  1. Telling Selenium/Webdriver to use the proxy should probably happen in chimp, right? How do we best wire the two together?
  2. Proxy supports HTTP only so far. Writing an actual HTTPS proxy with certificates and cross-browser settings seems a bit daunting for the task. I thought of just replacing any references to (intercepted) https:// calls directly in the DOM. Have a better idea or more experience setting up a proxy? :)
  3. Should we spawn the proxy process with sanjo:long-running-child-process? It seems to work fine without, but maybe I'm missing some edge cases or other benefits.
  4. Should we have the proxy run on the velocity mirror only?

TODO