Search code examples
javahttp-cachingwiremock

Can WireMock play and record be used at the same time?


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.


Solution

  • 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.