Change mock response based on request header?

410 views
Skip to first unread message

Rich Frost

unread,
Jul 18, 2017, 11:14:06 AM7/18/17
to wiremock-user
Hi,

In our UI application we want to test the edge cases of our APIs when they return a 500/404 error etc. On the assumption we ran WireMock in record mode and stored valid 200 responses; in playback mode can we make a request to WireMock asking it to return us a 500 error rather than the stored response?

Thanks,
Rich

Tom Akehurst

unread,
Jul 18, 2017, 11:19:16 AM7/18/17
to wiremock-user
This is pretty easily done by editing one of your recorded mappings. Just add the following to the mapping JSOM:
  1. A priority value higher than 5 (a lower number - 1 is highest, see http://wiremock.org/docs/stubbing/#stub-priority)
  2. A request header as detailed here: http://wiremock.org/docs/request-matching/#matching-other-attributes
  3. Whatever status code and body content you'd like in the response.

Rich Frost

unread,
Jul 18, 2017, 11:59:34 AM7/18/17
to wiremock-user
OK interesting. Would this work in a generic way so that whenever we re-run the recordings it does not overwrite our changes?

Ideally it would be nice to extend the core server response to handle an extra header attribute and change the response depending on this irrespective of the recorded mappings.

Tom Akehurst

unread,
Jul 18, 2017, 12:41:39 PM7/18/17
to wiremock-user
Re-recording won't wipe out the mapping you've created, so no problem there. The fact that it has a higher priority means it'll get matched before any other mappings, regardless of when you recorded them (since recorded mappings default to the middle priority of 5).

There's a new record feature in progress as we speak, which allows you to write your own transformation which gets applied to recorded mappings after they're captured. Maybe that will help?

Rich Frost

unread,
Jul 19, 2017, 4:19:30 AM7/19/17
to wiremock-user
Good to know, thanks for the info :)
Reply all
Reply to author
Forward
0 new messages