6

I have an application that connect to an external third-party API, and of late the test instance of the API has not been particularly reliable. This stops development progress and turns our build pipelines to red, so I'd like to add an HTTP recording proxy to stop this happening.

I've had WireMock recommended, and having tried its record and play features, it does seem to be very good - it pretty much worked out of the box. We simply change the configured URL for the external service, and then record like this:

java -jar wiremock-standalone-2.3.1.jar \
    --port 8080 \
    --proxy-all="https://test-api.example.com/" \
    --record-mappings \
    --verbose

This creates cache folders in the current working dir, and then it can be switched to playback mode by killing the recorder and switching to playback:

java -jar wiremock-standalone-2.3.1.jar \
    --port 8080 \
    --verbose

However, to use this in practice I would need to set up an instance of my app running through a test regression pack to record lots of API usage, and to kick that off say one a day. I'd then need to stop the recorder and copy the cache files over to a playback process and then restart the playback process.

This probably would work, but it feels like a lot of moving parts, and ideally I would like to run play and record at the same time. This would allow the cache to be automatically refreshed if a new API call becomes necessary (due to natural project changes) but would playback by default where a match is found.

Is this possible? I am not a Java programmer, but suppose it might be available if one were to write a WireMock plugin. It would be great if this could be done at the console, but the phrasing of the manual indicates that play and record are thought of by the maintainers as separate things.

I did wonder whether I might switch to Mountebank, which looked like it might support this, however it turns out that play and record are separate modes here too. In any case, I like how easy WireMock has been to get started, so would like to stick with it if possible.

Community
  • 1
  • 1
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

7

WireMock won't quite do what you're asking at the moment, however:

1) You can use the --proxy-all parameter and not --record-mappings when playing back. This will cause any request unmatched by an existing stub mapping (recorded or otherwise) to be proxied through to the actual service.

2) A workaround that'd get you pretty close to what you're after would be to send a POST to /__admin/mappings/reset endpoint after you'd collected some new recorded mappings. This causes the filesystem to be scanned and all the mappings (re)loaded.

Tom
  • 3,471
  • 21
  • 14
  • 2
    Thanks Tom, that's very helpful. I'm just in an investigation phase at present, but I'll point colleagues to this for our implementation. I'm thinking of a Docker container that offers a recorder that refreshes an API cache (running once a day) and then a playback mode to cache the real thing. The restart tip is useful - I was thinking of using Supervisord here with an auto-restart, but your approach feels more graceful. – halfer Nov 23 '16 at 09:57
  • 2
    No worries. I'm thinking about totally rewriting the recorder soon so I'll bear this use case in mind. – Tom Nov 23 '16 at 18:03
  • 1
    Great, I would expect that could be generally useful. – halfer Nov 23 '16 at 18:06
  • 2
    Tom, if you have a moment, would you be willing to take a [look at this new question](https://stackoverflow.com/questions/41474410/can-wiremock-play-back-requests-from-multiple-domains)? I have been building a system that needs WM to play back from multiple sites, and I am lately fearing that it won't do that without substantial modification. Thanks! – halfer Jan 04 '17 at 22:39