concept proxyalway in category microservice

This is an excerpt from Manning's book Testing Microservices with Mountebank.
In proxyOnce mode, mountebank captures only the first response (54). If your test cases relied on the volatility of inventory over time, you’d need a proxy that would let you capture a richer data set to replay. The proxyAlways mode ensures that all requests reach the downstream service, allowing you to capture multiple responses for a single request type (figure 5.7).
Listing 5.8. Creating a proxyAlways proxy response
{ "proxy": { "to": "http://localhost:8080", "mode": "proxyAlways", #1 "predicateGenerators": [{ "matches": { "path": true } }] } }The key difference in the mechanics between proxyOnce and proxyAlways, as shown in figures 5.5 and 5.7, is that proxyOnce generates new stubs before the stub containing the proxy response, whereas proxyAlways generates stubs after the proxy stub. Both approaches rely on mountebank’s first-match policy when matching a request to a stub. In the proxyOnce case, a subsequent request matching the generated predicates is guaranteed to match before the proxy stub, and in the proxyAlways case, the proxy stub is guaranteed to match before the generated stubs.
But proxyAlways does more than create new stubs. It first looks to see if a stub whose predicates already exists match the generated predicates, and, if so, it appends the saved response to that stub. This behavior allows multiple responses to be saved for the same request. You can see this by calling the imposter in listing 5.8 a few times and querying its state (by sending a GET request to http://localhost:2525/imposters/3000, assuming it was started on port 3000). To save space and make the salient bits stand out, I’ve omitted the full response inside each generated is response in the following listing.
Listing 5.9. The imposter state after a few calls to a proxyAlways response
{ "stubs": [ { "responses": [{ "proxy": { "to": "http://localhost:8080", "mode": "proxyAlways", #1 "predicateGenerators": [{ "matches": { "path": true } }] } }] }, { "predicates": [{ "deepEquals": { "path": "/inventory/2599b7f4" } #2 }], "responses": [ { "is": { "body": "54" } }, #3 { "is": { "body": "21" } }, #3 { "is": { "body": "0" } } #3 ] }, { "predicates": [{ "deepEquals": { "path": "/inventory/e1977c9e" } #4 }], "responses": [{ "is": { "body": "100" } #5 }] } ] }A proxyAlways proxy allows you to capture a full set of test data that is as rich as your downstream service (at least for the requests sent to it). Although this is great for supporting complex test cases, it comes with a significant problem. As you can see in listing 5.9, none of the saved responses will ever get called. With proxyOnce, you don’t need to worry about switching from recording to replaying; it happens automatically. Not so with proxyAlways, so it’s time to look at how you can tell mountebank to replay all the data it has captured.
The proxyAlways mode allows you to capture a full set of test data by capturing multiple responses for the same logical request. You have to explicitly switch from record mode to replay mode by using mb replay.