-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: a caching HTTP proxy for integration tests #1364
Conversation
Oh, I just realized that my proxy is not expiring old cache entries. I'll add that functionality next week. |
29a9550
to
6d8a2af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! I did a first round of review and it LGTM. I will have another look again and try it out too.
I was looking into this module. My understanding is that node-http-proxy does not provide a generic HTTP proxy capable of proxying requests to any backend. Instead, it works as a reverse-proxy for a single backend host ( Nice find! I did not come across this module in my search. AFAICT, they work as a regular proxy and the API is flexible enough to support caching: http://anyproxy.io/en/#beforesendrequest
BUT: The reason why I am reluctant to use anyproxy is the unnecessary complexity and dependency size. My current implementation is about 200 lines of production code with 6 dependencies (~1.9MiB). It should be easy for any developer to understand how the cache works and troubleshoot possible issues. Compare that with anyproxy, which has 29 dependencies (10.3MiB), over 2800 lines of code and a bunch of features like browser-based GUI that we don't really need. I am also not particularly happy about their decision to use generator functions for flow control. While generator functions do have advantages over promise-based async functions, I think that ship has sailed long time ago and now it's better to use standard async/await. Thoughts? |
@bajtos Fair enough. Two suggestions:
|
Done. I have also renamed
I already have a bold disclaimer in README, I added it to package.json |
c366d23
to
9ae77dd
Compare
Implement an HTTP proxy with aggressive cache persisted on file system. This proxy is intended for integration tests to reduce the time needed to access real backends.
9ae77dd
to
9488dfc
Compare
After spending several days searching for viable options allowing us to write an integration test against a slow REST service (see #1347), I figured out it will be faster to implement our own caching proxy.
Welcome
@loopback/http-caching-proxy
.This proxy makes it easy to record snap-hots of HTTP(S) communication and use this snapshot when running the tests. What's even more important, these snapshots are automatically updated whenever the backing service changes the API.
Why a caching proxy
Testing applications connecting to backend REST/SOAP services can be difficult:
The backend service may be slow, apply rate limiting, etc. Integration tests
become too slow in such case, which makes test-first development impractical.
This can be addressed by setting up a snapshot-based mock server or using
a caching HTTP client, but both of these solutions come with severe
disadvantages:
When using a snapshot-based mock server, we must ensure that snapshots
are up-to-date with the actual backend implementation.
Caching at HTTP-client side requires non-trivial changes of the application
code. We would have to update all our HTTP-based connectors to support client-side caching.
A filesystem-backed caching HTTP proxy offers a neat solution that combines
caching and snapshots:
is stored as a snapshot.
request will fetch the real response from the backend.
Basic use (as described in README)
Import the module at the top of your test file.
Create a proxy instance during test-suite setup (typically in Mocha's
before
hook):In your tests, configure the client library to use the caching proxy. Below is an example configuration for request:
Finally, stop the proxy when the test suite is done (typically in Mocha's
after
hook):Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated