Skip to content

Commit 7876940

Browse files
committed
add tpl func randFloat
1 parent 4747515 commit 7876940

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

docs/site/content/zh/latest/tasks/mock/simple.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ items:
77
response:
88
header:
99
server: mock
10+
content-type: application/json
1011
body: |
1112
{
1213
"count": 1,
1314
"items": [{
1415
"title": "fix: there is a bug on page {{ randEnum "one" }}",
15-
"number": 123,
16+
"number": {{randInt 100 199}},
17+
"float": {{randFloat 0.0 1.0}},
1618
"message": "{{.Response.Header.server}}",
1719
"author": "someone",
18-
"status": "success"
19-
}]
20+
"status": "success",
21+
"created": "{{ now.Format "2006-01-02T15:04:05Z07:00" }}"
22+
}],
23+
"uptime": "{{uptime}}"
2024
}
2125
- name: base64
2226
request:

pkg/render/template.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
mathrand "math/rand"
3131
"strings"
3232
"text/template"
33+
"time"
3334

3435
"github.com/linuxsuren/api-testing/pkg/version"
3536

@@ -179,6 +180,11 @@ var advancedFuncs = []AdvancedFunc{{
179180
h.Write(data)
180181
return hex.EncodeToString(h.Sum(nil))
181182
},
183+
}, {
184+
FuncName: "randFloat",
185+
Func: func(from float64, to float64) float64 {
186+
return mathrand.Float64()*(to-from) + from
187+
},
182188
}, {
183189
FuncName: "randEnum",
184190
Func: func(items ...string) string {
@@ -189,8 +195,15 @@ var advancedFuncs = []AdvancedFunc{{
189195
Func: func() string {
190196
return fmt.Sprintf("%s@%s.com", util.String(3), util.String(3))
191197
},
198+
}, {
199+
FuncName: "uptime",
200+
Func: func() string {
201+
return time.Since(uptime).String()
202+
},
192203
}}
193204

205+
var uptime = time.Now()
206+
194207
// GetAdvancedFuncs returns all the advanced functions
195208
func GetAdvancedFuncs() []AdvancedFunc {
196209
return advancedFuncs

0 commit comments

Comments
 (0)