Response Templating - String Helpers
WireMock Cloud provides a set of string manipulation helpers.
Regular expression extract
The regexExtract
helper supports extraction of values matching a regular expresson from a string.
A single value can be extracted like this:
Regex groups can be used to extract multiple parts into an object for later use (the last parameter is a variable name to which the object will be assigned):
String transformation helpers
Trim
Use the trim
helper to remove whitespace from the start and end of the input:
Abbreviate
abbreviate
truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence (“…”).
For instance the following template:
Capitalisation
capitalize
will make the first letter of each word in the passed string a capital e.g.
capitalizeFirst
will capitalise the first character of the value passed e.g.
Center
center
centers the value in a field of a given width e.g.
will output:
hello
You can also specify the padding character e.g.
will output:
########hello########
Cut
cut
removes all instances of the parameter from the given string.
Default if empty
defaultIfEmpty
outputs the passed value if it is not empty, or the default otherwise e.g.
Join
join
takes a set of parameters or a collection and builds a single string, with each item separated by the specified parameter.
You can optionally specify a prefix and suffix:
Justify left and right
ljust
left-aligns the value in a field of a given width, optionally taking a padding character.
rjust
right-aligns the value in the same manner
Lower and upper case
lower
and upper
convert the value to all lowercase and all uppercase:
Replace
replace
replaces all occurrences of the specified substring with the replacement value.
Slugify
slugify
converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace.
Strip tags
stripTags
strips all [X]HTML tags.
Substring
substring
outputs the portion of a string value between two indices. If only one index is specified the substring between this point and the end will be returned.
Word wrap
wordWrap
wraps words at specified line length.
will output:
one
two
three
Yes/no
yesno
maps values for true, false and optionally null, to the strings “yes”, “no”, “maybe”.
You can also specify different strings to represent each state: