Skip to content

Commit 9904927

Browse files
benjihtommysitu
authored andcommitted
Added currentDateTime, currentDateTimeAdd and currentDateTimeSubtract template functions
1 parent e52e795 commit 9904927

File tree

4 files changed

+226
-30
lines changed

4 files changed

+226
-30
lines changed

core/templating/template_helpers.go

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,93 @@ import (
1010
"github.com/icrowley/fake"
1111
)
1212

13-
func iso8601DateTime() string {
14-
return time.Now().UTC().Format("2006-01-02T15:04:05Z07:00")
13+
type templateHelpers struct {
14+
now func() time.Time
1515
}
1616

17-
func iso8601DateTimePlusDays(days string) string {
17+
func (t templateHelpers) iso8601DateTime() string {
18+
return t.now().UTC().Format("2006-01-02T15:04:05Z07:00")
19+
}
20+
21+
func (t templateHelpers) iso8601DateTimePlusDays(days string) string {
1822
atoi, _ := strconv.Atoi(days)
19-
return time.Now().AddDate(0, 0, atoi).UTC().Format("2006-01-02T15:04:05Z07:00")
23+
return t.now().AddDate(0, 0, atoi).UTC().Format("2006-01-02T15:04:05Z07:00")
24+
}
25+
26+
func (t templateHelpers) currentDateTime(format string) string {
27+
formatted := t.now().UTC().Format(format)
28+
if formatted == format {
29+
return t.now().UTC().Format("2006-01-02T15:04:05Z07:00")
30+
}
31+
return formatted
32+
}
33+
34+
func (t templateHelpers) currentDateTimeAdd(addTime string, format string) string {
35+
now := t.now()
36+
duration, err := ParseDuration(addTime)
37+
if err == nil {
38+
now = now.Add(duration)
39+
}
40+
formatted := now.UTC().Format(format)
41+
if formatted == format {
42+
return now.UTC().Format("2006-01-02T15:04:05Z07:00")
43+
}
44+
return formatted
45+
}
46+
47+
func (t templateHelpers) currentDateTimeSubtract(subtractTime string, format string) string {
48+
now := t.now()
49+
duration, err := ParseDuration(subtractTime)
50+
if err == nil {
51+
now = now.Add(-duration)
52+
}
53+
formatted := now.UTC().Format(format)
54+
if formatted == format {
55+
return now.UTC().Format("2006-01-02T15:04:05Z07:00")
56+
}
57+
return formatted
2058
}
2159

22-
func randomString() string {
60+
func (t templateHelpers) randomString() string {
2361
return util.RandomString()
2462
}
2563

26-
func randomStringLength(length int) string {
64+
func (t templateHelpers) randomStringLength(length int) string {
2765
return util.RandomStringWithLength(length)
2866
}
2967

30-
func randomBoolean() string {
68+
func (t templateHelpers) randomBoolean() string {
3169
return strconv.FormatBool(util.RandomBoolean())
3270
}
3371

34-
func randomInteger() string {
72+
func (t templateHelpers) randomInteger() string {
3573
return strconv.Itoa(util.RandomInteger())
3674
}
3775

38-
func randomIntegerRange(min, max int) string {
76+
func (t templateHelpers) randomIntegerRange(min, max int) string {
3977
return strconv.Itoa(util.RandomIntegerRange(min, max))
4078
}
4179

42-
func randomFloat() string {
80+
func (t templateHelpers) randomFloat() string {
4381
return strconv.FormatFloat(util.RandomFloat(), 'f', 6, 64)
4482
}
4583

46-
func randomFloatRange(min, max float64) string {
84+
func (t templateHelpers) randomFloatRange(min, max float64) string {
4785
return strconv.FormatFloat(util.RandomFloatRange(min, max), 'f', 6, 64)
4886
}
4987

50-
func randomEmail() string {
88+
func (t templateHelpers) randomEmail() string {
5189
return fake.EmailAddress()
5290
}
5391

54-
func randomIPv4() string {
92+
func (t templateHelpers) randomIPv4() string {
5593
return fake.IPv4()
5694
}
5795

58-
func randomIPv6() string {
96+
func (t templateHelpers) randomIPv6() string {
5997
return fake.IPv6()
6098
}
6199

62-
func randomUuid() string {
100+
func (t templateHelpers) randomUuid() string {
63101
return uuid.New()
64102
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package templating
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func testNow() time.Time {
11+
parsedTime, _ := time.Parse("2006-01-02T15:04:05Z", "2018-01-01T00:00:00Z")
12+
return parsedTime
13+
}
14+
15+
func Test_iso8601DateTime(t *testing.T) {
16+
RegisterTestingT(t)
17+
18+
unit := templateHelpers{
19+
now: testNow,
20+
}
21+
22+
Expect(unit.iso8601DateTime()).To(Equal("2018-01-01T00:00:00Z"))
23+
}
24+
25+
func Test_iso8601DateTimePlusDays(t *testing.T) {
26+
RegisterTestingT(t)
27+
28+
unit := templateHelpers{
29+
now: testNow,
30+
}
31+
32+
Expect(unit.iso8601DateTimePlusDays("14")).To(Equal("2018-01-15T00:00:00Z"))
33+
}
34+
35+
func Test_iso8601DateTimePlusDays_failure(t *testing.T) {
36+
RegisterTestingT(t)
37+
38+
unit := templateHelpers{
39+
now: testNow,
40+
}
41+
42+
Expect(unit.iso8601DateTimePlusDays("cat")).To(Equal("2018-01-01T00:00:00Z"))
43+
}
44+
45+
func Test_currentDateTime(t *testing.T) {
46+
RegisterTestingT(t)
47+
48+
unit := templateHelpers{
49+
now: testNow,
50+
}
51+
52+
Expect(unit.currentDateTime("Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 00:00:00 UTC 2018"))
53+
}
54+
55+
func Test_currentDateTime_failure(t *testing.T) {
56+
RegisterTestingT(t)
57+
58+
unit := templateHelpers{
59+
now: testNow,
60+
}
61+
62+
Expect(unit.currentDateTime("cat")).To(Equal("2018-01-01T00:00:00Z"))
63+
}
64+
65+
func Test_currentDateTimeAdd(t *testing.T) {
66+
RegisterTestingT(t)
67+
68+
unit := templateHelpers{
69+
now: testNow,
70+
}
71+
72+
Expect(unit.currentDateTimeAdd("1s", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 00:00:01 UTC 2018"))
73+
Expect(unit.currentDateTimeAdd("2m", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 00:02:00 UTC 2018"))
74+
Expect(unit.currentDateTimeAdd("3h", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 03:00:00 UTC 2018"))
75+
Expect(unit.currentDateTimeAdd("4d", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Fri Jan 5 00:00:00 UTC 2018"))
76+
Expect(unit.currentDateTimeAdd("5y", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Sat Dec 31 00:00:00 UTC 2022"))
77+
Expect(unit.currentDateTimeAdd("1y2d3h4m5s", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Thu Jan 3 03:04:05 UTC 2019"))
78+
}
79+
80+
func Test_currentDateTimeAdd_failure(t *testing.T) {
81+
RegisterTestingT(t)
82+
83+
unit := templateHelpers{
84+
now: testNow,
85+
}
86+
87+
Expect(unit.currentDateTimeAdd("cat", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 00:00:00 UTC 2018"))
88+
Expect(unit.currentDateTimeAdd("1s", "cat")).To(Equal("2018-01-01T00:00:01Z"))
89+
Expect(unit.currentDateTimeAdd("cat", "cat")).To(Equal("2018-01-01T00:00:00Z"))
90+
}
91+
92+
func Test_currentDateTimeSubtract(t *testing.T) {
93+
RegisterTestingT(t)
94+
95+
unit := templateHelpers{
96+
now: testNow,
97+
}
98+
99+
Expect(unit.currentDateTimeSubtract("1s", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Sun Dec 31 23:59:59 UTC 2017"))
100+
Expect(unit.currentDateTimeSubtract("2m", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Sun Dec 31 23:58:00 UTC 2017"))
101+
Expect(unit.currentDateTimeSubtract("3h", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Sun Dec 31 21:00:00 UTC 2017"))
102+
Expect(unit.currentDateTimeSubtract("4d", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Thu Dec 28 00:00:00 UTC 2017"))
103+
Expect(unit.currentDateTimeSubtract("5y", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Wed Jan 2 00:00:00 UTC 2013"))
104+
Expect(unit.currentDateTimeSubtract("1y2d3h4m5s", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Thu Dec 29 20:55:55 UTC 2016"))
105+
}
106+
107+
func Test_currentDateTimeSubtract_failure(t *testing.T) {
108+
RegisterTestingT(t)
109+
110+
unit := templateHelpers{
111+
now: testNow,
112+
}
113+
114+
Expect(unit.currentDateTimeSubtract("cat", "Mon Jan 2 15:04:05 MST 2006")).To(Equal("Mon Jan 1 00:00:00 UTC 2018"))
115+
Expect(unit.currentDateTimeSubtract("1s", "cat")).To(Equal("2017-12-31T23:59:59Z"))
116+
Expect(unit.currentDateTimeSubtract("cat", "cat")).To(Equal("2018-01-01T00:00:00Z"))
117+
}

core/templating/templating.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package templating
22

33
import (
44
"strings"
5+
"time"
56

67
"github.com/SpectoLabs/hoverfly/core/models"
78
"github.com/aymerick/raymond"
89
)
910

1011
type TemplatingData struct {
11-
Request Request
12-
State map[string]string
12+
Request Request
13+
State map[string]string
14+
CurrentDateTime func(string, string, string) string
1315
}
1416

1517
type Request struct {
@@ -24,21 +26,27 @@ type Templator struct {
2426
var helpersRegistered = false
2527

2628
func NewTemplator() *Templator {
29+
t := templateHelpers{
30+
now: time.Now,
31+
}
2732

2833
if !helpersRegistered {
29-
raymond.RegisterHelper("iso8601DateTime", iso8601DateTime)
30-
raymond.RegisterHelper("iso8601DateTimePlusDays", iso8601DateTimePlusDays)
31-
raymond.RegisterHelper("randomString", randomString)
32-
raymond.RegisterHelper("randomStringLength", randomStringLength)
33-
raymond.RegisterHelper("randomBoolean", randomBoolean)
34-
raymond.RegisterHelper("randomInteger", randomInteger)
35-
raymond.RegisterHelper("randomIntegerRange", randomIntegerRange)
36-
raymond.RegisterHelper("randomFloat", randomFloat)
37-
raymond.RegisterHelper("randomFloatRange", randomFloatRange)
38-
raymond.RegisterHelper("randomEmail", randomEmail)
39-
raymond.RegisterHelper("randomIPv4", randomIPv4)
40-
raymond.RegisterHelper("randomIPv6", randomIPv6)
41-
raymond.RegisterHelper("randomUuid", randomUuid)
34+
raymond.RegisterHelper("iso8601DateTime", t.iso8601DateTime)
35+
raymond.RegisterHelper("iso8601DateTimePlusDays", t.iso8601DateTimePlusDays)
36+
raymond.RegisterHelper("currentDateTime", t.currentDateTime)
37+
raymond.RegisterHelper("currentDateTimeAdd", t.currentDateTimeAdd)
38+
raymond.RegisterHelper("currentDateTimeSubtract", t.currentDateTimeSubtract)
39+
raymond.RegisterHelper("randomString", t.randomString)
40+
raymond.RegisterHelper("randomStringLength", t.randomStringLength)
41+
raymond.RegisterHelper("randomBoolean", t.randomBoolean)
42+
raymond.RegisterHelper("randomInteger", t.randomInteger)
43+
raymond.RegisterHelper("randomIntegerRange", t.randomIntegerRange)
44+
raymond.RegisterHelper("randomFloat", t.randomFloat)
45+
raymond.RegisterHelper("randomFloatRange", t.randomFloatRange)
46+
raymond.RegisterHelper("randomEmail", t.randomEmail)
47+
raymond.RegisterHelper("randomIPv4", t.randomIPv4)
48+
raymond.RegisterHelper("randomIPv6", t.randomIPv6)
49+
raymond.RegisterHelper("randomUuid", t.randomUuid)
4250

4351
helpersRegistered = true
4452
}
@@ -66,6 +74,9 @@ func NewTemplatingDataFromRequest(requestDetails *models.RequestDetails, state m
6674
Scheme: requestDetails.Scheme,
6775
},
6876
State: state,
77+
CurrentDateTime: func(a1, a2, a3 string) string {
78+
return a1 + " " + a2 + " " + a3
79+
},
6980
}
7081

7182
}

core/templating/templating_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ func TestTemplatingWithHelperMethodsForDates(t *testing.T) {
207207
Expect(template).To(Equal(time.Now().AddDate(0, 0, 2).UTC().Format("2006-01-02T15:04:05Z07:00")))
208208
}
209209

210+
func Test_ApplyTemplate_currentDateTime(t *testing.T) {
211+
RegisterTestingT(t)
212+
213+
template, err := templating.NewTemplator().ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{currentDateTime "2006-01-02T15:04:05Z07:00"}}`)
214+
215+
Expect(err).To(BeNil())
216+
217+
Expect(template).To(Not(Equal(ContainSubstring(`{{currentDateTime "2006-01-02T15:04:05Z07:00"}}`))))
218+
}
219+
220+
func Test_ApplyTemplate_currentDateTimeAdd(t *testing.T) {
221+
RegisterTestingT(t)
222+
223+
template, err := templating.NewTemplator().ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{currentDateTimeAdd "5m" "2006-01-02T15:04:05Z07:00"}}`)
224+
225+
Expect(err).To(BeNil())
226+
227+
Expect(template).To(Not(Equal(ContainSubstring(`{{currentDateTimeAdd "5m" "2006-01-02T15:04:05Z07:00"}}`))))
228+
}
229+
230+
func Test_ApplyTemplate_currentDateTimeSubtract(t *testing.T) {
231+
RegisterTestingT(t)
232+
233+
template, err := templating.NewTemplator().ApplyTemplate(&models.RequestDetails{}, make(map[string]string), `{{currentDateTimeSubtract "5m" "2006-01-02T15:04:05Z07:00"}}`)
234+
235+
Expect(err).To(BeNil())
236+
237+
Expect(template).To(Not(Equal(ContainSubstring(`{{currentDateTimeSubtract "5m" "2006-01-02T15:04:05Z07:00"}}`))))
238+
}
239+
210240
func Test_ApplyTemplate_randomString(t *testing.T) {
211241
RegisterTestingT(t)
212242

0 commit comments

Comments
 (0)