22
33
44Templating
5- ----------
5+ ==========
66
77Hoverfly can build responses dynamically through templating. This is particularly useful when combined with loose matching, as it allows a single
88matcher to represent an unlimited combination of responses.
99
1010
1111Enabling Templating
12- ~~~~~~~~~~~~~~~~~~~
12+ -------------------
1313
1414By default templating is disabled. In order to enable it, set the flag to true in the response of a simulation.
1515
1616
1717Available Data
18- ~~~~~~~~~~~~~~
18+ --------------
1919
2020Currently, the following data is available through templating:
2121
@@ -34,39 +34,97 @@ Currently, the following data is available through templating:
3434+------------------------------+----------------------------------------------+----------------------------------------------+--------+
3535
3636Helper Methods
37- ~~~~~~~~~~~~~~
37+ --------------
3838
3939Additional data can come from helper methods. Current we only have some for the current data, but this list is likely to expand:
4040
41- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
42- | Description | Example | Result |
43- +=========================================================+===========================================================+=========================================+
44- | The current date, formatted in iso8601 | {{ iso8601DateTime }} | 2006-01-02T15:04:05Z07:00 |
45- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
46- | The current date, formatted in iso8601, with days added | {{ iso8601DateTimePlusDays Request.QueryParam.plusDays }} | 2006-02-02T15:04:05Z07:00 |
47- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
48- | A random string | {{ randomString }} | hGfclKjnmwcCds |
49- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
50- | A random string with a specified length | {{ randomStringLength 2 }} | KC |
51- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
52- | A random boolean | {{ randomBoolean }} | true |
53- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
54- | A random integer | {{ randomInteger }} | 42 |
55- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
56- | A random integer within a range | {{ randomIntegerRange 1 10 }} | 7 |
57- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
58- | A random float | {{ randomFloat }} | 42 |
59- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
60- | A random float within a range | {{ randomFloatRange 1.0 10.0 }} | 7.4563213423 |
61- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
62- | A random email address | {{ randomEmail }} | LoriStewart@Photolist.com |
63- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
64- | A random IPv4 address | {{ randomIPv4 }} | 224.36.27.8 |
65- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
66- | A random IPv6 address | {{ randomIPv6 }} | 41d7:daa0:6e97:6fce:411e:681:f86f:e557 |
67- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
68- | A random UUID | {{ randomUuid }} | 7b791f3d-d7f4-4635-8ea1-99568d821562 |
69- +---------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
41+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
42+ | Description | Example | Result |
43+ +=====================================================================+===========================================================+=========================================+
44+ | The current date, formatted in iso8601 | {{ iso8601DateTime }} | 2006-01-02T15:04:05Z07:00 |
45+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
46+ | The current date, formatted in iso8601, with days added | {{ iso8601DateTimePlusDays Request.QueryParam.plusDays }} | 2006-02-02T15:04:05Z07:00 |
47+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
48+ | The current date, in the format specified | {{ curentDateTime "2006-Jan-02" }} | 2018-Jul-05 |
49+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
50+ | The current date, in the format specified, with duration added | {{ curentDateTimeAdd "1d" "2006-Jan-02" }} | 2018-Jul-06 |
51+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
52+ | The current date, in the format specified, with duration subtracted | {{ curentDateTimeSubtract "1d" "2006-Jan-02" }} | 2018-Jul-04 |
53+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
54+ | A random string | {{ randomString }} | hGfclKjnmwcCds |
55+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
56+ | A random string with a specified length | {{ randomStringLength 2 }} | KC |
57+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
58+ | A random boolean | {{ randomBoolean }} | true |
59+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
60+ | A random integer | {{ randomInteger }} | 42 |
61+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
62+ | A random integer within a range | {{ randomIntegerRange 1 10 }} | 7 |
63+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
64+ | A random float | {{ randomFloat }} | 42 |
65+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
66+ | A random float within a range | {{ randomFloatRange 1.0 10.0 }} | 7.4563213423 |
67+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
68+ | A random email address | {{ randomEmail }} | LoriStewart@Photolist.com |
69+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
70+ | A random IPv4 address | {{ randomIPv4 }} | 224.36.27.8 |
71+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
72+ | A random IPv6 address | {{ randomIPv6 }} | 41d7:daa0:6e97:6fce:411e:681:f86f:e557 |
73+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
74+ | A random UUID | {{ randomUuid }} | 7b791f3d-d7f4-4635-8ea1-99568d821562 |
75+ +---------------------------------------------------------------------+-----------------------------------------------------------+-----------------------------------------+
76+
77+ Durations
78+ ~~~~~~~~~
79+ When using template helper methods such as ``currentDateTimeAdd `` and ``currentDateTimeSubtract ``, durations must be formatted following the following syntax for durations.
80+
81+ +-----------+-------------+
82+ | Shorthand | Type |
83+ +===========+=============+
84+ | ns | Nanosecond |
85+ +-----------+-------------+
86+ | us/µs | Microsecond |
87+ +-----------+-------------+
88+ | ms | Millisecond |
89+ +-----------+-------------+
90+ | s | Second |
91+ +-----------+-------------+
92+ | m | Minute |
93+ +-----------+-------------+
94+ | h | Hour |
95+ +-----------+-------------+
96+ | d | Day |
97+ +-----------+-------------+
98+ | y | Year |
99+ +-----------+-------------+
100+
101+ Example Durations
102+ ~~~~~~~~~~~~~~~~~
103+
104+ +-----------+-------------------+
105+ | 5m | 5 minutes |
106+ +-----------+-------------------+
107+ | 1h30m | 1 hour 5 minutes |
108+ +-----------+-------------------+
109+ | 1y10d | Millisecond |
110+ +-----------+-------------------+
111+
112+ Date time formats
113+ ~~~~~~~~~~~~~~~~~
114+ When using template helper methods such as ``currentDateTime ``, ``currentDateTimeAdd `` and ``currentDateTimeSubtract ``, date time formats must follow
115+ the Golang syntax. More can be found out here https://golang.org/pkg/time/#Parse
116+
117+ Example date time formats
118+ ~~~~~~~~~~~~~~~~~
119+
120+ +-------------------------------+
121+ | 2006-01-02T15:04:05Z07:00 |
122+ +-------------------------------+
123+ | Mon, 02 Jan 2006 15:04:05 MST |
124+ +-------------------------------+
125+ | Jan _2 15:04:05 |
126+ +-------------------------------+
127+
70128
71129Conditional Templating, Looping and More
72130~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments